aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cfg_utils.c1
-rw-r--r--mem_pool.c23
-rw-r--r--mem_pool.h18
3 files changed, 38 insertions, 4 deletions
diff --git a/cfg_utils.c b/cfg_utils.c
index 3f46b0dd8..7f64e4552 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 fc14af790..56fc0ef78 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;
}
@@ -112,9 +113,31 @@ 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)
+{
+ 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 f5d3e4238..d20a5edcd 100644
--- a/mem_pool.h
+++ b/mem_pool.h
@@ -3,15 +3,24 @@
#include <sys/types.h>
#include <glib.h>
+
+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);