chiark / gitweb /
execute: support syscall filtering using seccomp filters
[elogind.git] / src / shared / macro.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #ifndef foomacrohfoo
4 #define foomacrohfoo
5
6 /***
7   This file is part of systemd.
8
9   Copyright 2010 Lennart Poettering
10
11   systemd is free software; you can redistribute it and/or modify it
12   under the terms of the GNU Lesser General Public License as published by
13   the Free Software Foundation; either version 2.1 of the License, or
14   (at your option) any later version.
15
16   systemd is distributed in the hope that it will be useful, but
17   WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19   Lesser General Public License for more details.
20
21   You should have received a copy of the GNU Lesser General Public License
22   along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 ***/
24
25 #include <assert.h>
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/uio.h>
29 #include <inttypes.h>
30
31 #define _printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
32 #define _sentinel_ __attribute__ ((sentinel))
33 #define _noreturn_ __attribute__((noreturn))
34 #define _unused_ __attribute__ ((unused))
35 #define _destructor_ __attribute__ ((destructor))
36 #define _pure_ __attribute__ ((pure))
37 #define _const_ __attribute__ ((const))
38 #define _deprecated_ __attribute__ ((deprecated))
39 #define _packed_ __attribute__ ((packed))
40 #define _malloc_ __attribute__ ((malloc))
41 #define _weak_ __attribute__ ((weak))
42 #define _likely_(x) (__builtin_expect(!!(x),1))
43 #define _unlikely_(x) (__builtin_expect(!!(x),0))
44 #define _public_ __attribute__ ((visibility("default")))
45 #define _hidden_ __attribute__ ((visibility("hidden")))
46 #define _weakref_(x) __attribute__((weakref(#x)))
47 #define _introspect_(x) __attribute__((section("introspect." x)))
48 #define _alignas_(x) __attribute__((aligned(__alignof(x))))
49
50 #define XSTRINGIFY(x) #x
51 #define STRINGIFY(x) XSTRINGIFY(x)
52
53 /* Rounds up */
54 #define ALIGN(l) ALIGN_TO((l), sizeof(void*))
55 static inline size_t ALIGN_TO(size_t l, size_t ali) {
56         return ((l + ali - 1) & ~(ali - 1));
57 }
58
59 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
60
61 /*
62  * container_of - cast a member of a structure out to the containing structure
63  * @ptr: the pointer to the member.
64  * @type: the type of the container struct this is embedded in.
65  * @member: the name of the member within the struct.
66  *
67  */
68 #define container_of(ptr, type, member) ({ \
69         const typeof( ((type *)0)->member ) *__mptr = (ptr); \
70         (type *)( (char *)__mptr - offsetof(type,member) );})
71
72 #ifndef MAX
73 #define MAX(a,b)                                \
74         __extension__ ({                        \
75                         typeof(a) _a = (a);     \
76                         typeof(b) _b = (b);     \
77                         _a > _b ? _a : _b;      \
78                 })
79 #endif
80
81 #define MAX3(a,b,c)                             \
82         MAX(MAX(a,b),c)
83
84 #ifndef MIN
85 #define MIN(a,b)                                \
86         __extension__ ({                        \
87                         typeof(a) _a = (a);     \
88                         typeof(b) _b = (b);     \
89                         _a < _b ? _a : _b;      \
90                 })
91 #endif
92
93 #define MIN3(a,b,c)                             \
94         MIN(MIN(a,b),c)
95
96 #define CLAMP(x, low, high)                                             \
97         __extension__ ({                                                \
98                         typeof(x) _x = (x);                             \
99                         typeof(low) _low = (low);                       \
100                         typeof(high) _high = (high);                    \
101                         ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
102                 })
103
104 #define assert_se(expr)                                                 \
105         do {                                                            \
106                 if (_unlikely_(!(expr)))                                \
107                         log_assert_failed(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
108         } while (false)                                                 \
109
110 /* We override the glibc assert() here. */
111 #undef assert
112 #ifdef NDEBUG
113 #define assert(expr) do {} while(false)
114 #else
115 #define assert(expr) assert_se(expr)
116 #endif
117
118 #define assert_not_reached(t)                                           \
119         do {                                                            \
120                 log_assert_failed_unreachable(t, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
121         } while (false)
122
123 #define assert_cc(expr)                            \
124         do {                                       \
125                 switch (0) {                       \
126                         case 0:                    \
127                         case !!(expr):             \
128                                 ;                  \
129                 }                                  \
130         } while (false)
131
132 #define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
133 #define UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))
134
135 #define PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
136 #define UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
137
138 #define PTR_TO_ULONG(p) ((unsigned long) ((uintptr_t) (p)))
139 #define ULONG_TO_PTR(u) ((void*) ((uintptr_t) (u)))
140
141 #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
142 #define INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
143
144 #define TO_INT32(p) ((int32_t) ((intptr_t) (p)))
145 #define INT32_TO_PTR(u) ((void*) ((intptr_t) (u)))
146
147 #define PTR_TO_LONG(p) ((long) ((intptr_t) (p)))
148 #define LONG_TO_PTR(u) ((void*) ((intptr_t) (u)))
149
150 #define memzero(x,l) (memset((x), 0, (l)))
151 #define zero(x) (memzero(&(x), sizeof(x)))
152
153 #define char_array_0(x) x[sizeof(x)-1] = 0;
154
155 #define IOVEC_SET_STRING(i, s)                  \
156         do {                                    \
157                 struct iovec *_i = &(i);        \
158                 char *_s = (char *)(s);         \
159                 _i->iov_base = _s;              \
160                 _i->iov_len = strlen(_s);       \
161         } while(false)
162
163 static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
164         unsigned j;
165         size_t r = 0;
166
167         for (j = 0; j < n; j++)
168                 r += i[j].iov_len;
169
170         return r;
171 }
172
173 static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
174         unsigned j;
175
176         for (j = 0; j < n; j++) {
177                 size_t sub;
178
179                 if (_unlikely_(k <= 0))
180                         break;
181
182                 sub = MIN(i[j].iov_len, k);
183                 i[j].iov_len -= sub;
184                 i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
185                 k -= sub;
186         }
187
188         return k;
189 }
190
191 #include "log.h"
192
193 #endif