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/blenkernel/intern/customdata.c | 2 +- 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 +- source/blender/bmesh/intern/bmesh_mesh.c | 10 +-- source/blender/bmesh/intern/bmesh_operators.c | 4 +- source/blender/bmesh/intern/bmesh_walkers.c | 2 +- source/blender/bmesh/operators/bmo_create.c | 4 +- source/blender/editors/mesh/knifetool.c | 6 +- source/blender/imbuf/intern/moviecache.c | 6 +- 11 files changed, 86 insertions(+), 50 deletions(-) (limited to 'source/blender') diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c index a3c4b943215..7b1b5e7dd37 100644 --- a/source/blender/blenkernel/intern/customdata.c +++ b/source/blender/blenkernel/intern/customdata.c @@ -2132,7 +2132,7 @@ void CustomData_bmesh_init_pool(CustomData *data, int totelem, const char htype) /* If there are no layers, no pool is needed just yet */ if (data->totlayer) { - data->pool = BLI_mempool_create(data->totsize, totelem, chunksize, TRUE, FALSE); + data->pool = BLI_mempool_create(data->totsize, totelem, chunksize, BLI_MEMPOOL_SYSMALLOC); } } 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; } diff --git a/source/blender/bmesh/intern/bmesh_mesh.c b/source/blender/bmesh/intern/bmesh_mesh.c index 3caeb2a8fbc..b8d30f72509 100644 --- a/source/blender/bmesh/intern/bmesh_mesh.c +++ b/source/blender/bmesh/intern/bmesh_mesh.c @@ -48,17 +48,17 @@ BMAllocTemplate bm_mesh_allocsize_default = {512, 1024, 2048, 512}; static void bm_mempool_init(BMesh *bm, const BMAllocTemplate *allocsize) { - bm->vpool = BLI_mempool_create(sizeof(BMVert), allocsize->totvert, allocsize->totvert, FALSE, TRUE); - bm->epool = BLI_mempool_create(sizeof(BMEdge), allocsize->totedge, allocsize->totedge, FALSE, TRUE); - bm->lpool = BLI_mempool_create(sizeof(BMLoop), allocsize->totloop, allocsize->totloop, FALSE, FALSE); - bm->fpool = BLI_mempool_create(sizeof(BMFace), allocsize->totface, allocsize->totface, FALSE, TRUE); + bm->vpool = BLI_mempool_create(sizeof(BMVert), allocsize->totvert, allocsize->totvert, BLI_MEMPOOL_ALLOW_ITER); + bm->epool = BLI_mempool_create(sizeof(BMEdge), allocsize->totedge, allocsize->totedge, BLI_MEMPOOL_ALLOW_ITER); + bm->lpool = BLI_mempool_create(sizeof(BMLoop), allocsize->totloop, allocsize->totloop, 0); + bm->fpool = BLI_mempool_create(sizeof(BMFace), allocsize->totface, allocsize->totface, BLI_MEMPOOL_ALLOW_ITER); #ifdef USE_BMESH_HOLES bm->looplistpool = BLI_mempool_create(sizeof(BMLoopList), allocsize[3], allocsize[3], FALSE, FALSE); #endif /* allocate one flag pool that we dont get rid of. */ - bm->toolflagpool = BLI_mempool_create(sizeof(BMFlagLayer), 512, 512, FALSE, FALSE); + bm->toolflagpool = BLI_mempool_create(sizeof(BMFlagLayer), 512, 512, 0); } /** diff --git a/source/blender/bmesh/intern/bmesh_operators.c b/source/blender/bmesh/intern/bmesh_operators.c index 31b291f7a36..8374be3ee5b 100644 --- a/source/blender/bmesh/intern/bmesh_operators.c +++ b/source/blender/bmesh/intern/bmesh_operators.c @@ -933,7 +933,7 @@ static void bmo_flag_layer_alloc(BMesh *bm) bm->totflags++; /* allocate new flag poo */ - bm->toolflagpool = newpool = BLI_mempool_create(sizeof(BMFlagLayer) * bm->totflags, 512, 512, FALSE, FALSE); + bm->toolflagpool = newpool = BLI_mempool_create(sizeof(BMFlagLayer) * bm->totflags, 512, 512, 0); /* now go through and memcpy all the flags. Loops don't get a flag layer at this time.. */ for (ele = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, bm), i = 0; ele; ele = BM_iter_step(&iter), i++) { @@ -978,7 +978,7 @@ static void bmo_flag_layer_free(BMesh *bm) /* de-increment the totflags first.. */ bm->totflags--; /* allocate new flag poo */ - bm->toolflagpool = newpool = BLI_mempool_create(new_totflags_size, 512, 512, TRUE, FALSE); + bm->toolflagpool = newpool = BLI_mempool_create(new_totflags_size, 512, 512, BLI_MEMPOOL_SYSMALLOC); /* now go through and memcpy all the flag */ for (ele = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, bm), i = 0; ele; ele = BM_iter_step(&iter), i++) { diff --git a/source/blender/bmesh/intern/bmesh_walkers.c b/source/blender/bmesh/intern/bmesh_walkers.c index f18a0835e5e..f20c91c4128 100644 --- a/source/blender/bmesh/intern/bmesh_walkers.c +++ b/source/blender/bmesh/intern/bmesh_walkers.c @@ -112,7 +112,7 @@ void BMW_init(BMWalker *walker, BMesh *bm, int type, BLI_assert(mask_face == 0 || (walker->valid_mask & BM_FACE)); } - walker->worklist = BLI_mempool_create(walker->structsize, 100, 100, TRUE, FALSE); + walker->worklist = BLI_mempool_create(walker->structsize, 100, 100, BLI_MEMPOOL_SYSMALLOC); walker->states.first = walker->states.last = NULL; } diff --git a/source/blender/bmesh/operators/bmo_create.c b/source/blender/bmesh/operators/bmo_create.c index 93121a6198b..67e4f09463d 100644 --- a/source/blender/bmesh/operators/bmo_create.c +++ b/source/blender/bmesh/operators/bmo_create.c @@ -621,8 +621,8 @@ static PathBase *edge_pathbase_new(void) { PathBase *pb = MEM_callocN(sizeof(PathBase), "PathBase"); - pb->nodepool = BLI_mempool_create(sizeof(EPathNode), 1, 512, TRUE, FALSE); - pb->pathpool = BLI_mempool_create(sizeof(EPath), 1, 512, TRUE, FALSE); + pb->nodepool = BLI_mempool_create(sizeof(EPathNode), 1, 512, BLI_MEMPOOL_SYSMALLOC); + pb->pathpool = BLI_mempool_create(sizeof(EPath), 1, 512, BLI_MEMPOOL_SYSMALLOC); return pb; } diff --git a/source/blender/editors/mesh/knifetool.c b/source/blender/editors/mesh/knifetool.c index ba0067faaaa..86e02d44de9 100644 --- a/source/blender/editors/mesh/knifetool.c +++ b/source/blender/editors/mesh/knifetool.c @@ -2076,9 +2076,9 @@ static int knifetool_init(bContext *C, wmOperator *op, int UNUSED(do_cut)) ED_region_tag_redraw(kcd->ar); - kcd->refs = BLI_mempool_create(sizeof(Ref), 1, 2048, FALSE, FALSE); - kcd->kverts = BLI_mempool_create(sizeof(KnifeVert), 1, 512, FALSE, TRUE); - kcd->kedges = BLI_mempool_create(sizeof(KnifeEdge), 1, 512, FALSE, TRUE); + kcd->refs = BLI_mempool_create(sizeof(Ref), 1, 2048, 0); + kcd->kverts = BLI_mempool_create(sizeof(KnifeVert), 1, 512, BLI_MEMPOOL_ALLOW_ITER); + kcd->kedges = BLI_mempool_create(sizeof(KnifeEdge), 1, 512, BLI_MEMPOOL_ALLOW_ITER); kcd->origedgemap = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "knife origedgemap"); kcd->origvertmap = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "knife origvertmap"); diff --git a/source/blender/imbuf/intern/moviecache.c b/source/blender/imbuf/intern/moviecache.c index fd0a0372a9c..67d9a602d12 100644 --- a/source/blender/imbuf/intern/moviecache.c +++ b/source/blender/imbuf/intern/moviecache.c @@ -204,9 +204,9 @@ struct MovieCache *IMB_moviecache_create(int keysize, GHashHashFP hashfp, GHashC MovieCache *cache; cache= MEM_callocN(sizeof(MovieCache), "MovieCache"); - cache->keys_pool= BLI_mempool_create(sizeof(MovieCacheKey), 64, 64, FALSE, FALSE); - cache->items_pool= BLI_mempool_create(sizeof(MovieCacheItem), 64, 64, FALSE, FALSE); - cache->userkeys_pool= BLI_mempool_create(keysize, 64, 64, FALSE, FALSE); + cache->keys_pool= BLI_mempool_create(sizeof(MovieCacheKey), 64, 64, 0); + cache->items_pool= BLI_mempool_create(sizeof(MovieCacheItem), 64, 64, 0); + cache->userkeys_pool= BLI_mempool_create(keysize, 64, 64, 0); cache->hash= BLI_ghash_new(moviecache_hashhash, moviecache_hashcmp, "MovieClip ImBuf cache hash"); cache->keysize= keysize; -- cgit v1.2.3