Lely core libraries  2.2.5
util.h
Go to the documentation of this file.
1 
21 #ifndef LELY_UTIL_UTIL_H_
22 #define LELY_UTIL_UTIL_H_
23 
24 #include <lely/features.h>
25 
26 #include <stddef.h>
27 
28 #ifndef ABS
29 #define ABS(a) ((a) < 0 ? -(a) : (a))
31 #endif
32 
33 #ifndef ALIGN
34 
40 #if defined(__clang__) || defined(__GNUC__)
41 #define ALIGN(x, a) ALIGN_MASK((x), (__typeof__(x))(a)-1)
42 #else
43 #define ALIGN(x, a) ALIGN_MASK((x), (a)-1)
44 #endif
45 #endif
46 
47 #ifndef ALIGN_MASK
48 #define ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
49 #endif
50 
51 #ifndef MIN
52 
57 #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 #endif
59 
60 #ifndef MAX
61 
65 #define MAX(a, b) ((a) < (b) ? (b) : (a))
66 #endif
67 
68 #ifndef countof
69 #define countof(a) (sizeof(a) / sizeof(0 [a]))
71 #endif
72 
73 #ifndef powerof2
74 
78 #define powerof2(x) (!((x) & ((x)-1)))
79 #endif
80 
81 #ifndef structof
82 
91 // clang-format off
92 #if defined(__clang__) || defined(__GNUC__)
93 #define structof(ptr, type, member) \
94  __extension__({ \
95  const __typeof__(((type *)NULL)->member) *_tmp_ = (ptr); \
96  ((type *)((char *)(_tmp_) - offsetof(type, member))); \
97  })
98 #else
99 #define structof(ptr, type, member) \
100  ((type *)((char *)(ptr) - offsetof(type, member)))
101 #endif
102 // clang-format on
103 #endif
104 
105 #endif // !LELY_UTIL_UTIL_H_
features.h
stddef.h