16#define restrict __restrict__
34#define PASTE(a, b) a##b
42#define XPASTE(a, b) PASTE(a, b)
50#define JOIN(a, b) XPASTE(a, XPASTE(_, b))
63#ifndef FSTACK_FOR_EACH
64#define FSTACK_FOR_EACH(self, index, value) \
65 for ((index) = (self)->count; (index) > 0 && ((value) = (self)->values[(index) - 1], true); (index)--)
78#ifndef FSTACK_FOR_EACH_REVERSE
79#define FSTACK_FOR_EACH_REVERSE(self, index, value) \
80 for ((index) = 0; (index) < (self)->count && ((value) = (self)->values[(index)], true); (index)++)
93#ifndef FSTACK_CALC_SIZEOF
94#define FSTACK_CALC_SIZEOF(fstack_name, capacity) \
95 (uint32_t)(offsetof(struct fstack_name, values) + capacity * sizeof(((struct fstack_name *)0)->values[0]))
108#ifndef FSTACK_CALC_SIZEOF_OVERFLOWS
109#define FSTACK_CALC_SIZEOF_OVERFLOWS(fstack_name, capacity) \
110 (capacity > (UINT32_MAX - offsetof(struct fstack_name, values)) / sizeof(((struct fstack_name *)0)->values[0]))
122#error "Must define NAME."
124#define FSTACK_NAME NAME
135#define VALUE_TYPE int
136#define FUNCTION_DEFINITIONS
137#define TYPE_DEFINITIONS
138#error "Must define VALUE_TYPE."
145#ifndef FUNCTION_LINKAGE
146#define FUNCTION_LINKAGE
150#define FSTACK_TYPE struct FSTACK_NAME
151#define FSTACK_IS_EMPTY JOIN(FSTACK_NAME, is_empty)
152#define FSTACK_IS_FULL JOIN(FSTACK_NAME, is_full)
153#define FSTACK_INIT JOIN(FSTACK_NAME, init)
166#ifdef TYPE_DEFINITIONS
189FUNCTION_LINKAGE FSTACK_TYPE *
JOIN(FSTACK_NAME, init)(FSTACK_TYPE *self,
const uint32_t capacity);
204 create_custom)(
const uint32_t capacity,
void *context_ptr,
205 void *(*allocate)(
void *context_ptr,
size_t alignment,
size_t size));
227 void (*deallocate)(
void *context_ptr,
void *mem));
326FUNCTION_LINKAGE void JOIN(FSTACK_NAME, copy)(FSTACK_TYPE *restrict dest_ptr,
const FSTACK_TYPE *restrict src_ptr);
336#ifdef FUNCTION_DEFINITIONS
348 self->capacity = capacity;
354 create_custom)(
const uint32_t capacity,
void *context_ptr,
355 void *(*allocate)(
void *context_ptr,
size_t alignment,
size_t size))
363 FSTACK_TYPE *self = (FSTACK_TYPE *)allocate(context_ptr,
alignof(FSTACK_TYPE), size);
369 memset(self, 0, size);
370 FSTACK_INIT(self, capacity);
376static inline void *
JOIN(internal,
JOIN(FSTACK_NAME, allocate))(
void *context_ptr,
size_t alignment,
size_t size)
386 return JOIN(FSTACK_NAME, create_custom)(capacity, NULL,
JOIN(internal,
JOIN(FSTACK_NAME, allocate)));
390 void (*deallocate)(
void *context_ptr,
void *mem))
392 assert(self != NULL);
394 deallocate(context_ptr, self);
398static inline void JOIN(internal,
JOIN(FSTACK_NAME, deallocate))(
void *context_ptr,
void *mem)
407 assert(self != NULL);
409 JOIN(FSTACK_NAME, destroy_custom)(self, NULL,
JOIN(internal,
JOIN(FSTACK_NAME, deallocate)));
414 assert(self != NULL);
416 return self->count == 0;
421 assert(self != NULL);
423 return self->count == self->capacity;
428 assert(self != NULL);
429 assert(index < self->count);
431 return self->values[self->count - 1 - index];
436 assert(self != NULL);
437 assert(FSTACK_IS_EMPTY(self) ==
false);
439 return self->values[self->count - 1];
444 assert(self != NULL);
445 assert(FSTACK_IS_EMPTY(self) ==
false);
447 return self->values[0];
452 return JOIN(FSTACK_NAME, get_top)(self);
457 assert(self != NULL);
458 assert(FSTACK_IS_FULL(self) ==
false);
460 self->values[self->count++] = value;
465 assert(self != NULL);
466 assert(FSTACK_IS_EMPTY(self) ==
false);
468 return self->values[--self->count];
473 assert(self != NULL);
477FUNCTION_LINKAGE void JOIN(FSTACK_NAME, copy)(FSTACK_TYPE *restrict dest_ptr,
const FSTACK_TYPE *restrict src_ptr)
479 assert(src_ptr != NULL);
480 assert(dest_ptr != NULL);
481 assert(src_ptr->count <= dest_ptr->capacity);
482 assert(FSTACK_IS_EMPTY(dest_ptr));
484 for (uint32_t i = 0; i < src_ptr->count; i++) {
485 dest_ptr->values[i] = src_ptr->values[i];
487 dest_ptr->count = src_ptr->count;
497#undef FUNCTION_LINKAGE
498#undef FUNCTION_DEFINITIONS
499#undef TYPE_DEFINITIONS
503#undef FSTACK_IS_EMPTY
#define FUNCTION_LINKAGE
Specify function linkage e.g. static inline.
Definition fstack_template.h:146
#define JOIN(a, b)
First expand tokens, then paste them together with a _ in between.
Definition fstack_template.h:50
#define VALUE_TYPE
Stack value type. This must be manually defined before including this header file.
Definition fstack_template.h:135
#define FSTACK_CALC_SIZEOF(fstack_name, capacity)
Calculate the size of the stack struct. No overflow checks.
Definition fstack_template.h:94
#define FSTACK_CALC_SIZEOF_OVERFLOWS(fstack_name, capacity)
Check for a given capacity, if the equivalent size of the stack struct overflows.
Definition fstack_template.h:109
uint32_t capacity
maximum number of values allocated for.
Definition fstack_template.h:173
VALUE_TYPE values[]
array of values.
Definition fstack_template.h:174
uint32_t count
number of values.
Definition fstack_template.h:172