From 8ec913756107cc8e6aee965d0dc080039d4995ad Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Thu, 9 Sep 2021 22:57:03 -0700 Subject: Chapter 19.5 --- clox/src/memory.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'clox/src/memory.c') diff --git a/clox/src/memory.c b/clox/src/memory.c index ead60c8..169d846 100644 --- a/clox/src/memory.c +++ b/clox/src/memory.c @@ -1,14 +1,36 @@ #include #include "memory.h" +#include "vm.h" -void* reallocate(void* pointer, size_t oldSize, size_t newSize) { +void *reallocate(void *pointer, size_t oldSize, size_t newSize) { if (newSize == 0) { free(pointer); return NULL; } - void* result = realloc(pointer, newSize); - if (result == NULL) exit(1); + void *result = realloc(pointer, newSize); + if (result == NULL) + exit(1); return result; } + +static void freeObject(Obj *object) { + switch (object->type) { + case OBJ_STRING: { + ObjString *string = (ObjString *)object; + FREE_ARRAY(char, string->chars, string->length + 1); + FREE(ObjString, object); + break; + } + } +} + +void freeObjects() { + Obj *object = vm.objects; + while (object != NULL) { + Obj *next = object->next; + freeObject(object); + object = next; + } +} -- cgit v1.2.3-54-g00ecf