xref: /aosp_15_r20/external/bcc/libbpf-tools/compat.h (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1*387f9dfdSAndroid Build Coastguard Worker // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*387f9dfdSAndroid Build Coastguard Worker /* Copyright (c) 2022 Hengqi Chen */
3*387f9dfdSAndroid Build Coastguard Worker 
4*387f9dfdSAndroid Build Coastguard Worker #ifndef __COMPAT_H
5*387f9dfdSAndroid Build Coastguard Worker #define __COMPAT_H
6*387f9dfdSAndroid Build Coastguard Worker 
7*387f9dfdSAndroid Build Coastguard Worker #include <stdlib.h>
8*387f9dfdSAndroid Build Coastguard Worker #include <limits.h>
9*387f9dfdSAndroid Build Coastguard Worker #include <sys/types.h>
10*387f9dfdSAndroid Build Coastguard Worker #include <linux/bpf.h>
11*387f9dfdSAndroid Build Coastguard Worker 
12*387f9dfdSAndroid Build Coastguard Worker #define POLL_TIMEOUT_MS 100
13*387f9dfdSAndroid Build Coastguard Worker 
14*387f9dfdSAndroid Build Coastguard Worker struct bpf_buffer;
15*387f9dfdSAndroid Build Coastguard Worker struct bpf_map;
16*387f9dfdSAndroid Build Coastguard Worker 
17*387f9dfdSAndroid Build Coastguard Worker typedef int (*bpf_buffer_sample_fn)(void *ctx, void *data, size_t size);
18*387f9dfdSAndroid Build Coastguard Worker typedef void (*bpf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
19*387f9dfdSAndroid Build Coastguard Worker 
20*387f9dfdSAndroid Build Coastguard Worker struct bpf_buffer *bpf_buffer__new(struct bpf_map *events, struct bpf_map *heap);
21*387f9dfdSAndroid Build Coastguard Worker int bpf_buffer__open(struct bpf_buffer *buffer, bpf_buffer_sample_fn sample_cb,
22*387f9dfdSAndroid Build Coastguard Worker 		     bpf_buffer_lost_fn lost_cb, void *ctx);
23*387f9dfdSAndroid Build Coastguard Worker int bpf_buffer__poll(struct bpf_buffer *, int timeout_ms);
24*387f9dfdSAndroid Build Coastguard Worker void bpf_buffer__free(struct bpf_buffer *);
25*387f9dfdSAndroid Build Coastguard Worker 
26*387f9dfdSAndroid Build Coastguard Worker /* taken from libbpf */
27*387f9dfdSAndroid Build Coastguard Worker #ifndef __has_builtin
28*387f9dfdSAndroid Build Coastguard Worker #define __has_builtin(x) 0
29*387f9dfdSAndroid Build Coastguard Worker #endif
30*387f9dfdSAndroid Build Coastguard Worker 
libbpf_reallocarray(void * ptr,size_t nmemb,size_t size)31*387f9dfdSAndroid Build Coastguard Worker static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
32*387f9dfdSAndroid Build Coastguard Worker {
33*387f9dfdSAndroid Build Coastguard Worker 	size_t total;
34*387f9dfdSAndroid Build Coastguard Worker 
35*387f9dfdSAndroid Build Coastguard Worker #if __has_builtin(__builtin_mul_overflow)
36*387f9dfdSAndroid Build Coastguard Worker 	if (__builtin_mul_overflow(nmemb, size, &total))
37*387f9dfdSAndroid Build Coastguard Worker 		return NULL;
38*387f9dfdSAndroid Build Coastguard Worker #else
39*387f9dfdSAndroid Build Coastguard Worker 	if (size == 0 || nmemb > ULONG_MAX / size)
40*387f9dfdSAndroid Build Coastguard Worker 		return NULL;
41*387f9dfdSAndroid Build Coastguard Worker 	total = nmemb * size;
42*387f9dfdSAndroid Build Coastguard Worker #endif
43*387f9dfdSAndroid Build Coastguard Worker 	return realloc(ptr, total);
44*387f9dfdSAndroid Build Coastguard Worker }
45*387f9dfdSAndroid Build Coastguard Worker 
46*387f9dfdSAndroid Build Coastguard Worker #endif /* __COMPAT_H */
47