summaryrefslogtreecommitdiffstats
path: root/src/mem_pool.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2014-04-20 08:16:49 -0700
committerVsevolod Stakhov <vsevolod@highsecure.ru>2014-04-20 08:16:49 -0700
commitad837700b86153be3587672124ee663c964f0f15 (patch)
tree7ec6e8fbe539a169d501a3d1767644b68fab2dab /src/mem_pool.c
parentbaeb17562f47dc31610c0f65079c45a7e6ad44bb (diff)
downloadrspamd-ad837700b86153be3587672124ee663c964f0f15.tar.gz
rspamd-ad837700b86153be3587672124ee663c964f0f15.zip
Refactor memory pool naming.
Diffstat (limited to 'src/mem_pool.c')
-rw-r--r--src/mem_pool.c237
1 files changed, 121 insertions, 116 deletions
diff --git a/src/mem_pool.c b/src/mem_pool.c
index 553b48fe1..8f1105add 100644
--- a/src/mem_pool.c
+++ b/src/mem_pool.c
@@ -56,7 +56,7 @@ pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER;
#undef MEMORY_GREEDY
/* Internal statistic */
-static memory_pool_stat_t *mem_pool_stat = NULL;
+static rspamd_mempool_stat_t *mem_pool_stat = NULL;
/**
* Function that return free space in pool page
@@ -149,42 +149,42 @@ pool_chain_new_shared (gsize size)
* @param size size of pool's page
* @return new memory pool object
*/
-memory_pool_t *
-memory_pool_new (gsize size)
+rspamd_mempool_t *
+rspamd_mempool_new (gsize size)
{
- memory_pool_t *new;
+ rspamd_mempool_t *new;
gpointer map;
g_return_val_if_fail (size > 0, NULL);
/* Allocate statistic structure if it is not allocated before */
if (mem_pool_stat == NULL) {
#if defined(HAVE_MMAP_ANON)
- map = mmap (NULL, sizeof (memory_pool_stat_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
+ map = mmap (NULL, sizeof (rspamd_mempool_stat_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
if (map == MAP_FAILED) {
- msg_err ("cannot allocate %z bytes, aborting", sizeof (memory_pool_stat_t));
+ msg_err ("cannot allocate %z bytes, aborting", sizeof (rspamd_mempool_stat_t));
abort ();
}
- mem_pool_stat = (memory_pool_stat_t *)map;
+ mem_pool_stat = (rspamd_mempool_stat_t *)map;
#elif defined(HAVE_MMAP_ZERO)
gint fd;
fd = open ("/dev/zero", O_RDWR);
g_assert (fd != -1);
- map = mmap (NULL, sizeof (memory_pool_stat_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ map = mmap (NULL, sizeof (rspamd_mempool_stat_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
- msg_err ("cannot allocate %z bytes, aborting", sizeof (memory_pool_stat_t));
+ msg_err ("cannot allocate %z bytes, aborting", sizeof (rspamd_mempool_stat_t));
abort ();
}
- mem_pool_stat = (memory_pool_stat_t *)map;
+ mem_pool_stat = (rspamd_mempool_stat_t *)map;
#else
# error No mmap methods are defined
#endif
- memset (map, 0, sizeof (memory_pool_stat_t));
+ memset (map, 0, sizeof (rspamd_mempool_stat_t));
}
- new = g_slice_alloc (sizeof (memory_pool_t));
+ new = g_slice_alloc (sizeof (rspamd_mempool_t));
if (new == NULL) {
- msg_err ("cannot allocate %z bytes, aborting", sizeof (memory_pool_t));
+ msg_err ("cannot allocate %z bytes, aborting", sizeof (rspamd_mempool_t));
abort ();
}
@@ -204,7 +204,7 @@ memory_pool_new (gsize size)
}
static void *
-memory_pool_alloc_common (memory_pool_t * pool, gsize size, gboolean is_tmp)
+memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gboolean is_tmp)
{
guint8 *tmp;
struct _pool_chain *new, *cur;
@@ -285,21 +285,21 @@ memory_pool_alloc_common (memory_pool_t * pool, gsize size, gboolean is_tmp)
void *
-memory_pool_alloc (memory_pool_t * pool, gsize size)
+rspamd_mempool_alloc (rspamd_mempool_t * pool, gsize size)
{
return memory_pool_alloc_common (pool, size, FALSE);
}
void *
-memory_pool_alloc_tmp (memory_pool_t * pool, gsize size)
+rspamd_mempool_alloc_tmp (rspamd_mempool_t * pool, gsize size)
{
return memory_pool_alloc_common (pool, size, TRUE);
}
void *
-memory_pool_alloc0 (memory_pool_t * pool, gsize size)
+rspamd_mempool_alloc0 (rspamd_mempool_t * pool, gsize size)
{
- void *pointer = memory_pool_alloc (pool, size);
+ void *pointer = rspamd_mempool_alloc (pool, size);
if (pointer) {
memset (pointer, 0, size);
}
@@ -307,9 +307,9 @@ memory_pool_alloc0 (memory_pool_t * pool, gsize size)
}
void *
-memory_pool_alloc0_tmp (memory_pool_t * pool, gsize size)
+rspamd_mempool_alloc0_tmp (rspamd_mempool_t * pool, gsize size)
{
- void *pointer = memory_pool_alloc_tmp (pool, size);
+ void *pointer = rspamd_mempool_alloc_tmp (pool, size);
if (pointer) {
memset (pointer, 0, size);
}
@@ -317,17 +317,71 @@ memory_pool_alloc0_tmp (memory_pool_t * pool, gsize size)
}
void *
-memory_pool_alloc0_shared (memory_pool_t * pool, gsize size)
+rspamd_mempool_alloc0_shared (rspamd_mempool_t * pool, gsize size)
{
- void *pointer = memory_pool_alloc_shared (pool, size);
+ void *pointer = rspamd_mempool_alloc_shared (pool, size);
if (pointer) {
memset (pointer, 0, size);
}
return pointer;
}
+void *
+rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size)
+{
+ guint8 *tmp;
+ struct _pool_chain_shared *new, *cur;
+ gint free;
+
+ if (pool) {
+ g_return_val_if_fail(size > 0, NULL);
+
+ POOL_MTX_LOCK ()
+ ;
+ cur = pool->shared_pool;
+ if (!cur) {
+ cur = pool_chain_new_shared (pool->first_pool->len);
+ pool->shared_pool = cur;
+ }
+
+ /* Find free space in pool chain */
+ while ((free = pool_chain_free ((struct _pool_chain *) cur))
+ < (gint) size && cur->next) {
+ cur = cur->next;
+ }
+ if (free < (gint) size && cur->next == NULL) {
+ /* Allocate new pool */
+
+ if (cur->len >= size + MEM_ALIGNMENT) {
+ new = pool_chain_new_shared (cur->len);
+ }
+ else {
+ mem_pool_stat->oversized_chunks++;
+ new = pool_chain_new_shared (
+ size + pool->first_pool->len + MEM_ALIGNMENT);
+ }
+ /* Attach new pool to chain */
+ cur->next = new;
+ new->pos += size;
+ STAT_LOCK ();
+ mem_pool_stat->bytes_allocated += size;
+ STAT_UNLOCK ();
+ POOL_MTX_UNLOCK ()
+ ;
+ return new->begin;
+ }
+ tmp = align_ptr(cur->pos, MEM_ALIGNMENT);
+ cur->pos = tmp + size;
+ POOL_MTX_UNLOCK ()
+ ;
+ return tmp;
+ }
+ return NULL;
+}
+
+
gchar *
-memory_pool_strdup (memory_pool_t * pool, const gchar *src)
+rspamd_mempool_strdup (rspamd_mempool_t * pool, const gchar *src)
{
gsize len;
gchar *newstr;
@@ -337,14 +391,14 @@ memory_pool_strdup (memory_pool_t * pool, const gchar *src)
}
len = strlen (src);
- newstr = memory_pool_alloc (pool, len + 1);
+ newstr = rspamd_mempool_alloc (pool, len + 1);
memcpy (newstr, src, len);
newstr[len] = '\0';
return newstr;
}
gchar *
-memory_pool_fstrdup (memory_pool_t * pool, const struct f_str_s *src)
+rspamd_mempool_fstrdup (rspamd_mempool_t * pool, const struct f_str_s *src)
{
gchar *newstr;
@@ -352,7 +406,7 @@ memory_pool_fstrdup (memory_pool_t * pool, const struct f_str_s *src)
return NULL;
}
- newstr = memory_pool_alloc (pool, src->len + 1);
+ newstr = rspamd_mempool_alloc (pool, src->len + 1);
memcpy (newstr, src->begin, src->len);
newstr[src->len] = '\0';
return newstr;
@@ -360,7 +414,7 @@ memory_pool_fstrdup (memory_pool_t * pool, const struct f_str_s *src)
gchar *
-memory_pool_strdup_shared (memory_pool_t * pool, const gchar *src)
+rspamd_mempool_strdup_shared (rspamd_mempool_t * pool, const gchar *src)
{
gsize len;
gchar *newstr;
@@ -370,64 +424,15 @@ memory_pool_strdup_shared (memory_pool_t * pool, const gchar *src)
}
len = strlen (src);
- newstr = memory_pool_alloc_shared (pool, len + 1);
+ newstr = rspamd_mempool_alloc_shared (pool, len + 1);
memcpy (newstr, src, len);
newstr[len] = '\0';
return newstr;
}
-
-void *
-memory_pool_alloc_shared (memory_pool_t * pool, gsize size)
-{
- guint8 *tmp;
- struct _pool_chain_shared *new, *cur;
- gint free;
-
- if (pool) {
- g_return_val_if_fail (size > 0, NULL);
-
- POOL_MTX_LOCK ();
- cur = pool->shared_pool;
- if (!cur) {
- cur = pool_chain_new_shared (pool->first_pool->len);
- pool->shared_pool = cur;
- }
-
- /* Find free space in pool chain */
- while ((free = pool_chain_free ((struct _pool_chain *)cur)) < (gint)size && cur->next) {
- cur = cur->next;
- }
- if (free < (gint)size && cur->next == NULL) {
- /* Allocate new pool */
-
- if (cur->len >= size + MEM_ALIGNMENT) {
- new = pool_chain_new_shared (cur->len);
- }
- else {
- mem_pool_stat->oversized_chunks++;
- new = pool_chain_new_shared (size + pool->first_pool->len + MEM_ALIGNMENT);
- }
- /* Attach new pool to chain */
- cur->next = new;
- new->pos += size;
- STAT_LOCK ();
- mem_pool_stat->bytes_allocated += size;
- STAT_UNLOCK ();
- POOL_MTX_UNLOCK ();
- return new->begin;
- }
- tmp = align_ptr (cur->pos, MEM_ALIGNMENT);
- cur->pos = tmp + size;
- POOL_MTX_UNLOCK ();
- return tmp;
- }
- return NULL;
-}
-
/* Find pool for a pointer, returns NULL if pointer is not in pool */
static struct _pool_chain_shared *
-memory_pool_find_pool (memory_pool_t * pool, void *pointer)
+memory_pool_find_pool (rspamd_mempool_t * pool, void *pointer)
{
struct _pool_chain_shared *cur = pool->shared_pool;
@@ -442,7 +447,7 @@ memory_pool_find_pool (memory_pool_t * pool, void *pointer)
}
static inline gint
-__mutex_spin (memory_pool_mutex_t * mutex)
+__mutex_spin (rspamd_mempool_mutex_t * mutex)
{
/* check spin count */
if (g_atomic_int_dec_and_test (&mutex->spin)) {
@@ -479,7 +484,7 @@ __mutex_spin (memory_pool_mutex_t * mutex)
}
static void
-memory_pool_mutex_spin (memory_pool_mutex_t * mutex)
+memory_pool_mutex_spin (rspamd_mempool_mutex_t * mutex)
{
while (!g_atomic_int_compare_and_exchange (&mutex->lock, 0, 1)) {
if (!__mutex_spin (mutex)) {
@@ -490,7 +495,7 @@ memory_pool_mutex_spin (memory_pool_mutex_t * mutex)
/* Simple implementation of spinlock */
void
-memory_pool_lock_shared (memory_pool_t * pool, void *pointer)
+rspamd_mempool_lock_shared (rspamd_mempool_t * pool, void *pointer)
{
struct _pool_chain_shared *chain;
@@ -499,13 +504,13 @@ memory_pool_lock_shared (memory_pool_t * pool, void *pointer)
return;
}
if (chain->lock == NULL) {
- chain->lock = memory_pool_get_mutex (pool);
+ chain->lock = rspamd_mempool_get_mutex (pool);
}
- memory_pool_lock_mutex (chain->lock);
+ rspamd_mempool_lock_mutex (chain->lock);
}
void
-memory_pool_unlock_shared (memory_pool_t * pool, void *pointer)
+rspamd_mempool_unlock_shared (rspamd_mempool_t * pool, void *pointer)
{
struct _pool_chain_shared *chain;
@@ -514,20 +519,20 @@ memory_pool_unlock_shared (memory_pool_t * pool, void *pointer)
return;
}
if (chain->lock == NULL) {
- chain->lock = memory_pool_get_mutex (pool);
+ chain->lock = rspamd_mempool_get_mutex (pool);
return;
}
- memory_pool_unlock_mutex (chain->lock);
+ rspamd_mempool_unlock_mutex (chain->lock);
}
void
-memory_pool_add_destructor_full (memory_pool_t * pool, pool_destruct_func func, void *data,
+rspamd_mempool_add_destructor_full (rspamd_mempool_t * pool, rspamd_mempool_destruct_t func, void *data,
const gchar *function, const gchar *line)
{
struct _pool_destructors *cur;
- cur = memory_pool_alloc (pool, sizeof (struct _pool_destructors));
+ cur = rspamd_mempool_alloc (pool, sizeof (struct _pool_destructors));
if (cur) {
POOL_MTX_LOCK ();
cur->func = func;
@@ -541,7 +546,7 @@ memory_pool_add_destructor_full (memory_pool_t * pool, pool_destruct_func func,
}
void
-memory_pool_replace_destructor (memory_pool_t * pool, pool_destruct_func func, void *old_data, void *new_data)
+rspamd_mempool_replace_destructor (rspamd_mempool_t * pool, rspamd_mempool_destruct_t func, void *old_data, void *new_data)
{
struct _pool_destructors *tmp;
@@ -558,7 +563,7 @@ memory_pool_replace_destructor (memory_pool_t * pool, pool_destruct_func func, v
}
void
-memory_pool_delete (memory_pool_t * pool)
+rspamd_mempool_delete (rspamd_mempool_t * pool)
{
struct _pool_chain *cur = pool->first_pool, *tmp;
struct _pool_chain_shared *cur_shared = pool->shared_pool, *tmp_shared;
@@ -613,11 +618,11 @@ memory_pool_delete (memory_pool_t * pool)
mem_pool_stat->pools_freed++;
POOL_MTX_UNLOCK ();
rspamd_mutex_free (pool->mtx);
- g_slice_free (memory_pool_t, pool);
+ g_slice_free (rspamd_mempool_t, pool);
}
void
-memory_pool_cleanup_tmp (memory_pool_t* pool)
+rspamd_mempool_cleanup_tmp (rspamd_mempool_t* pool)
{
struct _pool_chain *cur = pool->first_pool, *tmp;
@@ -638,7 +643,7 @@ memory_pool_cleanup_tmp (memory_pool_t* pool)
}
void
-memory_pool_stat (memory_pool_stat_t * st)
+rspamd_mempool_stat (rspamd_mempool_stat_t * st)
{
st->pools_allocated = mem_pool_stat->pools_allocated;
st->pools_freed = mem_pool_stat->pools_freed;
@@ -653,7 +658,7 @@ memory_pool_stat (memory_pool_stat_t * st)
/* By default allocate 8Kb chunks of memory */
#define FIXED_POOL_SIZE 8192
gsize
-memory_pool_get_size (void)
+rspamd_mempool_suggest_size (void)
{
#ifdef HAVE_GETPAGESIZE
return MAX (getpagesize (), FIXED_POOL_SIZE);
@@ -662,12 +667,12 @@ memory_pool_get_size (void)
#endif
}
-memory_pool_mutex_t *
-memory_pool_get_mutex (memory_pool_t * pool)
+rspamd_mempool_mutex_t *
+rspamd_mempool_get_mutex (rspamd_mempool_t * pool)
{
- memory_pool_mutex_t *res;
+ rspamd_mempool_mutex_t *res;
if (pool != NULL) {
- res = memory_pool_alloc_shared (pool, sizeof (memory_pool_mutex_t));
+ res = rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_mutex_t));
res->lock = 0;
res->owner = 0;
res->spin = MUTEX_SPIN_COUNT;
@@ -677,33 +682,33 @@ memory_pool_get_mutex (memory_pool_t * pool)
}
void
-memory_pool_lock_mutex (memory_pool_mutex_t * mutex)
+rspamd_mempool_lock_mutex (rspamd_mempool_mutex_t * mutex)
{
memory_pool_mutex_spin (mutex);
mutex->owner = getpid ();
}
void
-memory_pool_unlock_mutex (memory_pool_mutex_t * mutex)
+rspamd_mempool_unlock_mutex (rspamd_mempool_mutex_t * mutex)
{
mutex->owner = 0;
(void)g_atomic_int_compare_and_exchange (&mutex->lock, 1, 0);
}
-memory_pool_rwlock_t *
-memory_pool_get_rwlock (memory_pool_t * pool)
+rspamd_mempool_rwlock_t *
+rspamd_mempool_get_rwlock (rspamd_mempool_t * pool)
{
- memory_pool_rwlock_t *lock;
+ rspamd_mempool_rwlock_t *lock;
- lock = memory_pool_alloc_shared (pool, sizeof (memory_pool_rwlock_t));
- lock->__r_lock = memory_pool_get_mutex (pool);
- lock->__w_lock = memory_pool_get_mutex (pool);
+ lock = rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_rwlock_t));
+ lock->__r_lock = rspamd_mempool_get_mutex (pool);
+ lock->__w_lock = rspamd_mempool_get_mutex (pool);
return lock;
}
void
-memory_pool_rlock_rwlock (memory_pool_rwlock_t * lock)
+rspamd_mempool_rlock_rwlock (rspamd_mempool_rwlock_t * lock)
{
/* Spin on write lock */
while (g_atomic_int_get (&lock->__w_lock->lock)) {
@@ -717,10 +722,10 @@ memory_pool_rlock_rwlock (memory_pool_rwlock_t * lock)
}
void
-memory_pool_wlock_rwlock (memory_pool_rwlock_t * lock)
+rspamd_mempool_wlock_rwlock (rspamd_mempool_rwlock_t * lock)
{
/* Spin on write lock first */
- memory_pool_lock_mutex (lock->__w_lock);
+ rspamd_mempool_lock_mutex (lock->__w_lock);
/* Now we have write lock set up */
/* Wait all readers */
while (g_atomic_int_get (&lock->__r_lock->lock)) {
@@ -729,7 +734,7 @@ memory_pool_wlock_rwlock (memory_pool_rwlock_t * lock)
}
void
-memory_pool_runlock_rwlock (memory_pool_rwlock_t * lock)
+rspamd_mempool_runlock_rwlock (rspamd_mempool_rwlock_t * lock)
{
if (g_atomic_int_get (&lock->__r_lock->lock)) {
(void)g_atomic_int_dec_and_test (&lock->__r_lock->lock);
@@ -737,26 +742,26 @@ memory_pool_runlock_rwlock (memory_pool_rwlock_t * lock)
}
void
-memory_pool_wunlock_rwlock (memory_pool_rwlock_t * lock)
+rspamd_mempool_wunlock_rwlock (rspamd_mempool_rwlock_t * lock)
{
- memory_pool_unlock_mutex (lock->__w_lock);
+ rspamd_mempool_unlock_mutex (lock->__w_lock);
}
void
-memory_pool_set_variable (memory_pool_t *pool, const gchar *name, gpointer value, pool_destruct_func destructor)
+rspamd_mempool_set_variable (rspamd_mempool_t *pool, const gchar *name, gpointer value, rspamd_mempool_destruct_t destructor)
{
if (pool->variables == NULL) {
pool->variables = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
}
- g_hash_table_insert (pool->variables, memory_pool_strdup (pool, name), value);
+ g_hash_table_insert (pool->variables, rspamd_mempool_strdup (pool, name), value);
if (destructor != NULL) {
- memory_pool_add_destructor (pool, destructor, value);
+ rspamd_mempool_add_destructor (pool, destructor, value);
}
}
gpointer
-memory_pool_get_variable (memory_pool_t *pool, const gchar *name)
+rspamd_mempool_get_variable (rspamd_mempool_t *pool, const gchar *name)
{
if (pool->variables == NULL) {
return NULL;