XBPS Library API 20250719
The X Binary Package System
macro.h
1#ifndef _XBPS_MACRO_H
2#define _XBPS_MACRO_H
3
4#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x))[0])
5
6#define MIN(a, b) (((a) < (b)) ? (a) : (b))
7#define MAX(a, b) (((a) > (b)) ? (a) : (b))
8
9#if __has_builtin(__builtin_imaxabs)
10# define ABS(x) __builtin_imaxabs((x))
11#elif __has_builtin(__builtin_llabs)
12# define ABS(x) __builtin_llabs((x))
13#else
14# define ABS(x) ((x) > 0 ? (x) : -(x))
15#endif
16
17#define typeof(x) __typeof__(x)
18
19#define container_of(ptr, type, member) \
20 ({ \
21 const typeof(((type *)0)->member) *__mptr = ptr; \
22 ((type *)((uintptr_t)__mptr - offsetof(type, member))); \
23 })
24
25#define struct_size(ptr, member, count) \
26 ((sizeof(*(ptr)->member) * (count)) + sizeof(*(ptr)))
27
28#define STRLEN(x) (sizeof("" x "") - sizeof(typeof(x[0])))
29
30#define streq(a, b) (strcmp((a), (b)) == 0)
31#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
32#define strcaseeq(a, b) (strcasecmp((a), (b)) == 0)
33#define strcaseneq(a, b, n) (strcasencmp((a), (b), (n)) == 0)
34
35#endif /* !_XBPS_MACRO_H */