diff options
-rw-r--r-- | mem_pool.c | 35 | ||||
-rw-r--r-- | mem_pool.h | 13 | ||||
-rw-r--r-- | test/Makefile.in | 2 | ||||
-rw-r--r-- | test/rspamd_mem_pool_test.c | 27 | ||||
-rw-r--r-- | test/rspamd_test_suite.c | 3 | ||||
-rw-r--r-- | test/tests.h | 5 |
6 files changed, 67 insertions, 18 deletions
diff --git a/mem_pool.c b/mem_pool.c index 3c583d07c..c6afbfa9f 100644 --- a/mem_pool.c +++ b/mem_pool.c @@ -2,16 +2,27 @@ #include <glib.h> #include "mem_pool.h" +static struct _pool_chain * +pool_chain_new (size_t size) +{ + struct _pool_chain *chain; + chain = g_malloc (sizeof (struct _pool_chain)); + chain->begin = g_malloc (size); + chain->len = size; + chain->pos = chain->begin; + chain->next = NULL; + + return chain; +} + 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; - new->next = NULL; + new->cur_pool = pool_chain_new (size); + new->first_pool = new->cur_pool; return new; } @@ -20,10 +31,10 @@ void * memory_pool_alloc (memory_pool_t *pool, size_t size) { u_char *tmp; - memory_pool_t *new, *cur; + struct _pool_chain *new, *cur; if (pool) { - cur = pool; + cur = pool->cur_pool; /* Find free space in pool chain */ while (memory_pool_free (cur) < size && cur->next) { cur = cur->next; @@ -31,12 +42,14 @@ memory_pool_alloc (memory_pool_t *pool, size_t size) if (cur->next == NULL) { /* Allocate new pool */ if (cur->len > size) { - new = memory_pool_new (cur->len); + new = pool_chain_new (cur->len); } else { - new = memory_pool_new (size + cur->len); + new = pool_chain_new (size + cur->len); } + /* Attach new pool to chain */ cur->next = new; + pool->cur_pool = new; new->pos += size; return new->begin; } @@ -47,15 +60,17 @@ memory_pool_alloc (memory_pool_t *pool, size_t size) return NULL; } -void memory_pool_delete (memory_pool_t *pool) +void +memory_pool_delete (memory_pool_t *pool) { - memory_pool_t *cur = pool, *tmp; + struct _pool_chain *cur = pool->first_pool, *tmp; while (cur) { tmp = cur; cur = cur->next; g_free (tmp->begin); g_free (tmp); } + g_free (pool); } /* diff --git a/mem_pool.h b/mem_pool.h index fc487c97c..0927d9197 100644 --- a/mem_pool.h +++ b/mem_pool.h @@ -3,12 +3,15 @@ #include <sys/types.h> #include <glib.h> - +struct _pool_chain { + u_char *begin; + u_char *pos; + size_t len; + struct _pool_chain *next; +}; typedef struct memory_pool_s { - u_char *begin; - u_char *pos; - size_t len; - struct memory_pool_s *next; + struct _pool_chain *cur_pool; + struct _pool_chain *first_pool; } memory_pool_t; memory_pool_t* memory_pool_new (size_t size); diff --git a/test/Makefile.in b/test/Makefile.in index ddd298c7c..14a2646b9 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -3,7 +3,7 @@ all: rspamd_test_suite rspamd_test_suite: $(OBJECTS) ../url.o ../util.o ../memcached.o - $(CC) $(PTHREAD_LDFLAGS) $(LDFLAGS) $(OBJECTS) ../url.o ../util.o ../memcached.o $(LIBS) -o rspamd_test_suite + $(CC) $(PTHREAD_LDFLAGS) $(LDFLAGS) $(OBJECTS) ../url.o ../util.o ../memcached.o ../mem_pool.o $(LIBS) -o rspamd_test_suite run_test: rspamd_test_suite gtester --verbose -k -o=rspamd_test.xml ./rspamd_test_suite diff --git a/test/rspamd_mem_pool_test.c b/test/rspamd_mem_pool_test.c new file mode 100644 index 000000000..5b2bf3721 --- /dev/null +++ b/test/rspamd_mem_pool_test.c @@ -0,0 +1,27 @@ +#include "../mem_pool.h" +#include "tests.h" + +#include <stdio.h> +#include <glib.h> + +#define TEST_BUF "test bufffer" +#define TEST2_BUF "test bufffertest bufffer" + +void +rspamd_mem_pool_test_func () +{ + memory_pool_t *pool; + char *tmp, *tmp2; + + pool = memory_pool_new (sizeof (TEST_BUF)); + tmp = memory_pool_alloc (pool, sizeof (TEST_BUF)); + tmp2 = memory_pool_alloc (pool, sizeof (TEST_BUF) * 2); + + snprintf (tmp, sizeof (TEST_BUF), "%s", TEST_BUF); + snprintf (tmp2, sizeof (TEST_BUF) * 2, "%s", TEST2_BUF); + + g_assert (strncmp (tmp, TEST_BUF, sizeof (TEST_BUF)) == 0); + g_assert (strncmp (tmp2, TEST2_BUF, sizeof (TEST2_BUF)) == 0); + + memory_pool_delete (pool); +} diff --git a/test/rspamd_test_suite.c b/test/rspamd_test_suite.c index 09db78496..c7c239f87 100644 --- a/test/rspamd_test_suite.c +++ b/test/rspamd_test_suite.c @@ -20,8 +20,9 @@ main (int argc, char **argv) g_test_init (&argc, &argv, NULL); - g_test_add_func ("/rspamd/url", rspamd_url_test_func); g_test_add_func ("/rspamd/memcached", rspamd_memcached_test_func); + g_test_add_func ("/rspamd/mem_pool", rspamd_mem_pool_test_func); + g_test_add_func ("/rspamd/url", rspamd_url_test_func); g_test_run (); } diff --git a/test/tests.h b/test/tests.h index 85e111b2b..3eddf92fc 100644 --- a/test/tests.h +++ b/test/tests.h @@ -8,7 +8,10 @@ /* URL parser test */ void rspamd_url_test_func (); -/* Memceched library test */ +/* Memcached library test */ void rspamd_memcached_test_func (); +/* Memory pools */ +void rspamd_mem_pool_test_func (); + #endif |