From abecb5bb408f253cd24345ba94940ea0f7b59f4f Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Thu, 9 Jan 2025 16:44:51 +0100 Subject: [PATCH] lib: Unify alignment of allocators 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. Minor changes by commiter. --- lib/birdlib.h | 5 ++--- lib/mempool.c | 2 +- lib/resource.c | 2 +- lib/slab.c | 11 ++--------- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/birdlib.h b/lib/birdlib.h index da8985c7..8209a108 100644 --- a/lib/birdlib.h +++ b/lib/birdlib.h @@ -10,17 +10,16 @@ #define _BIRD_BIRDLIB_H_ #include "lib/alloca.h" +#include #include #include /* 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 */ diff --git a/lib/mempool.c b/lib/mempool.c index 1c02d64a..525eee8f 100644 --- a/lib/mempool.c +++ b/lib/mempool.c @@ -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]; }; diff --git a/lib/resource.c b/lib/resource.c index b1a3df10..7d1b6df4 100644 --- a/lib/resource.c +++ b/lib/resource.c @@ -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]; }; diff --git a/lib/slab.c b/lib/slab.c index 6a4573a3..68b6c485 100644 --- a/lib/slab.c +++ b/lib/slab.c @@ -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;