chiark / gitweb /
add missing header files
[elogind.git] / macro.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #ifndef foomacrohfoo
4 #define foomacrohfoo
5
6 #include <assert.h>
7 #include <sys/types.h>
8
9 #define __printf_attr(a,b) __attribute__ ((format (printf, a, b)))
10 #define __sentinel __attribute__ ((sentinel))
11 #define __noreturn __attribute__((noreturn))
12 #define __unused __attribute__ ((unused))
13 #define __destructor __attribute__ ((destructor))
14 #define __pure __attribute__ ((pure))
15 #define __const __attribute__ ((const))
16 #define __deprecated __attribute__ ((deprecated))
17 #define __packed __attribute__ ((packed))
18 #define __malloc __attribute__ ((malloc))
19
20 /* Rounds up */
21 static inline size_t ALIGN(size_t l) {
22         return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1));
23 }
24
25 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
26
27 #define MAX(a,b)                                \
28         __extension__ ({                        \
29                         typeof(a) _a = (a);     \
30                         typeof(b) _b = (b);     \
31                         _a > _b ? _a : _b;      \
32                 })
33
34 #define MIN(a,b)                                \
35         __extension__ ({                        \
36                         typeof(a) _a = (a);     \
37                         typeof(b) _b = (b);     \
38                         _a < _b ? _a : _b;      \
39                 })
40
41 #define CLAMP(x, low, high)                                             \
42         __extension__ ({                                                \
43                         typeof(x) _x = (x);                             \
44                         typeof(low) _low = (low);                       \
45                         typeof(high) _high = (high);                    \
46                         ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
47                 })
48
49
50
51 #define assert_not_reached(t) assert(!(t))
52
53 #define assert_se(x) assert(x)
54
55 #define assert_cc(expr)                            \
56         do {                                       \
57                 switch (0) {                       \
58                         case 0:                    \
59                         case !!(expr):             \
60                                 ;                  \
61                 }                                  \
62         } while (false)
63
64 #define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
65 #define UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))
66
67 #define PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
68 #define UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
69
70 #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
71 #define INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
72
73 #define TO_INT32(p) ((int32_t) ((intptr_t) (p)))
74 #define INT32_TO_PTR(u) ((void*) ((intptr_t) (u)))
75
76 #define memzero(x,l) (memset((x), 0, (l)))
77 #define zero(x) (memzero(&(x), sizeof(x)))
78
79 #define char_array_0(x) x[sizeof(x)-1] = 0;
80
81 #endif