summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2021-06-25 14:14:54 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2021-06-25 15:12:41 +0200
commit0880db76afe85391430c547a67bad54b655a66e1 (patch)
tree3fc8013c53f22771d1b2bfca1bcdf88f233a8a09 /lib
parent41dc1dfaa0a290ef62800e603bb6b51a48501129 (diff)
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/util/mempool.c32
1 files 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;