]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/gc.c
Removal of PLIB/SG from SimGear
[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 naCheckBottleneck()
122 {
123     if(globals->bottleneck) { LOCK(); bottleneck(); UNLOCK(); }
124 }
125
126 static void naCode_gcclean(struct naCode* o)
127 {
128     naFree(o->constants);  o->constants = 0;
129 }
130
131 static void naGhost_gcclean(struct naGhost* g)
132 {
133     if(g->ptr && g->gtype->destroy) g->gtype->destroy(g->ptr);
134     g->ptr = 0;
135 }
136
137 static void freeelem(struct naPool* p, struct naObj* o)
138 {
139     // Clean up any intrinsic storage the object might have...
140     switch(p->type) {
141     case T_STR:   naStr_gcclean  ((struct naStr*)  o); break;
142     case T_VEC:   naVec_gcclean  ((struct naVec*)  o); break;
143     case T_HASH:  naiGCHashClean ((struct naHash*) o); break;
144     case T_CODE:  naCode_gcclean ((struct naCode*) o); break;
145     case T_GHOST: naGhost_gcclean((struct naGhost*)o); break;
146     }
147     p->free[p->nfree++] = o;  // ...and add it to the free list
148 }
149
150 static void newBlock(struct naPool* p, int need)
151 {
152     int i;
153     struct Block* newb;
154
155     if(need < MIN_BLOCK_SIZE) need = MIN_BLOCK_SIZE;
156
157     newb = naAlloc(sizeof(struct Block));
158     newb->block = naAlloc(need * p->elemsz);
159     newb->size = need;
160     newb->next = p->blocks;
161     p->blocks = newb;
162     naBZero(newb->block, need * p->elemsz);
163     
164     if(need > p->freesz - p->freetop) need = p->freesz - p->freetop;
165     p->nfree = 0;
166     p->free = p->free0 + p->freetop;
167     for(i=0; i < need; i++) {
168         struct naObj* o = (struct naObj*)(newb->block + i*p->elemsz);
169         o->mark = 0;
170         p->free[p->nfree++] = o;
171     }
172     p->freetop += need;
173 }
174
175 void naGC_init(struct naPool* p, int type)
176 {
177     p->type = type;
178     p->elemsz = naTypeSize(type);
179     p->blocks = 0;
180
181     p->free0 = p->free = 0;
182     p->nfree = p->freesz = p->freetop = 0;
183     reap(p);
184 }
185
186 static int poolsize(struct naPool* p)
187 {
188     int total = 0;
189     struct Block* b = p->blocks;
190     while(b) { total += b->size; b = b->next; }
191     return total;
192 }
193
194 struct naObj** naGC_get(struct naPool* p, int n, int* nout)
195 {
196     struct naObj** result;
197     naCheckBottleneck();
198     LOCK();
199     while(globals->allocCount < 0 || (p->nfree == 0 && p->freetop >= p->freesz)) {
200         globals->needGC = 1;
201         bottleneck();
202     }
203     if(p->nfree == 0)
204         newBlock(p, poolsize(p)/8);
205     n = p->nfree < n ? p->nfree : n;
206     *nout = n;
207     p->nfree -= n;
208     globals->allocCount -= n;
209     result = (struct naObj**)(p->free + p->nfree);
210     UNLOCK();
211     return result;
212 }
213
214 static void markvec(naRef r)
215 {
216     int i;
217     struct VecRec* vr = PTR(r).vec->rec;
218     if(!vr) return;
219     for(i=0; i<vr->size; i++)
220         mark(vr->array[i]);
221 }
222
223 // Sets the reference bit on the object, and recursively on all
224 // objects reachable from it.  Uses the processor stack for recursion...
225 static void mark(naRef r)
226 {
227     int i;
228
229     if(IS_NUM(r) || IS_NIL(r))
230         return;
231
232     if(PTR(r).obj->mark == 1)
233         return;
234
235     PTR(r).obj->mark = 1;
236     switch(PTR(r).obj->type) {
237     case T_VEC: markvec(r); break;
238     case T_HASH: naiGCMarkHash(r); break;
239     case T_CODE:
240         mark(PTR(r).code->srcFile);
241         for(i=0; i<PTR(r).code->nConstants; i++)
242             mark(PTR(r).code->constants[i]);
243         break;
244     case T_FUNC:
245         mark(PTR(r).func->code);
246         mark(PTR(r).func->namespace);
247         mark(PTR(r).func->next);
248         break;
249     }
250 }
251
252 void naiGCMark(naRef r)
253 {
254     mark(r);
255 }
256
257 // Collects all the unreachable objects into a free list, and
258 // allocates more space if needed.
259 static void reap(struct naPool* p)
260 {
261     struct Block* b;
262     int elem, freesz, total = poolsize(p);
263     freesz = total < MIN_BLOCK_SIZE ? MIN_BLOCK_SIZE : total;
264     freesz = (3 * freesz / 2) + (globals->nThreads * OBJ_CACHE_SZ);
265     if(p->freesz < freesz) {
266         naFree(p->free0);
267         p->freesz = freesz;
268         p->free = p->free0 = naAlloc(sizeof(void*) * p->freesz);
269     }
270
271     p->nfree = 0;
272     p->free = p->free0;
273
274     for(b = p->blocks; b; b = b->next)
275         for(elem=0; elem < b->size; elem++) {
276             struct naObj* o = (struct naObj*)(b->block + elem * p->elemsz);
277             if(o->mark == 0)
278                 freeelem(p, o);
279             o->mark = 0;
280         }
281
282     p->freetop = p->nfree;
283
284     // allocs of this type until the next collection
285     globals->allocCount += total/2;
286     
287     // Allocate more if necessary (try to keep 25-50% of the objects
288     // available)
289     if(p->nfree < total/4) {
290         int used = total - p->nfree;
291         int avail = total - used;
292         int need = used/2 - avail;
293         if(need > 0)
294             newBlock(p, need);
295     }
296 }
297
298 // Does the swap, returning the old value
299 static void* doswap(void** target, void* val)
300 {
301     void* old = *target;
302     *target = val;
303     return old;
304 }
305
306 // Atomically replaces target with a new pointer, and adds the old one
307 // to the list of blocks to free the next time something holds the
308 // giant lock.
309 void naGC_swapfree(void** target, void* val)
310 {
311     void* old;
312     LOCK();
313     old = doswap(target, val);
314     while(globals->ndead >= globals->deadsz)
315         bottleneck();
316     globals->deadBlocks[globals->ndead++] = old;
317     UNLOCK();
318 }