-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathheap.h
98 lines (76 loc) · 1.9 KB
/
heap.h
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef VAIVEN_VISITOR_HEADER_HEAP
#define VAIVEN_VISITOR_HEADER_HEAP
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <vector>
#include "scope.h"
#include "value.h"
#include "util.h"
using std::unordered_map;
using std::unordered_set;
using std::string;
using std::vector;
namespace vaiven {
// make these bits so they can be stored in shapes bitmaps
enum GcableType {
GCABLE_TYPE_LIST = 1,
GCABLE_TYPE_STRING = 2,
GCABLE_TYPE_OBJECT = 4,
Internal_ForceMyEnumIntSize = 1 << 30
};
const int GcableMarkBit = 8;
const int HEAP_FACTOR = 2;
const int MIN_HEAP_SIZE = 65536;
struct Gcable {
public:
Gcable(GcableType type) : type(type) {};
GcableType getType();
bool isMarked();
bool mark();
void unmark();
private:
GcableType type;
};
struct GcableList : public Gcable {
public:
GcableList() : Gcable(GCABLE_TYPE_LIST) {};
vector<Value> list;
};
struct GcableString : public Gcable {
public:
GcableString() : Gcable(GCABLE_TYPE_STRING) {};
GcableString(string str) : Gcable(GCABLE_TYPE_STRING), str(str) {};
string str;
};
struct GcableObject : public Gcable {
public:
GcableObject() : Gcable(GCABLE_TYPE_OBJECT) {};
unordered_map<string, Value> properties;
};
class Heap {
public:
Heap(stack_with_container<Value>& interpreterStack, Scope<Value>& globalScope)
: size(MIN_HEAP_SIZE), heap_min(0xFFFFFFFFFFFFFFFF), heap_max(0),
interpreterStack(interpreterStack), globalScope(globalScope) {};
int size;
uint64_t heap_min;
uint64_t heap_max;
// for roots
Scope<Value>& globalScope;
stack_with_container<Value>& interpreterStack;
GcableList* newList();
GcableString* newString();
GcableObject* newObject();
void free(Gcable* ptr);
bool isFull();
bool owns(void* ptr);
void mark(Gcable* ptr);
void sweep();
void gc();
// TODO this is TERRIBLE
unordered_set<Gcable*> owned_ptrs;
};
extern Heap* globalHeap;
}
#endif