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