1 /* SPDX-License-Identifier: GPL-2.0 */
9 #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
11 static inline unsigned int bpf_num_possible_cpus(void)
13 int possible_cpus = libbpf_num_possible_cpus();
15 if (possible_cpus < 0) {
16 printf("Failed to get # of possible cpus: '%s'!\n",
17 strerror(-possible_cpus));
23 /* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
24 * is zero-terminated string no matter what (unless sz == 0, in which case
25 * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
26 * in what is returned. Given this is internal helper, it's trivial to extend
27 * this, when necessary. Use this instead of strncpy inside libbpf source code.
29 static inline void bpf_strlcpy(char *dst, const char *src, size_t sz)
37 for (i = 0; i < sz && src[i]; i++)
42 #define __bpf_percpu_val_align __attribute__((__aligned__(8)))
44 #define BPF_DECLARE_PERCPU(type, name) \
45 struct { type v; /* padding */ } __bpf_percpu_val_align \
46 name[bpf_num_possible_cpus()]
47 #define bpf_percpu(name, cpu) name[(cpu)].v
50 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
54 #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
58 #define offsetofend(TYPE, MEMBER) \
59 (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
62 #endif /* __BPF_UTIL__ */