Go to the source code of this file.
|
struct | SELF |
| An allocator that prints to stdout when it allocates or frees memory. More...
|
|
#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 17 of file template.h.
◆ USED
◆ calloc()
void * calloc |
( |
SELF * | self, |
|
|
size_t | count, |
|
|
size_t | size ) |
|
static |
Definition at line 138 of file template.h.
138 {
139
140
142}
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 52 of file template.h.
52 {
54
55#if !defined NDEBUG
56
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");
60 }
61 }
62#endif
65}
#define DEBUG_ASSERT(expr)
◆ free()
void free |
( |
SELF * | DEBUG_UNUSEDself, |
|
|
void * | DEBUG_UNUSEDptr ) |
|
static |
Definition at line 84 of file template.h.
84 {
87
88#if !defined NDEBUG
89
90
91
93 memset(ptr, 0, *used_ptr);
94 *used_ptr = 0;
95#endif
96}
◆ get_used()
Definition at line 67 of file template.h.
67 {
69 return self->used;
70}
◆ malloc()
void * malloc |
( |
SELF * | self, |
|
|
size_t | size ) |
|
static |
Definition at line 72 of file template.h.
72 {
75 return NULL;
76 }
81 return ptr +
sizeof(
USED);
82}
#define CAPACITY
A very simple bump allocator making use of a provided fixed size buffer (e.g. statically allocated).
◆ new()
Definition at line 42 of file template.h.
42 {
43 SELF self = {.used = 0, .derive_c_staticbumpalloc = {}};
44
45
46
48 return self;
49}
◆ realloc()
void * realloc |
( |
SELF * | self, |
|
|
void * | ptr, |
|
|
size_t | new_size ) |
|
static |
Definition at line 98 of file template.h.
98 {
100
101 if (!ptr) {
103 }
104
105 char* byte_ptr = (char*)ptr;
107 const bool was_last_alloc = (byte_ptr + *old_size == self->
buffer + self->
used);
108
109 if (was_last_alloc) {
111 return NULL;
112 }
113
114 self->
used += (new_size - *old_size);
115 *old_size = new_size;
116 return ptr;
117 }
118
119 if (new_size < *old_size) {
120
121
122
123 *old_size = new_size;
124 return ptr;
125 }
126
128 if (!new_buff) {
129 return NULL;
130 }
131
132 memcpy(new_buff, ptr, *old_size);
133
135 return new_buff;
136}
static void free(SELF *self, void *ptr)
◆ metadata_size
size_t metadata_size = sizeof(USED) |
|
static |