-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.c
47 lines (40 loc) · 1.01 KB
/
memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// Created by Fabian Simon on 29.09.23.
//
#include <stdlib.h>
#include "memory.h"
#include "object.h"
#include "vm.h"
void* reallocate(void* pointer, size_t old_size, size_t new_size) {
if (new_size == 0) {
free(pointer);
return NULL;
}
void* result = realloc(pointer, new_size);
if (result == NULL) exit(1);
return result;
}
static void free_object(Obj* object) {
switch (object->type) {
case OBJ_FUNCTION: {
ObjFunction* function = (ObjFunction*) object;
free_chunk(&function->chunk);
FREE(ObjFunction, object);
break;
}
case OBJ_STRING: {
ObjString* string = (ObjString*) object;
FREE_ARRAY(char, string->chars, string->length+1);
FREE(ObjString, object);
break;
}
}
}
void free_objects() {
Obj* object = vm.objects;
while (object != NULL) {
Obj* next = object->next;
free_object(object);
object = next;
}
}