13#ifndef __clang_analyzer__
14#error "The capacity of the static allocator must be defined"
19#if CAPACITY > (1ULL << 30)
20#error "CAPACITY must not exceed 1 GiB"
23#if CAPACITY <= UINT8_MAX
25#elif CAPACITY <= UINT16_MAX
27#elif CAPACITY <= UINT32_MAX
42 SELF self = {.used = 0, .derive_c_staticbumpalloc = {}};
56 for (
size_t i = 0; i < self->used; i++) {
57 if (self->buffer[i] != 0) {
58 PANIC(
"Data not freed before clearing the static bump allocator");
62 memset(self->buffer, 0, self->used);
76 char* ptr = &self->buffer[self->used];
80 return ptr +
sizeof(
USED);
92 memset(ptr, 0, *used_ptr);
101 char* byte_ptr = (
char*)ptr;
103 const bool was_last_alloc = (byte_ptr + *old_size == self->buffer + self->used);
105 if (was_last_alloc) {
106 if (self->used + (new_size - *old_size) >
CAPACITY) {
110 self->used += (new_size - *old_size);
111 *old_size = new_size;
115 if (new_size < *old_size) {
119 *old_size = new_size;
128 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)