some rename

This commit is contained in:
2025-11-30 19:43:49 +01:00
parent 7f85664f13
commit 3e1565e67f
4 changed files with 30 additions and 20 deletions
+6 -6
View File
@@ -20,7 +20,7 @@ void *arena_allocator_alloc_func(
}
allocated = borrow_allocator_alloc_func(&this->borrow_allocator, bytes, file, line);
if (allocated != NULL) {
this->total_allocated += bytes;
this->to_allocate += bytes;
return allocated;
}
return NULL;
@@ -33,7 +33,7 @@ void arena_allocator_reset_func(arena_allocator_t *this, const char *file, int l
(dyn_array_create_non_crashing_func_args_t) {
.allocator=allocator_stdlib(),
.itemsize=sizeof(uint8_t),
.initial_capacity=this->total_allocated,
.initial_capacity=this->to_allocate,
.file=file,
.line=line,
}
@@ -42,8 +42,8 @@ void arena_allocator_reset_func(arena_allocator_t *this, const char *file, int l
if (this->bytes == NULL) {
return;
}
if (dyn_array_capacity(this->bytes) < this->total_allocated) {
size_t needed_bytes = this->total_allocated - dyn_array_length(this->bytes);
if (dyn_array_capacity(this->bytes) < this->to_allocate) {
size_t needed_bytes = this->to_allocate - dyn_array_length(this->bytes);
uint8_t *new_bytes = dyn_array_grow_non_crashing_func(
this->bytes,
needed_bytes,
@@ -107,10 +107,10 @@ static void *arena_allocator_resize_impl(void *this, void *old_ptr, size_t bytes
} else {
void *new_buffer = borrow_allocator_resize_func(&t->borrow_allocator, old_ptr, bytes, file, line);
if (new_buffer == NULL) return NULL;
// The bytes added to total_allocated is just an educated guess. If
// The bytes added to to_allocate is just an educated guess. If
// calling realloc, you are often allocating twice what you got from the
// last malloc/realloc call.
t->total_allocated += bytes/2;
t->to_allocate += bytes/2;
return new_buffer;
}
return arena_allocator_alloc_func(
+12 -2
View File
@@ -121,13 +121,14 @@ typedef struct arena_allocator {
borrow_allocator_t borrow_allocator;
// Set to 0 when instantiating. Will be updated as the borrow allocator is
// used. Is used on reset to allocate a continous chunk of memory.
size_t total_allocated;
size_t to_allocate;
size_t i;
uint8_t *bytes;
} arena_allocator_t;
#define arena_allocator_create() \
(arena_allocator_t) { \
.borrow_allocator = borrow_allocator_create(), .total_allocated = 0, \
.borrow_allocator = borrow_allocator_create(), .to_allocate = 0, .i = 0, \
.bytes = NULL \
}
@@ -143,6 +144,15 @@ void *arena_allocator_alloc_func(
void arena_allocator_reset_func(arena_allocator_t *this, const char *file, int line);
#define arena_allocator_reset(THIS) \
arena_allocator_reset_func(THIS, __FILE__, __LINE__)
void *arena_allocator_resize_func(
arena_allocator_t *this,
void *old_ptr,
size_t bytes,
const char *file,
int line
);
#define arena_allocator_resize(THIS, OLD_PTR, BYTES) \
arena_allocator_resize_func(THIS, OLD_PTR, BYTES, __FILE__, __LINE__)
void arena_allocator_destroy(arena_allocator_t *this);
allocator_t arena_allocator_interface(arena_allocator_t *this);
+4 -4
View File
@@ -11,12 +11,12 @@ int main() {
allocator_alloc(allocator, 10);
}
assert(aalloc.total_allocated == 10);
assert(aalloc.to_allocate == 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);
assert(aalloc.to_allocate == 0);
arena_allocator_destroy(&aalloc);
// test 2 //////////////////////////////////////////////////////////////////////
@@ -36,8 +36,8 @@ int main() {
}
}
arena_allocator_reset(&aalloc);
fprintf(stderr, "%zu\n", aalloc.total_allocated);
//assert(aalloc.total_allocated == MAX_ALIGN * 1000 + 1);
fprintf(stderr, "%zu\n", aalloc.to_allocate);
//assert(aalloc.to_allocate == MAX_ALIGN * 1000 + 1);
// TODO: WHAT is happening here?
arena_allocator_destroy(&aalloc);
}
+4 -4
View File
@@ -9,12 +9,12 @@ Test(arena_allocator, repeated_allocations) {
allocator_alloc(allocator, 10);
}
cr_assert_eq(aalloc.total_allocated, 10);
cr_assert_eq(aalloc.to_allocate, 10);
cr_assert_eq(borrow_allocator_count_allocations(&aalloc.borrow_allocator), 0);
cr_assert_eq(dyn_array_capacity(aalloc.bytes), 10);
arena_allocator_destroy(&aalloc);
cr_assert_eq(aalloc.total_allocated, 0);
cr_assert_eq(aalloc.to_allocate, 0);
}
Test(arena_allocator, alignment) {
@@ -34,8 +34,8 @@ Test(arena_allocator, alignment) {
}
}
arena_allocator_reset(&aalloc);
fprintf(stderr, "%zu\n", aalloc.total_allocated);
cr_assert_eq(aalloc.total_allocated, MAX_ALIGN * 1000 + 1);
fprintf(stderr, "%zu\n", aalloc.to_allocate);
cr_assert_eq(aalloc.to_allocate, MAX_ALIGN * 1000 + 1);
}
// TODO somehow test reallocations