From 87a34772003413bd402ea380ce5d8d7c3f3a6337 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 21 Dec 2011 09:52:45 +0000 Subject: [PATCH] BLI_array.h: improve BLI_array_growitems so its not calling BLI_array_growone in a loop --- source/blender/blenlib/BLI_array.h | 42 +++++++++++++----------------- 1 file 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,17 +111,19 @@ ), \ (void) (arr = _##arr##_tmp \ ), \ - ++_##arr##_count \ + (_##arr##_count += num) \ ) \ ) +/* 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 += num)) : \ + _bli_array_grow_items(arr, num) \ +) /* returns length of array */ -#define BLI_array_growone(arr) ( \ - ((void *)(arr)==NULL && (void *)(_##arr##_static) != NULL) ? \ - ((arr= (void*)_##arr##_static), ++_##arr##_count) : \ - _bli_array_growone(arr) \ -) +#define BLI_array_growone(arr) BLI_array_growitems(arr, 1) /* appends an item to the array. */ @@ -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); \