-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCompiledValue.h
233 lines (178 loc) · 4.15 KB
/
CompiledValue.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*=====================================================================
CompiledValue.h
---------------
Copyright Glare Technologies Limited 2018 -
=====================================================================*/
#pragma once
#include "Value.h"
#include <string>
#include <vector>
namespace Winter
{
// In-memory string representation for a Winter VArray
class VArrayRep
{
public:
uint64 refcount;
uint64 len;
uint64 flags;
// Data follows..
};
// In-memory string representation for a Winter string
class StringRep
{
public:
uint64 refcount;
uint64 len;
uint64 flags;
// Data follows..
uint64 getRefCount() const { return refcount; }
const std::string toStdString() { return std::string(((const char*)this) + sizeof(StringRep), ((const char*)this) + sizeof(StringRep) + len); }
};
VArrayRep* allocateVArray(uint64 elem_size_B, uint64 num_elems);
int free(VArrayRep* varray_rep);
ValueRef allocateVArrayInterpreted(const std::vector<ValueRef>& args);
StringRep* allocateStringWithLen(size_t len);
StringRep* allocateString(const char* initial_string_val);
int free(StringRep* str);
ValueRef allocateStringInterpreted(const std::vector<ValueRef>& args);
void resetMemUsageStats();
uint64 getMaxHeapMemUsage();
uint64 getCurrentHeapMemUsage();
/*=====================================================================
CompiledValRef
--------------
=====================================================================*/
template <class T>
class CompiledValRef
{
public:
CompiledValRef()
: ob(0)
{}
CompiledValRef(T* ob_)
{
ob = ob_;
if(ob)
ob->refcount++;
}
template<class T2>
CompiledValRef(const CompiledValRef<T2>& other)
{
ob = other.getPointer();
if(ob)
ob->refcount++;
}
CompiledValRef(const CompiledValRef<T>& other)
{
ob = other.ob;
if(ob)
ob->refcount++;
}
~CompiledValRef()
{
if(ob)
{
int64 new_ref_count = --ob->refcount;
if(new_ref_count == 0)
free(ob);
}
}
CompiledValRef& operator = (const CompiledValRef& other)
{
T* old_ob = ob;
ob = other.ob;
// NOTE: if a reference is getting assigned to itself, old_ob == ob. So make sure we increment before we decrement, to avoid deletion.
if(ob)
ob->refcount++;
// Decrement reference count for the object that this reference used to refer to.
if(old_ob)
{
int64 ref_count = --ob->refcount;
if(ref_count == 0)
free(old_ob);
}
return *this;
}
// An assignment operator from a raw pointer.
// We have this method so that the code
//
// Reference<T> ref;
// ref = new T();
//
// will work without a temporary reference object being created and then assigned to ref.
CompiledValRef& operator = (T* new_ob)
{
T* old_ob = ob;
ob = new_ob;
// NOTE: if a reference is getting assigned to itself, old_ob == ob. So make sure we increment before we decrement, to avoid deletion.
if(ob)
ob->refcount++;
// Decrement reference count for the object that this reference used to refer to.
if(old_ob)
{
int64 ref_count = --ob->refcount;
if(ref_count == 0)
free(old_ob);
}
return *this;
}
// Compares the pointer values.
bool operator < (const CompiledValRef& other) const
{
return ob < other.ob;
}
// Compares the pointer values.
bool operator == (const CompiledValRef& other) const
{
return ob == other.ob;
}
inline T& operator * ()
{
assert(ob);
return *ob;
}
inline const T& operator * () const
{
assert(ob);
return *ob;
}
inline T* operator -> () const
{
return ob;
}
inline T* getPointer() const
{
return ob;
}
// Alias for getPointer()
inline T* ptr() const
{
return ob;
}
inline bool isNull() const
{
return ob == 0;
}
inline bool nonNull() const
{
return ob != 0;
}
inline T* getPointer()
{
return ob;
}
// Alias for getPointer()
inline T* ptr()
{
return ob;
}
template <class T2>
inline bool isType() const
{
return dynamic_cast<const T2*>(ob) != 0;
}
private:
T* ob;
};
} // end namespace Winter