altos/lisp: Make sure memmove only happens once per object. Other GC fixes
[fw/altos] / src / lisp / ao_lisp_cons.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 #include "ao_lisp.h"
16
17 #define OFFSET(a)       ((int) ((uint8_t *) (a) - ao_lisp_const))
18
19 static void cons_mark(void *addr)
20 {
21         struct ao_lisp_cons     *cons = addr;
22
23         for (;;) {
24                 ao_lisp_poly_mark(cons->car, 1);
25                 cons = ao_lisp_poly_cons(cons->cdr);
26                 if (!cons)
27                         break;
28                 if (ao_lisp_mark_memory(cons, sizeof (struct ao_lisp_cons)))
29                         break;
30         }
31 }
32
33 static int cons_size(void *addr)
34 {
35         (void) addr;
36         return sizeof (struct ao_lisp_cons);
37 }
38
39 static void cons_move(void *addr)
40 {
41         struct ao_lisp_cons     *cons = addr;
42
43         if (!cons)
44                 return;
45
46         for (;;) {
47                 struct ao_lisp_cons     *cdr;
48                 int                     ret;
49
50                 (void) ao_lisp_poly_move(&cons->car, 1);
51                 cdr = ao_lisp_poly_cons(cons->cdr);
52                 if (!cdr)
53                         break;
54                 ret = ao_lisp_move_memory((void **) &cdr, sizeof (struct ao_lisp_cons));
55                 if (cdr != ao_lisp_poly_cons(cons->cdr))
56                         cons->cdr = ao_lisp_cons_poly(cdr);
57                 if (ret)
58                         break;
59                 cons = cdr;
60         }
61 }
62
63 const struct ao_lisp_type ao_lisp_cons_type = {
64         .mark = cons_mark,
65         .size = cons_size,
66         .move = cons_move,
67 };
68
69 static ao_poly  cons_car;
70 static struct ao_lisp_cons *cons_cdr;
71 static int been_here;
72
73 struct ao_lisp_cons *
74 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr)
75 {
76         struct ao_lisp_cons     *cons;
77
78         if (!been_here) {
79                 ao_lisp_root_add(&ao_lisp_cons_type, &cons_cdr);
80                 ao_lisp_root_poly_add(&cons_car);
81                 been_here = 1;
82         }
83         cons_car = car;
84         cons_cdr = cdr;
85         cons = ao_lisp_alloc(sizeof (struct ao_lisp_cons));
86         if (!cons)
87                 return NULL;
88         cons->car = cons_car;
89         cons->cdr = ao_lisp_cons_poly(cons_cdr);
90         cons_car = AO_LISP_NIL;
91         cons_cdr = NULL;
92         return cons;
93 }
94
95 void
96 ao_lisp_cons_print(ao_poly c)
97 {
98         struct ao_lisp_cons *cons = ao_lisp_poly_cons(c);
99         int     first = 1;
100         printf("(");
101         while (cons) {
102                 if (!first)
103                         printf(" ");
104                 ao_lisp_poly_print(cons->car);
105                 cons = ao_lisp_poly_cons(cons->cdr);
106                 first = 0;
107         }
108         printf(")");
109 }
110
111 void
112 ao_lisp_cons_patom(ao_poly c)
113 {
114         struct ao_lisp_cons *cons = ao_lisp_poly_cons(c);
115
116         while (cons) {
117                 ao_lisp_poly_patom(cons->car);
118                 cons = ao_lisp_poly_cons(cons->cdr);
119         }
120 }
121
122 int
123 ao_lisp_cons_length(struct ao_lisp_cons *cons)
124 {
125         int     len = 0;
126         while (cons) {
127                 len++;
128                 cons = ao_lisp_poly_cons(cons->cdr);
129         }
130         return len;
131 }