]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
macro: Move ALIGN_TO to macro-fundamental.h and introduce CONST_ALIGN_TO 20905/head
authorJan Janssen <medhefgo@web.de>
Wed, 6 Oct 2021 08:21:42 +0000 (10:21 +0200)
committerJan Janssen <medhefgo@web.de>
Sun, 17 Oct 2021 09:56:05 +0000 (11:56 +0200)
src/basic/macro.h
src/boot/efi/util.h
src/boot/efi/xbootldr.c
src/fundamental/macro-fundamental.h
src/libsystemd/sd-journal/catalog.c
src/test/test-macro.c

index c05339832d7171fcedcb2f9959be34eb2dcce1c5..03a0d061b8b50394ad66fbfa0624718f23a51938 100644 (file)
 #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) */
index d1ef9efeed1aee39ef73ef10747e3092437046c5..e925f1728bddb252c941afd4a8754267dd977bec 100644 (file)
 #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);
index 471982c25152267d00f4f425dc8ae8beed93dd16..39c3c99d6e0a013828e2945f01058d9c1cbd5be0 100644 (file)
@@ -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) {
index 940b661b0e5f9808eb4f5418d9de864186701fcc..36d759ea1d0d63541c01f1d1dd0dc30a35346196 100644 (file)
@@ -5,6 +5,7 @@
 #include <assert.h>
 #endif
 
+#include <limits.h>
 #include "type.h"
 
 #define _align_(x) __attribute__((__aligned__(x)))
                 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)
index ce8d47ccc3d2564111af675dc6e3f220a7736873..4a2ba02ad0ed0ca90b5607666caaca5cf952a38f 100644 (file)
@@ -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),
         };
index 4acbbfb7afe64ae34871d74574bfcaec388a1981..495221c1d5daa31c31dfab5627ca1fe20c6e7293 100644 (file)
@@ -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[]) {