Skip to content

Commit 43f1c80

Browse files
committed
m_realloc: Account only allocation size difference in total_bytes_allocated.
1 parent 4b57fac commit 43f1c80

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

py/malloc.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
4141
printf("could not allocate memory, reallocating %d bytes\n", new_num_bytes);
4242
return NULL;
4343
}
44-
total_bytes_allocated += new_num_bytes;
44+
// At first thought, "Total bytes allocated" should only grow,
45+
// after all, it's *total*. But consider for example 2K block
46+
// shrunk to 1K and then grown to 2K again. It's still 2K
47+
// allocated total. If we process only positive increments,
48+
// we'll count 3K.
49+
total_bytes_allocated += new_num_bytes - old_num_bytes;
4550
return ptr;
4651
}
4752

0 commit comments

Comments
 (0)