some cleanup

This commit is contained in:
2025-12-06 14:36:57 +01:00
parent 536c1ef990
commit 234021cdf5
3 changed files with 17 additions and 50 deletions
-21
View File
@@ -30,19 +30,6 @@ static void *borrow_allocator_resize_func(borrow_allocator_t *this, void *old_pt
return &new_node->data; return &new_node->data;
} }
// TODO: remove
static void borrow_allocator_free(borrow_allocator_t *this, void *old_ptr) {
linked_allocation_node_t *node = PTR_FROM_FIELD_PTR(linked_allocation_node_t, data, old_ptr);
if (node->prev != NULL) { node->prev->next = node->next; }
if (node->next != NULL) { node->next->prev = node->prev; }
if (this->head == node) {
assert(node->prev == NULL);
if (node->next != NULL) { assert(node->next->prev == NULL); }
this->head = node->next;
}
free(node);
}
static void borrow_allocator_free_all(borrow_allocator_t *this) { static void borrow_allocator_free_all(borrow_allocator_t *this) {
if (this->head == NULL) { if (this->head == NULL) {
return; return;
@@ -57,14 +44,6 @@ static void borrow_allocator_free_all(borrow_allocator_t *this) {
this->head = NULL; this->head = NULL;
} }
static size_t borrow_allocator_count_allocations(borrow_allocator_t *this) {
size_t output = 0;
for (linked_allocation_node_t *node = this->head; node != NULL; node = node->next) {
output++;
}
return output;
}
static void *borrow_alloc_impl(void *this, size_t bytes) { static void *borrow_alloc_impl(void *this, size_t bytes) {
return borrow_allocator_alloc_func(this, bytes); return borrow_allocator_alloc_func(this, bytes);
} }
-25
View File
@@ -143,31 +143,6 @@ void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *fil
dyn_array_shrink(THIS, dyn_array_length(THIS)); \ dyn_array_shrink(THIS, dyn_array_length(THIS)); \
} while (0) } while (0)
typedef struct dyn_array_create_non_crashing_func_args {
allocator_t allocator;
size_t itemsize;
size_t initial_capacity;
const char *file;
size_t line;
} dyn_array_create_non_crashing_func_args_t;
void *dyn_array_create_non_crashing_func(dyn_array_create_non_crashing_func_args_t args);
// This version returns a NULL pointer instead of crashing if the allocator return NULL.
// It is up to you to check that the pointer returned isn't NULL
#define dyn_array_create_non_crashing(ALLOCATOR, TYPE, ...) \
((TYPE *)dyn_array_create_non_crashing_func( \
(dyn_array_create_non_crashing_func_args_t){.allocator = ALLOCATOR, \
.itemsize = sizeof(TYPE), \
.file = __FILE__, \
.line = __LINE__, \
__VA_ARGS__}))
// TODO: remove the non-crashing versions. they crash...
// This version returns a NULL pointer instead of crashing if the allocator return NULL.
// It is up to you to check that the pointer returned isn't NULL
// Always reassign the array. if multiple variables reference the same growing
// array, then you should be using pointer pointers.
void *dyn_array_grow_non_crashing_func(void *this, size_t n_new_items, const char *file, int line);
#define dyn_array_grow_non_crashing(THIS, N_NEW_ITEMS) dyn_array_grow_non_crashing_func(THIS, N_NEW_ITEMS)
size_t dyn_array_length(void *this); size_t dyn_array_length(void *this);
size_t dyn_array_capacity(void *this); size_t dyn_array_capacity(void *this);
+17 -4
View File
@@ -1,8 +1,18 @@
#include "cig.h" #include "cig.h"
#include <stdio.h> #include <stdio.h>
typedef struct dyn_array_create_non_crashing_func_args {
allocator_t allocator;
size_t itemsize;
size_t initial_capacity;
const char *file;
size_t line;
} dyn_array_create_non_crashing_func_args_t;
static inline void *todo_remove_create_func(dyn_array_create_non_crashing_func_args_t args);
void *dyn_array_create_func(dyn_array_create_func_args_t args) { void *dyn_array_create_func(dyn_array_create_func_args_t args) {
void *bytes = dyn_array_create_non_crashing_func( void *bytes = todo_remove_create_func(
(dyn_array_create_non_crashing_func_args_t) { (dyn_array_create_non_crashing_func_args_t) {
.allocator=args.allocator, .allocator=args.allocator,
.itemsize=args.itemsize, .itemsize=args.itemsize,
@@ -14,12 +24,15 @@ void *dyn_array_create_func(dyn_array_create_func_args_t args) {
return bytes; return bytes;
} }
static inline void *todo_remove_grow_func(void *this, size_t n_new_items, const char *file, int line);
void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int line) { void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int line) {
void *bytes = dyn_array_grow_non_crashing_func(this, n_new_items, file, line); void *bytes = todo_remove_grow_func(this, n_new_items, file, line);
return bytes; return bytes;
} }
void *dyn_array_create_non_crashing_func(dyn_array_create_non_crashing_func_args_t args) { // These functions are from when there were non-crashing versions of these
// allocating functions.
static inline void *todo_remove_create_func(dyn_array_create_non_crashing_func_args_t args) {
dyn_array_header_t *header = allocator_alloc_func( dyn_array_header_t *header = allocator_alloc_func(
args.allocator, args.allocator,
sizeof(dyn_array_header_t) + args.itemsize * args.initial_capacity, sizeof(dyn_array_header_t) + args.itemsize * args.initial_capacity,
@@ -33,7 +46,7 @@ void *dyn_array_create_non_crashing_func(dyn_array_create_non_crashing_func_args
return &header->bytes; return &header->bytes;
} }
void *dyn_array_grow_non_crashing_func(void *this, size_t n_new_items, const char *file, int line) { static inline void *todo_remove_grow_func(void *this, size_t n_new_items, const char *file, int line) {
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this); dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
size_t new_size = header->size + n_new_items; size_t new_size = header->size + n_new_items;