14 #if !defined __clang_analyzer__
15 #error "The capacity of the static allocator must be defined"
20#if CAPACITY > (1ULL << 30)
21 #error "CAPACITY must not exceed 1 GiB"
24#if CAPACITY <= UINT8_MAX
26#elif CAPACITY <= UINT16_MAX
28#elif CAPACITY <= UINT32_MAX
43 SELF self = {.used = 0, .derive_c_staticbumpalloc = {}};
57 for (
size_t i = 0; i < self->used; i++) {
58 if (self->buffer[i] != 0) {
59 PANIC(
"Data not freed before clearing the static bump allocator");
63 memset(self->buffer, 0, self->used);
77 char* ptr = &self->buffer[self->used];
81 return ptr +
sizeof(
USED);
93 memset(ptr, 0, *used_ptr);
105 char* byte_ptr = (
char*)ptr;
107 const bool was_last_alloc = (byte_ptr + *old_size == self->buffer + self->used);
109 if (was_last_alloc) {
110 if (self->used + (new_size - *old_size) >
CAPACITY) {
114 self->used += (new_size - *old_size);
115 *old_size = new_size;
119 if (new_size < *old_size) {
123 *old_size = new_size;
132 memcpy(new_buff, ptr, *old_size);
static void free(SELF *self, void *ptr)
static void * realloc(SELF *self, void *ptr, size_t size)
static void * malloc(SELF *self, size_t size)
static void * calloc(SELF *self, size_t count, size_t size)
static void clear(SELF *self)
Clear the allocator, note that all data should be freed before this occurs.
static size_t metadata_size
static USED get_used(SELF const *self)
#define CAPACITY
A very simple bump allocator making use of a provided fixed size buffer (e.g. statically allocated).
#define DEBUG_UNUSED(ident)
#define DEBUG_ASSERT(expr)
gdb_marker derive_c_staticbumpalloc
static INDEX_TYPE size(SELF const *self)