Skip to content

Commit 5c7a70f

Browse files
committed
chore: cleanup heap mess before close clox
1 parent c9b048a commit 5c7a70f

7 files changed

Lines changed: 42 additions & 5 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CFLAGS = -std=c99 -Wall -Wextra -g
33
CTEST_FLAGS = -std=c99 -g
44
LDFLAGS = -lcriterion
55

6-
SOURCES = src/scanner.c src/chunk.c src/compiler.c src/debug.c src/memory.c src/value.c src/vm.c
6+
SOURCES = src/scanner.c src/chunk.c src/compiler.c src/debug.c src/memory.c src/value.c src/vm.c src/object.c
77
TEST_SOURCES = tests/scanner_test.c tests/compiler_test.c
88

99
all: clox

src/memory.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <stdlib.h>
22

33
#include "memory.h"
4+
#include "object.h"
5+
#include "value.h"
6+
#include "vm.h"
47

58
void* reallocate(void* pointer, size_t oldSize, size_t newSize)
69
{
@@ -15,3 +18,27 @@ void* reallocate(void* pointer, size_t oldSize, size_t newSize)
1518
exit(1);
1619
return result;
1720
}
21+
22+
static void free_object(Obj* object)
23+
{
24+
switch (object->type)
25+
{
26+
case OBJ_STRING:
27+
{
28+
ObjString* string = (ObjString*)object;
29+
FREE_ARRAY(char, string->chars, string->length + 1);
30+
FREE(ObjString, object);
31+
break;
32+
}
33+
}
34+
}
35+
void free_objects()
36+
{
37+
Obj* object = vm.objects;
38+
while (object != NULL)
39+
{
40+
Obj* next = object->next;
41+
free_object(object);
42+
object = next;
43+
}
44+
}

src/memory.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#define GROW_CAPACITY(capacity) ((capacity) < 8 ? 8 : (capacity) * 2)
99

10+
#define FREE(type, pointer) reallocate(pointer, sizeof(type), 0)
11+
1012
#define GROW_ARRAY(type, pointer, oldCount, newCount) \
1113
(type*)reallocate(pointer, sizeof(type) * (oldCount), \
1214
sizeof(type) * (newCount))
@@ -21,5 +23,6 @@
2123
// size = smaller than old size => shrink existing allocation old size =
2224
// non-zero, new size = larger than old size => grow existing allocation
2325
void* reallocate(void* pointer, size_t oldSize, size_t newSize);
26+
void free_objects();
2427

2528
#endif

src/object.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ static Obj* allocate_object(size_t size, ObjType type)
1313
{
1414
Obj* object = (Obj*)reallocate(NULL, 0, size);
1515
object->type = type;
16+
object->next = vm.objects;
17+
vm.objects = object;
1618

1719
return object;
1820
}
1921

20-
static ObjString* allocate_string(char* chars, int length)
22+
static ObjString* allocate_string(const char* chars, int length)
2123
{
2224
ObjString* string = ALLOCATE_OBJ(ObjString, OBJ_STRING);
2325
string->length = length;
2426
string->chars = chars;
25-
2627
return string;
28+
2729
}
2830

2931
ObjString* take_string(const char* chars, int length)

src/object.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ typedef enum
1616

1717
struct Obj
1818
{
19-
ObjType type;
19+
ObjType type;
20+
struct Obj* next;
2021
};
2122

2223
struct ObjString

src/vm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ static void runtime_error(const char* format, ...)
3636
void init_VM()
3737
{
3838
reset_stack();
39+
vm.objects = NULL;
3940
}
4041

4142
void free_VM()
4243
{
43-
printf("close vm \n");
44+
free_objects();
4445
}
4546

4647
void push(Value value)

src/vm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ typedef struct
1212
uint8_t* ip; // aka of Instrction Pointer
1313
Value stack[STACK_MAX];
1414
Value* stack_top;
15+
Obj* objects;
1516
} VM;
1617

1718
typedef enum
@@ -21,6 +22,8 @@ typedef enum
2122
INTERPRET_RUNTIME_ERROR,
2223
} InterpretResult;
2324

25+
extern VM vm;
26+
2427
void init_VM();
2528
void free_VM();
2629
InterpretResult interpret(char* source);

0 commit comments

Comments
 (0)