1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/*
* Copyright © 2016 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#define AO_LISP_CONST_BITS
#include "ao_lisp.h"
#include <stdio.h>
#ifdef AO_LISP_MAKE_CONST
#include <stdlib.h>
uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));
#define ao_lisp_pool ao_lisp_const
#undef AO_LISP_POOL
#define AO_LISP_POOL AO_LISP_POOL_CONST
#else
uint8_t ao_lisp_pool[AO_LISP_POOL] __attribute__((aligned(4)));
#endif
#if 0
#define DBG_DUMP
#define DBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
#define DBG(...) printf(__VA_ARGS__)
static int move_dump;
static int move_depth;
#define DBG_RESET() (move_depth = 0)
#define DBG_MOVE(...) do { if(move_dump) { int d; for (d = 0; d < move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
#define DBG_MOVE_IN() (move_depth++)
#define DBG_MOVE_OUT() (move_depth--)
#else
#define DBG(...)
#define DBG_RESET()
#define DBG_MOVE(...)
#define DBG_MOVE_IN()
#define DBG_MOVE_OUT()
#endif
uint8_t ao_lisp_exception;
struct ao_lisp_root {
void **addr;
const struct ao_lisp_type *type;
};
#define AO_LISP_ROOT 16
static struct ao_lisp_root ao_lisp_root[AO_LISP_ROOT];
static uint8_t ao_lisp_busy[AO_LISP_POOL / 32];
static uint8_t ao_lisp_moving[AO_LISP_POOL / 32];
uint16_t ao_lisp_top;
static inline void mark(uint8_t *tag, int offset) {
int byte = offset >> 5;
int bit = (offset >> 2) & 7;
tag[byte] |= (1 << bit);
}
static inline void clear(uint8_t *tag, int offset) {
int byte = offset >> 5;
int bit = (offset >> 2) & 7;
tag[byte] &= ~(1 << bit);
}
static inline int busy(uint8_t *tag, int offset) {
int byte = offset >> 5;
int bit = (offset >> 2) & 7;
return (tag[byte] >> bit) & 1;
}
static inline int min(int a, int b) { return a < b ? a : b; }
static inline int max(int a, int b) { return a > b ? a : b; }
static inline int limit(int offset) {
return min(AO_LISP_POOL, max(offset, 0));
}
static int
mark_object(uint8_t *tag, void *addr, int size) {
int base;
int bound;
if (!addr)
return 1;
if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
return 1;
base = (uint8_t *) addr - ao_lisp_pool;
bound = base + size;
base = limit(base);
bound = limit(bound);
if (busy(tag, base))
return 1;
while (base < bound) {
mark(tag, base);
base += 4;
}
return 0;
}
static int
clear_object(uint8_t *tag, void *addr, int size) {
int base;
int bound;
if (!addr)
return 1;
base = (uint8_t *) addr - ao_lisp_pool;
bound = base + size;
base = limit(base);
bound = limit(bound);
if (!busy(tag, base))
return 1;
while (base < bound) {
clear(tag, base);
base += 4;
}
return 0;
}
static int
busy_object(uint8_t *tag, void *addr) {
int base;
if (!addr)
return 1;
if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
return 1;
base = (uint8_t *) addr - ao_lisp_pool;
base = limit(base);
if (busy(tag, base))
return 1;
return 0;
}
static void *move_old, *move_new;
static int move_size;
static void
move_object(void)
{
int i;
DBG_RESET();
DBG_MOVE("move %d -> %d\n", DBG_OFFSET(move_old), DBG_OFFSET(move_new));
DBG_MOVE_IN();
memset(ao_lisp_moving, '\0', sizeof (ao_lisp_moving));
for (i = 0; i < AO_LISP_ROOT; i++)
if (ao_lisp_root[i].addr && *ao_lisp_root[i].addr) {
void *new;
DBG_MOVE("root %d\n", DBG_OFFSET(*ao_lisp_root[i].addr));
new = ao_lisp_move(ao_lisp_root[i].type, *ao_lisp_root[i].addr);
if (new)
*ao_lisp_root[i].addr = new;
}
DBG_MOVE_OUT();
DBG_MOVE("move done\n");
}
#ifdef DBG_DUMP
static void
dump_busy(void)
{
int i;
printf("busy:");
for (i = 0; i < ao_lisp_top; i += 4) {
if ((i & 0xff) == 0)
printf("\n");
else if ((i & 0x1f) == 0)
printf(" ");
if (busy(ao_lisp_busy, i))
putchar('*');
else
putchar('-');
}
printf ("\n");
}
#define DUMP_BUSY() dump_busy()
#else
#define DUMP_BUSY()
#endif
void
ao_lisp_collect(void)
{
int i;
int top;
/* Mark */
memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
DBG("mark\n");
for (i = 0; i < AO_LISP_ROOT; i++)
if (ao_lisp_root[i].addr && *ao_lisp_root[i].addr) {
DBG("root %p\n", *ao_lisp_root[i].addr);
ao_lisp_mark(ao_lisp_root[i].type, *ao_lisp_root[i].addr);
}
DUMP_BUSY();
/* Compact */
DBG("find first busy\n");
for (i = 0; i < ao_lisp_top; i += 4) {
if (!busy(ao_lisp_busy, i))
break;
}
top = i;
while(i < ao_lisp_top) {
if (busy(ao_lisp_busy, i)) {
DBG("busy %d -> %d\n", i, top);
move_old = &ao_lisp_pool[i];
move_new = &ao_lisp_pool[top];
move_size = 0;
move_object();
DBG("\tbusy size %d\n", move_size);
if (move_size == 0)
abort();
clear_object(ao_lisp_busy, move_old, move_size);
mark_object(ao_lisp_busy, move_new, move_size);
i += move_size;
top += move_size;
DUMP_BUSY();
} else {
i += 4;
}
}
ao_lisp_top = top;
}
void
ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
{
if (!addr)
return;
if (mark_object(ao_lisp_busy, addr, type->size(addr)))
return;
type->mark(addr);
}
int
ao_lisp_mark_memory(void *addr, int size)
{
return mark_object(ao_lisp_busy, addr, size);
}
/*
* After the object has been moved, we have to reference it
* in the new location. This is only relevant for ao_lisp_poly_move
* as it needs to fetch the type byte from the object, which
* may have been overwritten by the copy
*/
void *
ao_lisp_move_map(void *addr)
{
if (addr == move_old) {
if (busy_object(ao_lisp_moving, addr))
return move_new;
}
return addr;
}
static void *
check_move(void *addr, int size)
{
if (addr == move_old) {
DBG_MOVE("mapping %d -> %d\n", DBG_OFFSET(addr), DBG_OFFSET(move_new));
if (!busy_object(ao_lisp_moving, addr)) {
DBG_MOVE(" copy %d\n", size);
memmove(move_new, move_old, size);
move_size = (size + 3) & ~3;
}
addr = move_new;
}
return addr;
}
void *
ao_lisp_move(const struct ao_lisp_type *type, void *addr)
{
uint8_t *a = addr;
int size = type->size(addr);
if (!addr)
return NULL;
#ifndef AO_LISP_MAKE_CONST
if (AO_LISP_IS_CONST(addr))
return addr;
#endif
DBG_MOVE("object %d\n", DBG_OFFSET(addr));
if (a < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= a)
abort();
DBG_MOVE_IN();
addr = check_move(addr, size);
if (mark_object(ao_lisp_moving, addr, size)) {
DBG_MOVE("already moved\n");
DBG_MOVE_OUT();
return addr;
}
DBG_MOVE_OUT();
DBG_MOVE("recursing...\n");
DBG_MOVE_IN();
type->move(addr);
DBG_MOVE_OUT();
DBG_MOVE("done %d\n", DBG_OFFSET(addr));
return addr;
}
void *
ao_lisp_move_memory(void *addr, int size)
{
if (!addr)
return NULL;
DBG_MOVE("memory %d\n", DBG_OFFSET(addr));
DBG_MOVE_IN();
addr = check_move(addr, size);
if (mark_object(ao_lisp_moving, addr, size)) {
DBG_MOVE("already moved\n");
DBG_MOVE_OUT();
return addr;
}
DBG_MOVE_OUT();
return addr;
}
void *
ao_lisp_alloc(int size)
{
void *addr;
size = ao_lisp_mem_round(size);
if (ao_lisp_top + size > AO_LISP_POOL) {
ao_lisp_collect();
if (ao_lisp_top + size > AO_LISP_POOL) {
ao_lisp_exception |= AO_LISP_OOM;
return NULL;
}
}
addr = ao_lisp_pool + ao_lisp_top;
ao_lisp_top += size;
return addr;
}
int
ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
{
int i;
DBG("add root type %p addr %p\n", type, addr);
for (i = 0; i < AO_LISP_ROOT; i++) {
if (!ao_lisp_root[i].addr) {
ao_lisp_root[i].addr = addr;
ao_lisp_root[i].type = type;
return 1;
}
}
abort();
return 0;
}
void
ao_lisp_root_clear(void *addr)
{
int i;
for (i = 0; i < AO_LISP_ROOT; i++) {
if (ao_lisp_root[i].addr == addr) {
ao_lisp_root[i].addr = 0;
ao_lisp_root[i].type = 0;
break;
}
}
}
|