Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2011-12-21 13:52:45 +0400
committerCampbell Barton <ideasman42@gmail.com>2011-12-21 13:52:45 +0400
commit87a34772003413bd402ea380ce5d8d7c3f3a6337 (patch)
tree7fe619184befaf74e134622cfde3668712ec4a08
parentdd20a50282ec21eef95b04b23e87044dc94d887c (diff)
BLI_array.h: improve BLI_array_growitems so its not calling BLI_array_growone in a loop
-rw-r--r--source/blender/blenlib/BLI_array.h42
1 files changed, 18 insertions, 24 deletions
diff --git a/source/blender/blenlib/BLI_array.h b/source/blender/blenlib/BLI_array.h
index f12a8b5a7a5..bd14793e0f9 100644
--- a/source/blender/blenlib/BLI_array.h
+++ b/source/blender/blenlib/BLI_array.h
@@ -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,18 +111,20 @@
), \
(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) ( \
@@ -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); \