7#if !defined(SKIP_INCLUDES)
14 #if !defined PLACEHOLDERS
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
48 .derive_c_staticbumpalloc = {},
70 (*self->buffer), self->used);
71 for (
size_t i = 0; i < self->used; i++) {
72 if ((*self->buffer)[i] != 0) {
73 DC_PANIC(
"Data not freed before clearing the static bump allocator");
79 memset((*self->buffer), 0, self->used);
95 char* ptr = &(*self->buffer)[self->used];
101 char* allocation_ptr = ptr +
sizeof(
USED);
109 return allocation_ptr;
123 memset(ptr, 0, *used_ptr);
138 char* byte_ptr = (
char*)ptr;
142 const bool was_last_alloc = (byte_ptr + *old_size == &(*self->buffer)[self->used]);
144 if (was_last_alloc) {
145 if (new_size > *old_size) {
146 size_t increase = new_size - *old_size;
147 if (self->used + increase >
CAPACITY) {
150 self->used += increase;
152 (
char*)ptr + *old_size, increase);
153 }
else if (new_size < *old_size) {
154 size_t decrease = *old_size - new_size;
155 self->used -= decrease;
159 memset((
char*)ptr + new_size, 0, decrease);
162 (
char*)ptr + new_size, decrease);
165 *old_size = new_size;
171 if (new_size < *old_size) {
175 *old_size = new_size;
186 memcpy(new_buff, ptr, *old_size);
static void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
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 DC_TRAIT_ALLOC(SELF)
static INDEX_TYPE size(SELF const *self)
dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
static void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
static void dc_memory_tracker_set(dc_memory_tracker_level level, dc_memory_tracker_capability cap, const volatile void *addr, size_t size)
@ DC_MEMORY_TRACKER_LVL_ALLOC
@ DC_MEMORY_TRACKER_CAP_WRITE
@ DC_MEMORY_TRACKER_CAP_NONE
@ DC_MEMORY_TRACKER_CAP_READ_WRITE
#define DC_ASSUME(expr,...)
An allocator that prints to stdout when it allocates or frees memory.
dc_gdb_marker derive_c_staticbumpalloc
Debug format helpers for debug printin data structures.
static FILE * stream(SELF *self)
Opens a file for.