diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2016-01-07 13:21:53 +0100 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2016-01-08 13:00:45 +0100 |
commit | 68e76ae63605193e89b05eab90dc6fdfa6f37053 (patch) | |
tree | 7f6205599463cfa44470baf84be62b00dc4c5655 /source/fitz | |
parent | 83d3ae68a281981bb1193adda61ee42f89810dbd (diff) | |
download | mupdf-68e76ae63605193e89b05eab90dc6fdfa6f37053.tar.xz |
Add pool allocator.
Diffstat (limited to 'source/fitz')
-rw-r--r-- | source/fitz/pool.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/source/fitz/pool.c b/source/fitz/pool.c new file mode 100644 index 00000000..e676947f --- /dev/null +++ b/source/fitz/pool.c @@ -0,0 +1,44 @@ +#include "mupdf/fitz.h" + +fz_pool *fz_new_pool(fz_context *ctx) +{ + fz_pool *pool = fz_malloc_struct(ctx, fz_pool); + fz_pool_node *node = fz_malloc_struct(ctx, fz_pool_node); + pool->head = pool->tail = node; + pool->pos = node->mem; + pool->end = node->mem + sizeof node->mem; + return pool; +} + +void *fz_pool_alloc(fz_context *ctx, fz_pool *pool, size_t size) +{ + char *ptr; + + /* round size to pointer alignment (we don't expect to use doubles) */ + size = ((size + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*); + + if (pool->pos + size > pool->end) + { + fz_pool_node *node = fz_malloc_struct(ctx, fz_pool_node); + pool->tail = pool->tail->next = node; + pool->pos = node->mem; + pool->end = node->mem + sizeof node->mem; + if (pool->pos + size > pool->end) + fz_throw(ctx, FZ_ERROR_GENERIC, "out of memory: allocation too large to fit in pool"); + } + ptr = pool->pos; + pool->pos += size; + return ptr; +} + +void fz_drop_pool(fz_context *ctx, fz_pool *pool) +{ + fz_pool_node *node = pool->head; + while (node) + { + fz_pool_node *next = node->next; + fz_free(ctx, node); + node = next; + } + fz_free(ctx, pool); +} |