diff options
Diffstat (limited to 'mem_pool.c')
-rw-r--r-- | mem_pool.c | 35 |
1 files changed, 25 insertions, 10 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); } /* |