(void) ao_lisp_poly_move(&cons->car, 1);
cdr = ao_lisp_poly_cons(cons->cdr);
+ if (!cdr)
+ break;
ret = ao_lisp_move_memory((void **) &cdr, sizeof (struct ao_lisp_cons));
if (cdr != ao_lisp_poly_cons(cons->cdr))
cons->cdr = ao_lisp_cons_poly(cdr);
.move = cons_move,
};
+static ao_poly cons_car;
+static struct ao_lisp_cons *cons_cdr;
+static int been_here;
+
struct ao_lisp_cons *
ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr)
{
struct ao_lisp_cons *cons;
- ao_lisp_root_add(&ao_lisp_cons_type, &cdr);
- ao_lisp_root_poly_add(&car);
+ if (!been_here) {
+ ao_lisp_root_add(&ao_lisp_cons_type, &cons_cdr);
+ ao_lisp_root_poly_add(&cons_car);
+ been_here = 1;
+ }
+ cons_car = car;
+ cons_cdr = cdr;
cons = ao_lisp_alloc(sizeof (struct ao_lisp_cons));
- ao_lisp_root_clear(&car);
- ao_lisp_root_clear(&cdr);
if (!cons)
return NULL;
- cons->car = car;
- cons->cdr = ao_lisp_cons_poly(cdr);
+ cons->car = cons_car;
+ cons->cdr = ao_lisp_cons_poly(cons_cdr);
+ cons_car = AO_LISP_NIL;
+ cons_cdr = NULL;
return cons;
}
#endif
#if 0
+#include <assert.h>
#define MDBG_INCLUDE
-#define MDBG_DUMP 1
-#define MDBG_MOVE 0
+#if 1
+#define MDBG_MOVE(...) do { int d; for (d = 0; d < move_depth; d++) printf (" "); printf(__VA_ARGS__); } while (0)
+#endif
#define MDBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
#define MDBG(...) printf(__VA_ARGS__)
#define MDBG_DO(a) a
-static int move_dump = MDBG_MOVE;
static int move_depth;
-#define MDBG_RESET() (move_depth = 0)
-#define MDBG_MOVE(...) do { if(move_dump) { int d; for (d = 0; d < move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
#define MDBG_MOVE_IN() (move_depth++)
-#define MDBG_MOVE_OUT() (move_depth--)
+#define MDBG_MOVE_OUT() (assert(--move_depth >= 0))
#else
#define MDBG(...)
#define MDBG_DO(a)
-#define MDBG_RESET()
#define MDBG_MOVE(...)
#define MDBG_MOVE_IN()
#define MDBG_MOVE_OUT()
static struct ao_lisp_root ao_lisp_root[AO_LISP_ROOT];
-static uint8_t ao_lisp_busy[AO_LISP_POOL / 32];
-static uint8_t ao_lisp_moving[AO_LISP_POOL / 32];
-static uint8_t ao_lisp_cons_note[AO_LISP_POOL / 32];
-static uint8_t ao_lisp_cons_last[AO_LISP_POOL / 32];
+#define AO_LISP_BUSY_SIZE ((AO_LISP_POOL + 31) / 32)
+
+static uint8_t ao_lisp_busy[AO_LISP_BUSY_SIZE];
+static uint8_t ao_lisp_moving[AO_LISP_BUSY_SIZE];
+static uint8_t ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
+static uint8_t ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
static uint8_t ao_lisp_cons_noted;
uint16_t ao_lisp_top;
static void
note_cons(void *addr)
{
- MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
if (AO_LISP_IS_POOL(addr)) {
int offset = (uint8_t *) addr - ao_lisp_pool;
+ MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
ao_lisp_cons_noted = 1;
mark(ao_lisp_cons_note, offset);
}
*/
static void
-walk_all(uint8_t *tag,
- int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
- int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
+walk(uint8_t *tag,
+ int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
+ int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
{
int i;
if (ao_lisp_root[i].type) {
void **a = ao_lisp_root[i].addr, *v;
if (a && (v = *a)) {
- MDBG("root %d\n", MDBG_OFFSET(v));
+ MDBG("root ptr %d\n", MDBG_OFFSET(v));
visit_addr(ao_lisp_root[i].type, a);
}
} else {
ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
if (a && (p = *a)) {
- MDBG("root 0x%04x\n", p);
+ MDBG("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
visit_poly(a, 0);
}
}
for (i = 0; i < AO_LISP_POOL; i += 4) {
if (busy(ao_lisp_cons_last, i)) {
void *v = ao_lisp_pool + i;
- MDBG("cons %d\n", MDBG_OFFSET(v));
+ MDBG("root cons %d\n", MDBG_OFFSET(v));
visit_addr(&ao_lisp_cons_type, &v);
}
}
static void *move_old, *move_new;
static int move_size;
-static void
-move_object(void)
-{
- walk_all(ao_lisp_moving, ao_lisp_move, ao_lisp_poly_move);
-}
-
#if MDBG_DUMP
static void
dump_busy(void)
return ao_lisp_poly_mark(*p, do_note_cons);
}
-static void
-ao_lisp_mark_busy(void)
-{
- walk_all(ao_lisp_busy, ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
-}
-
void
ao_lisp_collect(void)
{
MDBG("collect\n");
/* Mark */
- ao_lisp_mark_busy();
+ walk(ao_lisp_busy, ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
DUMP_BUSY();
/* Compact */
while(i < ao_lisp_top) {
if (busy(ao_lisp_busy, i)) {
MDBG("busy %d -> %d\n", i, top);
+ MDBG_MOVE_IN();
move_old = &ao_lisp_pool[i];
move_new = &ao_lisp_pool[top];
move_size = 0;
- move_object();
+ walk(ao_lisp_moving, ao_lisp_move, ao_lisp_poly_move);
MDBG("\tbusy size %d\n", move_size);
if (move_size == 0)
ao_lisp_abort();
#if MDBG_MOVE
DUMP_BUSY();
#endif
+ MDBG_MOVE_OUT();
} else {
i += 4;
}
{
if (!addr)
return 1;
- if (mark_object(ao_lisp_busy, addr, type->size(addr)))
+ MDBG_MOVE_IN();
+ MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
+ if (mark_object(ao_lisp_busy, addr, type->size(addr))) {
+ MDBG_MOVE("already marked\n");
+ MDBG_MOVE_OUT();
return 1;
+ }
type->mark(addr);
+ MDBG_MOVE_OUT();
return 0;
}
if (!p)
return 1;
if (type == AO_LISP_CONS && do_note_cons) {
+ MDBG_MOVE("note cons %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
note_cons(ao_lisp_ref(p));
return 0;
} else {
ao_lisp_move_map(void *addr)
{
if (addr == move_old) {
- if (busy_object(ao_lisp_moving, addr))
+ if (move_size != 0)
return move_new;
}
return addr;
{
if (addr == move_old) {
MDBG_MOVE("mapping %d -> %d\n", MDBG_OFFSET(addr), MDBG_OFFSET(move_new));
- if (!busy_object(ao_lisp_moving, addr)) {
+ if (move_size && move_size != ((size + 3) & ~3))
+ ao_lisp_abort();
+
+ /* Only copy the object once, otherwise we may
+ * smash stuff
+ */
+ if (move_size == 0) {
MDBG_MOVE(" copy %d\n", size);
memmove(move_new, move_old, size);
move_size = (size + 3) & ~3;
{
void *addr = *ref;
uint8_t *a = addr;
- int size = type->size(addr);
+ int size = type->size(ao_lisp_move_map(addr));
if (!addr)
return 1;
type = ao_lisp_poly_base_type(p);
addr = ao_lisp_ref(p);
+
+ if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
+ return 1;
+
if (type == AO_LISP_CONS && do_note_cons) {
- note_cons(addr);
addr = check_move(addr, sizeof (struct ao_lisp_cons));
+ note_cons(addr);
ret = 1;
} else {
if (addr != ao_lisp_ref(p)) {
ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
- MDBG("poly %d moved %04x -> %04x\n",
- type, p, np);
+ MDBG("poly %d moved %d -> %d\n",
+ type, MDBG_OFFSET(ao_lisp_ref(p)), MDBG_OFFSET(ao_lisp_ref(np)));
*ref = np;
}
return ret;