aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/memory.c
diff options
context:
space:
mode:
authorGravatar Tom Willemse2021-09-09 22:57:03 -0700
committerGravatar Tom Willemse2021-09-09 22:57:03 -0700
commit8ec913756107cc8e6aee965d0dc080039d4995ad (patch)
tree818fa7ca8314eaed7745d6fe4a7cc9f0c1b4ca6d /clox/src/memory.c
parentd5f352e577acf1f472150df5578ff6d693258ae3 (diff)
downloadcrafting-interpreters-8ec913756107cc8e6aee965d0dc080039d4995ad.tar.gz
crafting-interpreters-8ec913756107cc8e6aee965d0dc080039d4995ad.zip
Chapter 19.5
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;
+ }
+}