0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2025-01-29 04:10:02 +00:00

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.
This commit is contained in:
Ondrej Zajicek 2025-01-09 16:44:51 +01:00 committed by Maria Matejka
parent 85aa283071
commit abecb5bb40
4 changed files with 6 additions and 14 deletions

View File

@ -10,17 +10,16 @@
#define _BIRD_BIRDLIB_H_ #define _BIRD_BIRDLIB_H_
#include "lib/alloca.h" #include "lib/alloca.h"
#include <stddef.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdalign.h> #include <stdalign.h>
/* Ugly structure offset handling macros */ /* Ugly structure offset handling macros */
struct align_probe { char x; long int y; };
#define OFFSETOF(s, i) ((size_t) &((s *)0)->i) #define OFFSETOF(s, i) ((size_t) &((s *)0)->i)
#define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i))) #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
#define BIRD_ALIGN(s, a) (((s)+a-1)&~(a-1)) #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 */ /* Utility macros */

View File

@ -27,7 +27,7 @@
struct lp_chunk { struct lp_chunk {
struct lp_chunk *next; struct lp_chunk *next;
uintptr_t data_align[0]; max_align_t data_align[0];
byte data[0]; byte data[0];
}; };

View File

@ -316,7 +316,7 @@ resource_init(void)
struct mblock { struct mblock {
resource r; resource r;
unsigned size; unsigned size;
uintptr_t data_align[0]; max_align_t data_align[0];
byte data[0]; byte data[0];
}; };

View File

@ -68,7 +68,7 @@ static struct resclass sl_class = {
struct sl_obj { struct sl_obj {
node n; node n;
uintptr_t data_align[0]; max_align_t data_align[0];
byte data[0]; byte data[0];
}; };
@ -168,11 +168,6 @@ struct sl_head {
u32 used_bits[0]; 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_PREFIX sl_head
#define TLIST_TYPE struct sl_head #define TLIST_TYPE struct sl_head
#define TLIST_ITEM n #define TLIST_ITEM n
@ -219,9 +214,7 @@ slab *
sl_new(pool *p, uint size) sl_new(pool *p, uint size)
{ {
slab *s = ralloc(p, &sl_class); slab *s = ralloc(p, &sl_class);
uint align = sizeof(struct sl_alignment); uint align = CPU_STRUCT_ALIGN;
if (align < sizeof(void *))
align = sizeof(void *);
s->data_size = size; s->data_size = size;
size = (size + align - 1) / align * align; size = (size + align - 1) / align * align;
s->obj_size = size; s->obj_size = size;