aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/memory.c
diff options
context:
space:
mode:
Diffstat (limited to 'clox/src/memory.c')
-rw-r--r--clox/src/memory.c28
1 files changed, 25 insertions, 3 deletions
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 <stdlib.h>
#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;
+ }
+}