1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 #ifndef UCX_STACK_H
39 #define UCX_STACK_H
40
41 #include "ucx.h"
42 #include "allocator.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48
49
50
51
52 typedef struct {
53
54 UcxAllocator allocator;
55
56
57 size_t size;
58
59
60 char *space;
61
62
63 char *top;
64 } UcxStack;
65
66
67
68
69 struct ucx_stack_metadata {
70
71
72
73 char *prev;
74
75
76 size_t size;
77 };
78
79
80
81
82
83
84
85
86
87 void ucx_stack_init(UcxStack *stack,
char* space,
size_t size);
88
89
90
91
92
93
94
95
96
97 void *ucx_stack_malloc(UcxStack *stack,
size_t n);
98
99
100
101
102
103
104
105
106 #define ucx_stack_push(stack, n) ucx_stack_malloc(stack, n)
107
108
109
110
111
112
113
114
115
116
117
118
119 void *ucx_stack_calloc(UcxStack *stack,
size_t nelem,
size_t elsize);
120
121
122
123
124
125
126
127
128
129
130 #define ucx_stack_pusharr(stack,n,elsize) ucx_stack_calloc(stack,n,elssize)
131
132
133
134
135
136
137
138
139
140
141
142
143 void *ucx_stack_realloc(UcxStack *stack,
void *ptr,
size_t n);
144
145
146
147
148
149
150
151
152
153
154
155
156
157 void ucx_stack_free(UcxStack *stack,
void *ptr);
158
159
160
161
162
163
164
165 #define ucx_stack_topsize(stack) ((stack)->top ? ((
struct ucx_stack_metadata*)\
166 (stack)->top -
1)->size :
0)
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 #define ucx_stack_pop(stack, dest) ucx_stack_popn(stack, dest, (
size_t)-
1)
182
183
184
185
186
187
188
189
190
191
192
193
194
195 void ucx_stack_popn(UcxStack *stack,
void *dest,
size_t n);
196
197
198
199
200
201
202
203 size_t ucx_stack_avail(UcxStack *stack);
204
205
206
207
208
209
210
211 #define ucx_stack_empty(stack) (!(stack)->top)
212
213
214
215
216
217
218
219
220
221
222
223 #define ucx_stack_dim(size, elems) (size+
sizeof(
struct ucx_stack_metadata) * \
224 (elems +
1))
225
226
227 #ifdef __cplusplus
228 }
229 #endif
230
231 #endif
232
233