some small progress on the borrow allocator

This commit is contained in:
2025-10-07 20:38:19 +02:00
parent 4463c6a7c5
commit 6ffbc05f52
2 changed files with 14 additions and 3 deletions
+3
View File
@@ -10,6 +10,9 @@ typedef union any_align { char c; int i; long l; long long ll; float f; double d
#define KB (1024) #define KB (1024)
#define MB (KB * KB) #define MB (KB * KB)
#define GB (KB * KB * KB) #define GB (KB * KB * KB)
#define OFFSET(STRUCT, FIELD) ((size_t) (&((STRUCT*) NULL)->FIELD))
#define PTR_FROM_FIELD_PTR(STRUCT, FIELD, PTR) ((STRUCT *) (((char *) PTR) - OFFSET(STRUCT, FIELD)))
// Contains all operations an allocator can do. Similar interface to sdtlibs // Contains all operations an allocator can do. Similar interface to sdtlibs
// malloc, realloc and free. // malloc, realloc and free.
+11 -3
View File
@@ -1,17 +1,25 @@
#include "allocator.h" #include "allocator.h"
void *borrow_allcoator_alloc(borrow_allocator_t *this, size_t bytes) { void *borrow_allcoator_alloc(borrow_allocator_t *this, size_t bytes) {
linked_allocation_node_t *node = malloc(sizeof(*this->head) + bytes); linked_allocation_node_t *node = malloc(sizeof(*this->head) + bytes);
if (node == NULL) { return NULL; }
node->prev = NULL; node->prev = NULL;
node->next = this->head; node->next = this->head;
if (this->head != NULL) { if (this->head != NULL) {
// TODO: correct? this->head->prev = node;
this->head.prev = node;
} }
this->head = node; this->head = node;
} }
// WARNING: it is important to consider the case where the old_node is the head
// node.
void *borrow_allcoator_resize(borrow_allocator_t *this, void *old_ptr, size_t bytes) { void *borrow_allcoator_resize(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);
// TODO: update the links
} }
void borrow_allcoator_free(borrow_allocator_t *this, void *old_ptr) { void borrow_allcoator_free(borrow_allocator_t *this, void *old_ptr) {
linked_allocation_node_t *old_node = PTR_FROM_FIELD_PTR(linked_allocation_node_t, data, old_ptr);
// TODO: update the links
free(old_node);
} }