1 /* SPDX-License-Identifier: GPL-2.0 */
10 #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
12 static inline unsigned int bpf_num_possible_cpus(void)
14 int possible_cpus
= libbpf_num_possible_cpus();
16 if (possible_cpus
< 0) {
17 printf("Failed to get # of possible cpus: '%s'!\n",
18 strerror(-possible_cpus
));
24 /* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
25 * is zero-terminated string no matter what (unless sz == 0, in which case
26 * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
27 * in what is returned. Given this is internal helper, it's trivial to extend
28 * this, when necessary. Use this instead of strncpy inside libbpf source code.
30 static inline void bpf_strlcpy(char *dst
, const char *src
, size_t sz
)
38 for (i
= 0; i
< sz
&& src
[i
]; i
++)
43 #define __bpf_percpu_val_align __attribute__((__aligned__(8)))
45 #define BPF_DECLARE_PERCPU(type, name) \
46 struct { type v; /* padding */ } __bpf_percpu_val_align \
47 name[bpf_num_possible_cpus()]
48 #define bpf_percpu(name, cpu) name[(cpu)].v
51 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
55 #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
59 #define offsetofend(TYPE, MEMBER) \
60 (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
63 /* Availability of gettid across glibc versions is hit-and-miss, therefore
64 * fallback to syscall in this macro and use it everywhere.
67 #define sys_gettid() syscall(SYS_gettid)
74 #endif /* __BPF_UTIL__ */