fixed it somehow, still WIP

This commit is contained in:
2025-11-15 18:25:34 +01:00
parent d36b6d2c45
commit 92ca008516
5 changed files with 32 additions and 8 deletions
+2
View File
@@ -1,6 +1,8 @@
#include "cig.h"
void *buffer_allocator_alloc(buffer_allocator_t *this, size_t bytes) {
// TODO: use max-align
// also make the default function crash if outOf memory?
size_t new_size = this->size + bytes;
if (new_size > this->capacity) {
return NULL;
+17 -5
View File
@@ -122,14 +122,26 @@ typedef struct arena_allocator {
// initialization this total amount will be allocated as a single chunk.
// This freeing of memory happens on reset, the next alloc allocates the
// whole chunk.
size_t allocated;
// Initial chunk of memory to allocate.
size_t chunk_size;
size_t total_allocated;
uint8_t *bytes;
} arena_allocator_t;
void *arena_allocator_alloc_func(arena_allocator_t *this, size_t bytes, const char *file, int line);
#define arena_allocator_create() \
(arena_allocator_t) { \
.borrow_allocator = borrow_allocator_create(), .total_allocated = 0, \
.bytes = NULL \
}
void *arena_allocator_alloc_func(
arena_allocator_t *this,
size_t bytes,
const char *file,
int line
);
#define arena_allocator_alloc(THIS, BYTES) \
arena_allocator_alloc_func(THIS, BYTES, __FILE__, __LINE__)
// TODO finish this shite
void arena_allocator_reset(arena_allocator_t *this);
void arena_allocator_destroy(arena_allocator_t *this);
// dynamic arrays //////////////////////////////////////////////////////////////
+5 -1
View File
@@ -32,6 +32,7 @@ void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int
file,
line
);
exit(1);
}
return bytes;
}
@@ -70,8 +71,8 @@ void *dyn_array_grow_non_crashing_func(void *this, size_t n_new_items, const cha
);
if (header == NULL) { return NULL; }
header->capacity = new_capacity;
header->size = new_size;
}
header->size = new_size;
return &header->bytes;
}
@@ -90,17 +91,20 @@ void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *fil
}
size_t dyn_array_length(void *this) {
if (this == NULL) { return 0; }
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
return header->size;
}
size_t dyn_array_capacity(void *this) {
if (this == NULL) { return 0; }
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
return header->capacity;
}
void dyn_array_destroy(void *this) {
if (this == NULL) { return; }
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
allocator_free(header->allocator, header);
}
+7 -2
View File
@@ -7,6 +7,13 @@ TESTBIN := /tmp/all_tests
.PHONY: test
run_test: test
$(TESTBIN)
run_test_memcheck: test
valgrind --leak-check=full --show-leak-kinds=all --trace-children=yes --error-exitcode=1 $(TESTBIN); \
test:
@echo "Discovering test files..."
@files=$$(find . -type f -name 'test*.c'); \
@@ -15,8 +22,6 @@ test:
else \
echo "Compiling all test files into $(TESTBIN)..."; \
$(CC) $(CFLAGS) _allocator_impl.c $$files -o $(TESTBIN) $(LDFLAGS) || exit 1; \
echo "Running tests..."; \
valgrind --leak-check=full --show-leak-kinds=all --trace-children=yes --error-exitcode=1 $(TESTBIN); \
fi
PROJECT_ROOT := $(shell pwd)
+1
View File
@@ -38,6 +38,7 @@ Test(dynamic_arrays, pop) {
dyn_array_append(numbers, 48);
dyn_array_append(numbers, 49);
dyn_array_append(numbers, 50);
cr_assert_eq(dyn_array_length(numbers), 11);
// NOTE: you can stack for loops to have scoped variables you can abuse
// in macros. e.g.
// for (TYPE UNIQUE = (int)dyn_array_length(numbers); UNIQUE != 0; UNIQUE = 0;)