20,741
edits
| Line 162: | Line 162: | ||
The garbage collector itself is triggered by setting the global "needGC" flag to 1 in the globals structure and then calling the bottleneck() function: https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/gc.c#line199 | The garbage collector itself is triggered by setting the global "needGC" flag to 1 in the globals structure and then calling the bottleneck() function: https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/gc.c#line199 | ||
In line 209, the memory region from the global pool is cast to a pointer (struct naObj**): https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/gc.c#line209 | In line 209, the memory region from the global pool is cast to a pointer (struct naObj**): https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/gc.c#line209 | ||
== Allocations == | |||
A memory block is a singly linked list: https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/gc.c#line10 | |||
<syntaxhighlight lang="C"> | |||
struct Block { | |||
int size; | |||
char* block; | |||
struct Block* next; | |||
}; | |||
</syntaxhighlight> | |||
New memory blocks are allocated using newBlock(): https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/gc.c#line150 | |||
<syntaxhighlight lang="C"> | |||
static void newBlock(struct naPool* p, int need) | |||
{ | |||
int i; | |||
struct Block* newb; | |||
if(need < MIN_BLOCK_SIZE) need = MIN_BLOCK_SIZE; | |||
newb = naAlloc(sizeof(struct Block)); | |||
newb->block = naAlloc(need * p->elemsz); | |||
newb->size = need; | |||
newb->next = p->blocks; | |||
p->blocks = newb; | |||
naBZero(newb->block, need * p->elemsz); | |||
if(need > p->freesz - p->freetop) need = p->freesz - p->freetop; | |||
p->nfree = 0; | |||
p->free = p->free0 + p->freetop; | |||
for(i=0; i < need; i++) { | |||
struct naObj* o = (struct naObj*)(newb->block + i*p->elemsz); | |||
o->mark = 0; | |||
p->free[p->nfree++] = o; | |||
} | |||
p->freetop += need; | |||
} | |||
</syntaxhighlight> | |||
All core memory allocation takes place via wrappers in [https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/misc.c#line8 misc.c]: | |||
<syntaxhighlight lang="C"> | |||
void naFree(void* m) { free(m); } | |||
void* naAlloc(int n) { return malloc(n); } | |||
void* naRealloc(void* b, int n) { return realloc(b, n); } | |||
void naBZero(void* m, int n) { memset(m, 0, n); } | |||
</syntaxhighlight> | |||
= bottleneck() = | = bottleneck() = | ||