altos/lisp: Make sure memmove only happens once per object. Other GC fixes
[fw/altos] / src / lisp / ao_lisp_mem.c
index 66e09db0612f9eb4ce3d58a60dd20c75c6729b93..31ee9e1e339739ff4965b0469e13f8575727abe6 100644 (file)
@@ -28,32 +28,31 @@ uint8_t     ao_lisp_pool[AO_LISP_POOL] __attribute__((aligned(4)));
 #endif
 
 #if 0
-#define DBG_COLLECT_ALWAYS
+#define MDBG_COLLECT_ALWAYS
 #endif
 
 #if 0
-#define DBG_POOL
+#define MDBG_POOL
 #endif
 
 #if 0
-#define DBG_INCLUDE
-#define DBG_DUMP       0
-#define DBG_OFFSET(a)  ((int) ((uint8_t *) (a) - ao_lisp_pool))
-#define DBG(...) printf(__VA_ARGS__)
-#define DBG_DO(a)      a
-static int move_dump = 1;
+#include <assert.h>
+#define MDBG_INCLUDE
+#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_depth;
-#define DBG_RESET() (move_depth = 0)
-#define DBG_MOVE(...) do { if(move_dump) { int d; for (d = 0; d < move_depth; d++) printf ("  "); printf(__VA_ARGS__); } } while (0)
-#define DBG_MOVE_IN()  (move_depth++)
-#define DBG_MOVE_OUT() (move_depth--)
+#define MDBG_MOVE_IN() (move_depth++)
+#define MDBG_MOVE_OUT()        (assert(--move_depth >= 0))
 #else
-#define DBG(...)
-#define DBG_DO(a)
-#define DBG_RESET()
-#define DBG_MOVE(...)
-#define DBG_MOVE_IN()
-#define DBG_MOVE_OUT()
+#define MDBG(...)
+#define MDBG_DO(a)
+#define MDBG_MOVE(...)
+#define MDBG_MOVE_IN()
+#define MDBG_MOVE_OUT()
 #endif
 
 uint8_t        ao_lisp_exception;
