#include <stdint.h>
#include <string.h>
#include <derive-c/core.h>
#include <derive-c/panic.h>
#include <derive-c/self.h>
Go to the source code of this file.
|
#define | CAPACITY 1024 |
| A very simple bump allocator making use of a provided fixed size buffer (e.g. statically allocated).
|
#define | USED uint8_t |
◆ CAPACITY
A very simple bump allocator making use of a provided fixed size buffer (e.g. statically allocated).
- Useful when an upper bound for allocation size is known at compile time
- Can be used when no global allocator is available (e.g. embedded systems, kernel dev)
Definition at line 16 of file template.h.
◆ USED
◆ calloc()
void * calloc |
( |
SELF * | self, |
|
|
size_t | count, |
|
|
size_t | size ) |
|
static |
Definition at line 134 of file template.h.
134 {
135
136
138}
static void * malloc(SELF *self, size_t size)
static INDEX_TYPE size(SELF const *self)
◆ clear()
void clear |
( |
SELF * | self | ) |
|
|
static |
Clear the allocator, note that all data should be freed before this occurs.
Definition at line 51 of file template.h.
51 {
53
54#ifndef NDEBUG
55
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");
59 }
60 }
61#endif
64}
#define DEBUG_ASSERT(expr)
◆ free()
void free |
( |
SELF * | DEBUG_UNUSEDself, |
|
|
void * | DEBUG_UNUSEDptr ) |
|
static |
Definition at line 83 of file template.h.
83 {
86
87#ifndef NDEBUG
88
89
90
92 memset(ptr, 0, *used_ptr);
93 *used_ptr = 0;
94#endif
95}
◆ get_used()
Definition at line 66 of file template.h.
66 {
68 return self->used;
69}
◆ malloc()
void * malloc |
( |
SELF * | self, |
|
|
size_t | size ) |
|
static |
Definition at line 71 of file template.h.
71 {
74 return NULL;
75 }
80 return ptr +
sizeof(
USED);
81}
#define CAPACITY
A very simple bump allocator making use of a provided fixed size buffer (e.g. statically allocated).
◆ new()
Definition at line 41 of file template.h.
41 {
42 SELF self = {.used = 0, .derive_c_staticbumpalloc = {}};
43
44
45
47 return self;
48}
◆ realloc()
void * realloc |
( |
SELF * | self, |
|
|
void * | ptr, |
|
|
size_t | new_size ) |
|
static |
Definition at line 97 of file template.h.
97 {
100
101 char* byte_ptr = (char*)ptr;
103 const bool was_last_alloc = (byte_ptr + *old_size == self->
buffer + self->
used);
104
105 if (was_last_alloc) {
107 return NULL;
108 }
109
110 self->
used += (new_size - *old_size);
111 *old_size = new_size;
112 return ptr;
113 }
114
115 if (new_size < *old_size) {
116
117
118
119 *old_size = new_size;
120 return ptr;
121 }
122
124 if (!new_buff) {
125 return NULL;
126 }
127
128 memcpy(new_buff, ptr, *old_size);
129
131 return new_buff;
132}
static void free(SELF *self, void *ptr)
◆ metadata_size
size_t metadata_size = sizeof(USED) |
|
static |