-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.c
71 lines (65 loc) · 2.18 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdlib.h>
#include "memory.h"
#include "vm.h"
/*
This function is the single function we will use for all dynamic memory menagement in clogc
-- allocating memory, freeing it, and changing the size if current allocations
parameters:
oldSize newSize Operation
0 Non‑zero Allocate new block.
Non‑zero 0 Free allocation.
Non‑zero Smaller than oldSize Shrink existing allocation.
Non‑zero Larger than oldSize Grow existing allocation.
*/
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);
return result;
}
static void freeObject(Obj* object) {
switch (object->type) {
case OBJ_CLOSURE: {
/*
We free only the ObjClosure itself, not the ObjFunction. That’s because the closure doesn’t own the function.
*/
ObjClosure* closure = (ObjClosure*)object;
FREE_ARRAY(ObjUpvalue*, closure->upvalues, closure->upvalueCount);
FREE(ObjClosure, object);
break;
}
case OBJ_FUNCTION: {
/*
This switch case is responsible for freeing the ObjFunction itself as well as any other memory it owns.
Functions own their chunk, so we call Chunk’s destructor-like function.
*/
ObjFunction* function = (ObjFunction*)object;
freeChunk(&function->chunk);
FREE(OBJ_FUNCTION, object);
break;
}
case OBJ_NATIVE:
FREE(ObjNative, object);
break;
case OBJ_STRING: {
ObjString* string = (ObjString*)object;
FREE_ARRAY(char, string->chars, string->length + 1);
FREE(ObjString, object);
break;
}
case OBJ_UPVALUE:
FREE(ObjUpvalue, object);
break;
}
}
void freeObjects() {
Obj* object = vm.objects;
while (object != NULL) {
Obj* next = object->next;
freeObject(object);
object = next;
}
}