]> source.dussan.org Git - rspamd.git/commitdiff
* Fix alignment in memory_pool library (thanks to Marcin Rzewucki)
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Sun, 6 Feb 2011 01:39:52 +0000 (04:39 +0300)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Sun, 6 Feb 2011 01:39:52 +0000 (04:39 +0300)
src/mem_pool.c
src/mem_pool.h

index 63fe77809f3c32070b2b39a65831178831a8e29b..38e45e6627152cf977cb559d400b3348320a59b6 100644 (file)
@@ -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 ();
index df1b9aff28688a525c229c2659931ced44d2d737..9cc381f7da444efa28e1bd43c5e546fff7d5bc78 100644 (file)
 
 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