diff options
-rwxr-xr-x | configure | 4 | ||||
-rw-r--r-- | main.h | 3 | ||||
-rw-r--r-- | mem_pool.c | 51 | ||||
-rw-r--r-- | mem_pool.h | 18 | ||||
-rw-r--r-- | worker.c | 4 |
5 files changed, 78 insertions, 2 deletions
@@ -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 @@ -20,6 +20,7 @@ #include <event.h> #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 <sys/types.h> +#include <glib.h> +#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 <sys/types.h> +#include <glib.h> + +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 @@ -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"); } |