diff options
-rw-r--r-- | source/blender/blenlib/intern/BLI_memarena.c | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/source/blender/blenlib/intern/BLI_memarena.c b/source/blender/blenlib/intern/BLI_memarena.c index 12b4b5350ef..71260086afa 100644 --- a/source/blender/blenlib/intern/BLI_memarena.c +++ b/source/blender/blenlib/intern/BLI_memarena.c @@ -36,17 +36,21 @@ #include "BLI_utildefines.h" #include "BLI_memarena.h" -#include "BLI_linklist.h" #include "BLI_strict_flags.h" #ifdef WITH_MEM_VALGRIND # include "valgrind/memcheck.h" #endif +struct MemBuf { + struct MemBuf *next; + uchar data[0]; +}; + struct MemArena { unsigned char *curbuf; const char *name; - LinkNode *bufs; + struct MemBuf *bufs; size_t bufsize, cursize; size_t align; @@ -54,6 +58,15 @@ struct MemArena { bool use_calloc; }; +static void memarena_buf_free_all(struct MemBuf *mb) +{ + while (mb != NULL) { + struct MemBuf *mb_next = mb->next; + MEM_freeN(mb); + mb = mb_next; + } +} + MemArena *BLI_memarena_new(const size_t bufsize, const char *name) { MemArena *ma = MEM_callocN(sizeof(*ma), "memarena"); @@ -86,8 +99,7 @@ void BLI_memarena_use_align(struct MemArena *ma, const size_t align) void BLI_memarena_free(MemArena *ma) { - BLI_linklist_freeN(ma->bufs); - + memarena_buf_free_all(ma->bufs); #ifdef WITH_MEM_VALGRIND VALGRIND_DESTROY_MEMPOOL(ma); #endif @@ -124,8 +136,11 @@ void *BLI_memarena_alloc(MemArena *ma, size_t size) ma->cursize = ma->bufsize; } - ma->curbuf = (ma->use_calloc ? MEM_callocN : MEM_mallocN)(ma->cursize, ma->name); - BLI_linklist_prepend(&ma->bufs, ma->curbuf); + struct MemBuf *mb = (ma->use_calloc ? MEM_callocN : MEM_mallocN)(sizeof(*mb) + ma->cursize, ma->name); + ma->curbuf = mb->data; + mb->next = ma->bufs; + ma->bufs = mb; + memarena_curbuf_align(ma); } @@ -164,12 +179,12 @@ void BLI_memarena_clear(MemArena *ma) size_t curbuf_used; if (ma->bufs->next) { - BLI_linklist_freeN(ma->bufs->next); + memarena_buf_free_all(ma->bufs->next); ma->bufs->next = NULL; } curbuf_prev = ma->curbuf; - ma->curbuf = ma->bufs->link; + ma->curbuf = ma->bufs->data; memarena_curbuf_align(ma); /* restore to original size */ |