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