fix: track live memory via bytesAllocated instead of subtracting freed sizes

The old code subtracted oldSize from totalAllocated during deallocation,
which required knowing the object size at free time. This was unsafe
because it could read a freed object to determine its size. The new
approach tracks bytesAllocated by only adding new allocations and
relying on GC marking to account for freed memory, making it both
correct and faster with less code.
This commit is contained in:
Bob Nystrom
2013-12-14 22:17:16 +00:00
parent 78f6e3b96d
commit 07c3dff92f
4 changed files with 124 additions and 124 deletions
+9 -8
View File
@@ -5,21 +5,22 @@
typedef struct WrenVM WrenVM;
// A generic allocation that handles all explicit memory management used by
// Wren. It's used like so:
// A generic allocation function that handles all explicit memory management
// used by Wren. It's used like so:
//
// - To allocate new memory, [memory] is NULL and [oldSize] is zero.
// - To allocate new memory, [memory] is NULL and [oldSize] is zero. It should
// return the allocated memory or NULL on failure.
//
// - To attempt to grow an existing allocation, [memory] is the memory,
// [oldSize] is its previous size, and [newSize] is the desired size.
// It returns [memory] if it was able to grow it in place, or a new pointer
// if it had to move it.
// It should return [memory] if it was able to grow it in place, or a new
// pointer if it had to move it.
//
// - To shrink memory, [memory], [oldSize], and [newSize] are the same as above
// but it will always return [memory]. If [newSize] is zero, the memory will
// be freed and `NULL` will be returned.
// but it will always return [memory].
//
// - To free memory, [newSize] will be zero.
// - To free memory, [memory] will be the memory to free and [newSize] and
// [oldSize] will be zero. It should return NULL.
typedef void* (*WrenReallocateFn)(void* memory, size_t oldSize, size_t newSize);
// Creates a new Wren virtual machine. It allocates memory for the VM itself