code cleanup: BLI_array.h's BLI_array_grow_items macro was unmanageable.
[blender.git] / source / blender / blenlib / BLI_array.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Joseph Eagar.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #ifndef __BLI_ARRAY_H__
29 #define __BLI_ARRAY_H__
30
31 /** \file BLI_array.h
32  *  \ingroup bli
33  *  \brief A (mainly) macro array library.
34  */
35
36 /* -------------------------------------------------------------------- */
37 /* internal defines */
38
39 /* this returns the entire size of the array, including any buffering. */
40 #define _bli_array_totalsize_dynamic(arr)  (                                  \
41         ((arr) == NULL) ?                                                         \
42             0 :                                                                   \
43             MEM_allocN_len(arr) / sizeof(*arr)                                    \
44 )
45
46 #define _bli_array_totalsize_static(arr)  \
47         (sizeof(_##arr##_static) / sizeof(*arr))
48
49 #define _bli_array_totalsize(arr)  (                                          \
50         (size_t)                                                                  \
51         (((void *)(arr) == (void *)_##arr##_static && (void *)(arr) != NULL) ?    \
52             _bli_array_totalsize_static(arr) :                                    \
53             _bli_array_totalsize_dynamic(arr))                                    \
54 )
55
56 /* BLI_array.c
57  *
58  * Doing the realloc in a macro isn't so simple,
59  * so use a function the macros can use.
60  */
61 void _bli_array_grow_func(void **arr_p, const void *arr_static,
62                           const int sizeof_arr_p, const int arr_count, const int num,
63                           const char *alloc_str);
64
65
66 /* -------------------------------------------------------------------- */
67 /* public defines */
68
69 #define BLI_array_declare(arr)                                                \
70         int   _##arr##_count = 0;                                                 \
71         void *_##arr##_static = NULL
72
73 /* this will use stack space, up to maxstatic array elements, before
74  * switching to dynamic heap allocation */
75 #define BLI_array_staticdeclare(arr, maxstatic)                               \
76         int   _##arr##_count = 0;                                                 \
77         char  _##arr##_static[maxstatic * sizeof(arr)]
78
79 /* this returns the logical size of the array, not including buffering. */
80 #define BLI_array_count(arr) _##arr##_count
81
82 /* Grow the array by a fixed number of items. zeroes the new elements.
83  *
84  * Allow for a large 'num' value when the new size is more then double
85  * to allocate the exact sized array. */
86
87 /* grow an array by a specified number of items */
88 #define BLI_array_grow_items(arr, num)  ((                                    \
89         (((void *)(arr) == NULL) &&                                               \
90          ((void *)(_##arr##_static) != NULL) &&                                   \
91         /* don't add _##arr##_count below because it must be zero */              \
92          (_bli_array_totalsize_static(arr) >= _##arr##_count + num)) ?            \
93         /* we have an empty array and a static var big enough */                  \
94         (arr = (void *)_##arr##_static)                                           \
95             :                                                                     \
96         /* use existing static array or allocate */                               \
97         (LIKELY(_bli_array_totalsize(arr) >= _##arr##_count + num) ?              \
98          (void)0 /* do nothing */ :                                               \
99          (_bli_array_grow_func((void **)&(arr), _##arr##_static,                  \
100                                sizeof(*arr), _##arr##_count, num,                 \
101                                "BLI_array." #arr)))                               \
102         ),                                                                        \
103         /* increment the array count, all conditions above are accounted for. */  \
104         (_##arr##_count += num))
105
106 /* returns length of array */
107 #define BLI_array_grow_one(arr)  BLI_array_grow_items(arr, 1)
108
109
110 /* appends an item to the array. */
111 #define BLI_array_append(arr, item)  (                                        \
112         (void) BLI_array_grow_one(arr),                                           \
113         (void) (arr[_##arr##_count - 1] = item)                                   \
114 )
115
116 /* appends an item to the array and returns a pointer to the item in the array.
117  * item is not a pointer, but actual data value.*/
118 #define BLI_array_append_r(arr, item)  (                                      \
119         (void) BLI_array_grow_one(arr),                                           \
120         (void) (arr[_##arr##_count - 1] = item),                                  \
121         (&arr[_##arr##_count - 1])                                                \
122 )
123
124 #define BLI_array_reserve(arr, num)                                           \
125         BLI_array_grow_items(arr, num), (void)(_##arr##_count -= (num))
126
127
128 #define BLI_array_free(arr)                                                   \
129         if (arr && (char *)arr != _##arr##_static) {                              \
130             BLI_array_fake_user(arr);                                             \
131             MEM_freeN(arr);                                                       \
132         } (void)0
133
134 #define BLI_array_pop(arr)  (                                                 \
135         (arr && _##arr##_count) ?                                                 \
136             arr[--_##arr##_count] :                                               \
137             NULL                                                                  \
138 )
139
140 /* resets the logical size of an array to zero, but doesn't
141  * free the memory. */
142 #define BLI_array_empty(arr)                                                  \
143         { _##arr##_count = 0; } (void)0
144
145 /* set the count of the array, doesn't actually increase the allocated array
146  * size.  don't use this unless you know what you're doing. */
147 #define BLI_array_length_set(arr, count)                                      \
148         { _##arr##_count = (count); }(void)0
149
150 /* only to prevent unused warnings */
151 #define BLI_array_fake_user(arr)                                              \
152         (void)_##arr##_count,                                                     \
153         (void)_##arr##_static
154
155
156 /* -------------------------------------------------------------------- */
157 /* other useful defines
158  * (unrelated to the main array macros) */
159
160 /* not part of the 'API' but handy funcs,
161  * same purpose as BLI_array_staticdeclare()
162  * but use when the max size is known ahead of time */
163 #define BLI_array_fixedstack_declare(arr, maxstatic, realsize, allocstr)      \
164         char _##arr##_static[maxstatic * sizeof(*(arr))];                         \
165         const int _##arr##_is_static = ((void *)_##arr##_static) != (             \
166             arr = ((realsize) <= maxstatic) ?                                     \
167                 (void *)_##arr##_static :                                         \
168                 MEM_mallocN(sizeof(*(arr)) * (realsize), allocstr)                \
169             )                                                                     \
170
171 #define BLI_array_fixedstack_free(arr)                                        \
172         if (_##arr##_is_static) {                                                 \
173                 MEM_freeN(arr);                                                       \
174         } (void)0
175
176
177 /* alloca */
178 #ifdef _MSC_VER
179 #  define alloca _alloca
180 #endif
181
182 #if defined(__MINGW32__)
183 #  include <malloc.h>  /* mingw needs for alloca() */
184 #endif
185
186 #if defined(__GNUC__) || defined(__clang__)
187 #define BLI_array_alloca(arr, realsize) \
188         (typeof(arr))alloca(sizeof(*arr) * (realsize))
189
190 #define BLI_array_alloca_and_count(arr, realsize) \
191         (typeof(arr))alloca(sizeof(*arr) * (realsize));  \
192         const int _##arr##_count = (realsize)
193
194 #else
195 #define BLI_array_alloca(arr, realsize) \
196         alloca(sizeof(*arr) * (realsize))
197
198 #define BLI_array_alloca_and_count(arr, realsize) \
199         alloca(sizeof(*arr) * (realsize));  \
200         const int _##arr##_count = (realsize)
201 #endif
202
203 #endif  /* __BLI_ARRAY_H__ */