2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
18 frame_num_size(int num)
20 return sizeof (struct ao_lisp_frame) + num * sizeof (struct ao_lisp_val);
24 frame_size(void *addr)
26 struct ao_lisp_frame *frame = addr;
27 return frame_num_size(ao_lisp_frame_num(frame));
31 frame_mark(void *addr)
33 struct ao_lisp_frame *frame = addr;
37 MDBG_MOVE("frame mark %d\n", MDBG_OFFSET(frame));
38 if (!AO_LISP_IS_POOL(frame))
40 for (f = 0; f < ao_lisp_frame_num(frame); f++) {
41 struct ao_lisp_val *v = &frame->vals[f];
43 ao_lisp_poly_mark(v->val, 0);
44 MDBG_MOVE("frame mark atom %s %d val %d at %d\n",
45 ao_lisp_poly_atom(v->atom)->name,
46 MDBG_OFFSET(ao_lisp_ref(v->atom)),
47 MDBG_OFFSET(ao_lisp_ref(v->val)), f);
49 frame = ao_lisp_poly_frame(frame->prev);
50 MDBG_MOVE("frame next %d\n", MDBG_OFFSET(frame));
53 if (ao_lisp_mark_memory(&ao_lisp_frame_type, frame))
59 frame_move(void *addr)
61 struct ao_lisp_frame *frame = addr;
65 struct ao_lisp_frame *prev;
68 MDBG_MOVE("frame move %d\n", MDBG_OFFSET(frame));
69 if (!AO_LISP_IS_POOL(frame))
71 for (f = 0; f < ao_lisp_frame_num(frame); f++) {
72 struct ao_lisp_val *v = &frame->vals[f];
74 ao_lisp_poly_move(&v->atom, 0);
75 ao_lisp_poly_move(&v->val, 0);
76 MDBG_MOVE("frame move atom %s %d val %d at %d\n",
77 ao_lisp_poly_atom(v->atom)->name,
78 MDBG_OFFSET(ao_lisp_ref(v->atom)),
79 MDBG_OFFSET(ao_lisp_ref(v->val)), f);
81 prev = ao_lisp_poly_frame(frame->prev);
84 ret = ao_lisp_move_memory(&ao_lisp_frame_type, (void **) &prev);
85 if (prev != ao_lisp_poly_frame(frame->prev)) {
86 MDBG_MOVE("frame prev moved from %d to %d\n",
87 MDBG_OFFSET(ao_lisp_poly_frame(frame->prev)),
89 frame->prev = ao_lisp_frame_poly(prev);
97 const struct ao_lisp_type ao_lisp_frame_type = {
105 ao_lisp_frame_print(ao_poly p)
107 struct ao_lisp_frame *frame = ao_lisp_poly_frame(p);
112 for (f = 0; f < ao_lisp_frame_num(frame); f++) {
115 ao_lisp_poly_print(frame->vals[f].atom);
117 ao_lisp_poly_print(frame->vals[f].val);
120 ao_lisp_poly_print(frame->prev);
126 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom)
129 for (f = 0; f < ao_lisp_frame_num(frame); f++)
130 if (frame->vals[f].atom == atom)
131 return &frame->vals[f].val;
136 ao_lisp_frame_set(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val)
139 if (!AO_LISP_IS_CONST(frame)) {
140 ao_poly *ref = ao_lisp_frame_ref(frame, atom);
146 frame = ao_lisp_poly_frame(frame->prev);
152 ao_lisp_frame_get(struct ao_lisp_frame *frame, ao_poly atom)
155 ao_poly *ref = ao_lisp_frame_ref(frame, atom);
158 frame = ao_lisp_poly_frame(frame->prev);
163 struct ao_lisp_frame *ao_lisp_frame_free_list[AO_LISP_FRAME_FREE];
165 struct ao_lisp_frame *
166 ao_lisp_frame_new(int num)
168 struct ao_lisp_frame *frame;
170 if (num < AO_LISP_FRAME_FREE && (frame = ao_lisp_frame_free_list[num]))
171 ao_lisp_frame_free_list[num] = ao_lisp_poly_frame(frame->prev);
173 frame = ao_lisp_alloc(frame_num_size(num));
177 frame->type = AO_LISP_FRAME;
179 frame->prev = AO_LISP_NIL;
180 memset(frame->vals, '\0', num * sizeof (struct ao_lisp_val));
185 ao_lisp_frame_mark(struct ao_lisp_frame *frame)
189 if (frame->_num == 0xff)
191 frame->_num |= AO_LISP_FRAME_MARK;
192 return ao_lisp_frame_poly(frame);
196 ao_lisp_frame_free(struct ao_lisp_frame *frame)
198 if (!ao_lisp_frame_marked(frame)) {
199 int num = ao_lisp_frame_num(frame);
200 if (num < AO_LISP_FRAME_FREE) {
201 frame->prev = ao_lisp_frame_poly(ao_lisp_frame_free_list[num]);
202 ao_lisp_frame_free_list[num] = frame;
207 static struct ao_lisp_frame *
208 ao_lisp_frame_realloc(struct ao_lisp_frame **frame_ref, int new_num)
210 struct ao_lisp_frame *frame = *frame_ref;
211 struct ao_lisp_frame *new;
214 if (new_num == ao_lisp_frame_num(frame))
216 new = ao_lisp_frame_new(new_num);
220 * Re-fetch the frame as it may have moved
221 * during the allocation
225 if (copy > ao_lisp_frame_num(frame))
226 copy = ao_lisp_frame_num(frame);
227 memcpy(new->vals, frame->vals, copy * sizeof (struct ao_lisp_val));
228 new->prev = frame->prev;
229 ao_lisp_frame_free(frame);
234 ao_lisp_frame_add(struct ao_lisp_frame **frame_ref, ao_poly atom, ao_poly val)
236 struct ao_lisp_frame *frame = *frame_ref;
237 ao_poly *ref = frame ? ao_lisp_frame_ref(frame, atom) : NULL;
241 ao_lisp_poly_stash(0, atom);
242 ao_lisp_poly_stash(1, val);
244 f = ao_lisp_frame_num(frame);
245 frame = ao_lisp_frame_realloc(frame_ref, f + 1);
248 frame = ao_lisp_frame_new(1);
250 atom = ao_lisp_poly_fetch(0);
251 val = ao_lisp_poly_fetch(1);
255 frame->vals[f].atom = atom;
256 ref = &frame->vals[f].val;