Skip to content

Commit b6ea672

Browse files
authored
...
1 parent ec277b2 commit b6ea672

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

vm.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
#include "chunk.h"
77

88
#define STACK_MAX 256
9+
#define MAX_CONSTANTS 65536
10+
11+
typedef struct Object Object;
912

1013
typedef struct {
1114
Chunk* chunk;
1215
uint8_t* ip;
1316
Value stack[STACK_MAX];
1417
Value* stack_top;
18+
Object* objects;
19+
size_t bytes_allocated;
20+
size_t next_gc;
1521
} VM;
1622

1723
typedef enum {
@@ -20,6 +26,21 @@ typedef enum {
2026
INTERPRET_RUNTIME_ERROR
2127
} InterpretResult;
2228

29+
#define GROW_CAPACITY(capacity) \
30+
((capacity) < 8 ? 8 : (capacity) * 2)
31+
32+
#define GROW_ARRAY(type, pointer, oldCount, newCount) \
33+
(type*)reallocate(pointer, sizeof(type) * (oldCount), \
34+
sizeof(type) * (newCount))
35+
36+
#define FREE_ARRAY(type, pointer, oldCount) \
37+
reallocate(pointer, sizeof(type) * (oldCount), 0)
38+
39+
#define ALLOCATE(type, count) \
40+
(type*)reallocate(NULL, 0, sizeof(type) * (count))
41+
42+
#define FREE(type, pointer) reallocate(pointer, sizeof(type), 0)
43+
2344
void init_vm(VM* vm);
2445
void free_vm(VM* vm);
2546
InterpretResult interpret(VM* vm, const char* source);
@@ -29,4 +50,12 @@ Value pop(VM* vm);
2950
bool is_falsey(Value value);
3051
bool values_equal(Value a, Value b);
3152

53+
void* reallocate(void* pointer, size_t oldSize, size_t newSize);
54+
void collect_garbage(VM* vm);
55+
void mark_object(VM* vm, Object* object);
56+
void mark_value(VM* vm, Value value);
57+
58+
#define IS_OBJ(value) ((value).type == VAL_OBJ)
59+
#define AS_OBJ(value) ((value).as.obj)
60+
3261
#endif

0 commit comments

Comments
 (0)