[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58911] trunk/blender/source/blender/ blenlib: mempool api cleanup: differentiate mempool functions that allocate a pointer lookup table from allocating the data as a contiguous array ,
Campbell Barton
ideasman42 at gmail.com
Sun Aug 4 21:40:50 CEST 2013
Revision: 58911
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58911
Author: campbellbarton
Date: 2013-08-04 19:40:50 +0000 (Sun, 04 Aug 2013)
Log Message:
-----------
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.
Modified Paths:
--------------
trunk/blender/source/blender/blenlib/BLI_mempool.h
trunk/blender/source/blender/blenlib/intern/BLI_mempool.c
Modified: trunk/blender/source/blender/blenlib/BLI_mempool.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_mempool.h 2013-08-04 19:13:07 UTC (rev 58910)
+++ trunk/blender/source/blender/blenlib/BLI_mempool.h 2013-08-04 19:40:50 UTC (rev 58911)
@@ -86,19 +86,32 @@
__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_arrayN(BLI_mempool *pool, const char *allocstr)
+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)
+#ifdef __GNUC__
+__attribute__((warn_unused_result))
+__attribute__((nonnull(1, 2)))
+#endif
+;
+
/** iteration stuff. note: this may easy to produce bugs with **/
/* private structure */
typedef struct BLI_mempool_iter {
Modified: trunk/blender/source/blender/blenlib/intern/BLI_mempool.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/BLI_mempool.c 2013-08-04 19:13:07 UTC (rev 58910)
+++ trunk/blender/source/blender/blenlib/intern/BLI_mempool.c 2013-08-04 19:40:50 UTC (rev 58911)
@@ -334,38 +334,57 @@
}
/**
+ * 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_tableN(BLI_mempool *pool, const char *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);
+ 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_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_as_array(pool, data);
return data;
}
More information about the Bf-blender-cvs
mailing list