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 #define DBG(...) printf(__VA_ARGS__)
24 frame_num_size(int num)
26 return sizeof (struct ao_lisp_frame) + num * sizeof (struct ao_lisp_val);
30 frame_size(void *addr)
32 struct ao_lisp_frame *frame = addr;
33 return frame_num_size(frame->num);
36 #define OFFSET(a) ((int) ((uint8_t *) (ao_lisp_ref(a)) - ao_lisp_const))
39 frame_mark(void *addr)
41 struct ao_lisp_frame *frame = addr;
45 DBG("frame mark %p\n", frame);
46 if (!AO_LISP_IS_POOL(frame))
48 for (f = 0; f < frame->num; f++) {
49 struct ao_lisp_val *v = &frame->vals[f];
51 ao_lisp_poly_mark(v->val);
52 DBG ("\tframe mark atom %s %d val %d at %d\n",
53 ao_lisp_poly_atom(v->atom)->name,
54 OFFSET(v->atom), OFFSET(v->val), f);
56 frame = ao_lisp_poly_frame(frame->next);
57 DBG("frame next %p\n", frame);
60 if (ao_lisp_mark_memory(frame, frame_size(frame)))
66 frame_move(void *addr)
68 struct ao_lisp_frame *frame = addr;
72 DBG("frame move %p\n", frame);
73 if (!AO_LISP_IS_POOL(frame))
75 for (f = 0; f < frame->num; f++) {
76 struct ao_lisp_val *v = &frame->vals[f];
78 ao_lisp_poly_move(&v->atom);
79 DBG("moved atom %s\n", ao_lisp_poly_atom(v->atom)->name);
80 ao_lisp_poly_move(&v->val);
82 if (ao_lisp_poly_move(&frame->next))
84 frame = ao_lisp_poly_frame(frame->next);
88 const struct ao_lisp_type ao_lisp_frame_type = {
95 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom)
98 for (f = 0; f < frame->num; f++)
99 if (frame->vals[f].atom == atom)
100 return &frame->vals[f].val;
105 ao_lisp_frame_set(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val)
108 if (!AO_LISP_IS_CONST(frame)) {
109 ao_poly *ref = ao_lisp_frame_ref(frame, atom);
115 frame = ao_lisp_poly_frame(frame->next);
121 ao_lisp_frame_get(struct ao_lisp_frame *frame, ao_poly atom)
124 ao_poly *ref = ao_lisp_frame_ref(frame, atom);
127 frame = ao_lisp_poly_frame(frame->next);
132 struct ao_lisp_frame *
133 ao_lisp_frame_new(int num)
135 struct ao_lisp_frame *frame = ao_lisp_alloc(frame_num_size(num));
139 frame->type = AO_LISP_FRAME;
141 frame->next = AO_LISP_NIL;
142 memset(frame->vals, '\0', num * sizeof (struct ao_lisp_val));
146 static struct ao_lisp_frame *
147 ao_lisp_frame_realloc(struct ao_lisp_frame *frame, int new_num)
149 struct ao_lisp_frame *new;
152 if (new_num == frame->num)
154 new = ao_lisp_frame_new(new_num);
158 if (copy > frame->num)
160 memcpy(new->vals, frame->vals, copy * sizeof (struct ao_lisp_val));
162 new->next = frame->next;
166 struct ao_lisp_frame *
167 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val)
169 ao_poly *ref = frame ? ao_lisp_frame_ref(frame, atom) : NULL;
174 frame = ao_lisp_frame_realloc(frame, f + 1);
177 frame = ao_lisp_frame_new(1);
181 DBG ("add atom %s %d, val %d at %d\n", ao_lisp_poly_atom(atom)->name, OFFSET(atom), OFFSET(val), f);
182 frame->vals[f].atom = atom;
183 ref = &frame->vals[f].val;