From da99f5b139d699ec89a4b36bfa4d592c41b56342 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 6 Feb 2011 04:39:52 +0300 Subject: [PATCH] * Fix alignment in memory_pool library (thanks to Marcin Rzewucki) --- src/mem_pool.c | 11 ++++++----- src/mem_pool.h | 6 +++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/mem_pool.c b/src/mem_pool.c index 63fe77809..38e45e662 100644 --- a/src/mem_pool.c +++ b/src/mem_pool.c @@ -74,7 +74,7 @@ pool_chain_new (gsize size) } chain->len = size; - chain->pos = chain->begin; + chain->pos = align_ptr (chain->begin, MEM_ALIGNMENT); chain->next = NULL; STAT_LOCK (); mem_pool_stat->chunks_allocated++; @@ -116,6 +116,7 @@ pool_chain_new_shared (gsize size) #endif chain->len = size; chain->pos = chain->begin; + chain->pos = align_ptr (chain->begin, MEM_ALIGNMENT); chain->lock = NULL; chain->next = NULL; STAT_LOCK (); @@ -215,8 +216,8 @@ memory_pool_alloc (memory_pool_t * pool, gsize size) STAT_UNLOCK (); return new->begin; } - tmp = cur->pos; - cur->pos += size; + tmp = align_ptr (cur->pos, MEM_ALIGNMENT); + cur->pos = tmp + size; STAT_LOCK (); mem_pool_stat->bytes_allocated += size; STAT_UNLOCK (); @@ -332,8 +333,8 @@ memory_pool_alloc_shared (memory_pool_t * pool, gsize size) STAT_UNLOCK (); return new->begin; } - tmp = cur->pos; - cur->pos += size; + tmp = align_ptr (cur->pos, MEM_ALIGNMENT); + cur->pos = tmp + size; STAT_LOCK (); mem_pool_stat->bytes_allocated += size; STAT_UNLOCK (); diff --git a/src/mem_pool.h b/src/mem_pool.h index df1b9aff2..9cc381f7d 100644 --- a/src/mem_pool.h +++ b/src/mem_pool.h @@ -17,6 +17,10 @@ struct f_str_s; +#define MEM_ALIGNMENT sizeof(unsigned long) /* platform word */ +#define align_ptr(p, a) \ + (guint8 *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) + /** * Destructor type definition */ @@ -262,6 +266,6 @@ gpointer memory_pool_get_variable (memory_pool_t *pool, const gchar *name); * Macro that return free space in pool page * @param x pool page struct */ -#define memory_pool_free(x) ((x)->len - ((x)->pos - (x)->begin)) +#define memory_pool_free(x) ((x)->len - (align_ptr((x)->pos, MEM_ALIGNMENT) - (x)->begin)) #endif -- 2.39.5