/
GC.h
360 lines (292 loc) · 8.84 KB
/
GC.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
The garbage collector for SuperCollider.
Based on Wilson and Johnstone's real time collector and the Baker treadmill.
*/
#ifndef _GC_
#define _GC_
#include "PyrObject.h"
#include "VMGlobals.h"
#include "AdvancingAllocPool.h"
#include "function_attributes.h"
void DumpSimpleBackTrace(VMGlobals *g);
const int kMaxPoolSet = 7;
const int kNumGCSizeClasses = 28;
const int kFinalizerSet = kNumGCSizeClasses;
const int kNumGCSets = kNumGCSizeClasses + 1;
const int kScanThreshold = 256;
class GCSet
{
public:
GCSet() {}
void Init(int inSizeClass);
bool HasFree() { return mFree != &mBlack; }
private:
friend class PyrGC;
void MajorFlip();
void MinorFlip();
PyrObjectHdr mBlack;
PyrObjectHdr mWhite;
PyrObjectHdr* mFree;
};
struct SlotRef {
SlotRef(PyrObject* inObj, int32 inIndex) : obj(inObj), slotIndex(inIndex) {}
PyrObject *obj;
int32 slotIndex;
};
class PyrGC
{
static const int kLazyCollectThreshold = 1024;
public:
PyrGC(VMGlobals *g, AllocPool *inPool, PyrClass *mainProcessClass, long poolSize);
MALLOC PyrObject* New(size_t inNumBytes, long inFlags, long inFormat, bool inCollect);
MALLOC PyrObject* NewFrame(size_t inNumBytes, long inFlags, long inFormat, bool inAccount);
MALLOC static PyrObject* NewPermanent(size_t inNumBytes,
long inFlags, long inFormat);
MALLOC PyrObject* NewFinalizer(ObjFuncPtr finalizeFunc, PyrObject *inObject, bool inCollect);
bool IsBlack(PyrObjectHdr* inObj) { return inObj->gc_color == mBlackColor; }
bool IsWhite(PyrObjectHdr* inObj) { return inObj->gc_color == mWhiteColor; }
bool IsGrey(PyrObjectHdr* inObj) { return inObj->gc_color == mGreyColor; }
static bool IsMarker(PyrObjectHdr* inObj) { return inObj->gc_color == obj_gcmarker; }
bool IsFree(PyrObjectHdr* inObj) { return (!(IsMarker(inObj) ||
inObj->IsPermanent() ||
IsBlack(inObj) ||
IsWhite(inObj) ||
IsGrey(inObj))); }
bool ObjIsBlack(PyrObjectHdr* inObj) { return IsBlack(inObj); }
bool ObjIsGrey(PyrObjectHdr* inObj) { return IsGrey(inObj); }
bool ObjIsFree(PyrObjectHdr* inObj) { return IsFree(inObj); }
// general purpose write barriers:
void GCWrite(PyrObjectHdr* inParent, PyrSlot* inSlot)
{
if (IsBlack(inParent) && IsObj(inSlot) && IsWhite(slotRawObject(inSlot))) {
ToGrey(slotRawObject(inSlot));
}
}
void GCWrite(PyrObjectHdr* inParent, PyrObjectHdr* inChild)
{
if (IsBlack(inParent) && IsWhite(inChild)) {
ToGrey(inChild);
}
}
// when you know the parent is black:
void GCWriteBlack(PyrSlot* inSlot)
{
if (IsObj(inSlot)) {
if (IsWhite(slotRawObject(inSlot))) {
ToGrey(slotRawObject(inSlot));
}
}
}
void GCWriteBlack(PyrObjectHdr* inChild)
{
if (IsWhite(inChild)) {
ToGrey(inChild);
}
}
// when you know the child is white
void GCWriteNew(PyrObjectHdr* inParent, PyrObjectHdr* inChild)
{
if (IsBlack(inParent)) {
ToGrey(inChild);
}
}
// users should not call anything below.
void Collect();
void Collect(int32 inNumToScan);
void LazyCollect()
{
if (mUncollectedAllocations > kLazyCollectThreshold)
Collect();
}
void FullCollection();
void ScanFinalizers();
void RunAllFinalizers();
GCSet* GetGCSet(PyrObjectHdr* inObj);
void CompletePartialScan(PyrObject *obj);
inline void ToGrey(PyrObjectHdr* inObj);
inline void ToGrey2(PyrObjectHdr* inObj);
void ToBlack(PyrObjectHdr* inObj);
void ToWhite(PyrObjectHdr *inObj);
void Free(PyrObjectHdr* inObj);
long StackDepth() { return mVMGlobals->sp - mStack->slots + 1; }
PyrObject* Stack() { return mStack; }
void SetStack(PyrObject* inStack) { mStack = inStack; }
bool SanityCheck();
bool SanityCheck2();
bool LinkSanity();
bool ListSanity();
bool BlackToWhiteCheck(PyrObject *objA);
bool SanityMarkObj(PyrObject *objA, PyrObject *fromObj, int level);
bool SanityClearObj(PyrObject *objA, int level);
void DumpInfo();
void DumpGrey();
void DumpSet(int set);
void BecomePermanent(PyrObject *inObject);
void BecomeImmutable(PyrObject *inObject);
bool IsPartialScanObject(PyrObject* inObject) const { return inObject == mPartialScanObj; }
int32 GetPartialScanIndex() const { return mPartialScanSlot; }
private:
inline PyrObject * Allocate(size_t inNumBytes, int32 sizeclass, bool inCollect);
static void throwMemfailed(size_t inNumBytes);
void ScanSlots(PyrSlot *inSlots, long inNumToScan);
void SweepBigObjects();
void DoPartialScan(int32 inObjSize);
bool ScanOneObj();
void Flip();
void ScanStack();
void ScanFrames();
void DLRemove(PyrObjectHdr *obj);
void DLInsertAfter(PyrObjectHdr *after, PyrObjectHdr *obj);
void DLInsertBefore(PyrObjectHdr *before, PyrObjectHdr *obj);
void ClearMarks();
void Finalize(PyrObject *obj);
void beginPause();
void endPause();
void reportPause();
VMGlobals *mVMGlobals;
AllocPool *mPool;
AdvancingAllocPool mNewPool;
GCSet mSets[kNumGCSets];
PyrProcess *mProcess; // the root is the pyrprocess which contains this struct
PyrObject *mStack;
PyrObject *mPartialScanObj;
PyrObjectHdr mGrey;
int32 mPartialScanSlot;
int32 mNumToScan;
int32 mNumGrey;
int32 mCurSet;
int32 mFlips, mCollects, mAllocTotal, mScans, mNumAllocs, mStackScans, mNumPartialScans, mSlotsScanned, mUncollectedAllocations;
unsigned char mBlackColor, mGreyColor, mWhiteColor, mFreeColor;
bool mCanSweep;
bool mRunning;
};
inline void PyrGC::DLRemove(PyrObjectHdr *obj)
{
obj->next->prev = obj->prev;
obj->prev->next = obj->next;
}
inline void PyrGC::DLInsertAfter(PyrObjectHdr *after, PyrObjectHdr *obj)
{
obj->next = after->next;
obj->prev = after;
after->next->prev = obj;
after->next = obj;
}
inline void PyrGC::DLInsertBefore(PyrObjectHdr *before, PyrObjectHdr *obj)
{
obj->prev = before->prev;
obj->next = before;
before->prev->next = obj;
before->prev = obj;
}
inline GCSet* PyrGC::GetGCSet(PyrObjectHdr* inObj)
{
return mSets + (inObj->classptr == class_finalizer ? kFinalizerSet : inObj->obj_sizeclass);
}
inline void PyrGC::ToBlack(PyrObjectHdr *obj)
{
if (IsGrey(obj)) {
mNumGrey--;
//post("ToBlack %d\n", mNumGrey);
}
DLRemove(obj);
GCSet *gcs = GetGCSet(obj);
DLInsertAfter(&gcs->mBlack, obj);
obj->gc_color = mBlackColor;
}
inline void PyrGC::ToWhite(PyrObjectHdr *obj)
{
if (IsGrey(obj)) {
mNumGrey--;
//post("ToWhite %d\n", mNumGrey);
}
DLRemove(obj);
GCSet *gcs = GetGCSet(obj);
DLInsertAfter(&gcs->mWhite, obj);
obj->gc_color = mWhiteColor;
}
inline void PyrGC::Free(PyrObjectHdr* obj)
{
if (IsGrey(obj)) {
mNumGrey--;
//post("ToWhite %d\n", mNumGrey);
}
DLRemove(obj);
GCSet *gcs = GetGCSet(obj);
DLInsertBefore(gcs->mFree, obj);
gcs->mFree = obj;
obj->gc_color = mFreeColor;
obj->size = 0;
}
inline void PyrGC::ToGrey(PyrObjectHdr* obj)
{
/* move obj from white to grey */
/* link around object */
DLRemove(obj);
/* link in new place */
DLInsertAfter(&mGrey, obj);
/* set grey list pointer to obj */
obj->gc_color = mGreyColor;
mNumGrey ++ ;
mNumToScan += 1L << obj->obj_sizeclass;
}
inline void PyrGC::ToGrey2(PyrObjectHdr* obj)
{
/* move obj from white to grey */
/* link around object */
DLRemove(obj);
/* link in new place */
DLInsertAfter(&mGrey, obj);
/* set grey list pointer to obj */
obj->gc_color = mGreyColor;
mNumGrey ++ ;
}
inline PyrObject * PyrGC::Allocate(size_t inNumBytes, int32 sizeclass, bool inCollect)
{
if (inCollect && mNumToScan >= kScanThreshold)
Collect();
else {
if (inCollect)
mUncollectedAllocations = 0;
else
++mUncollectedAllocations;
}
GCSet *gcs = mSets + sizeclass;
PyrObject * obj = (PyrObject*)gcs->mFree;
if (!IsMarker(obj)) {
// from free list
gcs->mFree = obj->next;
assert(obj->obj_sizeclass == sizeclass);
} else {
if (sizeclass > kMaxPoolSet) {
SweepBigObjects();
size_t allocSize = sizeof(PyrObjectHdr) + (sizeof(PyrSlot) << sizeclass);
obj = (PyrObject*)mPool->Alloc(allocSize);
} else {
size_t allocSize = sizeof(PyrObjectHdr) + (sizeof(PyrSlot) << sizeclass);
obj = (PyrObject*)mNewPool.Alloc(allocSize);
}
if (!obj)
throwMemfailed(inNumBytes);
DLInsertAfter(&gcs->mWhite, obj);
obj->obj_sizeclass = sizeclass;
}
return obj;
}
#endif