From 616e82f4b5fa69ced9d4d6eb64014c0e756299e0 Mon Sep 17 00:00:00 2001 From: Simon Chopin Date: Mon, 22 Jun 2020 11:32:29 +0200 Subject: [PATCH] mempool: add some padding to ensure correct alignment The _item_t structure is 64-bit aligned on 64-bit architectures, whereas the allocated structures might very well be 32-bit aligned if, for instance, they only hold integer values. This patch ensures that we round up the size to a multiple of the natural alignement of the item structure, thus ensuring that two consecutive items are properly aligned. Unaligned access to non-packed structures is undefined behaviour. On Intel processors, unaligned access can result in those accesses being much slower than normal. On some other architectures, it could simply crash the program :-). Signed-off-by: Simon Chopin --- accel-pppd/triton/mempool.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/accel-pppd/triton/mempool.c b/accel-pppd/triton/mempool.c index ac444c5c..e76ad1fd 100644 --- a/accel-pppd/triton/mempool.c +++ b/accel-pppd/triton/mempool.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -72,7 +73,7 @@ mempool_t __export *mempool_create(int size) p->magic = (uint64_t)random() * (uint64_t)random(); #endif spinlock_init(&p->lock); - p->size = size; + p->size = size + alignof(struct _item_t) - size%alignof(struct _item_t); spin_lock(&pools_lock); list_add_tail(&p->entry, &pools);