Merged changes in the trunk up to revision 54110.
[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.
83  *
84  * Allow for a large 'num' value when the new size is more then double
85  * to allocate the exact sized array. */
86 #define BLI_array_grow_items(arr, num)  ((                                    \
87         (((void *)(arr) == NULL) &&                                               \
88          ((void *)(_##arr##_static) != NULL) &&                                   \
89         /* don't add _##arr##_count below because it must be zero */              \
90          (_bli_array_totalsize_static(arr) >= _##arr##_count + num)) ?            \
91         /* we have an empty array and a static var big enough */                  \
92         (arr = (void *)_##arr##_static)                                           \
93             :                                                                     \
94         /* use existing static array or allocate */                               \
95         (LIKELY(_bli_array_totalsize(arr) >= _##arr##_count + num) ?              \
96          (void)0 /* do nothing */ :                                               \
97          _bli_array_grow_func((void **)&(arr), _##arr##_static,                   \
98                                sizeof(*arr), _##arr##_count, num,                 \
99                                "BLI_array." #arr),                                \
100          (void)0)  /* msvc2008 needs this */                                      \
101         ),                                                                        \
102         /* increment the array count, all conditions above are accounted for. */  \
103         (_##arr##_count += num))
104
105 /* returns length of array */
106 #define BLI_array_grow_one(arr)  BLI_array_grow_items(arr, 1)
107
108
109 /* appends an item to the array. */
110 #define BLI_array_append(arr, item)  (                                        \
111         (void) BLI_array_grow_one(arr),                                           \
112         (void) (arr[_##arr##_count - 1] = item)                                   \
113 )
114
115 /* appends an item to the array and returns a pointer to the item in the array.
116  * item is not a pointer, but actual data value.*/
117 #define BLI_array_append_r(arr, item)  (                                      \
118         (void) BLI_array_grow_one(arr),                                           \
119         (void) (arr[_##arr##_count - 1] = item),                                  \
120         (&arr[_##arr##_count - 1])                                                \
121 )
122
123 #define BLI_array_reserve(arr, num)                                           \
124         BLI_array_grow_items(arr, num), (void)(_##arr##_count -= (num))
125
126
127 #define BLI_array_free(arr)                                                   \
128         if (arr && (char *)arr != _##arr##_static) {                              \
129             BLI_array_fake_user(arr);                                             \
130             MEM_freeN(arr);                                                       \
131         } (void)0
132
133 #define BLI_array_pop(arr)  (                                                 \
134         (arr && _##arr##_count) ?                                                 \
135             arr[--_##arr##_count] :                                               \
136             NULL                                                                  \
137 )
138
139 /* resets the logical size of an array to zero, but doesn't
140  * free the memory. */
141 #define BLI_array_empty(arr)                                                  \
142         { _##arr##_count = 0; } (void)0
143
144 /* set the count of the array, doesn't actually increase the allocated array
145  * size.  don't use this unless you know what you're doing. */
146 #define BLI_array_length_set(arr, count)                                      \
147         { _##arr##_count = (count); }(void)0
148
149 /* only to prevent unused warnings */
150 #define BLI_array_fake_user(arr)                                              \
151         (void)_##arr##_count,                                                     \
152         (void)_##arr##_static
153
154
155 /* -------------------------------------------------------------------- */
156 /* other useful defines
157  * (unrelated to the main array macros) */
158
159 /* not part of the 'API' but handy funcs,
160  * same purpose as BLI_array_staticdeclare()
161  * but use when the max size is known ahead of time */
162 #define BLI_array_fixedstack_declare(arr, maxstatic, realsize, allocstr)      \
163         char _##arr##_static[maxstatic * sizeof(*(arr))];                         \
164         const int _##arr##_is_static = ((void *)_##arr##_static) != (             \
165             arr = ((realsize) <= maxstatic) ?                                     \
166                 (void *)_##arr##_static :                                         \
167                 MEM_mallocN(sizeof(*(arr)) * (realsize), allocstr)                \
168             )                                                                     \
169
170 #define BLI_array_fixedstack_free(arr)                                        \
171         if (_##arr##_is_static) {                                                 \
172                 MEM_freeN(arr);                                                       \
173         } (void)0
174
175
176 /* alloca */
177 #ifdef _MSC_VER
178 #  define alloca _alloca
179 #endif
180
181 #if defined(__MINGW32__)
182 #  include <malloc.h>  /* mingw needs for alloca() */
183 #endif
184
185 #if defined(__GNUC__) || defined(__clang__)
186 #define BLI_array_alloca(arr, realsize) \
187         (typeof(arr))alloca(sizeof(*arr) * (realsize))
188
189 #define BLI_array_alloca_and_count(arr, realsize) \
190         (typeof(arr))alloca(sizeof(*arr) * (realsize));  \
191         const int _##arr##_count = (realsize)
192
193 #else
194 #define BLI_array_alloca(arr, realsize) \
195         alloca(sizeof(*arr) * (realsize))
196
197 #define BLI_array_alloca_and_count(arr, realsize) \
198         alloca(sizeof(*arr) * (realsize));  \
199         const int _##arr##_count = (realsize)
200 #endif
201
202 #endif  /* __BLI_ARRAY_H__ */