diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-08-04 23:40:50 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-08-04 23:40:50 +0400 |
commit | 2497084744b9a106cf200f8f3afa20557e6070b8 (patch) | |
tree | b6b2036aab1cc5ccd3da68bb1a4d74dca31ca5b5 | |
parent | fdc336510a16496bf0e215604a5afd976e613940 (diff) |
mempool api cleanup: differentiate mempool functions that allocate a pointer lookup table from allocating the data as a contiguous array,
call these functions BLI_mempool_as_table(), BLI_mempool_as_array(), the N prefixed versions of these functions return newly allocated arrays.
-rw-r--r-- | source/blender/blenlib/BLI_mempool.h | 19 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_mempool.c | 47 |
2 files changed, 49 insertions, 17 deletions
diff --git a/source/blender/blenlib/BLI_mempool.h b/source/blender/blenlib/BLI_mempool.h index a1cbad73239..f8775342e94 100644 --- a/source/blender/blenlib/BLI_mempool.h +++ b/source/blender/blenlib/BLI_mempool.h @@ -86,13 +86,26 @@ __attribute__((warn_unused_result)) __attribute__((nonnull(1))) #endif ; -void BLI_mempool_as_array(BLI_mempool *pool, void **data) +void BLI_mempool_as_table(BLI_mempool *pool, void **data) #ifdef __GNUC__ -__attribute__((nonnull(1))) +__attribute__((nonnull(1, 2))) +#endif +; + +void **BLI_mempool_as_tableN(BLI_mempool *pool, const char *allocstr) +#ifdef __GNUC__ +__attribute__((warn_unused_result)) +__attribute__((nonnull(1, 2))) +#endif +; + +void BLI_mempool_as_array(BLI_mempool *pool, void *data) +#ifdef __GNUC__ +__attribute__((nonnull(1, 2))) #endif ; -void *BLI_mempool_as_arrayN(BLI_mempool *pool, const char *allocstr) +void *BLI_mempool_as_arrayN(BLI_mempool *pool, const char *allocstr) #ifdef __GNUC__ __attribute__((warn_unused_result)) __attribute__((nonnull(1, 2))) diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c index f370f32c31d..bb326a23d59 100644 --- a/source/blender/blenlib/intern/BLI_mempool.c +++ b/source/blender/blenlib/intern/BLI_mempool.c @@ -334,38 +334,57 @@ void *BLI_mempool_findelem(BLI_mempool *pool, int index) } /** + * Fill in \a data with pointers to each element of the mempool, + * to create lookup table. + * * \param data array of pointers at least the size of 'pool->totused' */ -void BLI_mempool_as_array(BLI_mempool *pool, void **data) +void BLI_mempool_as_table(BLI_mempool *pool, void **data) { BLI_mempool_iter iter; void *elem; void **p = data; BLI_assert(pool->flag & BLI_MEMPOOL_ALLOW_ITER); BLI_mempool_iternew(pool, &iter); - for (elem = BLI_mempool_iterstep(&iter); elem; elem = BLI_mempool_iterstep(&iter)) { + while ((elem = BLI_mempool_iterstep(&iter))) { *p++ = elem; } BLI_assert((p - data) == pool->totused); } /** - * Allocate an array from the mempool. + * A version of #BLI_mempool_as_table that allocates and returns the data. */ -void *BLI_mempool_as_arrayN(BLI_mempool *pool, const char *allocstr) +void **BLI_mempool_as_tableN(BLI_mempool *pool, const char *allocstr) { - char *data = MEM_mallocN((size_t)(pool->totused * pool->esize), allocstr); + void **data = MEM_mallocN((size_t)pool->totused * sizeof(void *), allocstr); + BLI_mempool_as_table(pool, data); + return data; +} + +/** + * Fill in \a data with the contents of the mempool. + */ +void BLI_mempool_as_array(BLI_mempool *pool, void *data) +{ + BLI_mempool_iter iter; + char *elem, *p = data; BLI_assert(pool->flag & BLI_MEMPOOL_ALLOW_ITER); - if (data) { - BLI_mempool_iter iter; - char *elem, *p = data; - BLI_mempool_iternew(pool, &iter); - for (elem = BLI_mempool_iterstep(&iter); elem; elem = BLI_mempool_iterstep(&iter)) { - memcpy(p, elem, (size_t)pool->esize); - p += pool->esize; - } - BLI_assert((p - data) == pool->totused * pool->esize); + BLI_mempool_iternew(pool, &iter); + while ((elem = BLI_mempool_iterstep(&iter))) { + memcpy(p, elem, (size_t)pool->esize); + p += pool->esize; } + BLI_assert((p - (char *)data) == pool->totused * pool->esize); +} + +/** + * A version of #BLI_mempool_as_array that allocates and returns the data. + */ +void *BLI_mempool_as_arrayN(BLI_mempool *pool, const char *allocstr) +{ + char *data = MEM_mallocN((size_t)(pool->totused * pool->esize), allocstr); + BLI_mempool_as_array(pool, data); return data; } |