Blender  V2.93
BLI_array.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
27 /* -------------------------------------------------------------------- */
32 #define _bli_array_totalsize_dynamic(arr) \
33  (((arr) == NULL) ? 0 : MEM_allocN_len(arr) / sizeof(*(arr)))
34 
35 #define _bli_array_totalsize_static(arr) (sizeof(_##arr##_static) / sizeof(*(arr)))
36 
37 #define _bli_array_totalsize(arr) \
38  ((size_t)(((void *)(arr) == (void *)_##arr##_static && (void *)(arr) != NULL) ? \
39  _bli_array_totalsize_static(arr) : \
40  _bli_array_totalsize_dynamic(arr)))
41 
50 void _bli_array_grow_func(void **arr_p,
51  const void *arr_static,
52  const int sizeof_arr_p,
53  const int arr_len,
54  const int num,
55  const char *alloc_str);
56 
57 /* -------------------------------------------------------------------- */
62 #define BLI_array_declare(arr) \
63  int _##arr##_len = ((void)(sizeof(*(arr))), 0); \
64  void *_##arr##_static = NULL
65 
69 #define BLI_array_staticdeclare(arr, maxstatic) \
70  int _##arr##_len = 0; \
71  char _##arr##_static[maxstatic * sizeof(*(arr))]
72 
74 #define BLI_array_len(arr) ((void)0, _##arr##_len)
75 
81 #define BLI_array_reserve(arr, num) \
82  (void)((((void *)(arr) == NULL) && \
83  ((void *)(_##arr##_static) != \
84  NULL) && /* don't add _##arr##_len below because it must be zero */ \
85  (_bli_array_totalsize_static(arr) >= \
86  (size_t)(_##arr##_len + \
87  (num)))) ? /* we have an empty array and a static var big enough */ \
88  (void)(arr = (void *)_##arr##_static) : /* use existing static array or allocate */ \
89  (LIKELY(_bli_array_totalsize(arr) >= (size_t)(_##arr##_len + (num))) ? \
90  (void)0 /* do nothing */ : \
91  _bli_array_grow_func((void **)&(arr), \
92  _##arr##_static, \
93  sizeof(*(arr)), \
94  _##arr##_len, \
95  num, \
96  "BLI_array." #arr)))
97 
99 #define BLI_array_grow_items(arr, num) (BLI_array_reserve(arr, num), (_##arr##_len += num))
100 
101 #define BLI_array_grow_one(arr) BLI_array_grow_items(arr, 1)
102 
104 #define BLI_array_append(arr, item) \
105  ((void)BLI_array_grow_one(arr), (void)(arr[_##arr##_len - 1] = item))
106 
110 #define BLI_array_append_r(arr, item) \
111  ((void)BLI_array_grow_one(arr), (void)(arr[_##arr##_len - 1] = item), (&arr[_##arr##_len - 1]))
112 
114 #define BLI_array_append_ret(arr) (BLI_array_reserve(arr, 1), &arr[(_##arr##_len++)])
115 
116 #define BLI_array_free(arr) \
117  { \
118  if (arr && (char *)arr != _##arr##_static) { \
119  BLI_array_fake_user(arr); \
120  MEM_freeN((void *)arr); \
121  } \
122  } \
123  ((void)0)
124 
125 #define BLI_array_pop(arr) ((arr && _##arr##_len) ? arr[--_##arr##_len] : NULL)
126 
130 #define BLI_array_clear(arr) \
131  { \
132  _##arr##_len = 0; \
133  } \
134  ((void)0)
135 
139 #define BLI_array_len_set(arr, len) \
140  { \
141  _##arr##_len = (len); \
142  } \
143  ((void)0)
144 
146 #define BLI_array_fake_user(arr) ((void)_##arr##_len, (void)_##arr##_static)
147 
150 /* -------------------------------------------------------------------- */
161 #define BLI_array_fixedstack_declare(arr, maxstatic, realsize, allocstr) \
162  char _##arr##_static[maxstatic * sizeof(*(arr))]; \
163  const bool _##arr##_is_static = ((void *)_##arr##_static) != \
164  (arr = ((realsize) <= maxstatic) ? \
165  (void *)_##arr##_static : \
166  MEM_mallocN(sizeof(*(arr)) * (realsize), allocstr))
167 
168 #define BLI_array_fixedstack_free(arr) \
169  if (_##arr##_is_static) { \
170  MEM_freeN(arr); \
171  } \
172  ((void)0)
173 
void _bli_array_grow_func(void **arr_p, const void *arr_static, const int sizeof_arr_p, const int arr_len, const int num, const char *alloc_str)
Definition: BLI_array.c:62