[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [42789] branches/bmesh/blender/source/ blender/blenlib/BLI_array.h: BLI_array.h: improve BLI_array_growitems so its not calling BLI_array_growone in a loop

Campbell Barton ideasman42 at gmail.com
Wed Dec 21 10:52:57 CET 2011


Revision: 42789
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42789
Author:   campbellbarton
Date:     2011-12-21 09:52:45 +0000 (Wed, 21 Dec 2011)
Log Message:
-----------
BLI_array.h: improve BLI_array_growitems so its not calling BLI_array_growone in a loop

Modified Paths:
--------------
    branches/bmesh/blender/source/blender/blenlib/BLI_array.h

Modified: branches/bmesh/blender/source/blender/blenlib/BLI_array.h
===================================================================
--- branches/bmesh/blender/source/blender/blenlib/BLI_array.h	2011-12-21 09:27:17 UTC (rev 42788)
+++ branches/bmesh/blender/source/blender/blenlib/BLI_array.h	2011-12-21 09:52:45 UTC (rev 42789)
@@ -86,13 +86,18 @@
 /* this returns the logical size of the array, not including buffering. */
 #define BLI_array_count(arr) _##arr##_count
 
-/* grow the array by one.  zeroes the new elements. */
-#define _bli_array_growone(arr)  (                                            \
-	(BLI_array_totalsize(arr) > _##arr##_count) ?                             \
-	    ++_##arr##_count :                                                    \
+/* Grow the array by a fixed number of items. zeroes the new elements.
+ *
+ * Allow for a large 'num' value when the new size is more then double
+ * to allocate the exact sized array. */
+#define _bli_array_grow_items(arr, num)  (                                    \
+	(BLI_array_totalsize(arr) >= _##arr##_count + num) ?                      \
+	    (_##arr##_count += num) :                                             \
 	    (                                                                     \
 	        (void) (_##arr##_tmp = MEM_callocN(                               \
-	                sizeof(*arr) * (_##arr##_count * 2 + 2),                  \
+	                sizeof(*arr) * (num < _##arr##_count ?                    \
+	                                (_##arr##_count * 2 + 2) :                \
+	                                (_##arr##_count + num)),                  \
 	                #arr " " __FILE__ ":" STRINGIFY(__LINE__)                 \
 	                )                                                         \
 	                ),                                                        \
@@ -106,19 +111,21 @@
 	                ),                                                        \
 	        (void) (arr = _##arr##_tmp                                        \
 	                ),                                                        \
-	        ++_##arr##_count                                                  \
+	        (_##arr##_count += num)                                           \
 	    )                                                                     \
 )
 
-
-/* returns length of array */
-#define BLI_array_growone(arr)  (                                             \
+/* grow an array by a specified number of items */
+#define BLI_array_growitems(arr, num)  (                                      \
 	((void *)(arr)==NULL && (void *)(_##arr##_static) != NULL) ?              \
-	    ((arr= (void*)_##arr##_static), ++_##arr##_count) :                   \
-	    _bli_array_growone(arr)                                               \
+	    ((arr= (void*)_##arr##_static), (_##arr##_count += num)) :            \
+	    _bli_array_grow_items(arr, num)                                       \
 )
 
+/* returns length of array */
+#define BLI_array_growone(arr)  BLI_array_growitems(arr, 1)
 
+
 /* appends an item to the array. */
 #define BLI_array_append(arr, item)  (                                        \
 	(void) BLI_array_growone(arr),                                            \
@@ -133,19 +140,6 @@
 	(&arr[_##arr##_count - 1])                                                \
 )
 
-/* grow an array by a specified number of items. */
-/* TODO, this could be done in a less crappy way by not looping - campbell */
-#define BLI_array_growitems(arr, num)                                         \
-	if ((BLI_array_totalsize(arr) - _##arr##_count) >= num) {                 \
-	    _##arr##_count += num;                                                \
-	}                                                                         \
-	else {                                                                    \
-	    int _i;                                                               \
-	    for (_i = 0; _i < (num); _i++) {                                      \
-	        BLI_array_growone(arr);                                           \
-	    }                                                                     \
-	}
-
 #define BLI_array_free(arr)                                                   \
 	if (arr && (char *)arr != _##arr##_static) {                              \
 	    BLI_array_fake_user(arr);                                             \




More information about the Bf-blender-cvs mailing list