Track memory during deallocation correctly.

It used to subtract the number of bytes during deallocation, but
that required knowing the size of an object at free time. That
isn't always available. The old code could read a freed object
while doing this. Bad!

Instead, this tracks how much memory is still being used by
marked objects. It's correct, and is also a bit less code and
faster.
This commit is contained in:
Bob Nystrom
2013-12-14 14:17:16 -08:00
parent 3fd7fa56fb
commit 5c807c0ec5
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