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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* bstack_alloc — region-management layer on top of bstack.
*
* Key types
* ---------
* bstack_slice_t — lightweight handle (allocator ptr + offset + len)
* to a contiguous region of a bstack payload.
* bstack_allocator_t — vtable base for types that own a bstack and manage
* regions within it. Vtable methods: stack, alloc,
* realloc, dealloc. Convenience helpers (inline):
* bstack_allocator_len, bstack_allocator_is_empty.
* bstack_slice_reader_t — cursor-based reader over a bstack_slice_t.
* linear_bstack_allocator_t — bump allocator; every operation maps to one call.
*
* Compile with -DBSTACK_FEATURE_SET to enable bstack_slice_write and friends.
*/
/* -------------------------------------------------------------------------
* Forward declaration — bstack_slice_t holds a bstack_allocator_t pointer.
* ---------------------------------------------------------------------- */
typedef struct bstack_allocator bstack_allocator_t;
/* =========================================================================
* bstack_slice_t
* ====================================================================== */
typedef struct bstack_slice_t;
/*
* Accessor macros — zero-cost field reads.
* bstack_slice_start(s) → logical start offset in the payload
* bstack_slice_end(s) → exclusive logical end offset
* bstack_slice_len(s) → number of bytes in the slice
* bstack_slice_is_empty(s) → non-zero if the slice spans zero bytes
*/
extern "C" BSTACK_FEATURE_SET
/*
* Overwrite the first min(data_len, s.len) bytes of the slice in place.
* Requires -DBSTACK_FEATURE_SET.
*/
int ;
/*
* Overwrite [start, start+data_len) within this slice in place.
* start is 0-based within the slice.
* Returns -1 with errno = EINVAL if start + data_len exceeds s.len.
* Requires -DBSTACK_FEATURE_SET.
*/
int ;
/*
* Zero the entire slice in place.
* Requires -DBSTACK_FEATURE_SET.
*/
int ;
/*
* Zero [start, start+n) within this slice in place.
* start is 0-based within the slice.
* Returns -1 with errno = EINVAL if start + n exceeds s.len.
* Requires -DBSTACK_FEATURE_SET.
*/
int ;
/* BSTACK_FEATURE_SET */
/* =========================================================================
* bstack_slice_reader_t
*
* Cursor-based reader over a bstack_slice_t.
* All positions and lengths are in slice-relative coordinates [0, slice.len).
* ====================================================================== */
typedef struct bstack_slice_reader_t;
/* Current cursor position within the slice (0-based). */
/* The underlying slice. */
/* Construct a reader positioned at the start of the slice. */
bstack_slice_reader_t ;
/* Construct a reader positioned at offset bytes into the slice. */
bstack_slice_reader_t ;
/*
* Read up to buf_len bytes from the current cursor position into buf, then
* advance the cursor by the number of bytes read.
* If n_read is non-NULL it receives the number of bytes read.
* Returns 0 on success (including end-of-slice where *n_read = 0).
* Returns -1 on I/O failure (errno set).
*/
int ;
/*
* Seek to an absolute position within the slice.
* Seeking past slice.len is allowed; reads from that position return 0 bytes.
* Always succeeds. If out_pos is non-NULL it receives the new cursor position.
*/
int ;
/*
* Seek relative to the current cursor (cursor += delta).
* Returns -1 with errno = EINVAL if the resulting position would be negative.
* If out_pos is non-NULL it receives the new cursor position.
*/
int ;
/*
* Seek relative to the end of the slice (cursor = slice.len + delta).
* Returns -1 with errno = EINVAL if the resulting position would be negative.
* If out_pos is non-NULL it receives the new cursor position.
*/
int ;
/* =========================================================================
* bstack_allocator_t — vtable interface
*
* Base type for region allocators backed by a bstack. Embed as the first
* member of a concrete allocator struct so that a pointer to the concrete
* struct can be safely cast to bstack_allocator_t *.
*
* Vtable methods: stack, alloc, realloc, dealloc.
* Convenience helpers (inline functions below): len, is_empty.
* ====================================================================== */
typedef struct bstack_allocator_vtbl_t;
;
/* -------------------------------------------------------------------------
* Vtable forwarding helpers — thin static inline wrappers.
* ---------------------------------------------------------------------- */
static inline bstack_t *
static inline int
static inline int
/*
* Dispatch dealloc through the vtable. If the vtable entry is NULL the call
* is a no-op and returns 0 (equivalent to a default no-op dealloc).
*/
static inline int
/*
* Return the current logical payload size via the allocator's stack.
* Delegates to bstack_len; returns 0 on success, -1 on failure (errno set).
*/
static inline int
/*
* Set *out_empty to 1 if the backing stack is empty, 0 otherwise.
* Delegates to bstack_len; returns 0 on success, -1 on failure (errno set).
*/
static inline int
/* =========================================================================
* linear_bstack_allocator_t — bump allocator
*
* Allocates regions sequentially by appending to the tail. Every operation
* maps to exactly one bstack call and is therefore crash-safe by inheritance:
*
* alloc → bstack_extend
* realloc (grow) → bstack_extend
* realloc (shrink) → bstack_discard
* dealloc (tail) → bstack_discard
* dealloc (non-tail) → no-op
*
* realloc of a non-tail slice returns -1 with errno = ENOTSUP.
* ====================================================================== */
typedef struct linear_bstack_allocator_t;
/*
* Allocate and initialise a linear_bstack_allocator_t that takes ownership of bs.
* Returns NULL on allocation failure (errno = ENOMEM).
* Cast the result to bstack_allocator_t * to use the generic allocator interface.
*/
linear_bstack_allocator_t *;
/*
* Free the allocator wrapper without closing the underlying bstack.
* The caller must have already retrieved the bstack via
* linear_bstack_allocator_into_stack, or accepts losing the reference.
*/
void ;
/*
* Consume the allocator: free the wrapper and return the underlying bstack.
* The returned bstack_t * must eventually be passed to bstack_close.
*/
bstack_t *;
}
/* BSTACK_ALLOC_H */