Add ARRAY_SIZE macro to check fixed size arrays

This commit is contained in:
Campbell Barton 2014-05-18 23:50:44 +10:00
parent 07e8096c63
commit 2a49bf35f0
2 changed files with 14 additions and 6 deletions

@ -84,12 +84,11 @@ static IDType idtypes[] = {
{ ID_WO, "World", "worlds", IDTYPE_FLAGS_ISLINKABLE },
{ ID_WM, "WindowManager", "window_managers", 0 },
};
static int nidtypes = sizeof(idtypes) / sizeof(idtypes[0]);
static IDType *idtype_from_name(const char *str)
{
int i = nidtypes;
int i = ARRAY_SIZE(idtypes);
while (i--) {
if (STREQ(str, idtypes[i].name)) {
return &idtypes[i];
@ -100,8 +99,8 @@ static IDType *idtype_from_name(const char *str)
}
static IDType *idtype_from_code(int code)
{
int i = nidtypes;
int i = ARRAY_SIZE(idtypes);
while (i--)
if (code == idtypes[i].code)
return &idtypes[i];
@ -182,5 +181,5 @@ const char *BKE_idcode_to_name_plural(int code)
*/
int BKE_idcode_iter_step(int *index)
{
return (*index < nidtypes) ? idtypes[(*index)++].code : 0;
return (*index < ARRAY_SIZE(idtypes)) ? idtypes[(*index)++].code : 0;
}

@ -361,6 +361,15 @@
(((tot) - (index)) - (tot_delete)) * sizeof(*(arr))); \
} (void)0
/* assuming a static array */
#ifdef __GNUC__
# define ARRAY_SIZE(arr) \
((sizeof(struct {int isnt_array : ((void *)&(arr) == &(arr)[0]);}) * 0) + \
(sizeof(arr) / sizeof(*(arr))))
#else
# define ARRAY_SIZE(x) sizeof(arr) / sizeof(*(arr))
#endif
/* Warning-free macros for storing ints in pointers. Use these _only_
* for storing an int in a pointer, not a pointer in an int (64bit)! */
#define SET_INT_IN_POINTER(i) ((void *)(intptr_t)(i))