macro.h revision bdd29249a882e599e5e365536372d08dee398cd4
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#pragma once
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2010 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart 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
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
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/>.
8bdbb8d9cbe1d35708385573d70984ab4533812dLennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <assert.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <sys/param.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <sys/types.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <sys/uio.h>
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering#include <inttypes.h>
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering#define _printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering#define _sentinel_ __attribute__ ((sentinel))
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#define _noreturn_ __attribute__((noreturn))
eef46c372f64f40dd75415b2c504c73138719c8dLennart Poettering#define _unused_ __attribute__ ((unused))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#define _destructor_ __attribute__ ((destructor))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define _pure_ __attribute__ ((pure))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define _const_ __attribute__ ((const))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define _deprecated_ __attribute__ ((deprecated))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define _packed_ __attribute__ ((packed))
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering#define _malloc_ __attribute__ ((malloc))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#define _weak_ __attribute__ ((weak))
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)))
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering/* automake test harness */
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#define EXIT_TEST_SKIP 77
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering#define XSTRINGIFY(x) #x
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering#define STRINGIFY(x) XSTRINGIFY(x)
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier
288a74cce597f81d3ba01d8a5ca7d2ba5b654b7eRonny Chevalier/* Rounds up */
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define ALIGN4(l) (((l) + 3) & ~3)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define ALIGN8(l) (((l) + 7) & ~7)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#if __SIZEOF_POINTER__ == 8
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define ALIGN(l) ALIGN8(l)
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen#elif __SIZEOF_POINTER__ == 4
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define ALIGN(l) ALIGN4(l)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#else
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering#error "Wut? Pointers are neither 4 nor 8 bytes long?"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
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 Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poetteringstatic inline size_t ALIGN_TO(size_t l, size_t ali) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return ((l + ali - 1) & ~(ali - 1));
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering#define ALIGN_TO_PTR(p, ali) ((void*) ALIGN_TO((unsigned long) p))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*
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.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering */
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering#define container_of(ptr, type, member) \
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering __extension__ ({ \
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering const typeof( ((type *)0)->member ) *__mptr = (ptr); \
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering (type *)( (char *)__mptr - offsetof(type,member) ); \
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering })
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering#undef MAX
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering#define MAX(a,b) \
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering __extension__ ({ \
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering typeof(a) _a = (a); \
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering typeof(b) _b = (b); \
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering _a > _b ? _a : _b; \
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering })
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering#define MAX3(x,y,z) \
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering __extension__ ({ \
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering typeof(x) _c = MAX(x,y); \
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering MAX(_c, z); \
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering })
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering#undef MIN
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define MIN(a,b) \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering __extension__ ({ \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering typeof(a) _a = (a); \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering typeof(b) _b = (b); \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering _a < _b ? _a : _b; \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering })
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#ifndef CLAMP
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define CLAMP(x, low, high) \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering __extension__ ({ \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering typeof(x) _x = (x); \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering typeof(low) _low = (low); \
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering typeof(high) _high = (high); \
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering })
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#endif
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define assert_se(expr) \
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen do { \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (_unlikely_(!(expr))) \
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering log_assert_failed(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } while (false) \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering/* We override the glibc assert() here. */
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering#undef assert
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#ifdef NDEBUG
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define assert(expr) do {} while(false)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#else
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#define assert(expr) assert_se(expr)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#endif
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#define assert_not_reached(t) \
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen do { \
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen log_assert_failed_unreachable(t, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen } while (false)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#if defined(static_assert)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#define assert_cc(expr) \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering do { \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering static_assert(expr, #expr); \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } while (false)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering#else
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#define assert_cc(expr) \
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering do { \
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering switch (0) { \
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen case 0: \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case !!(expr): \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering ; \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering } \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering } while (false)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#endif
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
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
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
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
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define TO_INT32(p) ((int32_t) ((intptr_t) (p)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define INT32_TO_PTR(u) ((void*) ((intptr_t) (u)))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define PTR_TO_LONG(p) ((long) ((intptr_t) (p)))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define LONG_TO_PTR(u) ((void*) ((intptr_t) (u)))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering#define memzero(x,l) (memset((x), 0, (l)))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#define zero(x) (memzero(&(x), sizeof(x)))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#define CHAR_TO_STR(x) ((char[2]) { x, 0 })
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#define char_array_0(x) x[sizeof(x)-1] = 0;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering#define IOVEC_SET_STRING(i, s) \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering do { \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering struct iovec *_i = &(i); \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering char *_s = (char *)(s); \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering _i->iov_base = _s; \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering _i->iov_len = strlen(_s); \
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering } while(false)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering unsigned j;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering size_t r = 0;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering for (j = 0; j < n; j++)
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen r += i[j].iov_len;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering unsigned j;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering for (j = 0; j < n; j++) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering size_t sub;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (_unlikely_(k <= 0))
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering break;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering sub = MIN(i[j].iov_len, k);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering i[j].iov_len -= sub;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering k -= sub;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return k;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering#define VA_FORMAT_ADVANCE(format, ap) \
c19de71113f956809995fc68817e055e9f61f607Lennart Poetteringdo { \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering int _argtypes[128]; \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering size_t _i, _k; \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering _k = parse_printf_format((format), ELEMENTSOF(_argtypes), _argtypes); \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(_k < ELEMENTSOF(_argtypes)); \
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering for (_i = 0; _i < _k; _i++) { \
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (_argtypes[_i] & PA_FLAG_PTR) { \
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering (void) va_arg(ap, void*); \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering continue; \
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering } \
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering switch (_argtypes[_i]) { \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case PA_INT: \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case PA_INT|PA_FLAG_SHORT: \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case PA_CHAR: \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering (void) va_arg(ap, int); \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break; \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case PA_INT|PA_FLAG_LONG: \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering (void) va_arg(ap, long int); \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break; \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case PA_INT|PA_FLAG_LONG_LONG: \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering (void) va_arg(ap, long long int); \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break; \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case PA_WCHAR: \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering (void) va_arg(ap, wchar_t); \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break; \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case PA_WSTRING: \
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering case PA_STRING: \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case PA_POINTER: \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering (void) va_arg(ap, void*); \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break; \
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering case PA_FLOAT: \
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering case PA_DOUBLE: \
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering (void) va_arg(ap, double); \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break; \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case PA_DOUBLE|PA_FLAG_LONG_DOUBLE: \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering (void) va_arg(ap, long double); \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break; \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering default: \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert_not_reached("Unknown format string argument."); \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering } \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering } \
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering} while(false)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
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 */
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering#define F_TYPE_CMP(a, b) (a == (typeof(a)) b)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
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
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering#define DECIMAL_STR_MAX(type) \
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering (1+(sizeof(type) <= 1 ? 3 : \
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering sizeof(type) <= 2 ? 5 : \
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering sizeof(type) <= 4 ? 10 : \
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering#include "log.h"
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering