Blender V4.3
BLI_utildefines_stack.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
14/* only validate array-bounds in debug mode */
15#ifndef NDEBUG
16# define STACK_DECLARE(stack) unsigned int _##stack##_index, _##stack##_num_alloc
17# define STACK_INIT(stack, stack_num) \
18 ((void)stack, \
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)
25#else
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)
31#endif
32#define _STACK_BOUNDSTEST(stack, index) \
33 ((void)stack, BLI_assert((unsigned int)(index) < _##stack##_index))
34
35#define STACK_SIZE(stack) ((void)stack, (_##stack##_index))
36#define STACK_CLEAR(stack) \
37 { \
38 (void)stack; \
39 _##stack##_index = 0; \
40 } \
41 ((void)0)
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)++]))
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))
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]))
57#define STACK_REMOVE(stack, i) \
58 { \
59 const unsigned int _i = i; \
60 _STACK_BOUNDSTEST(stack, _i); \
61 if (--_##stack##_index != _i) { \
62 stack[_i] = stack[_##stack##_index]; \
63 } \
64 } \
65 ((void)0)
66#define STACK_DISCARD(stack, n) \
67 { \
68 const unsigned int _n = n; \
69 BLI_assert(_##stack##_index >= _n); \
70 (void)stack; \
71 _##stack##_index -= _n; \
72 } \
73 ((void)0)
74#ifdef __GNUC__
75# define STACK_SWAP(stack_a, stack_b) \
76 { \
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); \
80 } \
81 ((void)0)
82#else
83# define STACK_SWAP(stack_a, stack_b) \
84 { \
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); \
88 } \
89 ((void)0)
90#endif