Skip to content

Commit

Permalink
code: fixed issue with alignment on memory arena
Browse files Browse the repository at this point in the history
  • Loading branch information
inlife committed Sep 17, 2022
1 parent 320911e commit ece7637
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
10 changes: 7 additions & 3 deletions code/source/essentials/memory_custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,16 @@ ZPL_ALLOCATOR_PROC(zpl_arena_allocator_proc) {
switch (type) {
case ZPL_ALLOCATION_ALLOC: {
void *end = zpl_pointer_add(arena->physical_start, arena->total_allocated);
zpl_isize total_size = size + alignment;
zpl_isize total_size = zpl_align_forward_i64(size, alignment);

// NOTE: Out of memory
if (arena->total_allocated + total_size > cast(zpl_isize) arena->total_size) {
zpl__printf_err("%s", "Arena out of memory\n");
return NULL;
if (arena->auto_growth == 0) {
// zpl__printf_err("%s", "Arena out of memory\n");
return NULL;
}


}

ptr = zpl_align_forward(end, alignment);
Expand Down
20 changes: 20 additions & 0 deletions code/tests/cases/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MODULE(memory, {
IT("should be supporting plain memory arena", {
zpl_arena arena = {0};
zpl_arena_init_from_allocator(&arena, zpl_heap(), 1024);

char *buffer1 = (char *)zpl_alloc(zpl_arena_allocator(&arena), 512);
char *buffer2 = (char *)zpl_alloc(zpl_arena_allocator(&arena), 256);

EQUALS(arena.total_allocated, 512+256);

zpl_unused(buffer1);
zpl_unused(buffer2);

zpl_arena_free(&arena);
});

IT("should be support growing memory arena", {

});
});
2 changes: 2 additions & 0 deletions code/tests/tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "cases/csv.h"
#include "cases/alloc_pool.h"
#include "cases/hashing.h"
#include "cases/memory.h"
#include "cases/time.h"
#include "cases/stream.h"
#include "cases/print.h"
Expand All @@ -21,6 +22,7 @@ int main() {

UNIT_MODULE(alloc_pool);
UNIT_MODULE(hashing);
UNIT_MODULE(memory);
UNIT_MODULE(time);
UNIT_MODULE(stream);
UNIT_MODULE(print);
Expand Down
2 changes: 1 addition & 1 deletion code/tests/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ UNIT_DEF int32_t unit_main(const char *name, unit_case *cases, int32_t count) {
// Locally persisting buffer
static char* unit__bprintf(const char* fmt, ...)
{
static char buf[128];
static char buf[1024] = {0};
va_list args;
va_start(args, fmt);
vsprintf(buf, fmt, args);
Expand Down

0 comments on commit ece7637

Please sign in to comment.