From: Jan Janssen Date: Wed, 6 Oct 2021 08:21:42 +0000 (+0200) Subject: macro: Move ALIGN_TO to macro-fundamental.h and introduce CONST_ALIGN_TO X-Git-Tag: v250-rc1~473^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a36a0d154038797fdf360b7b5705f001e2900775;p=thirdparty%2Fsystemd.git macro: Move ALIGN_TO to macro-fundamental.h and introduce CONST_ALIGN_TO --- diff --git a/src/basic/macro.h b/src/basic/macro.h index c05339832d7..03a0d061b8b 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -145,24 +145,6 @@ #define ALIGN4_PTR(p) ((void*) ALIGN4((unsigned long) (p))) #define ALIGN8_PTR(p) ((void*) ALIGN8((unsigned long) (p))) -static inline size_t ALIGN_TO(size_t l, size_t ali) { - /* Check that alignment is exponent of 2 */ -#if SIZE_MAX == UINT_MAX - assert(__builtin_popcount(ali) == 1); -#elif SIZE_MAX == ULONG_MAX - assert(__builtin_popcountl(ali) == 1); -#elif SIZE_MAX == ULLONG_MAX - assert(__builtin_popcountll(ali) == 1); -#else -#error "Unexpected size_t" -#endif - - if (l > SIZE_MAX - (ali - 1)) - return SIZE_MAX; /* indicate overflow */ - - return ((l + ali - 1) & ~(ali - 1)); -} - #define ALIGN_TO_PTR(p, ali) ((void*) ALIGN_TO((unsigned long) (p), (ali))) /* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */ diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h index d1ef9efeed1..e925f1728bd 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -24,13 +24,6 @@ #define UINT64_MAX ((UINT64) -1) #endif -static inline UINTN ALIGN_TO(UINTN l, UINTN ali) { - if (l > UINTN_MAX - (ali - 1)) /* Overflow? */ - return UINTN_MAX; - - return ((l + (ali - 1)) & ~(ali - 1)); -} - EFI_STATUS parse_boolean(const CHAR8 *v, BOOLEAN *b); UINT64 ticks_read(void); diff --git a/src/boot/efi/xbootldr.c b/src/boot/efi/xbootldr.c index 471982c2515..39c3c99d6e0 100644 --- a/src/boot/efi/xbootldr.c +++ b/src/boot/efi/xbootldr.c @@ -9,7 +9,7 @@ union GptHeaderBuffer { EFI_PARTITION_TABLE_HEADER gpt_header; - uint8_t space[((sizeof(EFI_PARTITION_TABLE_HEADER) + 511) / 512) * 512]; + uint8_t space[CONST_ALIGN_TO(sizeof(EFI_PARTITION_TABLE_HEADER), 512)]; }; static EFI_DEVICE_PATH *path_parent(EFI_DEVICE_PATH *path, EFI_DEVICE_PATH *node) { diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index 940b661b0e5..36d759ea1d0 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -5,6 +5,7 @@ #include #endif +#include #include "type.h" #define _align_(x) __attribute__((__aligned__(x))) @@ -273,3 +274,34 @@ free(memory); \ (typeof(memory)) NULL; \ }) + +static inline size_t ALIGN_TO(size_t l, size_t ali) { + /* sd-boot uses UINTN for size_t, let's make sure SIZE_MAX is correct. */ + assert_cc(SIZE_MAX == ~(size_t)0); + + /* Check that alignment is exponent of 2 */ +#if SIZE_MAX == UINT_MAX + assert(__builtin_popcount(ali) == 1); +#elif SIZE_MAX == ULONG_MAX + assert(__builtin_popcountl(ali) == 1); +#elif SIZE_MAX == ULLONG_MAX + assert(__builtin_popcountll(ali) == 1); +#else + #error "Unexpected size_t" +#endif + + if (l > SIZE_MAX - (ali - 1)) + return SIZE_MAX; /* indicate overflow */ + + return ((l + ali - 1) & ~(ali - 1)); +} + +/* Same as ALIGN_TO but callable in constant contexts. */ +#define CONST_ALIGN_TO(l, ali) \ + __builtin_choose_expr( \ + __builtin_constant_p(l) && \ + __builtin_constant_p(ali) && \ + __builtin_popcountll(ali) == 1 && /* is power of 2? */ \ + (l <= SIZE_MAX - (ali - 1)), /* overflow? */ \ + ((l) + (ali) - 1) & ~((ali) - 1), \ + VOID_0) diff --git a/src/libsystemd/sd-journal/catalog.c b/src/libsystemd/sd-journal/catalog.c index ce8d47ccc3d..4a2ba02ad0e 100644 --- a/src/libsystemd/sd-journal/catalog.c +++ b/src/libsystemd/sd-journal/catalog.c @@ -395,7 +395,7 @@ static int64_t write_catalog( header = (CatalogHeader) { .signature = CATALOG_SIGNATURE, - .header_size = htole64(ALIGN_TO(sizeof(CatalogHeader), 8)), + .header_size = htole64(CONST_ALIGN_TO(sizeof(CatalogHeader), 8)), .catalog_item_size = htole64(sizeof(CatalogItem)), .n_items = htole64(n), }; diff --git a/src/test/test-macro.c b/src/test/test-macro.c index 4acbbfb7afe..495221c1d5d 100644 --- a/src/test/test-macro.c +++ b/src/test/test-macro.c @@ -325,6 +325,15 @@ static void test_align_to(void) { assert_se(ALIGN_TO(SIZE_MAX-2, 4) == SIZE_MAX); /* overflow */ assert_se(ALIGN_TO(SIZE_MAX-1, 4) == SIZE_MAX); /* overflow */ assert_se(ALIGN_TO(SIZE_MAX, 4) == SIZE_MAX); /* overflow */ + + assert_cc(CONST_ALIGN_TO(96, 512) == 512); + assert_cc(CONST_ALIGN_TO(511, 512) == 512); + assert_cc(CONST_ALIGN_TO(512, 512) == 512); + assert_cc(CONST_ALIGN_TO(513, 512) == 1024); + assert_cc(CONST_ALIGN_TO(sizeof(int), 64) == 64); + + assert_cc(__builtin_types_compatible_p(typeof(CONST_ALIGN_TO(4, 3)), void)); + assert_cc(__builtin_types_compatible_p(typeof(CONST_ALIGN_TO(SIZE_MAX, 512)), void)); } int main(int argc, char *argv[]) {