macro.h revision bac3c8eefe23a820caac930d41629cebafbfc7b2
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen This file is part of systemd.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright 2010 Lennart Poettering
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is free software; you can redistribute it and/or modify it
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen under the terms of the GNU Lesser General Public License as published by
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen (at your option) any later version.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is distributed in the hope that it will be useful, but
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Lesser General Public License for more details.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen You should have received a copy of the GNU Lesser General Public License
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#define _printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#define _sentinel_ __attribute__ ((sentinel))
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#define _noreturn_ __attribute__((noreturn))
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#define _destructor_ __attribute__ ((destructor))
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen#define _deprecated_ __attribute__ ((deprecated))
99634696183dfabae20104e58157c69029a11594Tom Gundersen#define _likely_(x) (__builtin_expect(!!(x),1))
99634696183dfabae20104e58157c69029a11594Tom Gundersen#define _unlikely_(x) (__builtin_expect(!!(x),0))
99634696183dfabae20104e58157c69029a11594Tom Gundersen#define _public_ __attribute__ ((visibility("default")))
99634696183dfabae20104e58157c69029a11594Tom Gundersen#define _hidden_ __attribute__ ((visibility("hidden")))
99634696183dfabae20104e58157c69029a11594Tom Gundersen#define _weakref_(x) __attribute__((weakref(#x)))
99634696183dfabae20104e58157c69029a11594Tom Gundersen#define _introspect_(x) __attribute__((section("introspect." x)))
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen#define _alignas_(x) __attribute__((aligned(__alignof(x))))
99634696183dfabae20104e58157c69029a11594Tom Gundersen/* Rounds up */
99634696183dfabae20104e58157c69029a11594Tom Gundersenstatic inline size_t ALIGN_TO(size_t l, size_t ali) {
99634696183dfabae20104e58157c69029a11594Tom Gundersen#define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
99634696183dfabae20104e58157c69029a11594Tom Gundersen * container_of - cast a member of a structure out to the containing structure
99634696183dfabae20104e58157c69029a11594Tom Gundersen * @ptr: the pointer to the member.
99634696183dfabae20104e58157c69029a11594Tom Gundersen * @type: the type of the container struct this is embedded in.
99634696183dfabae20104e58157c69029a11594Tom Gundersen * @member: the name of the member within the struct.
99634696183dfabae20104e58157c69029a11594Tom Gundersen const typeof( ((type *)0)->member ) *__mptr = (ptr); \
99634696183dfabae20104e58157c69029a11594Tom Gundersen (type *)( (char *)__mptr - offsetof(type,member) );})
99634696183dfabae20104e58157c69029a11594Tom Gundersen#define MAX(a,b) \
99634696183dfabae20104e58157c69029a11594Tom Gundersen#define MAX3(a,b,c) \
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#define MIN(a,b) \
99634696183dfabae20104e58157c69029a11594Tom Gundersen#define MIN3(a,b,c) \
756775814cf69471f74ce853745bba69f2ba94efThomas Hindoe Paaboel Andersen ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering log_assert_failed(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering } while (false) \
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering/* We override the glibc assert() here. */
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen log_assert_failed_unreachable(t, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen } while (false)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen } while (false)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen switch (0) { \
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen } while (false)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#define UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#define PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt#define UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt#define PTR_TO_ULONG(p) ((unsigned long) ((uintptr_t) (p)))
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt#define ULONG_TO_PTR(u) ((void*) ((uintptr_t) (u)))
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#define INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#define TO_INT32(p) ((int32_t) ((intptr_t) (p)))
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#define INT32_TO_PTR(u) ((void*) ((intptr_t) (u)))
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#define PTR_TO_LONG(p) ((long) ((intptr_t) (p)))
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#define LONG_TO_PTR(u) ((void*) ((intptr_t) (u)))
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering#define memzero(x,l) (memset((x), 0, (l)))
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering#define char_array_0(x) x[sizeof(x)-1] = 0;
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering char *_s = (char *)(s); \
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering } while(false)
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poetteringstatic inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering for (j = 0; j < n; j++)
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poetteringstatic inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering for (j = 0; j < n; j++) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering#define _cleanup_free_ __attribute__((cleanup(freep)))
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen#define _cleanup_fclose_ __attribute__((cleanup(fclosep)))
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen#define _cleanup_close_ __attribute__((cleanup(closep)))
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen#define _cleanup_closedir_ __attribute__((cleanup(closedirp)))
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen#define _cleanup_umask_ __attribute__((cleanup(umaskp)))
b3ec603ce8053ba3f95da1d36f15ea762c83d1e1Lennart Poettering#define _cleanup_set_free_ __attribute__((cleanup(set_freep)))
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt#define _cleanup_strv_free_ __attribute__((cleanup(strv_freep)))
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen _k = parse_printf_format((format), ELEMENTSOF(_argtypes), _argtypes); \
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert_not_reached("Unknown format string argument."); \
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen} while(false)