]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
lib: Unify alignment of allocators
authorOndrej Zajicek <santiago@crfreenet.org>
Thu, 9 Jan 2025 15:44:51 +0000 (16:44 +0100)
committerOndrej Zajicek <santiago@crfreenet.org>
Tue, 4 Feb 2025 15:10:07 +0000 (16:10 +0100)
Different internal allocators (memory blocks, linpools, and slabs) used
different way to compute alignment. Unify it to use alignment based on
standard max_align_t type.

On x86_64, this does not change alignment of memory blocks and linpools
(both old and new is 16), but it increases alignment of slabs from 8 to
16.

lib/birdlib.h
lib/mempool.c
lib/resource.c
lib/slab.c

index 9bbffe020862821e488263b25ecb61766c4adbcf..26ebf9cc5ed23e3aa26157669440834a446e7bac 100644 (file)
 #define _BIRD_BIRDLIB_H_
 
 #include "lib/alloca.h"
+#include <stddef.h>
 #include <stdarg.h>
 #include <stdalign.h>
 
 /* Ugly structure offset handling macros */
 
-struct align_probe { char x; long int y; };
-
 #define OFFSETOF(s, i) ((size_t) &((s *)0)->i)
 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
 #define BIRD_ALIGN(s, a) (((s)+a-1)&~(a-1))
-#define CPU_STRUCT_ALIGN (sizeof(struct align_probe))
+#define CPU_STRUCT_ALIGN (alignof(max_align_t))
 
 /* Utility macros */
 
index 1c02d64a29c047fc788a48d9068a8909ce90e100..525eee8fc7e21784549bebc78232aef9edae5686 100644 (file)
@@ -27,7 +27,7 @@
 
 struct lp_chunk {
   struct lp_chunk *next;
-  uintptr_t data_align[0];
+  max_align_t data_align[0];
   byte data[0];
 };
 
index b1a3df1053c782d2045330271070925b6fa56a7c..7d1b6df48dccae7e83de054526b82aaeedf14a80 100644 (file)
@@ -316,7 +316,7 @@ resource_init(void)
 struct mblock {
   resource r;
   unsigned size;
-  uintptr_t data_align[0];
+  max_align_t data_align[0];
   byte data[0];
 };
 
index 6a4573a373f6cefdeaff7bbda6cf591ad7603df7..68b6c4856f366b92971b0edf63fef8af7aa6b5b5 100644 (file)
@@ -68,7 +68,7 @@ static struct resclass sl_class = {
 
 struct sl_obj {
   node n;
-  uintptr_t data_align[0];
+  max_align_t data_align[0];
   byte data[0];
 };
 
@@ -168,11 +168,6 @@ struct sl_head {
   u32 used_bits[0];
 };
 
-struct sl_alignment {                  /* Magic structure for testing of alignment */
-  byte data;
-  int x[0];
-};
-
 #define TLIST_PREFIX sl_head
 #define TLIST_TYPE   struct sl_head
 #define TLIST_ITEM   n
@@ -219,9 +214,7 @@ slab *
 sl_new(pool *p, uint size)
 {
   slab *s = ralloc(p, &sl_class);
-  uint align = sizeof(struct sl_alignment);
-  if (align < sizeof(void *))
-    align = sizeof(void *);
+  uint align = CPU_STRUCT_ALIGN;
   s->data_size = size;
   size = (size + align - 1) / align * align;
   s->obj_size = size;