From 4a27384ddf229528c609934fae585822106de528 Mon Sep 17 00:00:00 2001 From: rfmineguy Date: Mon, 3 Jun 2024 12:44:31 -0700 Subject: [PATCH 1/3] Suggesstion for counting how much memory was used in the arena allocator --- arena.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arena.h b/arena.h index 410bae6..d0c5524 100644 --- a/arena.h +++ b/arena.h @@ -75,6 +75,7 @@ void *arena_memdup(Arena *a, void *data, size_t size); char *arena_sprintf(Arena *a, const char *format, ...); #endif // ARENA_NOSTDIO +size_t arena_bytes(Arena *a); void arena_reset(Arena *a); void arena_free(Arena *a); @@ -278,6 +279,14 @@ char *arena_sprintf(Arena *a, const char *format, ...) } #endif // ARENA_NOSTDIO +size_t arena_bytes(Arena *a) { + size_t bytes_total = 0; + for (Region* r = a->begin; r != NULL; r = r->next) { + bytes_total += r->count * 8; + } + return bytes_total; +} + void arena_reset(Arena *a) { for (Region *r = a->begin; r != NULL; r = r->next) { From 6ed3a0c91c888028c942389031cd9d76afd75722 Mon Sep 17 00:00:00 2001 From: rfmineguy Date: Mon, 3 Jun 2024 12:53:13 -0700 Subject: [PATCH 2/3] More accurate counting using sizeof(uintptr_t) rather than hardcoded 8. --- arena.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arena.h b/arena.h index d0c5524..ef3956e 100644 --- a/arena.h +++ b/arena.h @@ -103,6 +103,7 @@ void arena_free(Arena *a); #endif // ARENA_H_ +#define ARENA_IMPLEMENTATION #ifdef ARENA_IMPLEMENTATION #if ARENA_BACKEND == ARENA_BACKEND_LIBC_MALLOC @@ -282,7 +283,7 @@ char *arena_sprintf(Arena *a, const char *format, ...) size_t arena_bytes(Arena *a) { size_t bytes_total = 0; for (Region* r = a->begin; r != NULL; r = r->next) { - bytes_total += r->count * 8; + bytes_total += r->count * sizeof(uintptr_t); } return bytes_total; } From 9d7b86c2a30c2e0bff20a03c331b99aab6712507 Mon Sep 17 00:00:00 2001 From: rfmineguy Date: Mon, 3 Jun 2024 12:54:05 -0700 Subject: [PATCH 3/3] Removed test #define ARENA_IMPLEMENTATION to get ready for pull request --- arena.h | 1 - 1 file changed, 1 deletion(-) diff --git a/arena.h b/arena.h index ef3956e..82f35ad 100644 --- a/arena.h +++ b/arena.h @@ -103,7 +103,6 @@ void arena_free(Arena *a); #endif // ARENA_H_ -#define ARENA_IMPLEMENTATION #ifdef ARENA_IMPLEMENTATION #if ARENA_BACKEND == ARENA_BACKEND_LIBC_MALLOC