]> source.dussan.org Git - rspamd.git/commitdiff
* Add memory pool support.
authorcebka@mailsupport.rambler.ru <cebka@mailsupport.rambler.ru>
Thu, 18 Sep 2008 15:27:53 +0000 (19:27 +0400)
committercebka@mailsupport.rambler.ru <cebka@mailsupport.rambler.ru>
Thu, 18 Sep 2008 15:27:53 +0000 (19:27 +0400)
 - 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

configure
main.h
mem_pool.c [new file with mode: 0644]
mem_pool.h [new file with mode: 0644]
worker.c

index ef2665501e284f5fd2394d5ae9af832a918b5fda..a8a5ddd32a52841f62e1acd2e5655ab2cf257b76 100755 (executable)
--- 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 cedec5eb25aaf82e97d7964140cda7a61164274d..c7e39c38f0788820ff9146a1a5751b39dd76bc35 100644 (file)
--- a/main.h
+++ b/main.h
@@ -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 (file)
index 0000000..8a6c9d0
--- /dev/null
@@ -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 (file)
index 0000000..e86e418
--- /dev/null
@@ -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
index 34a096629898dd1d95940b9f0ba25ae16948f2be..8493aff258301b62b8285a4940dbbeeb4a457455 100644 (file)
--- 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");
        }