@@ -67,10 +66,12 @@ struct ao_lisp_root {
 
 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[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;
@@ -166,72 +167,61 @@ busy_object(uint8_t *tag, void *addr) {
 static void
 note_cons(void *addr)
 {
-       DBG_MOVE("note cons %d\n", DBG_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, (uint8_t *) addr - ao_lisp_pool);
+               mark(ao_lisp_cons_note, offset);
        }
 }
 
-
-static void    *move_old, *move_new;
-static int     move_size;
+/*
+ * Walk all referenced objects calling functions on each one
+ */
 
 static void
-move_object(void)
+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;
+       int i;
 
-       DBG_RESET();
-       DBG_MOVE("move %d -> %d\n", DBG_OFFSET(move_old), DBG_OFFSET(move_new));
-       DBG_MOVE_IN();
-       memset(ao_lisp_moving, '\0', sizeof (ao_lisp_moving));
-       memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
+       memset(tag, '\0', sizeof (ao_lisp_busy));
+       memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
        ao_lisp_cons_noted = 0;
        for (i = 0; i < AO_LISP_ROOT; i++) {
-               if (!ao_lisp_root[i].addr)
-                       continue;
                if (ao_lisp_root[i].type) {
-                       void *addr = *ao_lisp_root[i].addr;
-                       if (!addr)
-                               continue;
-                       DBG_MOVE("root %d\n", DBG_OFFSET(addr));
-                       if (!ao_lisp_move(ao_lisp_root[i].type,
-                                         ao_lisp_root[i].addr)) {
-                               DBG_MOVE("root moves from %p to %p\n",
-                                        addr,
-                                        *ao_lisp_root[i].addr);
+                       void **a = ao_lisp_root[i].addr, *v;
+                       if (a && (v = *a)) {
+                               MDBG("root ptr %d\n", MDBG_OFFSET(v));
+                               visit_addr(ao_lisp_root[i].type, a);
                        }
                } else {
-                       ao_poly p = *(ao_poly *) ao_lisp_root[i].addr;
-                       if (!p)
-                               continue;
-                       if (!ao_lisp_poly_move((ao_poly *) ao_lisp_root[i].addr, 0)) {
-                               DBG_MOVE("root poly move from %04x to %04x\n",
-                                        p, *(ao_poly *) ao_lisp_root[i].addr);
+                       ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
+                       if (a && (p = *a)) {
+                               MDBG("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
+                               visit_poly(a, 0);
                        }
                }
        }
        while (ao_lisp_cons_noted) {
-               memcpy(ao_lisp_cons_last, ao_lisp_cons, sizeof (ao_lisp_cons));
-               memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
+               memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
+               memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
                ao_lisp_cons_noted = 0;
                for (i = 0; i < AO_LISP_POOL; i += 4) {
                        if (busy(ao_lisp_cons_last, i)) {
-                               void *addr = ao_lisp_pool + i;
-                               DBG_MOVE("cons %d\n", DBG_OFFSET(addr));
-                               if (!ao_lisp_move(&ao_lisp_cons_type, &addr)) {
-                                       DBG_MOVE("cons moves from %p to %p\n",
-                                                ao_lisp_pool + i, addr);
-                               }
+                               void *v = ao_lisp_pool + i;
+                               MDBG("root cons %d\n", MDBG_OFFSET(v));
+                               visit_addr(&ao_lisp_cons_type, &v);
                        }
                }
        }
-       DBG_MOVE_OUT();
-       DBG_MOVE("move done\n");
 }
 
-#if DBG_DUMP
+static void    *move_old, *move_new;
+static int     move_size;
+
+#if MDBG_DUMP
 static void
 dump_busy(void)
 {
@@ -265,43 +255,16 @@ static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
        [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
 };
 
-
-static void
-ao_lisp_mark_busy(void)
+static int
+ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
 {
-       int i;
+       return ao_lisp_mark(type, *ref);
+}
 
-       memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
-       memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
-       ao_lisp_cons_noted = 0;
-       DBG("mark\n");
-       for (i = 0; i < AO_LISP_ROOT; i++) {
-               if (ao_lisp_root[i].type) {
-                       void **a = ao_lisp_root[i].addr, *v;
-                       if (a && (v = *a)) {
-                               DBG("root %d\n", DBG_OFFSET(v));
-                               ao_lisp_mark(ao_lisp_root[i].type, v);
-                       }
-               } else {
-                       ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
-                       if (a && (p = *a)) {
-                               DBG("root 0x%04x\n", p);
-                               ao_lisp_poly_mark(p, 0);
-                       }
-               }
-       }
-       while (ao_lisp_cons_noted) {
-               memcpy(ao_lisp_cons_last, ao_lisp_cons, sizeof (ao_lisp_cons));
-               memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
-               ao_lisp_cons_noted = 0;
-               for (i = 0; i < AO_LISP_POOL; i += 4) {
-                       if (busy(ao_lisp_cons_last, i)) {
-                               void *v = ao_lisp_pool + i;
-                               DBG("cons %d\n", DBG_OFFSET(v));
-                               ao_lisp_mark(&ao_lisp_cons_type, v);
-                       }
-               }
-       }
+static int
+ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
+{
+       return ao_lisp_poly_mark(*p, do_note_cons);
 }
 
 void
@@ -310,13 +273,13 @@ ao_lisp_collect(void)
        int     i;
        int     top;
 
-       DBG("collect\n");
+       MDBG("collect\n");
        /* Mark */
-       ao_lisp_mark_busy();
+       walk(ao_lisp_busy, ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
 
        DUMP_BUSY();
        /* Compact */
-       DBG("find first busy\n");
+       MDBG("find first busy\n");
        for (i = 0; i < ao_lisp_top; i += 4) {
                if (!busy(ao_lisp_busy, i))
                        break;
@@ -324,23 +287,27 @@ ao_lisp_collect(void)
        top = i;
        while(i < ao_lisp_top) {
                if (busy(ao_lisp_busy, i)) {
-                       DBG("busy %d -> %d\n", i, top);
+                       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();
-                       DBG("\tbusy size %d\n", move_size);
+                       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();
                        clear_object(ao_lisp_busy, move_old, move_size);
                        mark_object(ao_lisp_busy, move_new, move_size);
-                       if (busy_object(ao_lisp_cons, move_old)) {
-                               clear_object(ao_lisp_cons, move_old, move_size);
-                               mark_object(ao_lisp_cons, move_new, move_size);
+                       if (busy_object(ao_lisp_cons_note, move_old)) {
+                               clear_object(ao_lisp_cons_note, move_old, move_size);
+                               mark_object(ao_lisp_cons_note, move_new, move_size);
                        }
                        i += move_size;
                        top += move_size;
+#if MDBG_MOVE
                        DUMP_BUSY();
+#endif
+                       MDBG_MOVE_OUT();
                } else {
                        i += 4;
                }
@@ -354,9 +321,15 @@ ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
 {
        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;
 }
 
@@ -368,6 +341,7 @@ ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
        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 {
@@ -394,7 +368,7 @@ void *
 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;
@@ -404,9 +378,15 @@ static void *
 check_move(void *addr, int size)
 {
        if (addr == move_old) {
-               DBG_MOVE("mapping %d -> %d\n", DBG_OFFSET(addr), DBG_OFFSET(move_new));
-               if (!busy_object(ao_lisp_moving, addr)) {
-                       DBG_MOVE("  copy %d\n", size);
+               MDBG_MOVE("mapping %d -> %d\n", MDBG_OFFSET(addr), MDBG_OFFSET(move_new));
+               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;
                }
@@ -420,7 +400,7 @@ ao_lisp_move(const struct ao_lisp_type *type, void **ref)
 {
        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;
@@ -429,24 +409,24 @@ ao_lisp_move(const struct ao_lisp_type *type, void **ref)
        if (AO_LISP_IS_CONST(addr))
                return 1;
 #endif
-       DBG_MOVE("object %d\n", DBG_OFFSET(addr));
+       MDBG_MOVE("object %d\n", MDBG_OFFSET(addr));
        if (!AO_LISP_IS_POOL(a))
                ao_lisp_abort();
-       DBG_MOVE_IN();
+       MDBG_MOVE_IN();
        addr = check_move(addr, size);
        if (addr != *ref)
                *ref = addr;
        if (mark_object(ao_lisp_moving, addr, size)) {
-               DBG_MOVE("already moved\n");
-               DBG_MOVE_OUT();
+               MDBG_MOVE("already moved\n");
+               MDBG_MOVE_OUT();
                return 1;
        }
-       DBG_MOVE_OUT();
-       DBG_MOVE("recursing...\n");
-       DBG_MOVE_IN();
+       MDBG_MOVE_OUT();
+       MDBG_MOVE("recursing...\n");
+       MDBG_MOVE_IN();
        type->move(addr);
-       DBG_MOVE_OUT();
-       DBG_MOVE("done %d\n", DBG_OFFSET(addr));
+       MDBG_MOVE_OUT();
+       MDBG_MOVE("done %d\n", MDBG_OFFSET(addr));
        return 0;
 }
 
@@ -457,17 +437,17 @@ ao_lisp_move_memory(void **ref, int size)
        if (!addr)
                return 1;
 
-       DBG_MOVE("memory %d\n", DBG_OFFSET(addr));
-       DBG_MOVE_IN();
+       MDBG_MOVE("memory %d\n", MDBG_OFFSET(addr));
+       MDBG_MOVE_IN();
        addr = check_move(addr, size);
        if (addr != *ref)
                *ref = addr;
        if (mark_object(ao_lisp_moving, addr, size)) {
-               DBG_MOVE("already moved\n");
-               DBG_MOVE_OUT();
+               MDBG_MOVE("already moved\n");
+               MDBG_MOVE_OUT();
                return 1;
        }
-       DBG_MOVE_OUT();
+       MDBG_MOVE_OUT();
        return 0;
 }
 
@@ -485,9 +465,13 @@ ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
 
        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 {
 
@@ -505,14 +489,14 @@ ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
 
        if (addr != ao_lisp_ref(p)) {
                ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
-               DBG("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;
 }
 
-#ifdef DBG_POOL
+#ifdef MDBG_POOL
 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
 
 static void
@@ -557,18 +541,18 @@ ao_lisp_alloc(int size)
        void    *addr;
 
        size = ao_lisp_mem_round(size);
-#ifdef DBG_COLLECT_ALWAYS
+#ifdef MDBG_COLLECT_ALWAYS
        ao_lisp_collect();
 #endif
        if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
-#ifdef DBG_POOL
+#ifdef MDBG_POOL
                if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
                        AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
                        ao_lisp_poison();
                } else
 #endif
                ao_lisp_collect();
-#ifdef DBG_POOL
+#ifdef MDBG_POOL
                {
                        int     i;
 
@@ -580,7 +564,7 @@ ao_lisp_alloc(int size)
 #endif
 
                if (ao_lisp_top + size > AO_LISP_POOL) {
-                       ao_lisp_exception |= AO_LISP_OOM;
+                       ao_lisp_error(AO_LISP_OOM, "out of memory");
                        return NULL;
                }
        }
@@ -593,7 +577,7 @@ int
 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
 {
        int     i;
-       DBG("add root type %p addr %p\n", type, addr);
+       MDBG("add root type %p addr %p\n", type, addr);
        for (i = 0; i < AO_LISP_ROOT; i++) {
                if (!ao_lisp_root[i].addr) {
                        ao_lisp_root[i].addr = addr;