diff --git a/src/wren_vm.c b/src/wren_vm.c index 0fe7d763..0ec71eeb 100644 --- a/src/wren_vm.c +++ b/src/wren_vm.c @@ -10,13 +10,11 @@ WrenVM* wrenNewVM(WrenReallocateFn reallocateFn) { - // TODO(bob): Get rid of explicit malloc() here. WrenVM* vm = reallocateFn(NULL, 0, sizeof(WrenVM)); initSymbolTable(&vm->methods); initSymbolTable(&vm->globalSymbols); - // TODO(bob): Get rid of explicit malloc() here. - vm->fiber = malloc(sizeof(Fiber)); + vm->fiber = reallocateFn(NULL, 0, sizeof(Fiber)); vm->fiber->stackSize = 0; vm->fiber->numFrames = 0; vm->totalAllocated = 0; @@ -34,6 +32,8 @@ WrenVM* wrenNewVM(WrenReallocateFn reallocateFn) vm->globals[i] = NULL_VAL; } + vm->reallocate = reallocateFn; + wrenInitializeCore(vm); return vm; @@ -99,15 +99,8 @@ void* wrenReallocate(WrenVM* vm, void* memory, size_t oldSize, size_t newSize) } #endif - if (newSize == 0) - { - ASSERT(memory != NULL, "Must have pointer to free."); - free(memory); - return NULL; - } - - // TODO(bob): Let external code provide allocator. - return realloc(memory, newSize); + ASSERT(newSize != 0 || memory != NULL, "Must have pointer to free."); + return vm->reallocate(memory, oldSize, newSize); } static void markValue(Value value); diff --git a/src/wren_vm.h b/src/wren_vm.h index 0ef2d3e7..fc5d11bc 100644 --- a/src/wren_vm.h +++ b/src/wren_vm.h @@ -176,6 +176,9 @@ struct WrenVM // The head of the list of pinned objects. Will be `NULL` if nothing is // pinned. PinnedObj* pinned; + + // The externally-provided function used to allocate memory. + WrenReallocateFn reallocate; }; typedef struct