1 /* SPDX-FileCopyrightText: 2023 Blender Authors
3 * SPDX-License-Identifier: GPL-2.0-or-later */
10 * Macro's for a simple array based stack
11 * \note Caller handles alloc & free.
14 /* only validate array-bounds in debug mode */
16 # define STACK_DECLARE(stack) unsigned int _##stack##_index, _##stack##_num_alloc
17 # define STACK_INIT(stack, stack_num) \
19 (void)((_##stack##_index) = 0), \
20 (void)((_##stack##_num_alloc) = (unsigned int)(stack_num)))
21 # define _STACK_SIZETEST(stack, off) \
22 (BLI_assert((_##stack##_index) + (off) <= _##stack##_num_alloc))
23 # define _STACK_SWAP_TOTALLOC(stack_a, stack_b) \
24 SWAP(unsigned int, _##stack_a##_num_alloc, _##stack_b##_num_alloc)
26 # define STACK_DECLARE(stack) unsigned int _##stack##_index
27 # define STACK_INIT(stack, stack_num) \
28 ((void)stack, (void)((_##stack##_index) = 0), (void)(0 ? (stack_num) : 0))
29 # define _STACK_SIZETEST(stack, off) (void)(stack), (void)(off)
30 # define _STACK_SWAP_TOTALLOC(stack_a, stack_b) (void)(stack_a), (void)(stack_b)
32 #define _STACK_BOUNDSTEST(stack, index) \
33 ((void)stack, BLI_assert((unsigned int)(index) < _##stack##_index))
35 #define STACK_SIZE(stack) ((void)stack, (_##stack##_index))
36 #define STACK_CLEAR(stack) \
39 _##stack##_index = 0; \
42 /** add item to stack */
43 #define STACK_PUSH(stack, val) \
44 ((void)stack, _STACK_SIZETEST(stack, 1), ((stack)[(_##stack##_index)++] = (val)))
45 #define STACK_PUSH_RET(stack) \
46 ((void)stack, _STACK_SIZETEST(stack, 1), ((stack)[(_##stack##_index)++]))
47 #define STACK_PUSH_RET_PTR(stack) \
48 ((void)stack, _STACK_SIZETEST(stack, 1), &((stack)[(_##stack##_index)++]))
49 /** take last item from stack */
50 #define STACK_POP(stack) ((_##stack##_index) ? ((stack)[--(_##stack##_index)]) : NULL)
51 #define STACK_POP_PTR(stack) ((_##stack##_index) ? &((stack)[--(_##stack##_index)]) : NULL)
52 #define STACK_POP_DEFAULT(stack, r) ((_##stack##_index) ? ((stack)[--(_##stack##_index)]) : (r))
53 /** look at last item (assumes non-empty stack) */
54 #define STACK_PEEK(stack) (BLI_assert(_##stack##_index), ((stack)[_##stack##_index - 1]))
55 #define STACK_PEEK_PTR(stack) (BLI_assert(_##stack##_index), &((stack)[_##stack##_index - 1]))
56 /** remove any item from the stack, take care, re-orders */
57 #define STACK_REMOVE(stack, i) \
59 const unsigned int _i = i; \
60 _STACK_BOUNDSTEST(stack, _i); \
61 if (--_##stack##_index != _i) { \
62 stack[_i] = stack[_##stack##_index]; \
66 #define STACK_DISCARD(stack, n) \
68 const unsigned int _n = n; \
69 BLI_assert(_##stack##_index >= _n); \
71 _##stack##_index -= _n; \
75 # define STACK_SWAP(stack_a, stack_b) \
77 SWAP(typeof(stack_a), stack_a, stack_b); \
78 SWAP(unsigned int, _##stack_a##_index, _##stack_b##_index); \
79 _STACK_SWAP_TOTALLOC(stack_a, stack_b); \
83 # define STACK_SWAP(stack_a, stack_b) \
85 SWAP(void *, *(void **)&stack_a, *(void **)&stack_b); \
86 SWAP(unsigned int, _##stack_a##_index, _##stack_b##_index); \
87 _STACK_SWAP_TOTALLOC(stack_a, stack_b); \