diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-08-25 20:16:38 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-08-25 20:16:38 +0400 |
commit | e2bd3a4644aa0317356e801df1a2fda1d998df43 (patch) | |
tree | 245ea3f28ce772f4821a23b595d61e54a1cd7617 /source/blender/blenlib/intern/BLI_mempool.c | |
parent | 28243b2e5fd272d12c4349c7998b39893c20628c (diff) |
clearing the mempool can now keep more then a single element reserved.
Diffstat (limited to 'source/blender/blenlib/intern/BLI_mempool.c')
-rw-r--r-- | source/blender/blenlib/intern/BLI_mempool.c | 102 |
1 files changed, 67 insertions, 35 deletions
diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c index 8a05ade76ef..1c3c1cd8d7a 100644 --- a/source/blender/blenlib/intern/BLI_mempool.c +++ b/source/blender/blenlib/intern/BLI_mempool.c @@ -87,6 +87,7 @@ struct BLI_mempool { /* keeps aligned to 16 bits */ BLI_freenode *free; /* free element list. Interleaved into chunk datas. */ + int maxchunks; /* use to know how many chunks to keep for BLI_mempool_clear */ int totused; /* number of elements currently in use */ #ifdef USE_TOTALLOC int totalloc; /* number of elements allocated in total */ @@ -101,6 +102,14 @@ struct BLI_mempool { # define CHUNK_DATA(chunk) (CHECK_TYPE_INLINE(chunk, BLI_mempool_chunk *), (void *)((chunk) + 1)) #endif +/** + * \return the number of chunks to allocate based on how many elements are needed. + */ +BLI_INLINE int mempool_maxchunks(const int totelem, const int pchunk) +{ + return totelem / pchunk + 1; +} + static BLI_mempool_chunk *mempool_chunk_alloc(BLI_mempool *pool) { BLI_mempool_chunk *mpchunk; @@ -183,35 +192,37 @@ static BLI_freenode *mempool_chunk_add(BLI_mempool *pool, BLI_mempool_chunk *mpc return curnode; } -static void mempool_chunk_free_all(BLI_mempool *pool) +static void mempool_chunk_free(BLI_mempool_chunk *mpchunk, const int flag) { - BLI_mempool_chunk *mpchunk, *mpchunk_next; - - if (pool->flag & BLI_MEMPOOL_SYSMALLOC) { - for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk_next) { - mpchunk_next = mpchunk->next; + if (flag & BLI_MEMPOOL_SYSMALLOC) { #ifdef USE_DATA_PTR - free(CHUNK_DATA(mpchunk)); + free(CHUNK_DATA(mpchunk)); #endif - free(mpchunk); - } + free(mpchunk); } else { - for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk_next) { - mpchunk_next = mpchunk->next; #ifdef USE_DATA_PTR - MEM_freeN(CHUNK_DATA(mpchunk)); + MEM_freeN(CHUNK_DATA(mpchunk)); #endif - MEM_freeN(mpchunk); - } + MEM_freeN(mpchunk); } - pool->chunks.first = pool->chunks.last = NULL; +} + +static void mempool_chunk_free_all(ListBase *chunks, const int flag) +{ + BLI_mempool_chunk *mpchunk, *mpchunk_next; + + for (mpchunk = chunks->first; mpchunk; mpchunk = mpchunk_next) { + mpchunk_next = mpchunk->next; + mempool_chunk_free(mpchunk, flag); + } + chunks->first = chunks->last = NULL; } BLI_mempool *BLI_mempool_create(int esize, int totelem, int pchunk, int flag) { BLI_mempool *pool = NULL; - BLI_freenode *lasttail = NULL, *curnode = NULL; + BLI_freenode *lasttail = NULL; int i, maxchunks; /* allocate the pool structure */ @@ -234,29 +245,23 @@ BLI_mempool *BLI_mempool_create(int esize, int totelem, int pchunk, int flag) pool->esize = esize; } + maxchunks = mempool_maxchunks(totelem, pchunk); + pool->flag = flag; pool->pchunk = pchunk; pool->csize = esize * pchunk; pool->chunks.first = pool->chunks.last = NULL; pool->free = NULL; /* mempool_chunk_add assigns */ + pool->maxchunks = maxchunks; #ifdef USE_TOTALLOC pool->totalloc = 0; #endif pool->totused = 0; - maxchunks = totelem / pchunk + 1; - if (maxchunks == 0) { - maxchunks = 1; - } - /* allocate the actual chunks */ for (i = 0; i < maxchunks; i++) { BLI_mempool_chunk *mpchunk = mempool_chunk_alloc(pool); - - curnode = mempool_chunk_add(pool, mpchunk, lasttail); - - /* set the end of this chunks memory to the new tail for next iteration */ - lasttail = curnode; + lasttail = mempool_chunk_add(pool, mpchunk, lasttail); } return pool; @@ -338,7 +343,7 @@ void BLI_mempool_free(BLI_mempool *pool, void *addr) BLI_mempool_chunk *first = pool->chunks.first; BLI_remlink(&pool->chunks, first); - mempool_chunk_free_all(pool); + mempool_chunk_free_all(&pool->chunks, pool->flag); BLI_addtail(&pool->chunks, first); #ifdef USE_TOTALLOC pool->totalloc = pool->pchunk; @@ -504,22 +509,49 @@ void *BLI_mempool_iterstep(BLI_mempool_iter *iter) #endif -void BLI_mempool_clear(BLI_mempool *pool) +void BLI_mempool_clear_ex(BLI_mempool *pool, const int totelem_reserve) { - BLI_mempool_chunk *first = pool->chunks.first; + BLI_mempool_chunk *mpchunk; + BLI_mempool_chunk *mpchunk_next; + int maxchunks; + + ListBase chunks_temp; + BLI_freenode *lasttail = NULL; + + if (totelem_reserve == -1) { + maxchunks = pool->maxchunks; + } + else { + maxchunks = mempool_maxchunks(totelem_reserve, pool->pchunk); + } - BLI_remlink(&pool->chunks, first); + /* free all after pool->maxchunks */ - mempool_chunk_free_all(pool); + for (mpchunk = BLI_findlink(&pool->chunks, maxchunks); mpchunk; mpchunk = mpchunk_next) { + mpchunk_next = mpchunk->next; + BLI_remlink(&pool->chunks, mpchunk); + mempool_chunk_free(mpchunk, pool->flag); + } - /* important for re-initializing */ + /* re-initialize */ + pool->free = NULL; pool->totused = 0; #ifdef USE_TOTALLOC pool->totalloc = 0; #endif - pool->free = NULL; - mempool_chunk_add(pool, first, NULL); + chunks_temp = pool->chunks; + pool->chunks.first = pool->chunks.last = NULL; + + while ((mpchunk = chunks_temp.first)) { + BLI_remlink(&chunks_temp, mpchunk); + lasttail = mempool_chunk_add(pool, mpchunk, lasttail); + } +} + +void BLI_mempool_clear(BLI_mempool *pool) +{ + return BLI_mempool_clear_ex(pool, -1); } /** @@ -527,7 +559,7 @@ void BLI_mempool_clear(BLI_mempool *pool) */ void BLI_mempool_destroy(BLI_mempool *pool) { - mempool_chunk_free_all(pool); + mempool_chunk_free_all(&pool->chunks, pool->flag); if (pool->flag & BLI_MEMPOOL_SYSMALLOC) { free(pool); |