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(frame->num);
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 < frame->num; 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->next);
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 *next;
68 MDBG_MOVE("frame move %d\n", MDBG_OFFSET(frame));
69 if (!AO_LISP_IS_POOL(frame))
71 for (f = 0; f < frame->num; 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 next = ao_lisp_poly_frame(frame->next);
84 ret = ao_lisp_move_memory(&ao_lisp_frame_type, (void **) &next);
85 if (next != ao_lisp_poly_frame(frame->next)) {
86 MDBG_MOVE("frame next moved from %d to %d\n",
87 MDBG_OFFSET(ao_lisp_poly_frame(frame->next)),
89 frame->next = ao_lisp_frame_poly(next);
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 < frame->num; 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->next);
126 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom)
129 for (f = 0; f < frame->num; 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->next);
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->next);
163 struct ao_lisp_frame *
164 ao_lisp_frame_new(int num)
166 struct ao_lisp_frame *frame = ao_lisp_alloc(frame_num_size(num));
170 frame->type = AO_LISP_FRAME;
172 frame->next = AO_LISP_NIL;
173 memset(frame->vals, '\0', num * sizeof (struct ao_lisp_val));
177 static struct ao_lisp_frame *
178 ao_lisp_frame_realloc(struct ao_lisp_frame **frame_ref, int new_num)
180 struct ao_lisp_frame *frame = *frame_ref;
181 struct ao_lisp_frame *new;
184 if (new_num == frame->num)
186 new = ao_lisp_frame_new(new_num);
190 * Re-fetch the frame as it may have moved
191 * during the allocation
195 if (copy > frame->num)
197 memcpy(new->vals, frame->vals, copy * sizeof (struct ao_lisp_val));
198 new->next = frame->next;
203 ao_lisp_frame_add(struct ao_lisp_frame **frame_ref, ao_poly atom, ao_poly val)
205 struct ao_lisp_frame *frame = *frame_ref;
206 ao_poly *ref = frame ? ao_lisp_frame_ref(frame, atom) : NULL;
210 ao_lisp_poly_stash(0, atom);
211 ao_lisp_poly_stash(1, val);
214 frame = ao_lisp_frame_realloc(frame_ref, f + 1);
217 frame = ao_lisp_frame_new(1);
219 atom = ao_lisp_poly_fetch(0);
220 val = ao_lisp_poly_fetch(1);
224 frame->vals[f].atom = atom;
225 ref = &frame->vals[f].val;