code cleanup: ensure macros require ';' endings
[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 /** \file BLI_array.h
29  *  \ingroup bli
30  *  \brief A macro array library.
31  *
32  * this library needs to be changed to not use macros quite so heavily,
33  * and to be more of a complete array API.  The way arrays are
34  * exposed to client code as normal C arrays is very useful though, imho.
35  * it does require some use of macros, however.
36  *
37  * anyway, it's used a bit too heavily to simply rewrite as a
38  * more "correct" solution without macros entirely.  I originally wrote this
39  * to be very easy to use, without the normal pain of most array libraries.
40  * This was especially helpful when it came to the massive refactors necessary
41  * for bmesh, and really helped to speed the process up. - joeedh
42  *
43  * little array macro library.  example of usage:
44  *
45  * int *arr = NULL;
46  * BLI_array_declare(arr);
47  * int i;
48  *
49  * for (i=0; i<10; i++) {
50  *     BLI_array_grow_one(arr);
51  *     arr[i] = something;
52  * }
53  * BLI_array_free(arr);
54  *
55  * arrays are buffered, using double-buffering (so on each reallocation,
56  * the array size is doubled).  supposedly this should give good Big Oh
57  * behavior, though it may not be the best in practice.
58  */
59
60 #define BLI_array_declare(arr)                                                \
61         int   _##arr##_count = 0;                                                 \
62         void *_##arr##_tmp;                                                       \
63         void *_##arr##_static = NULL
64
65 /* this will use stack space, up to maxstatic array elements, before
66  * switching to dynamic heap allocation */
67 #define BLI_array_staticdeclare(arr, maxstatic)                               \
68         int   _##arr##_count = 0;                                                 \
69         void *_##arr##_tmp;                                                       \
70         char  _##arr##_static[maxstatic * sizeof(arr)]
71
72
73 /* this returns the entire size of the array, including any buffering. */
74 #define BLI_array_totalsize_dyn(arr)  (                                       \
75         ((arr) == NULL) ?                                                         \
76             0 :                                                                   \
77             MEM_allocN_len(arr) / sizeof(*arr)                                    \
78 )
79
80
81 #define BLI_array_totalsize(arr)  (                                           \
82         (size_t)                                                                  \
83         (((void *)(arr) == (void *)_##arr##_static && (void *)(arr) != NULL) ?    \
84             (sizeof(_##arr##_static) / sizeof(*arr)) :                            \
85             BLI_array_totalsize_dyn(arr))                                         \
86 )
87
88
89 /* this returns the logical size of the array, not including buffering. */
90 #define BLI_array_count(arr) _##arr##_count
91
92 /* Grow the array by a fixed number of items. zeroes the new elements.
93  *
94  * Allow for a large 'num' value when the new size is more then double
95  * to allocate the exact sized array. */
96 #define _bli_array_grow_items(arr, num)  (                                    \
97         (BLI_array_totalsize(arr) >= _##arr##_count + num) ?                      \
98             (_##arr##_count += num) :                                             \
99             (                                                                     \
100                 (void) (_##arr##_tmp = MEM_callocN(                               \
101                         sizeof(*arr) * (num < _##arr##_count ?                    \
102                                         (_##arr##_count * 2 + 2) :                \
103                                         (_##arr##_count + num)),                  \
104                         #arr " " __FILE__ ":" STRINGIFY(__LINE__)                 \
105                         )                                                         \
106                         ),                                                        \
107                 (void) (arr && memcpy(_##arr##_tmp,                               \
108                                       arr,                                        \
109                                       sizeof(*arr) * _##arr##_count)              \
110                         ),                                                        \
111                 (void) (arr && ((void *)(arr) != (void *)_##arr##_static ?        \
112                         (MEM_freeN(arr), arr) :                                   \
113                         arr)                                                      \
114                         ),                                                        \
115                 (void) (arr = _##arr##_tmp                                        \
116                         ),                                                        \
117                 (_##arr##_count += num)                                           \
118             )                                                                     \
119 )
120
121 /* grow an array by a specified number of items */
122 #define BLI_array_grow_items(arr, num)  (                                     \
123         ((void *)(arr) == NULL && (void *)(_##arr##_static) != NULL) ?            \
124             ((arr = (void *)_##arr##_static), (_##arr##_count += num)) :          \
125             _bli_array_grow_items(arr, num)                                       \
126 )
127
128 /* returns length of array */
129 #define BLI_array_grow_one(arr)  BLI_array_grow_items(arr, 1)
130
131
132 /* appends an item to the array. */
133 #define BLI_array_append(arr, item)  (                                        \
134         (void) BLI_array_grow_one(arr),                                           \
135         (void) (arr[_##arr##_count - 1] = item)                                   \
136 )
137
138 /* appends an item to the array and returns a pointer to the item in the array.
139  * item is not a pointer, but actual data value.*/
140 #define BLI_array_append_r(arr, item)  (                                      \
141         (void) BLI_array_grow_one(arr),                                           \
142         (void) (arr[_##arr##_count - 1] = item),                                  \
143         (&arr[_##arr##_count - 1])                                                \
144 )
145
146 #define BLI_array_reserve(arr, num)                                           \
147         BLI_array_grow_items(arr, num), (void)(_##arr##_count -= (num))
148
149
150 #define BLI_array_free(arr)                                                   \
151         if (arr && (char *)arr != _##arr##_static) {                              \
152             BLI_array_fake_user(arr);                                             \
153             MEM_freeN(arr);                                                       \
154         } (void)0
155
156 #define BLI_array_pop(arr)  (                                                 \
157         (arr && _##arr##_count) ?                                                 \
158             arr[--_##arr##_count] :                                               \
159             NULL                                                                  \
160 )
161
162 /* resets the logical size of an array to zero, but doesn't
163  * free the memory. */
164 #define BLI_array_empty(arr)                                                  \
165         _##arr##_count = 0; (void)0
166
167 /* set the count of the array, doesn't actually increase the allocated array
168  * size.  don't use this unless you know what you're doing. */
169 #define BLI_array_length_set(arr, count)                                      \
170         _##arr##_count = (count); (void)0
171
172 /* only to prevent unused warnings */
173 #define BLI_array_fake_user(arr)                                              \
174         (void)_##arr##_count,                                                     \
175         (void)_##arr##_tmp,                                                       \
176         (void)_##arr##_static
177
178
179 /* not part of the 'API' but handy funcs,
180  * same purpose as BLI_array_staticdeclare()
181  * but use when the max size is known ahead of time */
182 #define BLI_array_fixedstack_declare(arr, maxstatic, realsize, allocstr)      \
183         char _##arr##_static[maxstatic * sizeof(*(arr))];                         \
184         const int _##arr##_is_static = ((void *)_##arr##_static) != (             \
185             arr = ((realsize) <= maxstatic) ?                                     \
186                 (void *)_##arr##_static :                                         \
187                 MEM_mallocN(sizeof(*(arr)) * (realsize), allocstr)                \
188             )                                                                     \
189
190 #define BLI_array_fixedstack_free(arr)                                        \
191         if (_##arr##_is_static) {                                                 \
192                 MEM_freeN(arr);                                                       \
193         } (void)0