diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-11-28 01:11:17 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-11-28 01:11:17 +0400 |
commit | 1ca7c2e4f3b8dd224782f5c05333fd327d7bc3b3 (patch) | |
tree | 2ce8210bc49150fbac29ef27940ecc645e917d42 /source | |
parent | 8a37378adbb4383de98171608f5dd1e55234359a (diff) |
BLI_mempool_findelem() only worked when no elements were freed, use the iterator for now.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenlib/BLI_mempool.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_mempool.c | 23 |
2 files changed, 13 insertions, 12 deletions
diff --git a/source/blender/blenlib/BLI_mempool.h b/source/blender/blenlib/BLI_mempool.h index a46ba78aef0..77896709285 100644 --- a/source/blender/blenlib/BLI_mempool.h +++ b/source/blender/blenlib/BLI_mempool.h @@ -55,7 +55,7 @@ void *BLI_mempool_calloc(BLI_mempool *pool); void BLI_mempool_free(BLI_mempool *pool, void *addr); void BLI_mempool_destroy(BLI_mempool *pool); int BLI_mempool_count(BLI_mempool *pool); -void *BLI_mempool_findelem(BLI_mempool *pool, const int index); +void *BLI_mempool_findelem(BLI_mempool *pool, int index); /** iteration stuff. note: this may easy to produce bugs with **/ /*private structure*/ diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c index f8da524d097..0060878587b 100644 --- a/source/blender/blenlib/intern/BLI_mempool.c +++ b/source/blender/blenlib/intern/BLI_mempool.c @@ -241,18 +241,19 @@ void BLI_mempool_free(BLI_mempool *pool, void *addr) } } -void *BLI_mempool_findelem(BLI_mempool *pool, const int index) +void *BLI_mempool_findelem(BLI_mempool *pool, int index) { - if ((index >= 0) && (index < pool->totused)) { - BLI_mempool_chunk *mpchunk; - int i= 0; - - for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { - if (index < i + pool->pchunk) { - return ((char *)mpchunk->data) + (pool->esize * (index - i)); - } - i += pool->pchunk; - } + if (!pool->allow_iter) { + fprintf(stderr, "%s: Error! you can't iterate over this mempool!\n", __func__); + return NULL; + } + else if ((index >= 0) && (index < pool->totused)) { + /* we could have some faster mem chunk stepping code inline */ + BLI_mempool_iter iter; + void *elem; + BLI_mempool_iternew(pool, &iter); + for (elem= BLI_mempool_iterstep(&iter); index-- != 0; elem= BLI_mempool_iterstep(&iter)) { }; + return elem; } return NULL; |