]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* SPDX-License-Identifier: BSD-3-Clause |
2 | * Copyright(c) 2019-2020 Broadcom | |
3 | * All rights reserved. | |
4 | */ | |
5 | #ifndef _STACK_H_ | |
6 | #define _STACK_H_ | |
7 | ||
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | #include <stdbool.h> | |
11 | #include <stdint.h> | |
12 | ||
13 | /** Stack data structure | |
14 | */ | |
15 | struct stack { | |
16 | int max; /**< Maximum number of entries */ | |
17 | int top; /**< maximum value in stack */ | |
18 | uint32_t *items; /**< items in the stack */ | |
19 | }; | |
20 | ||
21 | /** Initialize stack of uint32_t elements | |
22 | * | |
23 | * [in] num_entries | |
24 | * maximum number of elements in the stack | |
25 | * | |
26 | * [in] items | |
27 | * pointer to items (must be sized to (uint32_t * num_entries) | |
28 | * | |
29 | * s[in] st | |
30 | * pointer to the stack structure | |
31 | * | |
32 | * return | |
33 | * 0 for success | |
34 | */ | |
35 | int stack_init(int num_entries, | |
36 | uint32_t *items, | |
37 | struct stack *st); | |
38 | ||
39 | /** Return the size of the stack | |
40 | * | |
41 | * [in] st | |
42 | * pointer to the stack | |
43 | * | |
44 | * return | |
45 | * number of elements | |
46 | */ | |
47 | int32_t stack_size(struct stack *st); | |
48 | ||
49 | /** Check if the stack is empty | |
50 | * | |
51 | * [in] st | |
52 | * pointer to the stack | |
53 | * | |
54 | * return | |
55 | * true or false | |
56 | */ | |
57 | bool stack_is_empty(struct stack *st); | |
58 | ||
59 | /** Check if the stack is full | |
60 | * | |
61 | * [in] st | |
62 | * pointer to the stack | |
63 | * | |
64 | * return | |
65 | * true or false | |
66 | */ | |
67 | bool stack_is_full(struct stack *st); | |
68 | ||
69 | /** Add element x to the stack | |
70 | * | |
71 | * [in] st | |
72 | * pointer to the stack | |
73 | * | |
74 | * [in] x | |
75 | * value to push on the stack | |
76 | * return | |
77 | * 0 for success | |
78 | */ | |
79 | int stack_push(struct stack *st, uint32_t x); | |
80 | ||
81 | /** Pop top element x from the stack and return | |
82 | * in user provided location. | |
83 | * | |
84 | * [in] st | |
85 | * pointer to the stack | |
86 | * | |
87 | * [in, out] x | |
88 | * pointer to where the value popped will be written | |
89 | * | |
90 | * return | |
91 | * 0 for success | |
92 | */ | |
93 | int stack_pop(struct stack *st, uint32_t *x); | |
94 | ||
95 | /** Dump stack information | |
96 | * | |
97 | * Warning: Don't use for large stacks due to prints | |
98 | * | |
99 | * [in] st | |
100 | * pointer to the stack | |
101 | * | |
102 | * return | |
103 | * none | |
104 | */ | |
105 | void stack_dump(struct stack *st); | |
106 | ||
107 | #endif /* _STACK_H_ */ |