-
Notifications
You must be signed in to change notification settings - Fork 4
/
umemory.pas
328 lines (285 loc) · 6.75 KB
/
umemory.pas
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
unit uMemory;
{$mode delphi}{$H+}
interface
uses
SysUtils, uValue, uCommon;
function Grow_Capacity(OldCapacity: LongInt): LongInt;
function Reallocate(Previous: Pointer; OldSize, NewSize: size_t): Pointer;
procedure MarkObject(Obj: Pointer);
procedure MarkValue(Value: TValue);
procedure CollectGarbage;
procedure FreeObjects;
// not for pointer types, use reallocate in that case directly
// function Grow_Array<T>(Previous: Pointer; const OldSize, NewSize: size_t): Pointer;
// procedure Free_Array<T>(Ptr: Pointer; OldCount: LongInt);
implementation
uses uVM, uChunk, uObject, uCompiler, uTable;
const
GC_HEAP_GROW_FACTOR = 2;
function Grow_Capacity(OldCapacity: LongInt): LongInt;
begin
if OldCapacity < 8 then
Result := 8
else
Result := OldCapacity * 2;
end;
// function Grow_Array<T>(Previous: Pointer; const OldSize, NewSize: size_t): Pointer;
//begin
// Result := Reallocate(Previous, SizeOf(T)*OldSize, SizeOf(T)*NewSize);
//end;
function Reallocate(Previous: Pointer; OldSize, NewSize: size_t): Pointer;
begin
Inc(VM.BytesAllocated, NewSize - OldSize);
if NewSize > OldSize then
begin
if DebugStressGC then
CollectGarbage;
if VM.BytesAllocated > VM.NextGC then
CollectGarbage;
end;
if NewSize = 0 then
begin
FreeMem(Previous);
Exit(Nil);
end;
Result := ReAllocMem(Previous, NewSize); // FPC heapmanager auto keeps track of old size.
end;
procedure Free<T>(ptr: Pointer);
begin
Reallocate(ptr, SizeOf(T), 0);
end;
procedure FreeObject(Obj: PObj);
var
ObjString: PObjString;
Func: PObjFunc;
Closure: PObjClosure;
Instance: PObjInstance;
Klass: PObjClass;
begin
if DebugLogGC then
WriteLnFmt('%p free type %s', [Pointer(Obj), Obj^.Typ.AsString]);
case Obj^.Typ of
obj_Bound_Method: Free<TObjBoundMethod>(Obj);
obj_Class:
begin
Klass := PObjClass(Obj);
FreeTable(@Klass^.Methods);
Free<TObjClass>(Obj);
end;
obj_Closure:
begin
Closure := PObjClosure(Obj);
Reallocate(Closure^.UpValues, SizeOf(PObjUpValue)*Closure^.UpValueCount, 0);
Free<TObjClosure>(Obj);
end;
obj_Func:
begin
Func := PObjFunc(Obj);
FreeChunk(@Func^.Chunk);
Free<TObjFunc>(Obj);
end;
obj_Instance:
begin
Instance := PObjInstance(Obj);
FreeTable(@Instance^.Fields);
Free<TObjInstance>(Obj);
end;
obj_Native: Free<TObjNative>(Obj);
obj_String:
begin
ObjString := PObjString(Obj);
StrDispose(ObjString^.Chars);
Free<TObjString>(Obj);
end;
obj_UpValue:
begin
Free<TObjUpValue>(Obj);
end;
end;
end;
procedure MarkObject(Obj: Pointer);
var
TheObj: PObj;
begin
if Obj = Nil then Exit;
TheObj := PObj(Obj);
if TheObj^.isMarked then Exit;
if DebugLogGC then
begin
WriteFmt('%p mark ', [Pointer(TheObj)]);
PrintValue(ObjVal(TheObj));
WriteLn;
end;
TheObj^.isMarked := True;
if VM.GrayCapacity < VM.GrayCount + 1 then
begin
VM.GrayCapacity := Grow_Capacity(VM.GrayCapacity);
VM.GrayStack := PPObj(ReAllocMem(VM.GrayStack, SizeOf(PObj)*VM.GrayCapacity));
end;
VM.GrayStack[VM.GrayCount] := TheObj;
Inc(VM.GrayCount);
end;
procedure MarkValue(Value: TValue);
begin
if not isObj(Value) then Exit;
MarkObject(asObj(Value));
end;
procedure MarkArray(var ValueArray: TValueArray);
var
i: Integer;
begin
for i := 0 to ValueArray.Count-1 do
MarkValue(ValueArray.Values[i]);
end;
procedure BlackenObject(Obj: PObj);
var
Func: PObjFunc;
Closure: PObjClosure;
i: Integer;
Klass: PObjClass;
Instance: PObjInstance;
Bound: PObjBoundMethod;
begin
if DebugLogGC then
begin
WriteFmt('%p blacken ', [Obj]);
PrintValue(ObjVal(Obj));
WriteLn;
end;
case Obj^.Typ of
obj_Bound_Method:
begin
Bound := PObjBoundMethod(Obj);
MarkValue(Bound^.Receiver);
MarkObject(PObj(Bound^.Method));
end;
obj_Class:
begin
Klass := PObjClass(Obj);
MarkObject(PObj(Klass^.Name));
MarkTable(@Klass^.Methods);
end;
obj_Closure:
begin
Closure := PObjClosure(Obj);
MarkObject(PObj(Closure^.Func));
for i := 0 to Closure^.UpValueCount-1 do
MarkObject(PObj(Closure^.UpValues[i]));
end;
obj_Func:
begin
Func := PObjFunc(Obj);
MarkObject(PObj(Func^.Name));
MarkArray(Func^.Chunk.Constants);
end;
obj_Instance:
begin
Instance := PObjInstance(Obj);
MarkObject(PObj(Instance^.Klass));
MarkTable(@Instance^.Fields);
end;
obj_UpValue: MarkValue(PObjUpValue(Obj)^.Closed);
obj_Native,
obj_String:;
end;
end;
procedure MarkRoots;
var
Slot: PValue;
i: Integer;
UpValue: PObjUpValue;
begin
Slot := @VM.Stack[0];
while Slot < VM.StackTop do
begin
MarkValue(Slot^);
Inc(Slot);
end;
for i := 0 to VM.FrameCount-1 do
MarkObject(PObj(VM.Frames[i].Closure));
UpValue := VM.OpenUpValues;
while UpValue <> Nil do
begin
MarkObject(PObj(UpValue));
UpValue := UpValue^.Next;
end;
MarkTable(@VM.Globals);
MarkCompilerRoots;
MarkObject(PObj(VM.InitString));
end;
procedure TraceReferences;
var
Obj: PObj;
begin
while VM.GrayCount > 0 do
begin
Dec(VM.GrayCount);
Obj := VM.GrayStack[VM.GrayCount];
BlackenObject(Obj);
end;
end;
procedure Sweep;
var
Obj, Unreached: PObj;
Previous: PObj = Nil;
begin
Obj := VM.Objects;
while Obj <> Nil do
begin
if Obj^.isMarked then
begin
Obj^.isMarked := False;
Previous := Obj;
Obj := Obj^.Next;
end
else
begin
Unreached := Obj;
Obj := Obj^.Next;
if Previous <> Nil then
Previous^.Next := Obj
else
VM.Objects := Obj;
FreeObject(Unreached);
end;
end;
end;
procedure CollectGarbage;
var
Before: size_t;
begin
if DebugLogGC then
begin
WriteLn('-- gc begin');
Before := VM.BytesAllocated;
end;
MarkRoots;
TraceReferences;
TableRemoveWhite(@VM.Strings);
Sweep;
VM.NextGC := VM.BytesAllocated * GC_HEAP_GROW_FACTOR;
if DebugLogGC then
begin
WriteLn('-- gc end');
WriteLnFmt(' collected %ld bytes (from %ld to %ld) next at %ld',
[Before - VM.BytesAllocated, Before, VM.BytesAllocated, VM.NextGC]);
end;
end;
procedure FreeObjects;
var
Obj, Next: PObj;
begin
Obj := VM.Objects;
while Obj <> Nil do
begin
Next := Obj^.Next;
FreeObject(Obj);
Obj := Next;
end;
FreeMem(VM.GrayStack);
end;
// procedure Free_Array<T>(Ptr: Pointer; OldCount: LongInt);
//begin
// Reallocate(Ptr, SizeOf(T)*OldCount, 0);
//end;
end.