From: Vsevolod Stakhov Date: Sun, 6 Feb 2011 01:39:52 +0000 (+0300) Subject: * Fix alignment in memory_pool library (thanks to Marcin Rzewucki) X-Git-Tag: 0.3.7~56 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=da99f5b139d699ec89a4b36bfa4d592c41b56342;p=rspamd.git * Fix alignment in memory_pool library (thanks to Marcin Rzewucki) --- 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