20,741
edits
m (→Memory blocks) |
|||
| Line 9: | Line 9: | ||
== Nasal memory pools == | == Nasal memory pools == | ||
== Memory blocks == | |||
A Nasal memory pool consists of memory blocks. | |||
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> | |||
The layout of the Nasal memory structure pool can be found in data.h, line 172: https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/data.h#line172 | The layout of the Nasal memory structure pool can be found in data.h, line 172: https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/data.h#line172 | ||
<syntaxhighlight lang="C"> | <syntaxhighlight lang="C"> | ||