1 | // SPDX-License-Identifier: GPL-3.0-or-later |
2 | |
3 | #pragma once |
4 | |
5 | #include <mos/moslib_global.h> |
6 | #include <mos/types.h> |
7 | |
8 | /** |
9 | * @defgroup down_stack libs.DownStack |
10 | * @ingroup libs |
11 | * @brief A stack that grows down. |
12 | * @{ |
13 | */ |
14 | typedef struct _downwards_stack_t |
15 | { |
16 | ptr_t top; |
17 | ptr_t head; |
18 | size_t capacity; |
19 | } downwards_stack_t; |
20 | |
21 | MOSAPI void stack_init(downwards_stack_t *stack, void *mem_region_bottom, size_t size); |
22 | MOSAPI void stack_deinit(downwards_stack_t *stack); |
23 | |
24 | MOSAPI void *stack_grow(downwards_stack_t *stack, size_t size); |
25 | MOSAPI void *stack_push(downwards_stack_t *stack, const void *data, size_t size); |
26 | #define stack_push_val(stack, val) \ |
27 | do \ |
28 | { \ |
29 | typeof(val) _val = (val); \ |
30 | stack_push(stack, &_val, sizeof(_val)); \ |
31 | } while (0) |
32 | |
33 | // ! WARN: Caller must ensure the data is at least size bytes long |
34 | MOSAPI void stack_pop(downwards_stack_t *stack, size_t size, void *data); |
35 | #define stack_pop_val(stack, val) \ |
36 | do \ |
37 | { \ |
38 | typeof(val) _val; \ |
39 | stack_pop(stack, sizeof(_val), &_val); \ |
40 | (val) = _val; \ |
41 | } while (0) |
42 | /** @} */ |
43 | |