Merging r58475 through r58700 from trunk into soc-2013-depsgraph_mt
[blender.git] / source / blender / blenlib / BLI_array.h
index 0ba0f138c2809eae74f335604c82f8a8b56382c9..ef5cb8bde04f7660cba0d7374836feee1956eaf2 100644 (file)
 #define _bli_array_totalsize_dynamic(arr)  (                                  \
        ((arr) == NULL) ?                                                         \
            0 :                                                                   \
-           MEM_allocN_len(arr) / sizeof(*arr)                                    \
+           MEM_allocN_len(arr) / sizeof(*(arr))                                  \
 )
 
 #define _bli_array_totalsize_static(arr)  \
-       (sizeof(_##arr##_static) / sizeof(*arr))
+       (sizeof(_##arr##_static) / sizeof(*(arr)))
 
 #define _bli_array_totalsize(arr)  (                                          \
        (size_t)                                                                  \
@@ -66,8 +66,9 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static,
 /* -------------------------------------------------------------------- */
 /* public defines */
 
+/* use sizeof(*(arr)) to ensure the array exists and is an array */
 #define BLI_array_declare(arr)                                                \
-       int   _##arr##_count = 0;                                                 \
+       int   _##arr##_count = ((void)(sizeof(*(arr))), 0);                       \
        void *_##arr##_static = NULL
 
 /* this will use stack space, up to maxstatic array elements, before
@@ -81,7 +82,7 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static,
 
 /* Grow the array by a fixed number of items.
  *
- * Allow for a large 'num' value when the new size is more then double
+ * Allow for a large 'num' value when the new size is more than double
  * to allocate the exact sized array. */
 #define BLI_array_grow_items(arr, num)  ((                                    \
        (((void *)(arr) == NULL) &&                                               \
@@ -89,13 +90,13 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static,
        /* don't add _##arr##_count below because it must be zero */              \
         (_bli_array_totalsize_static(arr) >= _##arr##_count + num)) ?            \
        /* we have an empty array and a static var big enough */                  \
-       (arr = (void *)_##arr##_static)                                           \
+       (void)(arr = (void *)_##arr##_static)                                     \
            :                                                                     \
        /* use existing static array or allocate */                               \
        (LIKELY(_bli_array_totalsize(arr) >= _##arr##_count + num) ?              \
         (void)0 /* do nothing */ :                                               \
         _bli_array_grow_func((void **)&(arr), _##arr##_static,                   \
-                              sizeof(*arr), _##arr##_count, num,                 \
+                              sizeof(*(arr)), _##arr##_count, num,               \
                               "BLI_array." #arr),                                \
         (void)0)  /* msvc2008 needs this */                                      \
        ),                                                                        \
@@ -148,8 +149,8 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static,
 
 /* only to prevent unused warnings */
 #define BLI_array_fake_user(arr)                                              \
-       (void)_##arr##_count,                                                     \
-       (void)_##arr##_static
+       ((void)_##arr##_count,                                                    \
+        (void)_##arr##_static)
 
 
 /* -------------------------------------------------------------------- */
@@ -161,7 +162,7 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static,
  * but use when the max size is known ahead of time */
 #define BLI_array_fixedstack_declare(arr, maxstatic, realsize, allocstr)      \
        char _##arr##_static[maxstatic * sizeof(*(arr))];                         \
-       const int _##arr##_is_static = ((void *)_##arr##_static) != (             \
+       const bool _##arr##_is_static = ((void *)_##arr##_static) != (            \
            arr = ((realsize) <= maxstatic) ?                                     \
                (void *)_##arr##_static :                                         \
                MEM_mallocN(sizeof(*(arr)) * (realsize), allocstr)                \
@@ -172,31 +173,4 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static,
                MEM_freeN(arr);                                                       \
        } (void)0
 
-
-/* alloca */
-#ifdef _MSC_VER
-#  define alloca _alloca
-#endif
-
-#if defined(__MINGW32__)
-#  include <malloc.h>  /* mingw needs for alloca() */
-#endif
-
-#if defined(__GNUC__) || defined(__clang__)
-#define BLI_array_alloca(arr, realsize) \
-       (typeof(arr))alloca(sizeof(*arr) * (realsize))
-
-#define BLI_array_alloca_and_count(arr, realsize) \
-       (typeof(arr))alloca(sizeof(*arr) * (realsize));  \
-       const int _##arr##_count = (realsize)
-
-#else
-#define BLI_array_alloca(arr, realsize) \
-       alloca(sizeof(*arr) * (realsize))
-
-#define BLI_array_alloca_and_count(arr, realsize) \
-       alloca(sizeof(*arr) * (realsize));  \
-       const int _##arr##_count = (realsize)
-#endif
-
 #endif  /* __BLI_ARRAY_H__ */