aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/mem_pool.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2014-07-23 12:45:28 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2014-07-23 12:45:28 +0100
commite0483657ff6cf1adc828ccce457814d61fe90a0d (patch)
tree5183e4163f40b81b3e7d5f51488d360883782154 /src/libutil/mem_pool.c
parent7962087e808fb824aa3af6d41d02abc92916ba1e (diff)
downloadrspamd-e0483657ff6cf1adc828ccce457814d61fe90a0d.tar.gz
rspamd-e0483657ff6cf1adc828ccce457814d61fe90a0d.zip
Unify code style.
Diffstat (limited to 'src/libutil/mem_pool.c')
-rw-r--r--src/libutil/mem_pool.c209
1 files changed, 125 insertions, 84 deletions
diff --git a/src/libutil/mem_pool.c b/src/libutil/mem_pool.c
index 6ace865a0..630ea6b3c 100644
--- a/src/libutil/mem_pool.c
+++ b/src/libutil/mem_pool.c
@@ -23,18 +23,18 @@
*/
#include "config.h"
-#include "mem_pool.h"
#include "fstring.h"
#include "logger.h"
-#include "util.h"
#include "main.h"
+#include "mem_pool.h"
+#include "util.h"
/* Sleep time for spin lock in nanoseconds */
#define MUTEX_SLEEP_TIME 10000000L
#define MUTEX_SPIN_COUNT 100
#ifdef _THREAD_SAFE
-pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER;
# define STAT_LOCK() do { pthread_mutex_lock (&stat_mtx); } while (0)
# define STAT_UNLOCK() do { pthread_mutex_unlock (&stat_mtx); } while (0)
#else
@@ -42,10 +42,10 @@ pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER;
# define STAT_UNLOCK() do {} while (0)
#endif
-#define POOL_MTX_LOCK() do { rspamd_mutex_lock (pool->mtx); } while (0)
-#define POOL_MTX_UNLOCK() do { rspamd_mutex_unlock (pool->mtx); } while (0)
+#define POOL_MTX_LOCK() do { rspamd_mutex_lock (pool->mtx); } while (0)
+#define POOL_MTX_UNLOCK() do { rspamd_mutex_unlock (pool->mtx); } while (0)
-/*
+/*
* This define specify whether we should check all pools for free space for new object
* or just begin scan from current (recently attached) pool
* If MEMORY_GREEDY is defined, then we scan all pools to find free space (more CPU usage, slower
@@ -56,7 +56,7 @@ pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER;
#undef MEMORY_GREEDY
/* Internal statistic */
-static rspamd_mempool_stat_t *mem_pool_stat = NULL;
+static rspamd_mempool_stat_t *mem_pool_stat = NULL;
/**
* Function that return free space in pool page
@@ -68,17 +68,18 @@ pool_chain_free (struct _pool_chain *chain)
return (gint)chain->len - (chain->pos - chain->begin + MEM_ALIGNMENT);
}
-static struct _pool_chain *
+static struct _pool_chain *
pool_chain_new (gsize size)
{
- struct _pool_chain *chain;
+ struct _pool_chain *chain;
g_return_val_if_fail (size > 0, NULL);
chain = g_slice_alloc (sizeof (struct _pool_chain));
if (chain == NULL) {
- msg_err ("cannot allocate %z bytes, aborting", sizeof (struct _pool_chain));
+ msg_err ("cannot allocate %z bytes, aborting",
+ sizeof (struct _pool_chain));
abort ();
}
@@ -102,34 +103,46 @@ pool_chain_new (gsize size)
static struct _pool_chain_shared *
pool_chain_new_shared (gsize size)
{
- struct _pool_chain_shared *chain;
- gpointer map;
+ struct _pool_chain_shared *chain;
+ gpointer map;
#if defined(HAVE_MMAP_ANON)
- map = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
+ map = mmap (NULL,
+ size + sizeof (struct _pool_chain_shared),
+ PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_SHARED,
+ -1,
+ 0);
if (map == MAP_FAILED) {
- msg_err ("cannot allocate %z bytes, aborting", size + sizeof (struct _pool_chain));
+ msg_err ("cannot allocate %z bytes, aborting", size +
+ sizeof (struct _pool_chain));
abort ();
}
chain = (struct _pool_chain_shared *)map;
chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain_shared);
#elif defined(HAVE_MMAP_ZERO)
- gint fd;
+ gint fd;
fd = open ("/dev/zero", O_RDWR);
if (fd == -1) {
return NULL;
}
- map = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ map = mmap (NULL,
+ size + sizeof (struct _pool_chain_shared),
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED,
+ fd,
+ 0);
if (map == MAP_FAILED) {
- msg_err ("cannot allocate %z bytes, aborting", size + sizeof (struct _pool_chain));
+ msg_err ("cannot allocate %z bytes, aborting", size +
+ sizeof (struct _pool_chain));
abort ();
}
chain = (struct _pool_chain_shared *)map;
chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain_shared);
#else
-# error No mmap methods are defined
+# error No mmap methods are defined
#endif
chain->pos = align_ptr (chain->begin, MEM_ALIGNMENT);
chain->len = size;
@@ -145,46 +158,59 @@ pool_chain_new_shared (gsize size)
/**
- * Allocate new memory poll
+ * Allocate new memory poll
* @param size size of pool's page
* @return new memory pool object
*/
-rspamd_mempool_t *
+rspamd_mempool_t *
rspamd_mempool_new (gsize size)
{
- rspamd_mempool_t *new;
- gpointer map;
+ 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 (rspamd_mempool_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 (rspamd_mempool_stat_t));
+ msg_err ("cannot allocate %z bytes, aborting",
+ sizeof (rspamd_mempool_stat_t));
abort ();
}
mem_pool_stat = (rspamd_mempool_stat_t *)map;
#elif defined(HAVE_MMAP_ZERO)
- gint fd;
+ gint fd;
fd = open ("/dev/zero", O_RDWR);
g_assert (fd != -1);
- map = mmap (NULL, sizeof (rspamd_mempool_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 (rspamd_mempool_stat_t));
+ msg_err ("cannot allocate %z bytes, aborting",
+ sizeof (rspamd_mempool_stat_t));
abort ();
}
mem_pool_stat = (rspamd_mempool_stat_t *)map;
#else
-# error No mmap methods are defined
+# error No mmap methods are defined
#endif
memset (map, 0, sizeof (rspamd_mempool_stat_t));
}
new = g_slice_alloc (sizeof (rspamd_mempool_t));
if (new == NULL) {
- msg_err ("cannot allocate %z bytes, aborting", sizeof (rspamd_mempool_t));
+ msg_err ("cannot allocate %z bytes, aborting",
+ sizeof (rspamd_mempool_t));
abort ();
}
@@ -203,12 +229,12 @@ rspamd_mempool_new (gsize size)
return new;
}
-static void *
+static void *
memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gboolean is_tmp)
{
- guint8 *tmp;
- struct _pool_chain *new, *cur;
- gint free;
+ guint8 *tmp;
+ struct _pool_chain *new, *cur;
+ gint free;
if (pool) {
POOL_MTX_LOCK ();
@@ -229,8 +255,8 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gboolean is_tmp)
#endif
/* Find free space in pool chain */
while (cur != NULL &&
- (free = pool_chain_free (cur)) < (gint)size &&
- cur->next != NULL) {
+ (free = pool_chain_free (cur)) < (gint)size &&
+ cur->next != NULL) {
cur = cur->next;
}
@@ -241,7 +267,8 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gboolean is_tmp)
new = pool_chain_new (pool->first_pool->len);
}
else {
- new = pool_chain_new (size + pool->first_pool->len + MEM_ALIGNMENT);
+ new = pool_chain_new (
+ size + pool->first_pool->len + MEM_ALIGNMENT);
}
/* Connect to pool subsystem */
if (is_tmp) {
@@ -257,7 +284,8 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gboolean is_tmp)
}
else {
mem_pool_stat->oversized_chunks++;
- new = pool_chain_new (size + pool->first_pool->len + MEM_ALIGNMENT);
+ new = pool_chain_new (
+ size + pool->first_pool->len + MEM_ALIGNMENT);
}
/* Attach new pool to chain */
cur->next = new;
@@ -284,42 +312,42 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gboolean is_tmp)
}
-void *
+void *
rspamd_mempool_alloc (rspamd_mempool_t * pool, gsize size)
{
return memory_pool_alloc_common (pool, size, FALSE);
}
-void *
+void *
rspamd_mempool_alloc_tmp (rspamd_mempool_t * pool, gsize size)
{
return memory_pool_alloc_common (pool, size, TRUE);
}
-void *
+void *
rspamd_mempool_alloc0 (rspamd_mempool_t * pool, gsize size)
{
- void *pointer = rspamd_mempool_alloc (pool, size);
+ void *pointer = rspamd_mempool_alloc (pool, size);
if (pointer) {
memset (pointer, 0, size);
}
return pointer;
}
-void *
+void *
rspamd_mempool_alloc0_tmp (rspamd_mempool_t * pool, gsize size)
{
- void *pointer = rspamd_mempool_alloc_tmp (pool, size);
+ void *pointer = rspamd_mempool_alloc_tmp (pool, size);
if (pointer) {
memset (pointer, 0, size);
}
return pointer;
}
-void *
+void *
rspamd_mempool_alloc0_shared (rspamd_mempool_t * pool, gsize size)
{
- void *pointer = rspamd_mempool_alloc_shared (pool, size);
+ void *pointer = rspamd_mempool_alloc_shared (pool, size);
if (pointer) {
memset (pointer, 0, size);
}
@@ -334,7 +362,7 @@ rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size)
gint free;
if (pool) {
- g_return_val_if_fail(size > 0, NULL);
+ g_return_val_if_fail (size > 0, NULL);
POOL_MTX_LOCK ()
;
@@ -346,7 +374,7 @@ rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size)
/* Find free space in pool chain */
while ((free = pool_chain_free ((struct _pool_chain *) cur))
- < (gint) size && cur->next) {
+ < (gint) size && cur->next) {
cur = cur->next;
}
if (free < (gint) size && cur->next == NULL) {
@@ -358,7 +386,7 @@ rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size)
else {
mem_pool_stat->oversized_chunks++;
new = pool_chain_new_shared (
- size + pool->first_pool->len + MEM_ALIGNMENT);
+ size + pool->first_pool->len + MEM_ALIGNMENT);
}
/* Attach new pool to chain */
cur->next = new;
@@ -370,7 +398,7 @@ rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size)
;
return new->begin;
}
- tmp = align_ptr(cur->pos, MEM_ALIGNMENT);
+ tmp = align_ptr (cur->pos, MEM_ALIGNMENT);
cur->pos = tmp + size;
POOL_MTX_UNLOCK ()
;
@@ -380,11 +408,11 @@ rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size)
}
-gchar *
+gchar *
rspamd_mempool_strdup (rspamd_mempool_t * pool, const gchar *src)
{
- gsize len;
- gchar *newstr;
+ gsize len;
+ gchar *newstr;
if (src == NULL) {
return NULL;
@@ -397,10 +425,10 @@ rspamd_mempool_strdup (rspamd_mempool_t * pool, const gchar *src)
return newstr;
}
-gchar *
+gchar *
rspamd_mempool_fstrdup (rspamd_mempool_t * pool, const struct f_str_s *src)
{
- gchar *newstr;
+ gchar *newstr;
if (src == NULL) {
return NULL;
@@ -413,11 +441,11 @@ rspamd_mempool_fstrdup (rspamd_mempool_t * pool, const struct f_str_s *src)
}
-gchar *
+gchar *
rspamd_mempool_strdup_shared (rspamd_mempool_t * pool, const gchar *src)
{
- gsize len;
- gchar *newstr;
+ gsize len;
+ gchar *newstr;
if (src == NULL) {
return NULL;
@@ -434,10 +462,11 @@ rspamd_mempool_strdup_shared (rspamd_mempool_t * pool, const gchar *src)
static struct _pool_chain_shared *
memory_pool_find_pool (rspamd_mempool_t * pool, void *pointer)
{
- struct _pool_chain_shared *cur = pool->shared_pool;
+ struct _pool_chain_shared *cur = pool->shared_pool;
while (cur) {
- if ((guint8 *) pointer >= cur->begin && (guint8 *) pointer <= (cur->begin + cur->len)) {
+ if ((guint8 *) pointer >= cur->begin && (guint8 *) pointer <=
+ (cur->begin + cur->len)) {
return cur;
}
cur = cur->next;
@@ -466,19 +495,19 @@ __mutex_spin (rspamd_mempool_mutex_t * mutex)
g_atomic_int_set (&mutex->spin, MUTEX_SPIN_COUNT);
}
#ifdef HAVE_ASM_PAUSE
- __asm __volatile ("pause");
+ __asm __volatile ("pause");
#elif defined(HAVE_SCHED_YIELD)
(void)sched_yield ();
#endif
#if defined(HAVE_NANOSLEEP)
- struct timespec ts;
+ struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = MUTEX_SLEEP_TIME;
/* Spin */
- while (nanosleep (&ts, &ts) == -1 && errno == EINTR);
+ while (nanosleep (&ts, &ts) == -1 && errno == EINTR) ;
#else
-# error No methods to spin are defined
+# error No methods to spin are defined
#endif
return 1;
}
@@ -497,7 +526,7 @@ memory_pool_mutex_spin (rspamd_mempool_mutex_t * mutex)
void
rspamd_mempool_lock_shared (rspamd_mempool_t * pool, void *pointer)
{
- struct _pool_chain_shared *chain;
+ struct _pool_chain_shared *chain;
chain = memory_pool_find_pool (pool, pointer);
if (chain == NULL) {
@@ -512,7 +541,7 @@ rspamd_mempool_lock_shared (rspamd_mempool_t * pool, void *pointer)
void
rspamd_mempool_unlock_shared (rspamd_mempool_t * pool, void *pointer)
{
- struct _pool_chain_shared *chain;
+ struct _pool_chain_shared *chain;
chain = memory_pool_find_pool (pool, pointer);
if (chain == NULL) {
@@ -527,10 +556,13 @@ rspamd_mempool_unlock_shared (rspamd_mempool_t * pool, void *pointer)
}
void
-rspamd_mempool_add_destructor_full (rspamd_mempool_t * pool, rspamd_mempool_destruct_t func, void *data,
- const gchar *function, const gchar *line)
+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;
+ struct _pool_destructors *cur;
cur = rspamd_mempool_alloc (pool, sizeof (struct _pool_destructors));
if (cur) {
@@ -546,9 +578,12 @@ rspamd_mempool_add_destructor_full (rspamd_mempool_t * pool, rspamd_mempool_dest
}
void
-rspamd_mempool_replace_destructor (rspamd_mempool_t * pool, rspamd_mempool_destruct_t 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;
+ struct _pool_destructors *tmp;
tmp = pool->destructors;
while (tmp) {
@@ -565,9 +600,9 @@ rspamd_mempool_replace_destructor (rspamd_mempool_t * pool, rspamd_mempool_destr
void
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;
- struct _pool_destructors *destructor = pool->destructors;
+ struct _pool_chain *cur = pool->first_pool, *tmp;
+ struct _pool_chain_shared *cur_shared = pool->shared_pool, *tmp_shared;
+ struct _pool_destructors *destructor = pool->destructors;
POOL_MTX_LOCK ();
/* Call all pool destructors */
@@ -609,7 +644,8 @@ rspamd_mempool_delete (rspamd_mempool_t * pool)
mem_pool_stat->chunks_freed++;
mem_pool_stat->bytes_allocated -= tmp_shared->len;
STAT_UNLOCK ();
- munmap ((void *)tmp_shared, tmp_shared->len + sizeof (struct _pool_chain_shared));
+ munmap ((void *)tmp_shared, tmp_shared->len +
+ sizeof (struct _pool_chain_shared));
}
if (pool->variables) {
g_hash_table_destroy (pool->variables);
@@ -622,9 +658,9 @@ rspamd_mempool_delete (rspamd_mempool_t * pool)
}
void
-rspamd_mempool_cleanup_tmp (rspamd_mempool_t* pool)
+rspamd_mempool_cleanup_tmp (rspamd_mempool_t * pool)
{
- struct _pool_chain *cur = pool->first_pool, *tmp;
+ struct _pool_chain *cur = pool->first_pool, *tmp;
POOL_MTX_LOCK ();
cur = pool->first_pool_tmp;
@@ -677,12 +713,13 @@ rspamd_mempool_suggest_size (void)
#endif
}
-rspamd_mempool_mutex_t *
+rspamd_mempool_mutex_t *
rspamd_mempool_get_mutex (rspamd_mempool_t * pool)
{
- rspamd_mempool_mutex_t *res;
+ rspamd_mempool_mutex_t *res;
if (pool != NULL) {
- res = rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_mutex_t));
+ res =
+ rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_mutex_t));
res->lock = 0;
res->owner = 0;
res->spin = MUTEX_SPIN_COUNT;
@@ -705,10 +742,10 @@ rspamd_mempool_unlock_mutex (rspamd_mempool_mutex_t * mutex)
(void)g_atomic_int_compare_and_exchange (&mutex->lock, 1, 0);
}
-rspamd_mempool_rwlock_t *
+rspamd_mempool_rwlock_t *
rspamd_mempool_get_rwlock (rspamd_mempool_t * pool)
{
- rspamd_mempool_rwlock_t *lock;
+ rspamd_mempool_rwlock_t *lock;
lock = rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_rwlock_t));
lock->__r_lock = rspamd_mempool_get_mutex (pool);
@@ -757,14 +794,18 @@ rspamd_mempool_wunlock_rwlock (rspamd_mempool_rwlock_t * lock)
rspamd_mempool_unlock_mutex (lock->__w_lock);
}
-void
-rspamd_mempool_set_variable (rspamd_mempool_t *pool, const gchar *name, gpointer value, rspamd_mempool_destruct_t destructor)
+void
+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, rspamd_mempool_strdup (pool, name), value);
+ g_hash_table_insert (pool->variables, rspamd_mempool_strdup (pool,
+ name), value);
if (destructor != NULL) {
rspamd_mempool_add_destructor (pool, destructor, value);
}