From: Jan Janssen Date: Wed, 11 Aug 2021 12:59:46 +0000 (+0200) Subject: macro: Move some macros to macro-fundamental.h X-Git-Tag: v250-rc1~835^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f862e847246b5588d9d6ed7d91da11b6adbf39e7;p=thirdparty%2Fsystemd.git macro: Move some macros to macro-fundamental.h Also, make sure STRLEN works with wide strings too. --- diff --git a/src/basic/macro.h b/src/basic/macro.h index 92498b0f20e..3e89ba5e4d3 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -23,28 +23,12 @@ #define _packed_ __attribute__((__packed__)) #define _malloc_ __attribute__((__malloc__)) #define _weak_ __attribute__((__weak__)) -#define _likely_(x) (__builtin_expect(!!(x), 1)) -#define _unlikely_(x) (__builtin_expect(!!(x), 0)) #define _public_ __attribute__((__visibility__("default"))) #define _hidden_ __attribute__((__visibility__("hidden"))) #define _weakref_(x) __attribute__((__weakref__(#x))) #define _alignas_(x) __attribute__((__aligned__(__alignof(x)))) #define _alignptr_ __attribute__((__aligned__(sizeof(void*)))) #define _warn_unused_result_ __attribute__((__warn_unused_result__)) -#if __GNUC__ >= 7 -#define _fallthrough_ __attribute__((__fallthrough__)) -#else -#define _fallthrough_ -#endif -/* Define C11 noreturn without and even on older gcc - * compiler versions */ -#ifndef _noreturn_ -#if __STDC_VERSION__ >= 201112L -#define _noreturn_ _Noreturn -#else -#define _noreturn_ __attribute__((__noreturn__)) -#endif -#endif #if !defined(HAS_FEATURE_MEMORY_SANITIZER) # if defined(__has_feature) @@ -209,13 +193,6 @@ static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) { return m; } -/* - * STRLEN - return the length of a string literal, minus the trailing NUL byte. - * Contrary to strlen(), this is a constant expression. - * @x: a string literal. - */ -#define STRLEN(x) (sizeof(""x"") - 1U) - /* * container_of - cast a member of a structure out to the containing structure * @ptr: the pointer to the member. diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index e9fb4d39380..91b24a18264 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -14,6 +14,22 @@ #define _used_ __attribute__((__used__)) #define _unused_ __attribute__((__unused__)) #define _cleanup_(x) __attribute__((__cleanup__(x))) +#define _likely_(x) (__builtin_expect(!!(x), 1)) +#define _unlikely_(x) (__builtin_expect(!!(x), 0)) +#if __GNUC__ >= 7 +#define _fallthrough_ __attribute__((__fallthrough__)) +#else +#define _fallthrough_ +#endif +/* Define C11 noreturn without and even on older gcc + * compiler versions */ +#ifndef _noreturn_ +#if __STDC_VERSION__ >= 201112L +#define _noreturn_ _Noreturn +#else +#define _noreturn_ __attribute__((__noreturn__)) +#endif +#endif #define XSTRINGIFY(x) #x #define STRINGIFY(x) XSTRINGIFY(x) @@ -233,3 +249,10 @@ (ptr) = NULL; \ _ptr_; \ }) + +/* + * STRLEN - return the length of a string literal, minus the trailing NUL byte. + * Contrary to strlen(), this is a constant expression. + * @x: a string literal. + */ +#define STRLEN(x) (sizeof(""x"") - sizeof(typeof(x[0]))) diff --git a/src/test/test-format-util.c b/src/test/test-format-util.c index 4e6c90adfae..306e0788d08 100644 --- a/src/test/test-format-util.c +++ b/src/test/test-format-util.c @@ -7,6 +7,8 @@ /* Do some basic checks on STRLEN() and DECIMAL_STR_MAX() */ assert_cc(STRLEN("xxx") == 3); assert_cc(STRLEN("") == 0); +assert_cc(STRLEN(L"xxx") == 3 * sizeof(wchar_t)); +assert_cc(STRLEN(L"") == 0); assert_cc(DECIMAL_STR_MAX(uint8_t) == 5); assert_cc(DECIMAL_STR_MAX(int8_t) == 5); assert_cc(DECIMAL_STR_MAX(uint64_t) == 22);