BLI_mempool_findelem() only worked when no elements were freed, use the iterator for now.

This commit is contained in:
2011-11-27 21:11:17 +00:00
parent 8a37378adb
commit 1ca7c2e4f3
2 changed files with 13 additions and 12 deletions

View File

@@ -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*/

View File

@@ -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;