diff options
author | Vsevolod Stakhov <vsevolod@rambler-co.ru> | 2011-02-06 04:39:52 +0300 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rambler-co.ru> | 2011-02-06 04:39:52 +0300 |
commit | da99f5b139d699ec89a4b36bfa4d592c41b56342 (patch) | |
tree | 834c1e1ab6e5e364a71160b925db6c49d7e7174b /src | |
parent | 267c8bf962fbfd5ab7384ba25043ae649dfbe928 (diff) | |
download | rspamd-da99f5b139d699ec89a4b36bfa4d592c41b56342.tar.gz rspamd-da99f5b139d699ec89a4b36bfa4d592c41b56342.zip |
* Fix alignment in memory_pool library (thanks to Marcin Rzewucki)
Diffstat (limited to 'src')
-rw-r--r-- | src/mem_pool.c | 11 | ||||
-rw-r--r-- | 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 |