macro.h revision 2ff7b0a54271c8480024d6d68edff4a92e781052
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#pragma once
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <inttypes.h>
#define _const_ __attribute__ ((const))
#define _unlikely_(x) (__builtin_expect(!!(x),0))
/* Temporarily disable some warnings */
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Wdeclaration-after-statement\"")
#define DISABLE_WARNING_FORMAT_NONLITERAL \
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
#define DISABLE_WARNING_MISSING_PROTOTYPES \
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Wmissing-prototypes\"")
#define DISABLE_WARNING_NONNULL \
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Wnonnull\"")
#define DISABLE_WARNING_SHADOW \
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Wshadow\"")
_Pragma("GCC diagnostic push"); \
_Pragma("GCC diagnostic ignored \"-Wincompatible-pointer-types\"")
#define REENABLE_WARNING \
_Pragma("GCC diagnostic pop")
/* automake test harness */
#define EXIT_TEST_SKIP 77
#define XSTRINGIFY(x) #x
#define STRINGIFY(x) XSTRINGIFY(x)
#define XCONCATENATE(x, y) x ## y
#define CONCATENATE(x, y) XCONCATENATE(x, y)
#define UNIQ __COUNTER__
/* Rounds up */
#if __SIZEOF_POINTER__ == 8
#else
#error "Wut? Pointers are neither 4 nor 8 bytes long?"
#endif
#define ALIGN4_PTR(p) ((void*) ALIGN4((unsigned long) (p)))
#define ALIGN8_PTR(p) ((void*) ALIGN8((unsigned long) (p)))
}
/* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */
static inline unsigned long ALIGN_POWER2(unsigned long u) {
/* clz(0) is undefined */
if (u == 1)
return 1;
/* left-shift overflow is undefined */
return 0;
}
#define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
/*
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*/
__extension__ ({ \
})
__extension__ ({ \
})
/* evaluates to (void) if _A or _B are not constant or of different types */
__builtin_constant_p(_A) && \
__builtin_constant_p(_B) && \
(void)0))
/* takes two types and returns the size of the larger one */
#define MAX3(x,y,z) \
__extension__ ({ \
})
__extension__ ({ \
})
#define MIN3(x,y,z) \
__extension__ ({ \
})
__extension__ ({ \
})
__extension__ ({ \
})
/* [(x + y - 1) / y] suffers from an integer overflow, even though the
* computation should be possible in the given type. Therefore, we use
* [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the
* quotient and the remainder, so both should be equally fast. */
__extension__ ({ \
})
do { \
if (_unlikely_(!(expr))) \
} while (false) \
/* We override the glibc assert() here. */
#ifdef NDEBUG
#else
#endif
#define assert_not_reached(t) \
do { \
} while (false)
#if defined(static_assert)
/* static_assert() is sometimes defined in a way that trips up
* -Wdeclaration-after-statement, hence let's temporarily turn off
* this warning around it. */
#else
char x[(expr) ? 0 : -1]; \
}; \
#endif
#define assert_return(expr, r) \
do { \
if (_unlikely_(!(expr))) { \
return (r); \
} \
} while (false)
#define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
#define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
#define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
#define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
#define PTR_TO_LONG(p) ((long) ((intptr_t) (p)))
#define LONG_TO_PTR(u) ((void *) ((intptr_t) (u)))
#define PTR_TO_ULONG(p) ((unsigned long) ((uintptr_t) (p)))
#define ULONG_TO_PTR(u) ((void *) ((uintptr_t) (u)))
#define INT32_TO_PTR(u) ((void *) ((intptr_t) (u)))
#define UINT32_TO_PTR(u) ((void *) ((uintptr_t) (u)))
#define INT64_TO_PTR(u) ((void *) ((intptr_t) (u)))
#define UINT64_TO_PTR(u) ((void *) ((uintptr_t) (u)))
#define SIZE_TO_PTR(u) ((void *) ((uintptr_t) (u)))
/* The following macros add 1 when converting things, since UID 0 is a
* valid UID, while the pointer NULL is special */
#define CHAR_TO_STR(x) ((char[2]) { x, 0 })
#define char_array_0(x) x[sizeof(x)-1] = 0;
#define IOVEC_SET_STRING(i, s) \
do { \
char *_s = (char *)(s); \
} while(false)
unsigned j;
size_t r = 0;
for (j = 0; j < n; j++)
r += i[j].iov_len;
return r;
}
unsigned j;
for (j = 0; j < n; j++) {
if (_unlikely_(k <= 0))
break;
k -= sub;
}
return k;
}
do { \
int _argtypes[128]; \
continue; \
} \
\
case PA_INT: \
case PA_INT|PA_FLAG_SHORT: \
case PA_CHAR: \
break; \
case PA_INT|PA_FLAG_LONG: \
break; \
case PA_INT|PA_FLAG_LONG_LONG: \
break; \
case PA_WCHAR: \
break; \
case PA_WSTRING: \
case PA_STRING: \
case PA_POINTER: \
break; \
case PA_FLOAT: \
case PA_DOUBLE: \
break; \
case PA_DOUBLE|PA_FLAG_LONG_DOUBLE: \
break; \
default: \
assert_not_reached("Unknown format string argument."); \
} \
} \
} while(false)
/* Because statfs.t_type can be int on some architectures, we have to cast
* the const magic to the type, otherwise the compiler warns about
*/
#define F_TYPE_EQUAL(a, b) (a == (typeof(a)) b)
/* Returns the number of chars needed to format variables of the
* specified type as a decimal string. Adds in extra space for a
* negative '-' prefix (hence works correctly on signed
* types). Includes space for the trailing NUL. */
#define DECIMAL_STR_MAX(type) \
#define IN_SET(x, y, ...) \
({ \
unsigned _i; \
bool _found = false; \
for (_i = 0; _i < 1 + sizeof((const typeof(_x)[]) { __VA_ARGS__ })/sizeof(const typeof(_x)); _i++) \
_found = true; \
break; \
} \
_found; \
})
/* Return a nulstr for a standard cascade of configuration directories,
* suitable to pass to conf_files_list_nulstr or config_parse_many. */
#define CONF_DIRS_NULSTR(n) \
"/etc/" n ".d\0" \
"/run/" n ".d\0" \
#ifdef HAVE_SPLIT_USR
#else
#define CONF_DIR_SPLIT_USR(n)
#endif
/* Define C11 thread_local attribute even on older gcc compiler
* version */
#ifndef thread_local
/*
* Don't break on glibc < 2.16 that doesn't define __STDC_NO_THREADS__
*/
#if __STDC_VERSION__ >= 201112L && !(defined(__STDC_NO_THREADS__) || (defined(__GNU_LIBRARY__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16))
#define thread_local _Thread_local
#else
#define thread_local __thread
#endif
#endif
/* Define C11 noreturn without <stdnoreturn.h> and even on older gcc
* compiler versions */
#ifndef noreturn
#if __STDC_VERSION__ >= 201112L
#else
#endif
#endif
if (*p) \
func(*p); \
} \
#include "log.h"