aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcebka@mailsupport.rambler.ru <cebka@mailsupport.rambler.ru>2008-09-18 19:46:21 +0400
committercebka@mailsupport.rambler.ru <cebka@mailsupport.rambler.ru>2008-09-18 19:46:21 +0400
commitdb82a6ee3abfa4191fa54a193a0107c5a27748ba (patch)
treee65cdec540c30bb991c798c51e22b8fb79bb5d30
parent2c879bedd2c9b835fb083a152e5f09d48b1dbca6 (diff)
downloadrspamd-db82a6ee3abfa4191fa54a193a0107c5a27748ba.tar.gz
rspamd-db82a6ee3abfa4191fa54a193a0107c5a27748ba.zip
* Avoid realloc for memory pools by making memory pool chains
-rw-r--r--mem_pool.c40
-rw-r--r--mem_pool.h6
-rw-r--r--worker.c2
3 files changed, 31 insertions, 17 deletions
diff --git a/mem_pool.c b/mem_pool.c
index 8a6c9d0a5..3c583d07c 100644
--- a/mem_pool.c
+++ b/mem_pool.c
@@ -11,6 +11,7 @@ memory_pool_new (size_t size)
new->begin = g_malloc (size);
new->len = size;
new->pos = new->begin;
+ new->next = NULL;
return new;
}
@@ -18,31 +19,42 @@ memory_pool_new (size_t size)
void *
memory_pool_alloc (memory_pool_t *pool, size_t size)
{
- u_char *tmp;
+ u_char *tmp;
+ memory_pool_t *new, *cur;
+
if (pool) {
- if (pool->len < (pool->pos - pool->begin) + size) {
- /* Grow pool */
- if (pool->len > size) {
- pool->len *= 2;
+ cur = pool;
+ /* Find free space in pool chain */
+ while (memory_pool_free (cur) < size && cur->next) {
+ cur = cur->next;
+ }
+ if (cur->next == NULL) {
+ /* Allocate new pool */
+ if (cur->len > size) {
+ new = memory_pool_new (cur->len);
}
else {
- pool->len += size + pool->len;
+ new = memory_pool_new (size + cur->len);
}
- pool->begin = g_realloc (pool->begin, pool->len);
- return memory_pool_alloc (pool, size);
+ cur->next = new;
+ new->pos += size;
+ return new->begin;
}
- tmp = pool->pos;
- pool->pos += size;
+ tmp = cur->pos;
+ cur->pos += size;
return tmp;
}
return NULL;
}
-void memory_pool_free (memory_pool_t *pool)
+void memory_pool_delete (memory_pool_t *pool)
{
- if (pool) {
- g_free (pool->begin);
- g_free (pool);
+ memory_pool_t *cur = pool, *tmp;
+ while (cur) {
+ tmp = cur;
+ cur = cur->next;
+ g_free (tmp->begin);
+ g_free (tmp);
}
}
diff --git a/mem_pool.h b/mem_pool.h
index e86e418dd..fc487c97c 100644
--- a/mem_pool.h
+++ b/mem_pool.h
@@ -8,11 +8,13 @@ typedef struct memory_pool_s {
u_char *begin;
u_char *pos;
size_t len;
- size_t used;
+ struct memory_pool_s *next;
} 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);
+void memory_pool_delete (memory_pool_t* pool);
+
+#define memory_pool_free(x) ((x)->pos - (x)->begin)
#endif
diff --git a/worker.c b/worker.c
index 8493aff25..ea22ffbe2 100644
--- a/worker.c
+++ b/worker.c
@@ -98,7 +98,7 @@ free_task (struct worker_task *task)
free (task->memc_ctx);
}
if (task->task_pool) {
- memory_pool_free (task->task_pool);
+ memory_pool_delete (task->task_pool);
}
while (!TAILQ_EMPTY (&task->urls)) {
cur = TAILQ_FIRST (&task->urls);