altos/lisp: Share mark function for mark and move
[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 #include <stdlib.h>
22 uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));
23 #define ao_lisp_pool ao_lisp_const
24 #undef AO_LISP_POOL
25 #define AO_LISP_POOL AO_LISP_POOL_CONST
26 #else
27 uint8_t ao_lisp_pool[AO_LISP_POOL] __attribute__((aligned(4)));
28 #endif
29
30 #if 0
31 #define MDBG_COLLECT_ALWAYS
32 #endif
33
34 #if 0
35 #define MDBG_POOL
36 #endif
37
38 #if 0
39 #define MDBG_INCLUDE
40 #define MDBG_DUMP       1
41 #define MDBG_MOVE       0
42 #define MDBG_OFFSET(a)  ((int) ((uint8_t *) (a) - ao_lisp_pool))
43 #define MDBG(...) printf(__VA_ARGS__)
44 #define MDBG_DO(a)      a
45 static int move_dump = MDBG_MOVE;
46 static int move_depth;
47 #define MDBG_RESET() (move_depth = 0)
48 #define MDBG_MOVE(...) do { if(move_dump) { int d; for (d = 0; d < move_depth; d++) printf ("  "); printf(__VA_ARGS__); } } while (0)
49 #define MDBG_MOVE_IN()  (move_depth++)
50 #define MDBG_MOVE_OUT() (move_depth--)
51 #else
52 #define MDBG(...)
53 #define MDBG_DO(a)
54 #define MDBG_RESET()
55 #define MDBG_MOVE(...)
56 #define MDBG_MOVE_IN()
57 #define MDBG_MOVE_OUT()
58 #endif
59
60 uint8_t ao_lisp_exception;
61
62 struct ao_lisp_root {
63         void                            **addr;
64         const struct ao_lisp_type       *type;
65 };
66
67 #define AO_LISP_ROOT    16
68
69 static struct ao_lisp_root      ao_lisp_root[AO_LISP_ROOT];
70
71 static uint8_t  ao_lisp_busy[AO_LISP_POOL / 32];
72 static uint8_t  ao_lisp_moving[AO_LISP_POOL / 32];
73 static uint8_t  ao_lisp_cons_note[AO_LISP_POOL / 32];
74 static uint8_t  ao_lisp_cons_last[AO_LISP_POOL / 32];
75 static uint8_t  ao_lisp_cons_noted;
76
77 uint16_t        ao_lisp_top;
78
79 static inline void mark(uint8_t *tag, int offset) {
80         int     byte = offset >> 5;
81         int     bit = (offset >> 2) & 7;
82         tag[byte] |= (1 << bit);
83 }
84
85 static inline void clear(uint8_t *tag, int offset) {
86         int     byte = offset >> 5;
87         int     bit = (offset >> 2) & 7;
88         tag[byte] &= ~(1 << bit);
89 }
90
91 static inline int busy(uint8_t *tag, int offset) {
92         int     byte = offset >> 5;
93         int     bit = (offset >> 2) & 7;
94         return (tag[byte] >> bit) & 1;
95 }
96
97 static inline int min(int a, int b) { return a < b ? a : b; }
98 static inline int max(int a, int b) { return a > b ? a : b; }
99
100 static inline int limit(int offset) {
101         return min(AO_LISP_POOL, max(offset, 0));
102 }
103
104 static int
105 mark_object(uint8_t *tag, void *addr, int size) {
106         int     base;
107         int     bound;
108
109         if (!addr)
110                 return 1;
111
112         if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
113                 return 1;
114
115         base = (uint8_t *) addr - ao_lisp_pool;
116         bound = base + size;
117
118         base = limit(base);
119         bound = limit(bound);
120         if (busy(tag, base))
121                 return 1;
122         while (base < bound) {
123                 mark(tag, base);
124                 base += 4;
125         }
126         return 0;
127 }
128
129 static int
130 clear_object(uint8_t *tag, void *addr, int size) {
131         int     base;
132         int     bound;
133         if (!addr)
134                 return 1;
135
136         base = (uint8_t *) addr - ao_lisp_pool;
137         bound = base + size;
138
139         base = limit(base);
140         bound = limit(bound);
141         if (!busy(tag, base))
142                 return 1;
143         while (base < bound) {
144                 clear(tag, base);
145                 base += 4;
146         }
147         return 0;
148 }
149
150 static int
151 busy_object(uint8_t *tag, void *addr) {
152         int     base;
153
154         if (!addr)
155                 return 1;
156
157         if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
158                 return 1;
159
160         base = (uint8_t *) addr - ao_lisp_pool;
161         base = limit(base);
162         if (busy(tag, base))
163                 return 1;
164         return 0;
165 }
166
167 static void
168 note_cons(void *addr)
169 {
170         MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
171         if (AO_LISP_IS_POOL(addr)) {
172                 int     offset = (uint8_t *) addr - ao_lisp_pool;
173                 ao_lisp_cons_noted = 1;
174                 mark(ao_lisp_cons_note, offset);
175         }
176 }
177
178 /*
179  * Walk all referenced objects calling functions on each one
180  */
181
182 static void
183 walk_all(uint8_t *tag,
184          int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
185          int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
186 {
187         int i;
188
189         memset(tag, '\0', sizeof (ao_lisp_busy));
190         memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
191         ao_lisp_cons_noted = 0;
192         for (i = 0; i < AO_LISP_ROOT; i++) {
193                 if (ao_lisp_root[i].type) {
194                         void **a = ao_lisp_root[i].addr, *v;
195                         if (a && (v = *a)) {
196                                 MDBG("root %d\n", MDBG_OFFSET(v));
197                                 visit_addr(ao_lisp_root[i].type, a);
198                         }
199                 } else {
200                         ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
201                         if (a && (p = *a)) {
202                                 MDBG("root 0x%04x\n", p);
203                                 visit_poly(a, 0);
204                         }
205                 }
206         }
207         while (ao_lisp_cons_noted) {
208                 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
209                 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
210                 ao_lisp_cons_noted = 0;
211                 for (i = 0; i < AO_LISP_POOL; i += 4) {
212                         if (busy(ao_lisp_cons_last, i)) {
213                                 void *v = ao_lisp_pool + i;
214                                 MDBG("cons %d\n", MDBG_OFFSET(v));
215                                 visit_addr(&ao_lisp_cons_type, &v);
216                         }
217                 }
218         }
219 }
220
221 static void     *move_old, *move_new;
222 static int      move_size;
223
224 static void
225 move_object(void)
226 {
227         walk_all(ao_lisp_moving, ao_lisp_move, ao_lisp_poly_move);
228 }
229
230 #if MDBG_DUMP
231 static void
232 dump_busy(void)
233 {
234         int     i;
235         printf("busy:");
236         for (i = 0; i < ao_lisp_top; i += 4) {
237                 if ((i & 0xff) == 0)
238                         printf("\n");
239                 else if ((i & 0x1f) == 0)
240                         printf(" ");
241                 if (busy(ao_lisp_busy, i))
242                         putchar('*');
243                 else
244                         putchar('-');
245         }
246         printf ("\n");
247 }
248 #define DUMP_BUSY()     dump_busy()
249 #else
250 #define DUMP_BUSY()
251 #endif
252
253 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
254         [AO_LISP_CONS] = &ao_lisp_cons_type,
255         [AO_LISP_INT] = NULL,
256         [AO_LISP_STRING] = &ao_lisp_string_type,
257         [AO_LISP_OTHER] = (void *) 0x1,
258         [AO_LISP_ATOM] = &ao_lisp_atom_type,
259         [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
260         [AO_LISP_FRAME] = &ao_lisp_frame_type,
261         [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
262 };
263
264 static int
265 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
266 {
267         return ao_lisp_mark(type, *ref);
268 }
269
270 static int
271 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
272 {
273         return ao_lisp_poly_mark(*p, do_note_cons);
274 }
275
276 static void
277 ao_lisp_mark_busy(void)
278 {
279         walk_all(ao_lisp_busy, ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
280 }
281
282 void
283 ao_lisp_collect(void)
284 {
285         int     i;
286         int     top;
287
288         MDBG("collect\n");
289         /* Mark */
290         ao_lisp_mark_busy();
291
292         DUMP_BUSY();
293         /* Compact */
294         MDBG("find first busy\n");
295         for (i = 0; i < ao_lisp_top; i += 4) {
296                 if (!busy(ao_lisp_busy, i))
297                         break;
298         }
299         top = i;
300         while(i < ao_lisp_top) {
301                 if (busy(ao_lisp_busy, i)) {
302                         MDBG("busy %d -> %d\n", i, top);
303                         move_old = &ao_lisp_pool[i];
304                         move_new = &ao_lisp_pool[top];
305                         move_size = 0;
306                         move_object();
307                         MDBG("\tbusy size %d\n", move_size);
308                         if (move_size == 0)
309                                 ao_lisp_abort();
310                         clear_object(ao_lisp_busy, move_old, move_size);
311                         mark_object(ao_lisp_busy, move_new, move_size);
312                         if (busy_object(ao_lisp_cons_note, move_old)) {
313                                 clear_object(ao_lisp_cons_note, move_old, move_size);
314                                 mark_object(ao_lisp_cons_note, move_new, move_size);
315                         }
316                         i += move_size;
317                         top += move_size;
318 #if MDBG_MOVE
319                         DUMP_BUSY();
320 #endif
321                 } else {
322                         i += 4;
323                 }
324         }
325         ao_lisp_top = top;
326 }
327
328
329 int
330 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
331 {
332         if (!addr)
333                 return 1;
334         if (mark_object(ao_lisp_busy, addr, type->size(addr)))
335                 return 1;
336         type->mark(addr);
337         return 0;
338 }
339
340 int
341 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
342 {
343         uint8_t type = ao_lisp_poly_type(p);
344
345         if (!p)
346                 return 1;
347         if (type == AO_LISP_CONS && do_note_cons) {
348                 note_cons(ao_lisp_ref(p));
349                 return 0;
350         } else {
351                 const struct ao_lisp_type *lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
352                 if (lisp_type)
353                         return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
354                 return 1;
355         }
356 }
357
358 int
359 ao_lisp_mark_memory(void *addr, int size)
360 {
361         return mark_object(ao_lisp_busy, addr, size);
362 }
363
364 /*
365  * After the object has been moved, we have to reference it
366  * in the new location. This is only relevant for ao_lisp_poly_move
367  * as it needs to fetch the type byte from the object, which
368  * may have been overwritten by the copy
369  */
370 void *
371 ao_lisp_move_map(void *addr)
372 {
373         if (addr == move_old) {
374                 if (busy_object(ao_lisp_moving, addr))
375                         return move_new;
376         }
377         return addr;
378 }
379
380 static void *
381 check_move(void *addr, int size)
382 {
383         if (addr == move_old) {
384                 MDBG_MOVE("mapping %d -> %d\n", MDBG_OFFSET(addr), MDBG_OFFSET(move_new));
385                 if (!busy_object(ao_lisp_moving, addr)) {
386                         MDBG_MOVE("  copy %d\n", size);
387                         memmove(move_new, move_old, size);
388                         move_size = (size + 3) & ~3;
389                 }
390                 addr = move_new;
391         }
392         return addr;
393 }
394
395 int
396 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
397 {
398         void            *addr = *ref;
399         uint8_t         *a = addr;
400         int             size = type->size(addr);
401
402         if (!addr)
403                 return 1;
404
405 #ifndef AO_LISP_MAKE_CONST
406         if (AO_LISP_IS_CONST(addr))
407                 return 1;
408 #endif
409         MDBG_MOVE("object %d\n", MDBG_OFFSET(addr));
410         if (!AO_LISP_IS_POOL(a))
411                 ao_lisp_abort();
412         MDBG_MOVE_IN();
413         addr = check_move(addr, size);
414         if (addr != *ref)
415                 *ref = addr;
416         if (mark_object(ao_lisp_moving, addr, size)) {
417                 MDBG_MOVE("already moved\n");
418                 MDBG_MOVE_OUT();
419                 return 1;
420         }
421         MDBG_MOVE_OUT();
422         MDBG_MOVE("recursing...\n");
423         MDBG_MOVE_IN();
424         type->move(addr);
425         MDBG_MOVE_OUT();
426         MDBG_MOVE("done %d\n", MDBG_OFFSET(addr));
427         return 0;
428 }
429
430 int
431 ao_lisp_move_memory(void **ref, int size)
432 {
433         void *addr = *ref;
434         if (!addr)
435                 return 1;
436
437         MDBG_MOVE("memory %d\n", MDBG_OFFSET(addr));
438         MDBG_MOVE_IN();
439         addr = check_move(addr, size);
440         if (addr != *ref)
441                 *ref = addr;
442         if (mark_object(ao_lisp_moving, addr, size)) {
443                 MDBG_MOVE("already moved\n");
444                 MDBG_MOVE_OUT();
445                 return 1;
446         }
447         MDBG_MOVE_OUT();
448         return 0;
449 }
450
451 int
452 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
453 {
454         uint8_t                         type;
455         ao_poly                         p = *ref;
456         const struct ao_lisp_type       *lisp_type;
457         int                             ret;
458         void                            *addr;
459
460         if (!p)
461                 return 1;
462
463         type = ao_lisp_poly_base_type(p);
464         addr = ao_lisp_ref(p);
465         if (type == AO_LISP_CONS && do_note_cons) {
466                 note_cons(addr);
467                 addr = check_move(addr, sizeof (struct ao_lisp_cons));
468                 ret = 1;
469         } else {
470
471                 if (type == AO_LISP_OTHER)
472                         type = ao_lisp_other_type(ao_lisp_move_map(ao_lisp_poly_other(p)));
473
474                 if (type >= AO_LISP_NUM_TYPE)
475                         ao_lisp_abort();
476
477                 lisp_type = ao_lisp_types[type];
478                 if (!lisp_type)
479                         return 1;
480                 ret = ao_lisp_move(lisp_type, &addr);
481         }
482
483         if (addr != ao_lisp_ref(p)) {
484                 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
485                 MDBG("poly %d moved %04x -> %04x\n",
486                     type, p, np);
487                 *ref = np;
488         }
489         return ret;
490 }
491
492 #ifdef MDBG_POOL
493 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
494
495 static void
496 ao_lisp_poison(void)
497 {
498         int     i;
499
500         printf("poison\n");
501         ao_lisp_mark_busy();
502         for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
503                 uint32_t        *a = (uint32_t *) &ao_lisp_pool[i];
504                 if (!busy_object(ao_lisp_busy, a))
505                         *a = 0xBEEFBEEF;
506         }
507         for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
508                 ao_poly         *a = (uint16_t *) &ao_lisp_pool[i];
509                 ao_poly         p = *a;
510
511                 if (!ao_lisp_is_const(p)) {
512                         void    *r = ao_lisp_ref(p);
513
514                         if (ao_lisp_pool <= (uint8_t *) r &&
515                             (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
516                         {
517                                 if (!busy_object(ao_lisp_busy, r)) {
518                                         printf("missing reference from %d to %d\n",
519                                                (int) ((uint8_t *) a - ao_lisp_pool),
520                                                (int) ((uint8_t *) r - ao_lisp_pool));
521                                 }
522                         }
523                 }
524         }
525 }
526
527 #else
528 #define AO_LISP_POOL_CUR AO_LISP_POOL
529 #endif
530
531 void *
532 ao_lisp_alloc(int size)
533 {
534         void    *addr;
535
536         size = ao_lisp_mem_round(size);
537 #ifdef MDBG_COLLECT_ALWAYS
538         ao_lisp_collect();
539 #endif
540         if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
541 #ifdef MDBG_POOL
542                 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
543                         AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
544                         ao_lisp_poison();
545                 } else
546 #endif
547                 ao_lisp_collect();
548 #ifdef MDBG_POOL
549                 {
550                         int     i;
551
552                         for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
553                                 uint32_t        *p = (uint32_t *) &ao_lisp_pool[i];
554                                 *p = 0xbeefbeef;
555                         }
556                 }
557 #endif
558
559                 if (ao_lisp_top + size > AO_LISP_POOL) {
560                         ao_lisp_error(AO_LISP_OOM, "out of memory");
561                         return NULL;
562                 }
563         }
564         addr = ao_lisp_pool + ao_lisp_top;
565         ao_lisp_top += size;
566         return addr;
567 }
568
569 int
570 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
571 {
572         int     i;
573         MDBG("add root type %p addr %p\n", type, addr);
574         for (i = 0; i < AO_LISP_ROOT; i++) {
575                 if (!ao_lisp_root[i].addr) {
576                         ao_lisp_root[i].addr = addr;
577                         ao_lisp_root[i].type = type;
578                         return 1;
579                 }
580         }
581         ao_lisp_abort();
582         return 0;
583 }
584
585 int
586 ao_lisp_root_poly_add(ao_poly *p)
587 {
588         return ao_lisp_root_add(NULL, p);
589 }
590
591 void
592 ao_lisp_root_clear(void *addr)
593 {
594         int     i;
595         for (i = 0; i < AO_LISP_ROOT; i++) {
596                 if (ao_lisp_root[i].addr == addr) {
597                         ao_lisp_root[i].addr = 0;
598                         ao_lisp_root[i].type = 0;
599                         break;
600                 }
601         }
602 }