Google Ads

Monday, September 7, 2009

Copying Collection

Suitable routine for copying collection


struct cell
{
struct cell *c[2];
}

struct cell space[2][HALFSIZE];
struct cell *roots[ROOTS];
struct cell *free = &(space[0][0]);
struct cell *end = &(space[0][HALFSIZE]);
int from_space = 0;
int to_space = 1;
struct cell *allocate()
{
if (free == end) { /* no room */
gc();
if (free == end) /* still no room */
die();
};
return free++;
}

gc()
{
int i;
struct cell *scan = &(space[to_space][0]);
free = scan;
for (i = 0 ; i < ROOTS; i++)
roots[i] = forward(roots[i]);

while (scan < free)
{
scan->c[0] = forward(scan->c[0]);
scan->c[1] = forward(scan->c[1]);
scan++;
};
from_space = 1-from_space;
to_space = 1-to_space;
end = *(space[from_space][HALFSIZE]);
}

struct cell *forward(struct cell *p)
{
if (p >=&(space[from_space][0]) && p < &(space[from_space][HALFSIZE]))
if (p->c[0] >= &(space[to_space][0]) && p->c[0] < &(space[to_space][HALFSIZE]))
return p->c[0];

else
{
*free = *p;
p->c[0] = free++;
return p->c[0];
}
else return p;
}

No comments:

Post a Comment