xref: /third_party/eudev/src/shared/macro.h (revision 99ca880a)
1/***
2  This file is part of eudev, forked from systemd.
3
4  Copyright 2010 Lennart Poettering
5
6  systemd is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation; either version 2.1 of the License, or
9  (at your option) any later version.
10
11  systemd is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public License
17  along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#pragma once
21
22#include <assert.h>
23#include <sys/param.h>
24#include <sys/types.h>
25#include <sys/uio.h>
26#include <inttypes.h>
27
28#define _printf_(a,b) __attribute__ ((format (printf, a, b)))
29#define _alloc_(...) __attribute__ ((alloc_size(__VA_ARGS__)))
30#define _sentinel_ __attribute__ ((sentinel))
31#define _pure_ __attribute__ ((pure))
32#define _const_ __attribute__ ((const))
33#define _packed_ __attribute__ ((packed))
34#define _malloc_ __attribute__ ((malloc))
35#define _likely_(x) (__builtin_expect(!!(x),1))
36#define _unlikely_(x) (__builtin_expect(!!(x),0))
37#define _public_ __attribute__ ((visibility("default")))
38#define _alignas_(x) __attribute__((aligned(__alignof(x))))
39#define _cleanup_(x) __attribute__((cleanup(x)))
40
41/* Temporarily disable some warnings */
42#define DISABLE_WARNING_DECLARATION_AFTER_STATEMENT                     \
43        _Pragma("GCC diagnostic push");                                 \
44        _Pragma("GCC diagnostic ignored \"-Wdeclaration-after-statement\"")
45
46#define DISABLE_WARNING_FORMAT_NONLITERAL                               \
47        _Pragma("GCC diagnostic push");                                 \
48        _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
49
50#define REENABLE_WARNING                                                \
51        _Pragma("GCC diagnostic pop")
52
53#define XCONCATENATE(x, y) x ## y
54#define CONCATENATE(x, y) XCONCATENATE(x, y)
55
56#define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq))
57#define UNIQ __COUNTER__
58
59/* Rounds up */
60
61#define ALIGN4(l) (((l) + 3) & ~3)
62#define ALIGN8(l) (((l) + 7) & ~7)
63
64#if __SIZEOF_POINTER__ == 8
65#define ALIGN(l) ALIGN8(l)
66#elif __SIZEOF_POINTER__ == 4
67#define ALIGN(l) ALIGN4(l)
68#else
69#error "Wut? Pointers are neither 4 nor 8 bytes long?"
70#endif
71
72static inline size_t ALIGN_TO(size_t l, size_t ali) {
73        return ((l + ali - 1) & ~(ali - 1));
74}
75
76
77#define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
78
79/*
80 * container_of - cast a member of a structure out to the containing structure
81 * @ptr: the pointer to the member.
82 * @type: the type of the container struct this is embedded in.
83 * @member: the name of the member within the struct.
84 */
85#define container_of(ptr, type, member) __container_of(UNIQ, (ptr), type, member)
86#define __container_of(uniq, ptr, type, member)                         \
87        __extension__ ({                                                \
88                const typeof( ((type*)0)->member ) *UNIQ_T(A, uniq) = (ptr); \
89                (type*)( (char *)UNIQ_T(A, uniq) - offsetof(type,member) ); \
90        })
91
92#define assert_se(expr)                                                 \
93        do {                                                            \
94                if (_unlikely_(!(expr)))                                \
95                        log_assert_failed(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
96        } while (false)                                                 \
97
98/* We override the glibc assert() here. */
99#undef assert
100#ifdef NDEBUG
101#define assert(expr) do {} while(false)
102#else
103#define assert(expr) assert_se(expr)
104#endif
105
106#define assert_not_reached(t)                                           \
107        do {                                                            \
108                log_assert_failed_unreachable(t, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
109        } while (false)
110
111#if defined(static_assert)
112/* static_assert() is sometimes defined in a way that trips up
113 * -Wdeclaration-after-statement, hence let's temporarily turn off
114 * this warning around it. */
115#define assert_cc(expr)                                                 \
116        DISABLE_WARNING_DECLARATION_AFTER_STATEMENT;                    \
117        static_assert(expr, #expr);                                     \
118        REENABLE_WARNING
119#else
120#define assert_cc(expr)                                                 \
121        DISABLE_WARNING_DECLARATION_AFTER_STATEMENT;                    \
122        struct CONCATENATE(_assert_struct_, __COUNTER__) {              \
123                char x[(expr) ? 0 : -1];                                \
124        };                                                              \
125        REENABLE_WARNING
126#endif
127
128#define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
129#define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
130#define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
131#define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
132
133#define PTR_TO_LONG(p) ((long) ((intptr_t) (p)))
134#define LONG_TO_PTR(u) ((void *) ((intptr_t) (u)))
135#define PTR_TO_ULONG(p) ((unsigned long) ((uintptr_t) (p)))
136#define ULONG_TO_PTR(u) ((void *) ((uintptr_t) (u)))
137
138#define PTR_TO_INT32(p) ((int32_t) ((intptr_t) (p)))
139#define INT32_TO_PTR(u) ((void *) ((intptr_t) (u)))
140#define PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
141#define UINT32_TO_PTR(u) ((void *) ((uintptr_t) (u)))
142
143#define PTR_TO_INT64(p) ((int64_t) ((intptr_t) (p)))
144#define INT64_TO_PTR(u) ((void *) ((intptr_t) (u)))
145#define PTR_TO_UINT64(p) ((uint64_t) ((uintptr_t) (p)))
146#define UINT64_TO_PTR(u) ((void *) ((uintptr_t) (u)))
147
148#define memzero(x,l) (memset((x), 0, (l)))
149
150#define char_array_0(x) x[sizeof(x)-1] = 0;
151
152#define IOVEC_SET_STRING(i, s)                  \
153        do {                                    \
154                struct iovec *_i = &(i);        \
155                char *_s = (char *)(s);         \
156                _i->iov_base = _s;              \
157                _i->iov_len = strlen(_s);       \
158        } while(false)
159
160static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
161        unsigned j;
162        size_t r = 0;
163
164        for (j = 0; j < n; j++)
165                r += i[j].iov_len;
166
167        return r;
168}
169
170static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
171        unsigned j;
172
173        for (j = 0; j < n; j++) {
174                size_t sub;
175
176                if (_unlikely_(k <= 0))
177                        break;
178
179                sub = MIN(i[j].iov_len, k);
180                i[j].iov_len -= sub;
181                i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
182                k -= sub;
183        }
184
185        return k;
186}
187
188 /* Because statfs.t_type can be int on some architecures, we have to cast
189  * the const magic to the type, otherwise the compiler warns about
190  * signed/unsigned comparison, because the magic can be 32 bit unsigned.
191 */
192#define F_TYPE_EQUAL(a, b) (a == (typeof(a)) b)
193
194/* Returns the number of chars needed to format variables of the
195 * specified type as a decimal string. Adds in extra space for a
196 * negative '-' prefix. */
197#define DECIMAL_STR_MAX(type)                                           \
198        (2+(sizeof(type) <= 1 ? 3 :                                     \
199            sizeof(type) <= 2 ? 5 :                                     \
200            sizeof(type) <= 4 ? 10 :                                    \
201            sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
202#define IN_SET(x, y, ...)                                               \
203        ({                                                              \
204                const typeof(y) _y = (y);                               \
205                const typeof(_y) _x = (x);                              \
206                unsigned _i;                                            \
207                bool _found = false;                                    \
208                for (_i = 0; _i < 1 + sizeof((const typeof(_x)[]) { __VA_ARGS__ })/sizeof(const typeof(_x)); _i++) \
209                        if (((const typeof(_x)[]) { _y, __VA_ARGS__ })[_i] == _x) { \
210                                _found = true;                          \
211                                break;                                  \
212                        }                                               \
213                _found;                                                 \
214        })
215
216/* Define C11 thread_local attribute even on older gcc compiler
217 * version */
218#ifndef thread_local
219/*
220 * Don't break on glibc < 2.16 that doesn't define __STDC_NO_THREADS__
221 * see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53769
222 */
223#if __STDC_VERSION__ >= 201112L && !(defined(__STDC_NO_THREADS__) || (defined(__GNU_LIBRARY__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16))
224#define thread_local _Thread_local
225#else
226#define thread_local __thread
227#endif
228#endif
229
230/* Define C11 noreturn without <stdnoreturn.h> and even on older gcc
231 * compiler versions */
232#ifndef noreturn
233#if __STDC_VERSION__ >= 201112L
234#define noreturn _Noreturn
235#else
236#define noreturn __attribute__((noreturn))
237#endif
238#endif
239
240#define UID_INVALID ((uid_t) -1)
241#define GID_INVALID ((gid_t) -1)
242#define MODE_INVALID ((mode_t) -1)
243
244#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func)                 \
245        static inline void func##p(type *p) {                   \
246                if (*p)                                         \
247                        func(*p);                               \
248        }                                                       \
249        struct __useless_struct_to_allow_trailing_semicolon__
250
251#include "log.h"
252