Skip to content

Commit

Permalink
Fix: also count after realloc
Browse files Browse the repository at this point in the history
  • Loading branch information
tcdude committed Mar 24, 2023
1 parent 4a6fdfe commit 7c15086
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Sources/krink/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ static void kr_alloctrack_malloc(void *ptr, size_t size) {
kr_allocs[i].size = size;
++kr_pool_num_allocs;
kr_pool_allocated += size;
kr_pool_allocated_max = (kr_pool_allocated > kr_pool_allocated_max) ? kr_pool_allocated : kr_pool_allocated_max;
kr_pool_allocated_max = (kr_pool_allocated > kr_pool_allocated_max)
? kr_pool_allocated
: kr_pool_allocated_max;
return;
}
}
Expand All @@ -64,12 +66,13 @@ static void kr_alloctrack_malloc(void *ptr, size_t size) {
}
kr_allocs[kr_allocs_count].ptr = ptr;
kr_allocs[kr_allocs_count].size = size;
++kr_pool_num_allocs;
kr_allocs_count *= 2;
}

static void kr_alloctrack_free(void *ptr) {
if (ptr == NULL) return;

for (int i = 0; i < kr_allocs_count; ++i) {
if (kr_allocs[i].ptr == ptr) {
kr_allocs[i].ptr = NULL;
Expand All @@ -87,7 +90,9 @@ static void kr_alloctrack_realloc(void *old, void *ptr, size_t size) {
if (kr_allocs[i].ptr == old) {
kr_allocs[i].ptr = ptr;
kr_pool_allocated += size - kr_allocs[i].size;
kr_pool_allocated_max = (kr_pool_allocated > kr_pool_allocated_max) ? kr_pool_allocated : kr_pool_allocated_max;
kr_pool_allocated_max = (kr_pool_allocated > kr_pool_allocated_max)
? kr_pool_allocated
: kr_pool_allocated_max;
kr_allocs[i].size = size;
return;
}
Expand Down

0 comments on commit 7c15086

Please sign in to comment.