From 0880db76afe85391430c547a67bad54b655a66e1 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 25 Jun 2021 14:14:54 +0200 Subject: libutil: cleanup alignment trickery in mempool - Store the return value of the page allocation directly into the pool variable instead of an intermediate unsigned char pointer. - Make the blob[] array the same type as the bitmap, this saves us manual alignment trickery. - Cleanup the pointer arithmetic, let the compiler do the sizeof() multiplication. - Use uintptr_t for the manual alignment of the data pointer, so we don't run into signdness problems there. Signed-off-by: David Oberhollenzer --- lib/util/mempool.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/lib/util/mempool.c b/lib/util/mempool.c index 3e0b0c9..9b2d905 100644 --- a/lib/util/mempool.c +++ b/lib/util/mempool.c @@ -31,7 +31,7 @@ typedef struct pool_t { size_t obj_free; - unsigned char blob[]; + unsigned int blob[]; } pool_t; struct mem_pool_t { @@ -66,36 +66,26 @@ static pool_t *create_pool(const mem_pool_t *mem) pool_t *pool; #if defined(_WIN32) || defined(__WINDOWS__) - ptr = VirtualAlloc(NULL, mem->pool_size, MEM_RESERVE | MEM_COMMIT, - PAGE_READWRITE); + pool = VirtualAlloc(NULL, mem->pool_size, MEM_RESERVE | MEM_COMMIT, + PAGE_READWRITE); - if (ptr == NULL) + if (pool == NULL) return NULL; #else - ptr = mmap(NULL, mem->pool_size, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + pool = mmap(NULL, mem->pool_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (ptr == MAP_FAILED) + if (pool == MAP_FAILED) return NULL; #endif - - pool = (pool_t *)ptr; - - ptr = pool->blob; - - if (((intptr_t)ptr) % sizeof(unsigned int)) { - ptr += sizeof(unsigned int); - ptr -= ((intptr_t)ptr) % sizeof(unsigned int); - } - - pool->bitmap = (unsigned int *)ptr; + pool->bitmap = pool->blob; pool->obj_free = mem->bitmap_count * sizeof(unsigned int) * CHAR_BIT; - ptr += mem->bitmap_count * sizeof(unsigned int); + ptr = (unsigned char *)(pool->bitmap + mem->bitmap_count); - if (((intptr_t)ptr) % mem->obj_size) { + if (((uintptr_t)ptr) % mem->obj_size) { ptr += mem->obj_size; - ptr -= ((intptr_t)ptr) % mem->obj_size; + ptr -= ((uintptr_t)ptr) % mem->obj_size; } pool->data = ptr; -- cgit v1.2.3