BLI_array_growone wasn't always returning the new size. also some formatting edits

This commit is contained in:
2011-12-21 08:41:13 +00:00
parent 444833a8fb
commit dd20a50282

View File

@@ -1,6 +1,4 @@
/** /*
* Array Library
*
* ***** BEGIN GPL LICENSE BLOCK ***** * ***** BEGIN GPL LICENSE BLOCK *****
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@@ -28,43 +26,43 @@
*/ */
/* /*
this library needs to be changed to not use macros quite so heavily, * this library needs to be changed to not use macros quite so heavily,
and to be more of a complete array API. The way arrays are * and to be more of a complete array API. The way arrays are
exposed to client code as normal C arrays is very useful though, imho. * exposed to client code as normal C arrays is very useful though, imho.
it does require some use of macros, however. * it does require some use of macros, however.
*
anyway, it's used a bit too heavily to simply rewrite as a * anyway, it's used a bit too heavily to simply rewrite as a
more "correct" solution without macros entirely. I originally wrote this * more "correct" solution without macros entirely. I originally wrote this
to be very easy to use, without the normal pain of most array libraries. * to be very easy to use, without the normal pain of most array libraries.
This was especially helpful when it came to the massive refactors necessary for * This was especially helpful when it came to the massive refactors necessary
bmesh, and really helped to speed the process up. - joeedh * for bmesh, and really helped to speed the process up. - joeedh
*
little array macro library. example of usage: * little array macro library. example of usage:
*
int *arr = NULL; * int *arr = NULL;
BLI_array_declare(arr); * BLI_array_declare(arr);
int i; * int i;
*
for (i=0; i<10; i++) { * for (i=0; i<10; i++) {
BLI_array_growone(arr); * BLI_array_growone(arr);
arr[i] = something; * arr[i] = something;
} * }
BLI_array_free(arr); * BLI_array_free(arr);
*
arrays are buffered, using double-buffering (so on each reallocation, * arrays are buffered, using double-buffering (so on each reallocation,
the array size is doubled). supposedly this should give good Big Oh * the array size is doubled). supposedly this should give good Big Oh
behaviour, though it may not be the best in practice. * behaviour, though it may not be the best in practice.
*/ */
#define BLI_array_declare(arr) \ #define BLI_array_declare(arr) \
int _##arr##_count=0; \ int _##arr##_count = 0; \
void *_##arr##_tmp; \ void *_##arr##_tmp; \
void *_##arr##_static = NULL void *_##arr##_static = NULL
/* this will use stack space, up to maxstatic array elements, before /* this will use stack space, up to maxstatic array elements, before
* switching to dynamic heap allocation */ * switching to dynamic heap allocation */
#define BLI_array_staticdeclare(arr, maxstatic) \ #define BLI_array_staticdeclare(arr, maxstatic) \
int _##arr##_count=0; \ int _##arr##_count = 0; \
void *_##arr##_tmp; \ void *_##arr##_tmp; \
char _##arr##_static[maxstatic*sizeof(arr)] char _##arr##_static[maxstatic*sizeof(arr)]
@@ -72,16 +70,16 @@ behaviour, though it may not be the best in practice.
/* this returns the entire size of the array, including any buffering. */ /* this returns the entire size of the array, including any buffering. */
#define BLI_array_totalsize_dyn(arr) ( \ #define BLI_array_totalsize_dyn(arr) ( \
((arr)==NULL) ? \ ((arr)==NULL) ? \
0 : \ 0 : \
MEM_allocN_len(arr) / sizeof(*arr) \ MEM_allocN_len(arr) / sizeof(*arr) \
) )
#define BLI_array_totalsize(arr) ( \ #define BLI_array_totalsize(arr) ( \
(signed int) \ (size_t) \
(((void *)(arr) == (void *)_##arr##_static && (void *)(arr) != NULL) ? \ (((void *)(arr) == (void *)_##arr##_static && (void *)(arr) != NULL) ? \
(sizeof(_##arr##_static) / sizeof(*arr)) : \ (sizeof(_##arr##_static) / sizeof(*arr)) : \
BLI_array_totalsize_dyn(arr)) \ BLI_array_totalsize_dyn(arr)) \
) )
@@ -94,7 +92,7 @@ behaviour, though it may not be the best in practice.
++_##arr##_count : \ ++_##arr##_count : \
( \ ( \
(void) (_##arr##_tmp = MEM_callocN( \ (void) (_##arr##_tmp = MEM_callocN( \
sizeof(*arr)*(_##arr##_count*2+2), \ sizeof(*arr) * (_##arr##_count * 2 + 2), \
#arr " " __FILE__ ":" STRINGIFY(__LINE__) \ #arr " " __FILE__ ":" STRINGIFY(__LINE__) \
) \ ) \
), \ ), \
@@ -108,7 +106,7 @@ behaviour, though it may not be the best in practice.
), \ ), \
(void) (arr = _##arr##_tmp \ (void) (arr = _##arr##_tmp \
), \ ), \
_##arr##_count++ \ ++_##arr##_count \
) \ ) \
) )
@@ -116,23 +114,23 @@ behaviour, though it may not be the best in practice.
/* returns length of array */ /* returns length of array */
#define BLI_array_growone(arr) ( \ #define BLI_array_growone(arr) ( \
((void *)(arr)==NULL && (void *)(_##arr##_static) != NULL) ? \ ((void *)(arr)==NULL && (void *)(_##arr##_static) != NULL) ? \
((arr=(void*)_##arr##_static), ++_##arr##_count) : \ ((arr= (void*)_##arr##_static), ++_##arr##_count) : \
_bli_array_growone(arr) \ _bli_array_growone(arr) \
) )
/* appends an item to the array. */ /* appends an item to the array. */
#define BLI_array_append(arr, item) ( \ #define BLI_array_append(arr, item) ( \
(void) BLI_array_growone(arr), \ (void) BLI_array_growone(arr), \
(void) (arr[_##arr##_count-1] = item) \ (void) (arr[_##arr##_count - 1] = item) \
) )
/* appends an item to the array and returns a pointer to the item in the array. /* appends an item to the array and returns a pointer to the item in the array.
* item is not a pointer, but actual data value.*/ * item is not a pointer, but actual data value.*/
#define BLI_array_append_r(arr, item) ( \ #define BLI_array_append_r(arr, item) ( \
(void) BLI_array_growone(arr), \ (void) BLI_array_growone(arr), \
(void) (arr[_##arr##_count-1] = item), \ (void) (arr[_##arr##_count - 1] = item), \
(&arr[_##arr##_count-1]) \ (&arr[_##arr##_count - 1]) \
) )
/* grow an array by a specified number of items. */ /* grow an array by a specified number of items. */
@@ -142,22 +140,22 @@ behaviour, though it may not be the best in practice.
_##arr##_count += num; \ _##arr##_count += num; \
} \ } \
else { \ else { \
int _i; \ int _i; \
for (_i = 0; _i < (num); _i++) { \ for (_i = 0; _i < (num); _i++) { \
BLI_array_growone(arr); \ BLI_array_growone(arr); \
} \ } \
} }
#define BLI_array_free(arr) \ #define BLI_array_free(arr) \
if (arr && (char *)arr != _##arr##_static) { \ if (arr && (char *)arr != _##arr##_static) { \
BLI_array_fake_user(arr); \ BLI_array_fake_user(arr); \
MEM_freeN(arr); \ MEM_freeN(arr); \
} }
#define BLI_array_pop(arr) ( \ #define BLI_array_pop(arr) ( \
(arr&&_##arr##_count) ? \ (arr&&_##arr##_count) ? \
arr[--_##arr##_count] : \ arr[--_##arr##_count] : \
0 \ 0 \
) )
/* resets the logical size of an array to zero, but doesn't /* resets the logical size of an array to zero, but doesn't