]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/gc.c
c3f1e66020c70a491ef5c6423e55e16521b93eab
[simgear.git] / simgear / nasal / gc.c
1 #include "nasal.h"
2 #include "data.h"
3 #include "code.h"
4
5 #define MIN_BLOCK_SIZE 32
6
7 static void reap(struct naPool* p);
8 static void mark(naRef r);
9
10 struct Block {
11     int   size;
12     char* block;
13     struct Block* next;
14 };
15
16 // Must be called with the giant exclusive lock!
17 static void freeDead()
18 {
19     int i;
20     for(i=0; i<globals->ndead; i++)
21         naFree(globals->deadBlocks[i]);
22     globals->ndead = 0;
23 }
24
25 static void marktemps(struct Context* c)
26 {
27     int i;
28     naRef r = naNil();
29     for(i=0; i<c->ntemps; i++) {
30         SETPTR(r, c->temps[i]);
31         mark(r);
32     }
33 }
34
35 // Must be called with the big lock!
36 static void garbageCollect()
37 {
38     int i;
39     struct Context* c;
40     globals->allocCount = 0;
41     c = globals->allContexts;
42     while(c) {
43         for(i=0; i<NUM_NASAL_TYPES; i++)
44             c->nfree[i] = 0;
45         for(i=0; i < c->fTop; i++) {
46             mark(c->fStack[i].func);
47             mark(c->fStack[i].locals);
48         }
49         for(i=0; i < c->opTop; i++)
50             mark(c->opStack[i]);
51         mark(c->dieArg);
52         marktemps(c);
53         c = c->nextAll;
54     }
55
56     mark(globals->save);
57     mark(globals->symbols);
58     mark(globals->meRef);
59     mark(globals->argRef);
60     mark(globals->parentsRef);
61
62     // Finally collect all the freed objects
63     for(i=0; i<NUM_NASAL_TYPES; i++)
64         reap(&(globals->pools[i]));
65
66     // Make enough space for the dead blocks we need to free during
67     // execution.  This works out to 1 spot for every 2 live objects,
68     // which should be limit the number of bottleneck operations
69     // without imposing an undue burden of extra "freeable" memory.
70     if(globals->deadsz < globals->allocCount) {
71         globals->deadsz = globals->allocCount;
72         if(globals->deadsz < 256) globals->deadsz = 256;
73         naFree(globals->deadBlocks);
74         globals->deadBlocks = naAlloc(sizeof(void*) * globals->deadsz);
75     }
76     globals->needGC = 0;
77 }
78
79 void naModLock()
80 {
81     LOCK();
82     globals->nThreads++;
83     UNLOCK();
84     naCheckBottleneck();
85 }
86
87 void naModUnlock()
88 {
89     LOCK();
90     globals->nThreads--;
91     // We might be the "last" thread needed for collection.  Since
92     // we're releasing our modlock to do something else for a while,
93     // wake someone else up to do it.
94     if(globals->waitCount == globals->nThreads)
95         naSemUp(globals->sem, 1);
96     UNLOCK();
97 }
98
99 // Must be called with the main lock.  Engages the "bottleneck", where
100 // all threads will block so that one (the last one to call this
101 // function) can run alone.  This is done for GC, and also to free the
102 // list of "dead" blocks when it gets full (which is part of GC, if
103 // you think about it).
104 static void bottleneck()
105 {
106     struct Globals* g = globals;
107     g->bottleneck = 1;
108     while(g->bottleneck && g->waitCount < g->nThreads - 1) {
109         g->waitCount++;
110         UNLOCK(); naSemDown(g->sem); LOCK();
111         g->waitCount--;
112     }
113     if(g->waitCount >= g->nThreads - 1) {
114         freeDead();
115         if(g->needGC) garbageCollect();
116         if(g->waitCount) naSemUp(g->sem, g->waitCount);
117         g->bottleneck = 0;
118     }
119 }
120
121 void naGC()
122 {
123     LOCK();
124     globals->needGC = 1;
125     bottleneck();
126     UNLOCK();
127     naCheckBottleneck();
128 }
129
130 void naCheckBottleneck()
131 {
132     if(globals->bottleneck) { LOCK(); bottleneck(); UNLOCK(); }
133 }
134
135 static void naCode_gcclean(struct naCode* o)
136 {
137     naFree(o->constants);  o->constants = 0;
138 }
139
140 static void naCCode_gcclean(struct naCCode* c)
141 {
142     if(c->fptru && c->user_data && c->destroy) c->destroy(c->user_data);
143     c->user_data = 0;
144 }
145
146 static void naGhost_gcclean(struct naGhost* g)
147 {
148     if(g->ptr && g->gtype->destroy) g->gtype->destroy(g->ptr);
149     g->ptr = 0;
150 }
151
152 static void freeelem(struct naPool* p, struct naObj* o)
153 {
154     // Clean up any intrinsic storage the object might have...
155     switch(p->type) {
156     case T_STR:   naStr_gcclean  ((struct naStr*)  o); break;
157     case T_VEC:   naVec_gcclean  ((struct naVec*)  o); break;
158     case T_HASH:  naiGCHashClean ((struct naHash*) o); break;
159     case T_CODE:  naCode_gcclean ((struct naCode*) o); break;
160     case T_CCODE: naCCode_gcclean((struct naCCode*)o); break;
161     case T_GHOST: naGhost_gcclean((struct naGhost*)o); break;
162     }
163     p->free[p->nfree++] = o;  // ...and add it to the free list
164 }
165
166 static void newBlock(struct naPool* p, int need)
167 {
168     int i;
169     struct Block* newb;
170
171     if(need < MIN_BLOCK_SIZE) need = MIN_BLOCK_SIZE;
172
173     newb = naAlloc(sizeof(struct Block));
174     newb->block = naAlloc(need * p->elemsz);
175     newb->size = need;
176     newb->next = p->blocks;
177     p->blocks = newb;
178     naBZero(newb->block, need * p->elemsz);
179     
180     if(need > p->freesz - p->freetop) need = p->freesz - p->freetop;
181     p->nfree = 0;
182     p->free = p->free0 + p->freetop;
183     for(i=0; i < need; i++) {
184         struct naObj* o = (struct naObj*)(newb->block + i*p->elemsz);
185         o->mark = 0;
186         p->free[p->nfree++] = o;
187     }
188     p->freetop += need;
189 }
190
191 void naGC_init(struct naPool* p, int type)
192 {
193     p->type = type;
194     p->elemsz = naTypeSize(type);
195     p->blocks = 0;
196
197     p->free0 = p->free = 0;
198     p->nfree = p->freesz = p->freetop = 0;
199     reap(p);
200 }
201
202 static int poolsize(struct naPool* p)
203 {
204     int total = 0;
205     struct Block* b = p->blocks;
206     while(b) { total += b->size; b = b->next; }
207     return total;
208 }
209
210 struct naObj** naGC_get(struct naPool* p, int n, int* nout)
211 {
212     struct naObj** result;
213     naCheckBottleneck();
214     LOCK();
215     while(globals->allocCount < 0 || (p->nfree == 0 && p->freetop >= p->freesz)) {
216         globals->needGC = 1;
217         bottleneck();
218     }
219     if(p->nfree == 0)
220         newBlock(p, poolsize(p)/8);
221     n = p->nfree < n ? p->nfree : n;
222     *nout = n;
223     p->nfree -= n;
224     globals->allocCount -= n;
225     result = (struct naObj**)(p->free + p->nfree);
226     UNLOCK();
227     return result;
228 }
229
230 static void markvec(naRef r)
231 {
232     int i;
233     struct VecRec* vr = PTR(r).vec->rec;
234     if(!vr) return;
235     for(i=0; i<vr->size; i++)
236         mark(vr->array[i]);
237 }
238
239 // Sets the reference bit on the object, and recursively on all
240 // objects reachable from it.  Uses the processor stack for recursion...
241 static void mark(naRef r)
242 {
243     int i;
244
245     if(IS_NUM(r) || IS_NIL(r))
246         return;
247
248     if(PTR(r).obj->mark == 1)
249         return;
250
251     PTR(r).obj->mark = 1;
252     switch(PTR(r).obj->type) {
253     case T_VEC: markvec(r); break;
254     case T_HASH: naiGCMarkHash(r); break;
255     case T_CODE:
256         mark(PTR(r).code->srcFile);
257         for(i=0; i<PTR(r).code->nConstants; i++)
258             mark(PTR(r).code->constants[i]);
259         break;
260     case T_FUNC:
261         mark(PTR(r).func->code);
262         mark(PTR(r).func->namespace);
263         mark(PTR(r).func->next);
264         break;
265     }
266 }
267
268 void naiGCMark(naRef r)
269 {
270     mark(r);
271 }
272
273 // Collects all the unreachable objects into a free list, and
274 // allocates more space if needed.
275 static void reap(struct naPool* p)
276 {
277     struct Block* b;
278     int elem, freesz, total = poolsize(p);
279     freesz = total < MIN_BLOCK_SIZE ? MIN_BLOCK_SIZE : total;
280     freesz = (3 * freesz / 2) + (globals->nThreads * OBJ_CACHE_SZ);
281     if(p->freesz < freesz) {
282         naFree(p->free0);
283         p->freesz = freesz;
284         p->free = p->free0 = naAlloc(sizeof(void*) * p->freesz);
285     }
286
287     p->nfree = 0;
288     p->free = p->free0;
289
290     for(b = p->blocks; b; b = b->next)
291         for(elem=0; elem < b->size; elem++) {
292             struct naObj* o = (struct naObj*)(b->block + elem * p->elemsz);
293             if(o->mark == 0)
294                 freeelem(p, o);
295             o->mark = 0;
296         }
297
298     p->freetop = p->nfree;
299
300     // allocs of this type until the next collection
301     globals->allocCount += total/2;
302     
303     // Allocate more if necessary (try to keep 25-50% of the objects
304     // available)
305     if(p->nfree < total/4) {
306         int used = total - p->nfree;
307         int avail = total - used;
308         int need = used/2 - avail;
309         if(need > 0)
310             newBlock(p, need);
311     }
312 }
313
314 // Does the swap, returning the old value
315 static void* doswap(void** target, void* val)
316 {
317     void* old = *target;
318     *target = val;
319     return old;
320 }
321
322 // Atomically replaces target with a new pointer, and adds the old one
323 // to the list of blocks to free the next time something holds the
324 // giant lock.
325 void naGC_swapfree(void** target, void* val)
326 {
327     void* old;
328     LOCK();
329     old = doswap(target, val);
330     while(globals->ndead >= globals->deadsz)
331         bottleneck();
332     globals->deadBlocks[globals->ndead++] = old;
333     UNLOCK();
334 }