trying to find out what I dont understand

This commit is contained in:
2025-11-26 20:57:26 +01:00
parent 0a4120bccd
commit 7f85664f13
4 changed files with 79 additions and 9 deletions
+7 -8
View File
@@ -119,17 +119,12 @@ allocator_t borrow_allocator_interface(borrow_allocator_t *this);
// memory chunk that is needed for a single frame. // memory chunk that is needed for a single frame.
typedef struct arena_allocator { typedef struct arena_allocator {
borrow_allocator_t borrow_allocator; borrow_allocator_t borrow_allocator;
// Total size of bytes allocated. This allocator will only grow the // Set to 0 when instantiating. Will be updated as the borrow allocator is
// allocated field as allocations are made, keeping track of the total // used. Is used on reset to allocate a continous chunk of memory.
// memory allocated, and if the allocations have to be done in several
// chunks because your initial guess of how much memory was needed doesn't
// meet reality, all chunks will be freed, and then for the next
// 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 total_allocated; size_t total_allocated;
uint8_t *bytes; uint8_t *bytes;
} arena_allocator_t; } arena_allocator_t;
#define arena_allocator_create() \ #define arena_allocator_create() \
(arena_allocator_t) { \ (arena_allocator_t) { \
.borrow_allocator = borrow_allocator_create(), .total_allocated = 0, \ .borrow_allocator = borrow_allocator_create(), .total_allocated = 0, \
@@ -151,6 +146,10 @@ void arena_allocator_reset_func(arena_allocator_t *this, const char *file, int l
void arena_allocator_destroy(arena_allocator_t *this); void arena_allocator_destroy(arena_allocator_t *this);
allocator_t arena_allocator_interface(arena_allocator_t *this); allocator_t arena_allocator_interface(arena_allocator_t *this);
// TODO: make the reset happen at the start, not the end. That allows the user
// to specify some starting size of the buffer.
// You cannot use return within this block.
#define with_arena(ARENA, ALLOCATOR_NAME) \ #define with_arena(ARENA, ALLOCATOR_NAME) \
for (allocator_t ALLOCATOR_NAME = arena_allocator_interface(ARENA); \ for (allocator_t ALLOCATOR_NAME = arena_allocator_interface(ARENA); \
ALLOCATOR_NAME.vtbl != NULL; \ ALLOCATOR_NAME.vtbl != NULL; \
+7 -1
View File
@@ -5,7 +5,7 @@ LDFLAGS := -lcriterion
TESTBIN := /tmp/all_tests TESTBIN := /tmp/all_tests
.PHONY: test .PHONY: test scratch
run_test: test run_test: test
$(TESTBIN) $(TESTBIN)
@@ -30,3 +30,9 @@ CRITERION_HEADERS := $(shell find /usr/include/criterion)
ALL_FILES := $(PROJECT_C_FILES) $(CRITERION_HEADERS) ALL_FILES := $(PROJECT_C_FILES) $(CRITERION_HEADERS)
tags: $(ALL_FILES) tags: $(ALL_FILES)
ctags -R $(ALL_FILES) ctags -R $(ALL_FILES)
scratch: /tmp/scratch
valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1 /tmp/scratch
/tmp/scratch: scratch.c
gcc scratch.c -o /tmp/scratch
+43
View File
@@ -0,0 +1,43 @@
#define ALLOCATOR_IMPLEMENTATION
#include "cig.h"
#include <stdio.h>
#include <assert.h>
int main() {
// test 1///////////////////////////////////////////////////////////////////////
arena_allocator_t aalloc = arena_allocator_create();
for ( int i = 0; i < 10; i++ ) with_arena(&aalloc, allocator) {
allocator_alloc(allocator, 10);
}
assert(aalloc.total_allocated == 10);
assert(borrow_allocator_count_allocations(&aalloc.borrow_allocator) == 0);
assert(dyn_array_capacity(aalloc.bytes) == 10);
arena_allocator_destroy(&aalloc);
assert(aalloc.total_allocated == 0);
arena_allocator_destroy(&aalloc);
// test 2 //////////////////////////////////////////////////////////////////////
aalloc = arena_allocator_create();
for ( int i = 0; i < 10; i++ ) with_arena(&aalloc, allocator) {
allocator_alloc(allocator, 1000);
}
with_arena(&aalloc, allocator) {
size_t prev_addr = 0;
prev_addr = ~prev_addr;
for (int i = 0; i < 1001; i++) {
size_t addr = (size_t)allocator_alloc(allocator, 1);
assert(addr != prev_addr);
assert(addr % MAX_ALIGN == 0);
}
}
arena_allocator_reset(&aalloc);
fprintf(stderr, "%zu\n", aalloc.total_allocated);
//assert(aalloc.total_allocated == MAX_ALIGN * 1000 + 1);
// TODO: WHAT is happening here?
arena_allocator_destroy(&aalloc);
}
+22
View File
@@ -1,5 +1,6 @@
#include <criterion/criterion.h> #include <criterion/criterion.h>
#include "cig.h" #include "cig.h"
#include <stdio.h>
Test(arena_allocator, repeated_allocations) { Test(arena_allocator, repeated_allocations) {
arena_allocator_t aalloc = arena_allocator_create(); arena_allocator_t aalloc = arena_allocator_create();
@@ -16,4 +17,25 @@ Test(arena_allocator, repeated_allocations) {
cr_assert_eq(aalloc.total_allocated, 0); cr_assert_eq(aalloc.total_allocated, 0);
} }
Test(arena_allocator, alignment) {
arena_allocator_t aalloc = arena_allocator_create();
for ( int i = 0; i < 10; i++ ) with_arena(&aalloc, allocator) {
allocator_alloc(allocator, 1000);
}
with_arena(&aalloc, allocator) {
size_t prev_addr = 0;
prev_addr = ~prev_addr;
for (int i = 0; i < 1001; i++) {
size_t addr = (size_t)allocator_alloc(allocator, 1);
cr_assert_neq(addr, prev_addr);
cr_assert_eq(addr % MAX_ALIGN, 0);
}
}
arena_allocator_reset(&aalloc);
fprintf(stderr, "%zu\n", aalloc.total_allocated);
cr_assert_eq(aalloc.total_allocated, MAX_ALIGN * 1000 + 1);
}
// TODO somehow test reallocations // TODO somehow test reallocations