From bc767059cb21d2763067e304f6905c11c4eb5074 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 1 Mar 2012 22:59:18 +0000 Subject: Code Cleanup: update to mempool, use flag rather then bool args. --- source/blender/blenlib/BLI_mempool.h | 9 ++- source/blender/blenlib/intern/BLI_ghash.c | 2 +- source/blender/blenlib/intern/BLI_mempool.c | 89 +++++++++++++++++++---------- source/blender/blenlib/intern/edgehash.c | 2 +- 4 files changed, 69 insertions(+), 33 deletions(-) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_mempool.h b/source/blender/blenlib/BLI_mempool.h index 0b190c63559..ea3305ab04b 100644 --- a/source/blender/blenlib/BLI_mempool.h +++ b/source/blender/blenlib/BLI_mempool.h @@ -48,8 +48,7 @@ typedef struct BLI_mempool BLI_mempool; * first four bytes of the elements never contain the character string * 'free'. use with care.*/ -BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, - short use_sysmalloc, short allow_iter); +BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, int flag); void *BLI_mempool_alloc(BLI_mempool *pool); void *BLI_mempool_calloc(BLI_mempool *pool); void BLI_mempool_free(BLI_mempool *pool, void *addr); @@ -65,6 +64,12 @@ typedef struct BLI_mempool_iter { int curindex; } BLI_mempool_iter; +/* flag */ +enum { + BLI_MEMPOOL_SYSMALLOC = (1 << 0), + BLI_MEMPOOL_ALLOW_ITER = (1 << 1) +}; + void BLI_mempool_iternew(BLI_mempool *pool, BLI_mempool_iter *iter); void *BLI_mempool_iterstep(BLI_mempool_iter *iter); diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index 943b67cce8e..b05a1c00d0f 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -61,7 +61,7 @@ GHash *BLI_ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info) GHash *gh= MEM_mallocN(sizeof(*gh), info); gh->hashfp= hashfp; gh->cmpfp= cmpfp; - gh->entrypool = BLI_mempool_create(sizeof(Entry), 64, 64, FALSE, FALSE); + gh->entrypool = BLI_mempool_create(sizeof(Entry), 64, 64, 0); gh->cursize= 0; gh->nentries= 0; diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c index aac36e5b79b..1b3f02804f2 100644 --- a/source/blender/blenlib/intern/BLI_mempool.c +++ b/source/blender/blenlib/intern/BLI_mempool.c @@ -71,7 +71,7 @@ struct BLI_mempool { int esize; /* element size in bytes */ int csize; /* chunk size in bytes */ int pchunk; /* number of elements per chunk */ - short use_sysmalloc, allow_iter; + int flag; /* keeps aligned to 16 bits */ BLI_freenode *free; /* free element list. Interleaved into chunk datas.*/ @@ -81,8 +81,7 @@ struct BLI_mempool { #define MEMPOOL_ELEM_SIZE_MIN (sizeof(void *) * 2) -BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, - short use_sysmalloc, short allow_iter) +BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, int flag) { BLI_mempool *pool = NULL; BLI_freenode *lasttail = NULL, *curnode = NULL; @@ -93,29 +92,38 @@ BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, esize = MEMPOOL_ELEM_SIZE_MIN; /*allocate the pool structure*/ - pool = use_sysmalloc ? malloc(sizeof(BLI_mempool)) : MEM_mallocN(sizeof(BLI_mempool), "memory pool"); - pool->esize = allow_iter ? MAX2(esize, sizeof(BLI_freenode)) : esize; - pool->use_sysmalloc = use_sysmalloc; + pool = (flag & BLI_MEMPOOL_SYSMALLOC) ? malloc(sizeof(BLI_mempool)) : MEM_mallocN(sizeof(BLI_mempool), "memory pool"); + pool->esize = (flag & BLI_MEMPOOL_ALLOW_ITER) ? MAX2(esize, sizeof(BLI_freenode)) : esize; + pool->flag = flag; pool->pchunk = pchunk; pool->csize = esize * pchunk; pool->chunks.first = pool->chunks.last = NULL; pool->totused= 0; - pool->allow_iter= allow_iter; maxchunks = tote / pchunk + 1; if (maxchunks==0) maxchunks = 1; - /*allocate the actual chunks*/ - for (i=0; i < maxchunks; i++) { - BLI_mempool_chunk *mpchunk = use_sysmalloc ? malloc(sizeof(BLI_mempool_chunk)) : MEM_mallocN(sizeof(BLI_mempool_chunk), "BLI_Mempool Chunk"); + /* allocate the actual chunks */ + for (i = 0; i < maxchunks; i++) { + BLI_mempool_chunk *mpchunk; + + if (flag & BLI_MEMPOOL_SYSMALLOC) { + mpchunk = malloc(sizeof(BLI_mempool_chunk)); + mpchunk->data = malloc(pool->csize); + } + else { + mpchunk = MEM_mallocN(sizeof(BLI_mempool_chunk), "BLI_Mempool Chunk"); + mpchunk->data = MEM_mallocN(pool->csize, "BLI Mempool Chunk Data"); + } + mpchunk->next = mpchunk->prev = NULL; - mpchunk->data = use_sysmalloc ? malloc(pool->csize) : MEM_mallocN(pool->csize, "BLI Mempool Chunk Data"); BLI_addtail(&(pool->chunks), mpchunk); if (i==0) { pool->free = mpchunk->data; /*start of the list*/ - if (pool->allow_iter) + if (pool->flag & BLI_MEMPOOL_ALLOW_ITER) { pool->free->freeword = FREEWORD; + } } /*loop through the allocated data, building the pointer structures*/ @@ -123,7 +131,7 @@ BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, curnode = ((BLI_freenode*)addr); addr += pool->esize; curnode->next = (BLI_freenode*)addr; - if (pool->allow_iter) { + if (pool->flag & BLI_MEMPOOL_ALLOW_ITER) { if (j != pool->pchunk-1) curnode->next->freeword = FREEWORD; curnode->freeword = FREEWORD; @@ -132,8 +140,9 @@ BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, /*final pointer in the previously allocated chunk is wrong.*/ if (lasttail) { lasttail->next = mpchunk->data; - if (pool->allow_iter) + if (pool->flag & BLI_MEMPOOL_ALLOW_ITER) { lasttail->freeword = FREEWORD; + } } /*set the end of this chunks memoryy to the new tail for next iteration*/ @@ -158,20 +167,32 @@ void *BLI_mempool_alloc(BLI_mempool *pool) int j; /*need to allocate a new chunk*/ - BLI_mempool_chunk *mpchunk = pool->use_sysmalloc ? malloc(sizeof(BLI_mempool_chunk)) : MEM_mallocN(sizeof(BLI_mempool_chunk), "BLI_Mempool Chunk"); + BLI_mempool_chunk *mpchunk; + + if (pool->flag & BLI_MEMPOOL_SYSMALLOC) { + mpchunk = malloc(sizeof(BLI_mempool_chunk)); + mpchunk->data = malloc(pool->csize); + } + else { + mpchunk = MEM_mallocN(sizeof(BLI_mempool_chunk), "BLI_Mempool Chunk"); + mpchunk->data = MEM_mallocN(pool->csize, "BLI_Mempool Chunk Data"); + } + mpchunk->next = mpchunk->prev = NULL; - mpchunk->data = pool->use_sysmalloc ? malloc(pool->csize) : MEM_mallocN(pool->csize, "BLI_Mempool Chunk Data"); BLI_addtail(&(pool->chunks), mpchunk); - pool->free = mpchunk->data; /*start of the list*/ - if (pool->allow_iter) + pool->free = mpchunk->data; /* start of the list */ + + if (pool->flag & BLI_MEMPOOL_ALLOW_ITER) { pool->free->freeword = FREEWORD; + } + for(addr = mpchunk->data, j=0; j < pool->pchunk; j++) { curnode = ((BLI_freenode*)addr); addr += pool->esize; curnode->next = (BLI_freenode*)addr; - if (pool->allow_iter) { + if (pool->flag & BLI_MEMPOOL_ALLOW_ITER) { curnode->freeword = FREEWORD; if (j != pool->pchunk-1) curnode->next->freeword = FREEWORD; @@ -183,8 +204,10 @@ void *BLI_mempool_alloc(BLI_mempool *pool) } retval = pool->free; - if (pool->allow_iter) + + if (pool->flag & BLI_MEMPOOL_ALLOW_ITER) { pool->free->freeword = 0x7FFFFFFF; + } pool->free = pool->free->next; //memset(retval, 0, pool->esize); @@ -203,8 +226,10 @@ void BLI_mempool_free(BLI_mempool *pool, void *addr) { BLI_freenode *newhead = addr; - if (pool->allow_iter) + if (pool->flag & BLI_MEMPOOL_ALLOW_ITER) { newhead->freeword = FREEWORD; + } + newhead->next = pool->free; pool->free = newhead; @@ -221,13 +246,19 @@ void BLI_mempool_free(BLI_mempool *pool, void *addr) BLI_remlink(&pool->chunks, first); - for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { - if (pool->use_sysmalloc) free(mpchunk->data); - else MEM_freeN(mpchunk->data); + if (pool->flag & BLI_MEMPOOL_SYSMALLOC) { + for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { + free(mpchunk->data); + } + BLI_freelist(&(pool->chunks)); + } + else { + for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { + MEM_freeN(mpchunk->data); + } + BLI_freelistN(&(pool->chunks)); } - pool->use_sysmalloc ? BLI_freelist(&(pool->chunks)) : BLI_freelistN(&(pool->chunks)); - BLI_addtail(&pool->chunks, first); pool->totalloc = pool->pchunk; @@ -248,7 +279,7 @@ int BLI_mempool_count(BLI_mempool *pool) void *BLI_mempool_findelem(BLI_mempool *pool, int index) { - if (!pool->allow_iter) { + if (!(pool->flag & BLI_MEMPOOL_ALLOW_ITER)) { fprintf(stderr, "%s: Error! you can't iterate over this mempool!\n", __func__); return NULL; } @@ -266,7 +297,7 @@ void *BLI_mempool_findelem(BLI_mempool *pool, int index) void BLI_mempool_iternew(BLI_mempool *pool, BLI_mempool_iter *iter) { - if (!pool->allow_iter) { + if (!(pool->flag & BLI_MEMPOOL_ALLOW_ITER)) { fprintf(stderr, "%s: Error! you can't iterate over this mempool!\n", __func__); iter->curchunk = NULL; iter->curindex = 0; @@ -346,7 +377,7 @@ void BLI_mempool_destroy(BLI_mempool *pool) { BLI_mempool_chunk *mpchunk=NULL; - if (pool->use_sysmalloc) { + if (pool->flag & BLI_MEMPOOL_SYSMALLOC) { for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { free(mpchunk->data); } diff --git a/source/blender/blenlib/intern/edgehash.c b/source/blender/blenlib/intern/edgehash.c index df23f792aa3..531a4de361e 100644 --- a/source/blender/blenlib/intern/edgehash.c +++ b/source/blender/blenlib/intern/edgehash.c @@ -83,7 +83,7 @@ EdgeHash *BLI_edgehash_new(void) eh->nbuckets = _ehash_hashsizes[eh->cursize]; eh->buckets = MEM_callocN(eh->nbuckets * sizeof(*eh->buckets), "eh buckets 2"); - eh->epool = BLI_mempool_create(sizeof(EdgeEntry), 512, 512, TRUE, FALSE); + eh->epool = BLI_mempool_create(sizeof(EdgeEntry), 512, 512, BLI_MEMPOOL_SYSMALLOC); return eh; } -- cgit v1.2.3