4 * Copyright 2004-2009 by Marc Feeley and Vincent St-Amour, All Rights Reserved.
7 #include "picobit-vm.h"
9 void init_ram_heap () {
11 obj o
= MAX_RAM_ENCODING
;
12 uint16 bound
= MIN_RAM_ENCODING
+ ((glovars
+ 1) >> 1);
17 // we don't want to add globals to the free list, and globals occupy the
18 // beginning of memory at the rate of 2 globals per word (car and cdr)
19 ram_set_gc_tags (o
, GC_TAG_UNMARKED
);
20 ram_set_car (o
, free_list
);
25 free_list_vec
= MIN_VEC_ENCODING
;
26 ram_set_car (free_list_vec
, 0);
27 // each node of the free list must know the free length that follows it
28 // this free length is stored in words, not in bytes
29 // if we did count in bytes, the number might need more than 13 bits
30 ram_set_cdr (free_list_vec
, (VEC_BYTES
>> 2));
32 for (i
=0; i
<glovars
; i
++)
33 set_global (i
, OBJ_FALSE
);
44 void mark (obj temp
) {
58 IF_GC_TRACE(printf ("push stack=%d visit=%d (tag=%d)\n", stack
, visit
, ram_get_gc_tags (visit
)>>5));
60 if ((HAS_1_OBJECT_FIELD (visit
) && ram_get_gc_tag0 (visit
))
61 || (HAS_2_OBJECT_FIELDS (visit
)
62 && (ram_get_gc_tags (visit
) != GC_TAG_UNMARKED
)))
63 IF_GC_TRACE(printf ("case 1\n"));
65 if (HAS_2_OBJECT_FIELDS(visit
)) { // pairs and continuations
66 IF_GC_TRACE(printf ("case 2\n"));
70 temp
= ram_get_cdr (visit
);
73 IF_GC_TRACE(printf ("case 3\n"));
74 ram_set_gc_tags (visit
, GC_TAG_1_LEFT
);
75 ram_set_cdr (visit
, stack
);
79 IF_GC_TRACE(printf ("case 4\n"));
84 if (HAS_1_OBJECT_FIELD(visit
)) {
85 IF_GC_TRACE(printf ("case 5\n"));
89 // closures have the pointer in the cdr, not in the car as others
90 temp
= ram_get_car (visit
);
93 IF_GC_TRACE(printf ("case 6\n"));
94 ram_set_gc_tag0 (visit
, GC_TAG_0_LEFT
);
95 ram_set_car (visit
, stack
);
100 IF_GC_TRACE(printf ("case 7\n"));
103 IF_GC_TRACE(printf ("case 8\n"));
105 ram_set_gc_tag0 (visit
, GC_TAG_0_LEFT
);
110 IF_GC_TRACE(printf ("pop stack=%d visit=%d (tag=%d)\n", stack
, visit
, ram_get_gc_tags (visit
)>>6));
113 if (HAS_2_OBJECT_FIELDS(stack
) && ram_get_gc_tag1 (stack
)) {
114 IF_GC_TRACE(printf ("case 9\n"));
116 temp
= ram_get_cdr (stack
); /* pop through cdr */
117 ram_set_cdr (stack
, visit
);
121 ram_set_gc_tag1(visit
, GC_TAG_UNMARKED
);
122 // we unset the "1-left" bit
127 IF_GC_TRACE(printf ("case 11\n"));
129 temp
= ram_get_car (stack
); /* pop through car */
130 ram_set_car (stack
, visit
);
150 obj visit
= MAX_RAM_ENCODING
;
154 while (visit
>= (MIN_RAM_ENCODING
+ ((glovars
+ 1) >> 1))) {
155 // we don't want to sweep the global variables area
156 if ((RAM_COMPOSITE(visit
)
157 && (ram_get_gc_tags (visit
) == GC_TAG_UNMARKED
)) // 2 mark bit
158 || !(ram_get_gc_tags (visit
) & GC_TAG_0_LEFT
)) { // 1 mark bit
160 if (RAM_VECTOR(visit
)) {
161 // when we sweep a vector, we also have to sweep its contents
162 obj o
= ram_get_cdr (visit
);
163 uint16 i
= ram_get_car (visit
); // number of elements
164 ram_set_car (o
, free_list_vec
);
165 ram_set_cdr (o
, ((i
+ 3) >> 2)); // free length, in words
167 // TODO merge free spaces
169 ram_set_car (visit
, free_list
);
173 if (RAM_COMPOSITE(visit
))
174 ram_set_gc_tags (visit
, GC_TAG_UNMARKED
);
175 else // only 1 mark bit to unset
176 ram_set_gc_tag0 (visit
, GC_TAG_UNMARKED
);
187 printf ("**************** memory needed = %d\n", max_live
+1);
196 IF_TRACE(printf("\nGC BEGINS\n"));
198 IF_GC_TRACE(printf("arg1\n"));
200 IF_GC_TRACE(printf("arg2\n"));
202 IF_GC_TRACE(printf("arg3\n"));
204 IF_GC_TRACE(printf("arg4\n"));
206 IF_GC_TRACE(printf("cont\n"));
208 IF_GC_TRACE(printf("env\n"));
211 IF_GC_TRACE(printf("globals\n"));
212 for (i
=0; i
<glovars
; i
++)
213 mark (get_global (i
));
218 obj
alloc_ram_cell () {
225 if (free_list
== 0) {
230 ERROR("alloc_ram_cell", "memory is full");
235 free_list
= ram_get_car (o
);
240 obj
alloc_ram_cell_init (uint8 f0
, uint8 f1
, uint8 f2
, uint8 f3
) {
241 obj o
= alloc_ram_cell ();
243 ram_set_field0 (o
, f0
);
244 ram_set_field1 (o
, f1
);
245 ram_set_field2 (o
, f2
);
246 ram_set_field3 (o
, f3
);
251 obj
alloc_vec_cell (uint16 n
) {
252 obj o
= free_list_vec
;
261 while ((ram_get_cdr (o
) * 4) < n
) { // free space too small
262 if (o
== 0) { // no free space, or none big enough
263 if (gc_done
) // we gc'd, but no space is big enough for the vector
264 ERROR("alloc_vec_cell", "no room for vector");
272 } // TODO merge adjacent free spaces, maybe compact ?
277 // case 1 : the new vector fills every free word advertized, we remove the
278 // node from the free list
279 if (((ram_get_cdr(o
) * 4) - n
) < 4) {
281 ram_set_car (prec
, ram_get_car (o
));
283 free_list_vec
= ram_get_car (o
);
285 // case 2 : there is still some space left in the free section, create a new
286 // node to represent this space
288 obj new_free
= o
+ ((n
+ 3) >> 2);
290 ram_set_car (prec
, new_free
);
292 free_list_vec
= new_free
;
293 ram_set_car (new_free
, ram_get_car (o
));
294 ram_set_cdr (new_free
, ram_get_cdr (o
) - ((n
+ 3) >> 2));