remove the free function entirely. all allocators are now expected to
implement reset instead. I reallized I never ever want to free an
individual allocation ever again. The forever_allocator is a special
case wherEreset is a no-op. The arena allocator was deleted. It will be
replaced soon.
This commit is contained in:
2025-12-02 20:21:12 +01:00
parent ef8d51f153
commit 01b8150625
11 changed files with 62 additions and 385 deletions
+12 -28
View File
@@ -4,11 +4,9 @@
#include <assert.h>
#include <stdbool.h>
void *borrow_allocator_alloc_func(borrow_allocator_t *this, size_t bytes, const char *file, int line) {
void *borrow_allocator_alloc_func(borrow_allocator_t *this, size_t bytes) {
linked_allocation_node_t *node = malloc(sizeof(*this->head) + bytes);
if (node == NULL) { return NULL; }
node->file = file;
node->line = line;
node->prev = NULL;
node->next = this->head;
if (this->head != NULL) {
@@ -19,12 +17,10 @@ void *borrow_allocator_alloc_func(borrow_allocator_t *this, size_t bytes, const
return &node->data;
}
void *borrow_allocator_resize_func(borrow_allocator_t *this, void *old_ptr, size_t bytes, const char *file, int line) {
void *borrow_allocator_resize_func(borrow_allocator_t *this, void *old_ptr, size_t bytes) {
linked_allocation_node_t *old_node = PTR_FROM_FIELD_PTR(linked_allocation_node_t, data, old_ptr);
linked_allocation_node_t *new_node = realloc(old_node, sizeof(*old_node) + bytes);
if (new_node == NULL) { return NULL; }
new_node->file = file;
new_node->line = line;
if (this->head == old_node) {
assert(new_node->prev == NULL);
this->head = new_node;
@@ -68,39 +64,27 @@ size_t borrow_allocator_count_allocations(borrow_allocator_t *this) {
return output;
}
static void *borrow_alloc(void *this, size_t bytes, const char *file, int line) {
return borrow_allocator_alloc_func(this, bytes, file, line);
static void *borrow_alloc_impl(void *this, size_t bytes) {
return borrow_allocator_alloc_func(this, bytes);
}
static void *borrow_resize(void *this, void *old_ptr, size_t bytes, const char *file, int line) {
return borrow_allocator_resize_func(this, old_ptr, bytes, file, line);
static void *borrow_resize_impl(void *this, void *old_ptr, size_t bytes) {
return borrow_allocator_resize_func(this, old_ptr, bytes);
}
static void borrow_free(void *this, void *ptr, const char *file, int line) {
(void)file;
(void)line;
borrow_allocator_free(this, ptr);
static void borrow_reset(void *this) {
borrow_allocator_free_all(this);
}
static const allocator_vtbl_t borrow_vtbl = {
.alloc = borrow_alloc,
.resize = borrow_resize,
.free = borrow_free,
.alloc = borrow_alloc_impl,
.resize = borrow_resize_impl,
.reset = borrow_reset,
};
allocator_t borrow_allocator_interface(borrow_allocator_t *this) {
allocator_t borrow_allocator(borrow_allocator_t *this) {
return (allocator_t) {
.this=this,
.vtbl=&borrow_vtbl,
};
}
void borrow_allocator_assert_all_freed(borrow_allocator_t *this) {
bool should_crash = false;
for (linked_allocation_node_t *curr = this->head; curr != NULL; curr = curr->next) {
fprintf(stderr, "%s:%d: allocation not freed\n", curr->file, curr->line);
}
if (should_crash) {
exit(1);
}
}