macro.h revision bdd29249a882e599e5e365536372d08dee398cd4
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2010 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering#define _printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering#define _sentinel_ __attribute__ ((sentinel))
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#define _noreturn_ __attribute__((noreturn))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#define _destructor_ __attribute__ ((destructor))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define _deprecated_ __attribute__ ((deprecated))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#define _likely_(x) (__builtin_expect(!!(x),1))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define _unlikely_(x) (__builtin_expect(!!(x),0))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define _public_ __attribute__ ((visibility("default")))
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering#define _hidden_ __attribute__ ((visibility("hidden")))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define _weakref_(x) __attribute__((weakref(#x)))
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering#define _introspect_(x) __attribute__((section("introspect." x)))
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering#define _alignas_(x) __attribute__((aligned(__alignof(x))))
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering#define _cleanup_(x) __attribute__((cleanup(x)))
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering/* automake test harness */
288a74cce597f81d3ba01d8a5ca7d2ba5b654b7eRonny Chevalier/* Rounds up */
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering#error "Wut? Pointers are neither 4 nor 8 bytes long?"
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#define ALIGN_PTR(p) ((void*) ALIGN((unsigned long) p))
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering#define ALIGN4_PTR(p) ((void*) ALIGN4((unsigned long) p))
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering#define ALIGN8_PTR(p) ((void*) ALIGN8((unsigned long) p))
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poetteringstatic inline size_t ALIGN_TO(size_t l, size_t ali) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering#define ALIGN_TO_PTR(p, ali) ((void*) ALIGN_TO((unsigned long) p))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * container_of - cast a member of a structure out to the containing structure
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * @ptr: the pointer to the member.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * @type: the type of the container struct this is embedded in.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * @member: the name of the member within the struct.
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering const typeof( ((type *)0)->member ) *__mptr = (ptr); \
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering (type *)( (char *)__mptr - offsetof(type,member) ); \
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering#define MAX3(x,y,z) \
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering log_assert_failed(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } while (false) \
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering/* We override the glibc assert() here. */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen log_assert_failed_unreachable(t, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen } while (false)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } while (false)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering switch (0) { \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering } while (false)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define PTR_TO_ULONG(p) ((unsigned long) ((uintptr_t) (p)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define ULONG_TO_PTR(u) ((void*) ((uintptr_t) (u)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define TO_INT32(p) ((int32_t) ((intptr_t) (p)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define INT32_TO_PTR(u) ((void*) ((intptr_t) (u)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define PTR_TO_LONG(p) ((long) ((intptr_t) (p)))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define LONG_TO_PTR(u) ((void*) ((intptr_t) (u)))
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering#define memzero(x,l) (memset((x), 0, (l)))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define char_array_0(x) x[sizeof(x)-1] = 0;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering char *_s = (char *)(s); \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering } while(false)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering for (j = 0; j < n; j++)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering for (j = 0; j < n; j++) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering _k = parse_printf_format((format), ELEMENTSOF(_argtypes), _argtypes); \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert_not_reached("Unknown format string argument."); \
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering /* Because statfs.t_type can be int on some architecures, we have to cast
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering * the const magic to the type, otherwise the compiler warns about
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering * signed/unsigned comparison, because the magic can be 32 bit unsigned.
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering#define F_TYPE_CMP(a, b) (a == (typeof(a)) b)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering/* Returns the number of chars needed to format variables of the
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering * specified type as a decimal string. Adds in extra space for a
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering * negative '-' prefix. */
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))