From: cebka@mailsupport.rambler.ru Date: Tue, 14 Oct 2008 01:24:07 +0000 (+0400) Subject: * Implement destructors stack in memory pool, so there is now X-Git-Tag: 0.2.7~361 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3f8572f20960a02b334d09c809513d5ab25012a5;p=thirdparty%2Frspamd.git * Implement destructors stack in memory pool, so there is now convinient way to free objects in pool that was allocated by other memory allocation mechanics (e.g. GObject). Destructors must be added in pool manually by calling memory_pool_add_destructor function. --- diff --git a/cfg_utils.c b/cfg_utils.c index 3f46b0dd81..7f64e45520 100644 --- a/cfg_utils.c +++ b/cfg_utils.c @@ -494,6 +494,7 @@ parse_regexp (memory_pool_t *pool, char *line) *end = '\0'; result->regexp = g_regex_new (begin, regexp_flags, 0, &err); result->regexp_text = memory_pool_strdup (pool, begin); + memory_pool_add_destructor (pool, (pool_destruct_func)g_regex_unref, (void *)result->regexp); *end = '/'; return result; diff --git a/mem_pool.c b/mem_pool.c index fc14af7904..56fc0ef78e 100644 --- a/mem_pool.c +++ b/mem_pool.c @@ -42,6 +42,7 @@ memory_pool_new (size_t size) new = g_malloc (sizeof (memory_pool_t)); new->cur_pool = pool_chain_new (size); new->first_pool = new->cur_pool; + new->destructors = NULL; return new; } @@ -111,10 +112,32 @@ memory_pool_strdup (memory_pool_t *pool, const char *src) return newstr; } +void +memory_pool_add_destructor (memory_pool_t *pool, pool_destruct_func func, void *data) +{ + struct _pool_destructors *cur; + + cur = memory_pool_alloc (pool, sizeof (struct _pool_destructors)); + if (cur) { + cur->func = func; + cur->data = data; + cur->prev = pool->destructors; + pool->destructors = cur; + } +} + void memory_pool_delete (memory_pool_t *pool) { struct _pool_chain *cur = pool->first_pool, *tmp; + struct _pool_destructors *destructor = pool->destructors; + + /* Call all pool destructors */ + while (destructor) { + destructor->func (destructor->data); + destructor = destructor->prev; + } + while (cur) { tmp = cur; cur = cur->next; diff --git a/mem_pool.h b/mem_pool.h index f5d3e42386..d20a5edcd3 100644 --- a/mem_pool.h +++ b/mem_pool.h @@ -3,15 +3,24 @@ #include #include + +typedef void (*pool_destruct_func)(void *ptr); + struct _pool_chain { - u_char *begin; - u_char *pos; - size_t len; - struct _pool_chain *next; + u_char *begin; + u_char *pos; + size_t len; + struct _pool_chain *next; +}; +struct _pool_destructors { + pool_destruct_func func; + void *data; + struct _pool_destructors *prev; }; typedef struct memory_pool_s { struct _pool_chain *cur_pool; struct _pool_chain *first_pool; + struct _pool_destructors *destructors; } memory_pool_t; typedef struct memory_pool_stat_s { @@ -24,6 +33,7 @@ memory_pool_t* memory_pool_new (size_t size); void* memory_pool_alloc (memory_pool_t* pool, size_t size); void* memory_pool_alloc0 (memory_pool_t* pool, size_t size); char* memory_pool_strdup (memory_pool_t* pool, const char *src); +void memory_pool_add_destructor (memory_pool_t *pool, pool_destruct_func func, void *data); void memory_pool_delete (memory_pool_t* pool); void memory_pool_stat (memory_pool_stat_t *st);