890eba1baf1ac44a19b6e13063fcd9c83f6f317e
[fw/altos] / src / lisp / ao_lisp_mem.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #define AO_LISP_CONST_BITS
16
17 #include "ao_lisp.h"
18 #include <stdio.h>
19
20 #ifdef AO_LISP_MAKE_CONST
21
22 /*
23  * When building the constant table, it is the
24  * pool for allocations.
25  */
26
27 #include <stdlib.h>
28 uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));
29 #define ao_lisp_pool ao_lisp_const
30 #undef AO_LISP_POOL
31 #define AO_LISP_POOL AO_LISP_POOL_CONST
32
33 #else
34
35 uint8_t ao_lisp_pool[AO_LISP_POOL + AO_LISP_POOL_EXTRA] __attribute__((aligned(4)));
36
37 #endif
38
39 #ifndef DBG_MEM_STATS
40 #define DBG_MEM_STATS   DBG_MEM
41 #endif
42
43 #if DBG_MEM
44 int dbg_move_depth;
45 int dbg_mem = DBG_MEM_START;
46 int dbg_validate = 0;
47
48 struct ao_lisp_record {
49         struct ao_lisp_record           *next;
50         const struct ao_lisp_type       *type;
51         void                            *addr;
52         int                             size;
53 };
54
55 static struct ao_lisp_record    *record_head, **record_tail;
56
57 static void
58 ao_lisp_record_free(struct ao_lisp_record *record)
59 {
60         while (record) {
61                 struct ao_lisp_record *next = record->next;
62                 free(record);
63                 record = next;
64         }
65 }
66
67 static void
68 ao_lisp_record_reset(void)
69 {
70         ao_lisp_record_free(record_head);
71         record_head = NULL;
72         record_tail = &record_head;
73 }
74
75 static void
76 ao_lisp_record(const struct ao_lisp_type        *type,
77                void                             *addr,
78                int                              size)
79 {
80         struct ao_lisp_record   *r = malloc(sizeof (struct ao_lisp_record));
81
82         r->next = NULL;
83         r->type = type;
84         r->addr = addr;
85         r->size = size;
86         *record_tail = r;
87         record_tail = &r->next;
88 }
89
90 static struct ao_lisp_record *
91 ao_lisp_record_save(void)
92 {
93         struct ao_lisp_record *r = record_head;
94
95         record_head = NULL;
96         record_tail = &record_head;
97         return r;
98 }
99
100 static void
101 ao_lisp_record_compare(char *where,
102                        struct ao_lisp_record *a,
103                        struct ao_lisp_record *b)
104 {
105         while (a && b) {
106                 if (a->type != b->type || a->size != b->size) {
107                         printf("%s record difers %d %s %d -> %d %s %d\n",
108                                where,
109                                MDBG_OFFSET(a->addr),
110                                a->type->name,
111                                a->size,
112                                MDBG_OFFSET(b->addr),
113                                b->type->name,
114                                b->size);
115                         ao_lisp_abort();
116                 }
117                 a = a->next;
118                 b = b->next;
119         }
120         if (a) {
121                 printf("%s record differs %d %s %d -> NULL\n",
122                        where,
123                        MDBG_OFFSET(a->addr),
124                        a->type->name,
125                        a->size);
126                 ao_lisp_abort();
127         }
128         if (b) {
129                 printf("%s record differs NULL -> %d %s %d\n",
130                        where,
131                        MDBG_OFFSET(b->addr),
132                        b->type->name,
133                        b->size);
134                 ao_lisp_abort();
135         }
136 }
137
138 #else
139 #define ao_lisp_record_reset()
140 #endif
141
142 uint8_t ao_lisp_exception;
143
144 struct ao_lisp_root {
145         const struct ao_lisp_type       *type;
146         void                            **addr;
147 };
148
149 static struct ao_lisp_cons      *save_cons[2];
150 static char                     *save_string[2];
151 static struct ao_lisp_frame     *save_frame[1];
152 static ao_poly                  save_poly[3];
153
154 static const struct ao_lisp_root        ao_lisp_root[] = {
155         {
156                 .type = &ao_lisp_cons_type,
157                 .addr = (void **) &save_cons[0],
158         },
159         {
160                 .type = &ao_lisp_cons_type,
161                 .addr = (void **) &save_cons[1],
162         },
163         {
164                 .type = &ao_lisp_string_type,
165                 .addr = (void **) &save_string[0],
166         },
167         {
168                 .type = &ao_lisp_string_type,
169                 .addr = (void **) &save_string[1],
170         },
171         {
172                 .type = &ao_lisp_frame_type,
173                 .addr = (void **) &save_frame[0],
174         },
175         {
176                 .type = NULL,
177                 .addr = (void **) (void *) &save_poly[0]
178         },
179         {
180                 .type = NULL,
181                 .addr = (void **) (void *) &save_poly[1]
182         },
183         {
184                 .type = NULL,
185                 .addr = (void **) (void *) &save_poly[2]
186         },
187         {
188                 .type = &ao_lisp_atom_type,
189                 .addr = (void **) &ao_lisp_atoms
190         },
191         {
192                 .type = &ao_lisp_frame_type,
193                 .addr = (void **) &ao_lisp_frame_global,
194         },
195         {
196                 .type = &ao_lisp_frame_type,
197                 .addr = (void **) &ao_lisp_frame_current,
198         },
199         {
200                 .type = &ao_lisp_stack_type,
201                 .addr = (void **) &ao_lisp_stack,
202         },
203         {
204                 .type = NULL,
205                 .addr = (void **) (void *) &ao_lisp_v,
206         },
207         {
208                 .type = &ao_lisp_cons_type,
209                 .addr = (void **) &ao_lisp_read_cons,
210         },
211         {
212                 .type = &ao_lisp_cons_type,
213                 .addr = (void **) &ao_lisp_read_cons_tail,
214         },
215         {
216                 .type = &ao_lisp_cons_type,
217                 .addr = (void **) &ao_lisp_read_stack,
218         },
219 #ifdef AO_LISP_MAKE_CONST
220         {
221                 .type = &ao_lisp_bool_type,
222                 .addr = (void **) &ao_lisp_false,
223         },
224         {
225                 .type = &ao_lisp_bool_type,
226                 .addr = (void **) &ao_lisp_true,
227         },
228 #endif
229 };
230
231 #define AO_LISP_ROOT    (sizeof (ao_lisp_root) / sizeof (ao_lisp_root[0]))
232
233 static const void ** const ao_lisp_cache[] = {
234         (const void **) &ao_lisp_cons_free_list,
235         (const void **) &ao_lisp_stack_free_list,
236         (const void **) &ao_lisp_frame_free_list[0],
237         (const void **) &ao_lisp_frame_free_list[1],
238         (const void **) &ao_lisp_frame_free_list[2],
239         (const void **) &ao_lisp_frame_free_list[3],
240         (const void **) &ao_lisp_frame_free_list[4],
241         (const void **) &ao_lisp_frame_free_list[5],
242 };
243
244 #if AO_LISP_FRAME_FREE != 6
245 #error Unexpected AO_LISP_FRAME_FREE value
246 #endif
247
248 #define AO_LISP_CACHE   (sizeof (ao_lisp_cache) / sizeof (ao_lisp_cache[0]))
249
250 #define AO_LISP_BUSY_SIZE       ((AO_LISP_POOL + 31) / 32)
251
252 static uint8_t  ao_lisp_busy[AO_LISP_BUSY_SIZE];
253 static uint8_t  ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
254 static uint8_t  ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
255 static uint8_t  ao_lisp_cons_noted;
256
257 uint16_t        ao_lisp_top;
258
259 struct ao_lisp_chunk {
260         uint16_t                old_offset;
261         union {
262                 uint16_t        size;
263                 uint16_t        new_offset;
264         };
265 };
266
267 #define AO_LISP_NCHUNK  64
268
269 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
270
271 /* Offset of an address within the pool. */
272 static inline uint16_t pool_offset(void *addr) {
273 #if DBG_MEM
274         if (!AO_LISP_IS_POOL(addr))
275                 ao_lisp_abort();
276 #endif
277         return ((uint8_t *) addr) - ao_lisp_pool;
278 }
279
280 static inline void mark(uint8_t *tag, int offset) {
281         int     byte = offset >> 5;
282         int     bit = (offset >> 2) & 7;
283         tag[byte] |= (1 << bit);
284 }
285
286 static inline void clear(uint8_t *tag, int offset) {
287         int     byte = offset >> 5;
288         int     bit = (offset >> 2) & 7;
289         tag[byte] &= ~(1 << bit);
290 }
291
292 static inline int busy(uint8_t *tag, int offset) {
293         int     byte = offset >> 5;
294         int     bit = (offset >> 2) & 7;
295         return (tag[byte] >> bit) & 1;
296 }
297
298 static inline int min(int a, int b) { return a < b ? a : b; }
299 static inline int max(int a, int b) { return a > b ? a : b; }
300
301 static inline int limit(int offset) {
302         return min(AO_LISP_POOL, max(offset, 0));
303 }
304
305 static void
306 note_cons(uint16_t offset)
307 {
308         MDBG_MOVE("note cons %d\n", offset);
309         ao_lisp_cons_noted = 1;
310         mark(ao_lisp_cons_note, offset);
311 }
312
313 static uint16_t chunk_low, chunk_high;
314 static uint16_t chunk_first, chunk_last;
315
316 static int
317 find_chunk(uint16_t offset)
318 {
319         int l, r;
320         /* Binary search for the location */
321         l = chunk_first;
322         r = chunk_last - 1;
323         while (l <= r) {
324                 int m = (l + r) >> 1;
325                 if (ao_lisp_chunk[m].old_offset < offset)
326                         l = m + 1;
327                 else
328                         r = m - 1;
329         }
330         return l;
331 }
332
333 static void
334 note_chunk(uint16_t offset, uint16_t size)
335 {
336         int l;
337
338         if (offset < chunk_low || chunk_high <= offset)
339                 return;
340
341         l = find_chunk(offset);
342
343         /*
344          * The correct location is always in 'l', with r = l-1 being
345          * the entry before the right one
346          */
347
348 #if DBG_MEM
349         /* Off the right side */
350         if (l >= AO_LISP_NCHUNK)
351                 ao_lisp_abort();
352
353         /* Off the left side */
354         if (l == 0 && chunk_last && offset > ao_lisp_chunk[0].old_offset)
355                 ao_lisp_abort();
356 #endif
357
358         /* Shuffle existing entries right */
359         int end = min(AO_LISP_NCHUNK, chunk_last + 1);
360
361         memmove(&ao_lisp_chunk[l+1],
362                 &ao_lisp_chunk[l],
363                 (end - (l+1)) * sizeof (struct ao_lisp_chunk));
364
365         /* Add new entry */
366         ao_lisp_chunk[l].old_offset = offset;
367         ao_lisp_chunk[l].size = size;
368
369         /* Increment the number of elements up to the size of the array */
370         if (chunk_last < AO_LISP_NCHUNK)
371                 chunk_last++;
372
373         /* Set the top address if the array is full */
374         if (chunk_last == AO_LISP_NCHUNK)
375                 chunk_high = ao_lisp_chunk[AO_LISP_NCHUNK-1].old_offset +
376                         ao_lisp_chunk[AO_LISP_NCHUNK-1].size;
377 }
378
379 static void
380 reset_chunks(void)
381 {
382         chunk_high = ao_lisp_top;
383         chunk_last = 0;
384         chunk_first = 0;
385 }
386
387 /*
388  * Walk all referenced objects calling functions on each one
389  */
390
391 static void
392 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
393      int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
394 {
395         int i;
396
397         ao_lisp_record_reset();
398         memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
399         memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
400         ao_lisp_cons_noted = 0;
401         for (i = 0; i < (int) AO_LISP_ROOT; i++) {
402                 if (ao_lisp_root[i].type) {
403                         void **a = ao_lisp_root[i].addr, *v;
404                         if (a && (v = *a)) {
405                                 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
406                                 visit_addr(ao_lisp_root[i].type, a);
407                         }
408                 } else {
409                         ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
410                         if (a && (p = *a)) {
411                                 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
412                                 visit_poly(a, 0);
413                         }
414                 }
415         }
416         while (ao_lisp_cons_noted) {
417                 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
418                 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
419                 ao_lisp_cons_noted = 0;
420                 for (i = 0; i < AO_LISP_POOL; i += 4) {
421                         if (busy(ao_lisp_cons_last, i)) {
422                                 void *v = ao_lisp_pool + i;
423                                 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
424                                 visit_addr(&ao_lisp_cons_type, &v);
425                         }
426                 }
427         }
428 }
429
430 #if MDBG_DUMP
431 static void
432 dump_busy(void)
433 {
434         int     i;
435         MDBG_MOVE("busy:");
436         for (i = 0; i < ao_lisp_top; i += 4) {
437                 if ((i & 0xff) == 0) {
438                         MDBG_MORE("\n");
439                         MDBG_MOVE("%s", "");
440                 }
441                 else if ((i & 0x1f) == 0)
442                         MDBG_MORE(" ");
443                 if (busy(ao_lisp_busy, i))
444                         MDBG_MORE("*");
445                 else
446                         MDBG_MORE("-");
447         }
448         MDBG_MORE ("\n");
449 }
450 #define DUMP_BUSY()     dump_busy()
451 #else
452 #define DUMP_BUSY()
453 #endif
454
455 static const struct ao_lisp_type *ao_lisp_types[AO_LISP_NUM_TYPE] = {
456         [AO_LISP_CONS] = &ao_lisp_cons_type,
457         [AO_LISP_INT] = NULL,
458         [AO_LISP_STRING] = &ao_lisp_string_type,
459         [AO_LISP_OTHER] = (void *) 0x1,
460         [AO_LISP_ATOM] = &ao_lisp_atom_type,
461         [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
462         [AO_LISP_FRAME] = &ao_lisp_frame_type,
463         [AO_LISP_FRAME_VALS] = &ao_lisp_frame_vals_type,
464         [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
465         [AO_LISP_STACK] = &ao_lisp_stack_type,
466         [AO_LISP_BOOL] = &ao_lisp_bool_type,
467         [AO_LISP_BIGINT] = &ao_lisp_bigint_type,
468         [AO_LISP_FLOAT] = &ao_lisp_float_type,
469 };
470
471 static int
472 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
473 {
474         return ao_lisp_mark(type, *ref);
475 }
476
477 static int
478 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
479 {
480         return ao_lisp_poly_mark(*p, do_note_cons);
481 }
482
483 #if DBG_MEM_STATS
484 int ao_lisp_collects[2];
485 int ao_lisp_freed[2];
486 int ao_lisp_loops[2];
487 #endif
488
489 int ao_lisp_last_top;
490
491 int
492 ao_lisp_collect(uint8_t style)
493 {
494         int     i;
495         int     top;
496 #if DBG_MEM_STATS
497         int     loops = 0;
498 #endif
499 #if DBG_MEM
500         struct ao_lisp_record   *mark_record = NULL, *move_record = NULL;
501
502         MDBG_MOVE("collect %d\n", ao_lisp_collects[style]);
503 #endif
504
505         /* The first time through, we're doing a full collect */
506         if (ao_lisp_last_top == 0)
507                 style = AO_LISP_COLLECT_FULL;
508
509         /* Clear references to all caches */
510         for (i = 0; i < (int) AO_LISP_CACHE; i++)
511                 *ao_lisp_cache[i] = NULL;
512         if (style == AO_LISP_COLLECT_FULL) {
513                 chunk_low = top = 0;
514         } else {
515                 chunk_low = top = ao_lisp_last_top;
516         }
517         for (;;) {
518 #if DBG_MEM_STATS
519                 loops++;
520 #endif
521                 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
522                 /* Find the sizes of the first chunk of objects to move */
523                 reset_chunks();
524                 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
525 #if DBG_MEM
526
527                 ao_lisp_record_free(mark_record);
528                 mark_record = ao_lisp_record_save();
529                 if (mark_record && move_record)
530                         ao_lisp_record_compare("mark", move_record, mark_record);
531 #endif
532
533                 DUMP_BUSY();
534
535                 /* Find the first moving object */
536                 for (i = 0; i < chunk_last; i++) {
537                         uint16_t        size = ao_lisp_chunk[i].size;
538
539 #if DBG_MEM
540                         if (!size)
541                                 ao_lisp_abort();
542 #endif
543
544                         if (ao_lisp_chunk[i].old_offset > top)
545                                 break;
546
547                         MDBG_MOVE("chunk %d %d not moving\n",
548                                   ao_lisp_chunk[i].old_offset,
549                                   ao_lisp_chunk[i].size);
550 #if DBG_MEM
551                         if (ao_lisp_chunk[i].old_offset != top)
552                                 ao_lisp_abort();
553 #endif
554                         top += size;
555                 }
556
557                 /*
558                  * Limit amount of chunk array used in mapping moves
559                  * to the active region
560                  */
561                 chunk_first = i;
562                 chunk_low = ao_lisp_chunk[i].old_offset;
563
564                 /* Copy all of the objects */
565                 for (; i < chunk_last; i++) {
566                         uint16_t        size = ao_lisp_chunk[i].size;
567
568 #if DBG_MEM
569                         if (!size)
570                                 ao_lisp_abort();
571 #endif
572
573                         MDBG_MOVE("chunk %d %d -> %d\n",
574                                   ao_lisp_chunk[i].old_offset,
575                                   size,
576                                   top);
577                         ao_lisp_chunk[i].new_offset = top;
578
579                         memmove(&ao_lisp_pool[top],
580                                 &ao_lisp_pool[ao_lisp_chunk[i].old_offset],
581                                 size);
582
583                         top += size;
584                 }
585
586                 if (chunk_first < chunk_last) {
587                         /* Relocate all references to the objects */
588                         walk(ao_lisp_move, ao_lisp_poly_move);
589
590 #if DBG_MEM
591                         ao_lisp_record_free(move_record);
592                         move_record = ao_lisp_record_save();
593                         if (mark_record && move_record)
594                                 ao_lisp_record_compare("move", mark_record, move_record);
595 #endif
596                 }
597
598                 /* If we ran into the end of the heap, then
599                  * there's no need to keep walking
600                  */
601                 if (chunk_last != AO_LISP_NCHUNK)
602                         break;
603
604                 /* Next loop starts right above this loop */
605                 chunk_low = chunk_high;
606         }
607
608 #if DBG_MEM_STATS
609         /* Collect stats */
610         ++ao_lisp_collects[style];
611         ao_lisp_freed[style] += ao_lisp_top - top;
612         ao_lisp_loops[style] += loops;
613 #endif
614
615         ao_lisp_top = top;
616         if (style == AO_LISP_COLLECT_FULL)
617                 ao_lisp_last_top = top;
618
619         MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
620                 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
621
622         return AO_LISP_POOL - ao_lisp_top;
623 }
624
625 /*
626  * Mark interfaces for objects
627  */
628
629
630 /*
631  * Mark a block of memory with an explicit size
632  */
633
634 int
635 ao_lisp_mark_block(void *addr, int size)
636 {
637         int offset;
638         if (!AO_LISP_IS_POOL(addr))
639                 return 1;
640
641         offset = pool_offset(addr);
642         MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
643         if (busy(ao_lisp_busy, offset)) {
644                 MDBG_MOVE("already marked\n");
645                 return 1;
646         }
647         mark(ao_lisp_busy, offset);
648         note_chunk(offset, size);
649         return 0;
650 }
651
652 /*
653  * Note a reference to memory and collect information about a few
654  * object sizes at a time
655  */
656
657 int
658 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
659 {
660         int offset;
661         if (!AO_LISP_IS_POOL(addr))
662                 return 1;
663
664         offset = pool_offset(addr);
665         MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
666         if (busy(ao_lisp_busy, offset)) {
667                 MDBG_MOVE("already marked\n");
668                 return 1;
669         }
670         mark(ao_lisp_busy, offset);
671         note_chunk(offset, ao_lisp_size(type, addr));
672         return 0;
673 }
674
675 /*
676  * Mark an object and all that it refereces
677  */
678 int
679 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
680 {
681         int ret;
682         MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
683         MDBG_MOVE_IN();
684         ret = ao_lisp_mark_memory(type, addr);
685         if (!ret) {
686                 MDBG_MOVE("mark recurse\n");
687                 type->mark(addr);
688         }
689         MDBG_MOVE_OUT();
690         return ret;
691 }
692
693 /*
694  * Mark an object, unless it is a cons cell and
695  * do_note_cons is set. In that case, just
696  * set a bit in the cons note array; those
697  * will be marked in a separate pass to avoid
698  * deep recursion in the collector
699  */
700 int
701 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
702 {
703         uint8_t type;
704         void    *addr;
705
706         type = ao_lisp_poly_base_type(p);
707
708         if (type == AO_LISP_INT)
709                 return 1;
710
711         addr = ao_lisp_ref(p);
712         if (!AO_LISP_IS_POOL(addr))
713                 return 1;
714
715         if (type == AO_LISP_CONS && do_note_cons) {
716                 note_cons(pool_offset(addr));
717                 return 1;
718         } else {
719                 if (type == AO_LISP_OTHER)
720                         type = ao_lisp_other_type(addr);
721
722                 const struct ao_lisp_type *lisp_type = ao_lisp_types[type];
723 #if DBG_MEM
724                 if (!lisp_type)
725                         ao_lisp_abort();
726 #endif
727
728                 return ao_lisp_mark(lisp_type, addr);
729         }
730 }
731
732 /*
733  * Find the current location of an object
734  * based on the original location. For unmoved
735  * objects, this is simple. For moved objects,
736  * go search for it
737  */
738
739 static uint16_t
740 move_map(uint16_t offset)
741 {
742         int             l;
743
744         if (offset < chunk_low || chunk_high <= offset)
745                 return offset;
746
747         l = find_chunk(offset);
748
749 #if DBG_MEM
750         if (ao_lisp_chunk[l].old_offset != offset)
751                 ao_lisp_abort();
752 #endif
753         return ao_lisp_chunk[l].new_offset;
754 }
755
756 int
757 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
758 {
759         void            *addr = *ref;
760         uint16_t        offset, orig_offset;
761
762         if (!AO_LISP_IS_POOL(addr))
763                 return 1;
764
765         (void) type;
766
767         MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
768         orig_offset = pool_offset(addr);
769         offset = move_map(orig_offset);
770         if (offset != orig_offset) {
771                 MDBG_MOVE("update ref %d %d -> %d\n",
772                           AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
773                           orig_offset, offset);
774                 *ref = ao_lisp_pool + offset;
775         }
776         if (busy(ao_lisp_busy, offset)) {
777                 MDBG_MOVE("already moved\n");
778                 return 1;
779         }
780         mark(ao_lisp_busy, offset);
781         MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
782         return 0;
783 }
784
785 int
786 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
787 {
788         int ret;
789         MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
790         MDBG_MOVE_IN();
791         ret = ao_lisp_move_memory(type, ref);
792         if (!ret) {
793                 MDBG_MOVE("move recurse\n");
794                 type->move(*ref);
795         }
796         MDBG_MOVE_OUT();
797         return ret;
798 }
799
800 int
801 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
802 {
803         uint8_t         type;
804         ao_poly         p = *ref;
805         int             ret;
806         void            *addr;
807         uint16_t        offset, orig_offset;
808         uint8_t         base_type;
809
810         base_type = type = ao_lisp_poly_base_type(p);
811
812         if (type == AO_LISP_INT)
813                 return 1;
814
815         addr = ao_lisp_ref(p);
816         if (!AO_LISP_IS_POOL(addr))
817                 return 1;
818
819         orig_offset = pool_offset(addr);
820         offset = move_map(orig_offset);
821
822         if (type == AO_LISP_CONS && do_note_cons) {
823                 note_cons(orig_offset);
824                 ret = 1;
825         } else {
826                 if (type == AO_LISP_OTHER)
827                         type = ao_lisp_other_type(ao_lisp_pool + offset);
828
829                 const struct ao_lisp_type *lisp_type = ao_lisp_types[type];
830 #if DBG_MEM
831                 if (!lisp_type)
832                         ao_lisp_abort();
833 #endif
834
835                 ret = ao_lisp_move(lisp_type, &addr);
836         }
837
838         /* Re-write the poly value */
839         if (offset != orig_offset) {
840                 ao_poly np = ao_lisp_poly(ao_lisp_pool + offset, base_type);
841                 MDBG_MOVE("poly %d moved %d -> %d\n",
842                           type, orig_offset, offset);
843                 *ref = np;
844         }
845         return ret;
846 }
847
848 #if DBG_MEM
849 void
850 ao_lisp_validate(void)
851 {
852         chunk_low = 0;
853         memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
854         walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
855 }
856
857 int dbg_allocs;
858
859 #endif
860
861 void *
862 ao_lisp_alloc(int size)
863 {
864         void    *addr;
865
866         MDBG_DO(++dbg_allocs);
867         MDBG_DO(if (dbg_validate) ao_lisp_validate());
868         size = ao_lisp_size_round(size);
869         if (AO_LISP_POOL - ao_lisp_top < size &&
870             ao_lisp_collect(AO_LISP_COLLECT_INCREMENTAL) < size &&
871             ao_lisp_collect(AO_LISP_COLLECT_FULL) < size)
872         {
873                 ao_lisp_error(AO_LISP_OOM, "out of memory");
874                 return NULL;
875         }
876         addr = ao_lisp_pool + ao_lisp_top;
877         ao_lisp_top += size;
878         return addr;
879 }
880
881 void
882 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
883 {
884         save_cons[id] = cons;
885 }
886
887 struct ao_lisp_cons *
888 ao_lisp_cons_fetch(int id)
889 {
890         struct ao_lisp_cons *cons = save_cons[id];
891         save_cons[id] = NULL;
892         return cons;
893 }
894
895 void
896 ao_lisp_poly_stash(int id, ao_poly poly)
897 {
898         save_poly[id] = poly;
899 }
900
901 ao_poly
902 ao_lisp_poly_fetch(int id)
903 {
904         ao_poly poly = save_poly[id];
905         save_poly[id] = AO_LISP_NIL;
906         return poly;
907 }
908
909 void
910 ao_lisp_string_stash(int id, char *string)
911 {
912         save_string[id] = string;
913 }
914
915 char *
916 ao_lisp_string_fetch(int id)
917 {
918         char *string = save_string[id];
919         save_string[id] = NULL;
920         return string;
921 }
922
923 void
924 ao_lisp_frame_stash(int id, struct ao_lisp_frame *frame)
925 {
926         save_frame[id] = frame;
927 }
928
929 struct ao_lisp_frame *
930 ao_lisp_frame_fetch(int id)
931 {
932         struct ao_lisp_frame *frame = save_frame[id];
933         save_frame[id] = NULL;
934         return frame;
935 }