Blender  V3.3
BLI_utildefines_stack.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
12 /* only validate array-bounds in debug mode */
13 #ifdef DEBUG
14 # define STACK_DECLARE(stack) unsigned int _##stack##_index, _##stack##_num_alloc
15 # define STACK_INIT(stack, stack_num) \
16  ((void)stack, (void)((_##stack##_index) = 0), (void)((_##stack##_num_alloc) = (stack_num)))
17 # define _STACK_SIZETEST(stack, off) \
18  (BLI_assert((_##stack##_index) + (off) <= _##stack##_num_alloc))
19 # define _STACK_SWAP_TOTALLOC(stack_a, stack_b) \
20  SWAP(unsigned int, _##stack_a##_num_alloc, _##stack_b##_num_alloc)
21 #else
22 # define STACK_DECLARE(stack) unsigned int _##stack##_index
23 # define STACK_INIT(stack, stack_num) \
24  ((void)stack, (void)((_##stack##_index) = 0), (void)(0 ? (stack_num) : 0))
25 # define _STACK_SIZETEST(stack, off) (void)(stack), (void)(off)
26 # define _STACK_SWAP_TOTALLOC(stack_a, stack_b) (void)(stack_a), (void)(stack_b)
27 #endif
28 #define _STACK_BOUNDSTEST(stack, index) \
29  ((void)stack, BLI_assert((unsigned int)(index) < _##stack##_index))
30 
31 #define STACK_SIZE(stack) ((void)stack, (_##stack##_index))
32 #define STACK_CLEAR(stack) \
33  { \
34  (void)stack; \
35  _##stack##_index = 0; \
36  } \
37  ((void)0)
39 #define STACK_PUSH(stack, val) \
40  ((void)stack, _STACK_SIZETEST(stack, 1), ((stack)[(_##stack##_index)++] = (val)))
41 #define STACK_PUSH_RET(stack) \
42  ((void)stack, _STACK_SIZETEST(stack, 1), ((stack)[(_##stack##_index)++]))
43 #define STACK_PUSH_RET_PTR(stack) \
44  ((void)stack, _STACK_SIZETEST(stack, 1), &((stack)[(_##stack##_index)++]))
46 #define STACK_POP(stack) ((_##stack##_index) ? ((stack)[--(_##stack##_index)]) : NULL)
47 #define STACK_POP_PTR(stack) ((_##stack##_index) ? &((stack)[--(_##stack##_index)]) : NULL)
48 #define STACK_POP_DEFAULT(stack, r) ((_##stack##_index) ? ((stack)[--(_##stack##_index)]) : (r))
50 #define STACK_PEEK(stack) (BLI_assert(_##stack##_index), ((stack)[_##stack##_index - 1]))
51 #define STACK_PEEK_PTR(stack) (BLI_assert(_##stack##_index), &((stack)[_##stack##_index - 1]))
53 #define STACK_REMOVE(stack, i) \
54  { \
55  const unsigned int _i = i; \
56  _STACK_BOUNDSTEST(stack, _i); \
57  if (--_##stack##_index != _i) { \
58  stack[_i] = stack[_##stack##_index]; \
59  } \
60  } \
61  ((void)0)
62 #define STACK_DISCARD(stack, n) \
63  { \
64  const unsigned int _n = n; \
65  BLI_assert(_##stack##_index >= _n); \
66  (void)stack; \
67  _##stack##_index -= _n; \
68  } \
69  ((void)0)
70 #ifdef __GNUC__
71 # define STACK_SWAP(stack_a, stack_b) \
72  { \
73  SWAP(typeof(stack_a), stack_a, stack_b); \
74  SWAP(unsigned int, _##stack_a##_index, _##stack_b##_index); \
75  _STACK_SWAP_TOTALLOC(stack_a, stack_b); \
76  } \
77  ((void)0)
78 #else
79 # define STACK_SWAP(stack_a, stack_b) \
80  { \
81  SWAP(void *, stack_a, stack_b); \
82  SWAP(unsigned int, _##stack_a##_index, _##stack_b##_index); \
83  _STACK_SWAP_TOTALLOC(stack_a, stack_b); \
84  } \
85  ((void)0)
86 #endif