From: cebka@mailsupport.rambler.ru Date: Thu, 18 Sep 2008 15:27:53 +0000 (+0400) Subject: * Add memory pool support. X-Git-Tag: 0.2.7~376 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2c879bedd2c9b835fb083a152e5f09d48b1dbca6;p=rspamd.git * Add memory pool support. - memory pools would be used in modules for allocating task specific data without freeing it separately - memory pools growth is implemented as multiplying by 2 its length (for avoiding multiply reallocs) - when memory pool is freed all data that was allocated from this pool is freed too --- diff --git a/configure b/configure index ef2665501..a8a5ddd32 100755 --- a/configure +++ b/configure @@ -21,7 +21,7 @@ YACC_OUTPUT="cfg_yacc.c" LEX_OUTPUT="cfg_lex.c" CONFIG="config.h" -SOURCES="upstream.c cfg_utils.c memcached.c main.c util.c worker.c fstring.c url.c perl.c plugins/surbl.c ${LEX_OUTPUT} ${YACC_OUTPUT}" +SOURCES="upstream.c cfg_utils.c memcached.c main.c util.c worker.c fstring.c url.c perl.c mem_pool.c plugins/surbl.c ${LEX_OUTPUT} ${YACC_OUTPUT}" MODULES="surbl" CFLAGS="$CFLAGS -W -Wpointer-arith -Wno-unused-parameter" @@ -30,7 +30,7 @@ CFLAGS="$CFLAGS -Wunused-value -ggdb -I${LOCALBASE}/include" CFLAGS="$CFLAGS " LDFLAGS="$LDFLAGS -L/usr/lib -L${LOCALBASE}/lib" OPT_FLAGS="-O -pipe -fno-omit-frame-pointer" -DEPS="config.h cfg_file.h memcached.h util.h main.h upstream.h fstring.h url.h perl.h ${LEX_OUTPUT} ${YACC_OUTPUT}" +DEPS="config.h cfg_file.h memcached.h util.h main.h upstream.h fstring.h url.h perl.h mem_pool.h ${LEX_OUTPUT} ${YACC_OUTPUT}" EXEC=rspamd USER=postfix GROUP=postfix diff --git a/main.h b/main.h index cedec5eb2..c7e39c38f 100644 --- a/main.h +++ b/main.h @@ -20,6 +20,7 @@ #include #include "fstring.h" +#include "mem_pool.h" #include "url.h" #include "memcached.h" @@ -135,6 +136,8 @@ struct worker_task { TAILQ_HEAD (chainsq, chain_result) chain_results; struct config_file *cfg; struct save_point save; + /* Memory pool that is associated with this task */ + memory_pool_t *task_pool; }; struct module_ctx { diff --git a/mem_pool.c b/mem_pool.c new file mode 100644 index 000000000..8a6c9d0a5 --- /dev/null +++ b/mem_pool.c @@ -0,0 +1,51 @@ +#include +#include +#include "mem_pool.h" + +memory_pool_t* +memory_pool_new (size_t size) +{ + memory_pool_t *new; + + new = g_malloc (sizeof (memory_pool_t)); + new->begin = g_malloc (size); + new->len = size; + new->pos = new->begin; + + return new; +} + +void * +memory_pool_alloc (memory_pool_t *pool, size_t size) +{ + u_char *tmp; + if (pool) { + if (pool->len < (pool->pos - pool->begin) + size) { + /* Grow pool */ + if (pool->len > size) { + pool->len *= 2; + } + else { + pool->len += size + pool->len; + } + pool->begin = g_realloc (pool->begin, pool->len); + return memory_pool_alloc (pool, size); + } + tmp = pool->pos; + pool->pos += size; + return tmp; + } + return NULL; +} + +void memory_pool_free (memory_pool_t *pool) +{ + if (pool) { + g_free (pool->begin); + g_free (pool); + } +} + +/* + * vi:ts=4 + */ diff --git a/mem_pool.h b/mem_pool.h new file mode 100644 index 000000000..e86e418dd --- /dev/null +++ b/mem_pool.h @@ -0,0 +1,18 @@ +#ifndef RSPAMD_MEM_POOL_H +#define RSPAMD_MEM_POOL_H + +#include +#include + +typedef struct memory_pool_s { + u_char *begin; + u_char *pos; + size_t len; + size_t used; +} memory_pool_t; + +memory_pool_t* memory_pool_new (size_t size); +void* memory_pool_alloc (memory_pool_t* pool, size_t size); +void memory_pool_free (memory_pool_t* pool); + +#endif diff --git a/worker.c b/worker.c index 34a096629..8493aff25 100644 --- a/worker.c +++ b/worker.c @@ -97,6 +97,9 @@ free_task (struct worker_task *task) memc_close_ctx (task->memc_ctx); free (task->memc_ctx); } + if (task->task_pool) { + memory_pool_free (task->task_pool); + } while (!TAILQ_EMPTY (&task->urls)) { cur = TAILQ_FIRST (&task->urls); TAILQ_REMOVE (&task->urls, cur, next); @@ -610,6 +613,7 @@ accept_socket (int fd, short what, void *arg) TAILQ_INIT (&new_task->results); TAILQ_INIT (&new_task->parts); new_task->memc_ctx = malloc (sizeof (memcached_ctx_t)); + new_task->task_pool = memory_pool_new (1024); if (new_task->memc_ctx == NULL) { msg_err ("accept_socket: cannot allocate memory for memcached ctx, %m"); }