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
|
/*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
void DumpSimpleBackTrace(VMGlobals *g);
const int kMaxPoolSet = 7;
const int kNumGCSizeClasses = 28;
const int kFinalizerSet = kNumGCSizeClasses;
const int kNumGCSets = kNumGCSizeClasses + 1;
class GCSet
{
public:
GCSet() {}
void Init(int inSizeClass);
bool HasFree() { return mFree != &mBlack; }
private:
friend class PyrGC;
void MoveWhiteToFree();
PyrObjectHdr mBlack;
PyrObjectHdr mWhite;
PyrObjectHdr* mFree;
};
struct SlotRef {
SlotRef(PyrObject* inObj, int32 inIndex) : obj(inObj), slotIndex(inIndex) {}
PyrObject *obj;
int32 slotIndex;
};
class PyrGC
{
public:
PyrGC(VMGlobals *g, AllocPool *inPool, PyrClass *mainProcessClass, long poolSize);
PyrObject* New(size_t inNumBytes, long inFlags, long inFormat, bool inCollect);
static PyrObject* NewPermanent(size_t inNumBytes,
long inFlags, long inFormat);
PyrObject* NewFinalizer(ObjFuncPtr finalizeFunc, PyrObject *inObject, bool inCollect);
int32 ProcessID() { return mProcessID; }
#if 0
// Codewarrior is not inlining these.. why?
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; }
bool IsMarker(PyrObjectHdr* inObj) { return inObj->gc_color == obj_gcmarker; }
#else
#define IsBlack(inObj) ((inObj)->gc_color == mBlackColor)
#define IsWhite(inObj) ((inObj)->gc_color == mWhiteColor)
#define IsGrey(inObj) ((inObj)->gc_color == mGreyColor)
#define IsFree(inObj) (!(IsMarker(inObj) || inObj->IsPermanent() || \
IsBlack(inObj) || IsWhite(inObj) || IsGrey(inObj)))
#define IsMarker(inObj) ((inObj)->gc_color == obj_gcmarker)
#endif
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(inSlot->uo)) {
ToGrey(inSlot->uo);
}
}
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(inSlot->uo)) {
ToGrey(inSlot->uo);
}
}
}
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 FullCollection();
void ScanFinalizers();
GCSet* GetGCSet(PyrObjectHdr* inObj);
void CompletePartialScan(PyrObject *obj);
void ToGrey(PyrObjectHdr* inObj);
void ToGrey2(PyrObjectHdr* inObj);
void ToBlack(PyrObjectHdr* inObj);
void ToWhite(PyrObjectHdr *obj);
int32 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 DumpEverything();
void BecomePermanent(PyrObject *inObject);
void BecomeImmutable(PyrObject *inObject);
private:
void Free(PyrObject* inObj);
void ScanSlots(PyrSlot *inSlots, long inNumToScan);
void SweepBigObjects();
void DoPartialScan(int32 inObjSize);
bool ScanOneObj();
void Flip();
void ScanStack();
void DLRemove(PyrObjectHdr *obj);
void DLInsertAfter(PyrObjectHdr *after, PyrObjectHdr *obj);
void DLInsertBefore(PyrObjectHdr *before, PyrObjectHdr *obj);
void ClearMarks();
void Finalize(PyrObject *obj);
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;
GCSet *mPartialScanSet;
int32 mPartialScanSlot;
int32 mNumToScan;
int32 mNumGrey;
int32 mCurSet;
int32 mFlips, mCollects, mAllocTotal, mScans, mNumAllocs;
unsigned char mBlackColor, mGreyColor, mWhiteColor, mFreeColor;
int8 mProcessID;
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;
}
#endif
|