1 /* Set by the -S command line argument */
4 /* set up guard pages to check for under/overflow.
5 size must be a multiple of the page size */
6 F_SEGMENT *alloc_segment(CELL size);
7 void dealloc_segment(F_SEGMENT *block);
9 CELL untagged_object_size(CELL pointer);
10 CELL unaligned_object_size(CELL pointer);
11 CELL object_size(CELL pointer);
12 CELL binary_payload_start(CELL pointer);
13 void begin_scan(void);
14 CELL next_object(void);
16 void primitive_data_room(void);
17 void primitive_size(void);
18 void primitive_begin_scan(void);
19 void primitive_next_object(void);
20 void primitive_end_scan(void);
23 DLLEXPORT void minor_gc(void);
25 /* generational copying GC divides memory into zones */
27 /* allocation pointer is 'here'; its offset is hardcoded in the
28 compiler backends, see core/compiler/.../allot.factor */
48 CELL *allot_markers_end;
57 F_DATA_HEAP *data_heap;
59 /* card marking write barrier. a card is a byte storing a mark flag,
60 and the offset (in cells) of the first object in the card.
62 the mark flag is set by the write barrier when an object in the
63 card has a slot written to.
65 the offset of the first object is set by the allocator. */
67 /* if CARD_POINTS_TO_NURSERY is set, CARD_POINTS_TO_AGING must also be set. */
68 #define CARD_POINTS_TO_NURSERY 0x80
69 #define CARD_POINTS_TO_AGING 0x40
70 #define CARD_MARK_MASK (CARD_POINTS_TO_NURSERY | CARD_POINTS_TO_AGING)
74 #define CARD_SIZE (1<<CARD_BITS)
75 #define ADDR_CARD_MASK (CARD_SIZE-1)
77 DLLEXPORT CELL cards_offset;
79 #define ADDR_TO_CARD(a) (F_CARD*)(((CELL)(a) >> CARD_BITS) + cards_offset)
80 #define CARD_TO_ADDR(c) (CELL*)(((CELL)(c) - cards_offset)<<CARD_BITS)
84 #define DECK_BITS (CARD_BITS + 10)
85 #define DECK_SIZE (1<<DECK_BITS)
86 #define ADDR_DECK_MASK (DECK_SIZE-1)
88 DLLEXPORT CELL decks_offset;
90 #define ADDR_TO_DECK(a) (F_DECK*)(((CELL)(a) >> DECK_BITS) + decks_offset)
91 #define DECK_TO_ADDR(c) (CELL*)(((CELL)(c) - decks_offset) << DECK_BITS)
93 #define DECK_TO_CARD(d) (F_CARD*)((((CELL)(d) - decks_offset) << (DECK_BITS - CARD_BITS)) + cards_offset)
95 #define ADDR_TO_ALLOT_MARKER(a) (F_CARD*)(((CELL)(a) >> CARD_BITS) + allot_markers_offset)
96 #define CARD_OFFSET(c) (*((c) - (CELL)data_heap->cards + (CELL)data_heap->allot_markers))
98 #define INVALID_ALLOT_MARKER 0xff
100 DLLEXPORT CELL allot_markers_offset;
102 void init_card_decks(void);
104 /* the write barrier must be called any time we are potentially storing a
105 pointer from an older generation to a younger one */
106 INLINE void write_barrier(CELL address)
108 *ADDR_TO_CARD(address) = CARD_MARK_MASK;
109 *ADDR_TO_DECK(address) = CARD_MARK_MASK;
112 #define SLOT(obj,slot) (UNTAG(obj) + (slot) * CELLS)
114 INLINE void set_slot(CELL obj, CELL slot, CELL value)
116 put(SLOT(obj,slot),value);
120 /* we need to remember the first object allocated in the card */
121 INLINE void allot_barrier(CELL address)
123 F_CARD *ptr = ADDR_TO_ALLOT_MARKER(address);
124 if(*ptr == INVALID_ALLOT_MARKER)
125 *ptr = (address & ADDR_CARD_MASK);
128 void clear_cards(CELL from, CELL to);
129 void collect_cards(void);
131 /* the 0th generation is where new objects are allocated. */
133 #define HAVE_NURSERY_P (data_heap->gen_count>1)
134 /* where objects hang around */
135 #define AGING (data_heap->gen_count-2)
136 #define HAVE_AGING_P (data_heap->gen_count>2)
137 /* the oldest generation */
138 #define TENURED (data_heap->gen_count-1)
140 #define MIN_GEN_COUNT 1
141 #define MAX_GEN_COUNT 3
143 /* used during garbage collection only */
146 /* new objects are allocated here */
147 DLLEXPORT F_ZONE nursery;
149 INLINE bool in_zone(F_ZONE *z, CELL pointer)
151 return pointer >= z->start && pointer < z->end;
154 CELL init_zone(F_ZONE *z, CELL size, CELL base);
156 void init_data_heap(CELL gens,
171 F_GC_STATS gc_stats[MAX_GEN_COUNT];
174 CELL code_heap_scans;
176 /* only meaningful during a GC */
180 /* if true, we collecting AGING space for the second time, so if it is still
181 full, we go on to collect TENURED */
182 bool collecting_aging_again;
184 INLINE bool collecting_accumulation_gen_p(void)
186 return ((HAVE_AGING_P
187 && collecting_gen == AGING
188 && !collecting_aging_again)
189 || collecting_gen == TENURED);
192 /* What generation was being collected when collect_literals() was last
193 called? Until the next call to primitive_add_compiled_block(), future
194 collections of younger generations don't have to touch the code
196 CELL last_code_heap_scan;
198 /* sometimes we grow the heap */
199 bool growing_data_heap;
200 F_DATA_HEAP *old_data_heap;
202 /* Every object has a regular representation in the runtime, which makes GC
203 much simpler. Every slot of the object until binary_payload_start is a pointer
204 to some other object. */
205 INLINE void do_slots(CELL obj, void (* iter)(CELL *))
208 CELL payload_start = binary_payload_start(obj);
209 CELL end = obj + payload_start;
220 /* test if the pointer is in generation being collected, or a younger one. */
221 INLINE bool should_copy(CELL untagged)
223 if(in_zone(newspace,untagged))
225 if(collecting_gen == TENURED)
227 else if(HAVE_AGING_P && collecting_gen == AGING)
228 return !in_zone(&data_heap->generations[TENURED],untagged);
229 else if(HAVE_NURSERY_P && collecting_gen == NURSERY)
230 return in_zone(&nursery,untagged);
233 critical_error("Bug in should_copy",untagged);
238 void copy_handle(CELL *handle);
240 /* in case a generation fills up in the middle of a gc, we jump back
241 up to try collecting the next generation. */
244 /* A heap walk allows useful things to be done, like finding all
245 references to an object for debugging purposes. */
248 /* GC is off during heap walking */
251 void garbage_collection(volatile CELL gen,
252 bool growing_data_heap_,
253 CELL requested_bytes);
255 /* If a runtime function needs to call another function which potentially
256 allocates memory, it must store any local variable references to Factor
257 objects on the root stack */
259 /* GC locals: stores addresses of pointers to objects. The GC updates these
260 pointers, so you can do
262 REGISTER_ROOT(some_local);
264 ... allocate memory ...
270 UNREGISTER_ROOT(some_local); */
271 F_SEGMENT *gc_locals_region;
274 DEFPUSHPOP(gc_local_,gc_locals)
276 #define REGISTER_ROOT(obj) gc_local_push((CELL)&obj)
277 #define UNREGISTER_ROOT(obj) \
279 if(gc_local_pop() != (CELL)&obj) \
280 critical_error("Mismatched REGISTER_ROOT/UNREGISTER_ROOT",0); \
283 /* Extra roots: stores pointers to objects in the heap. Requires extra work
284 (you have to unregister before accessing the object) but more flexible. */
285 F_SEGMENT *extra_roots_region;
288 DEFPUSHPOP(root_,extra_roots)
290 #define REGISTER_UNTAGGED(obj) root_push(obj ? tag_object(obj) : 0)
291 #define UNREGISTER_UNTAGGED(obj) obj = untag_object(root_pop())
293 INLINE bool in_data_heap_p(CELL ptr)
295 return (ptr >= data_heap->segment->start
296 && ptr <= data_heap->segment->end);
299 /* We ignore strings which point outside the data heap, but we might be given
300 a char* which points inside the data heap, in which case it is a root, for
301 example if we call unbox_char_string() the result is placed in a byte array */
302 INLINE bool root_push_alien(const void *ptr)
304 if(in_data_heap_p((CELL)ptr))
306 F_BYTE_ARRAY *objptr = ((F_BYTE_ARRAY *)ptr) - 1;
307 if(objptr->header == tag_header(BYTE_ARRAY_TYPE))
309 root_push(tag_object(objptr));
317 #define REGISTER_C_STRING(obj) \
318 bool obj##_root = root_push_alien(obj)
319 #define UNREGISTER_C_STRING(obj) \
320 if(obj##_root) obj = alien_offset(root_pop())
322 #define REGISTER_BIGNUM(obj) if(obj) root_push(tag_bignum(obj))
323 #define UNREGISTER_BIGNUM(obj) if(obj) obj = (untag_object(root_pop()))
325 INLINE void *allot_zone(F_ZONE *z, CELL a)
328 z->here = h + align8(a);
332 /* We leave this many bytes free at the top of the nursery so that inline
333 allocation (which does not call GC because of possible roots in volatile
334 registers) does not run out of memory */
335 #define ALLOT_BUFFER_ZONE 1024
338 * It is up to the caller to fill in the object's fields in a meaningful
341 INLINE void* allot_object(CELL type, CELL a)
345 if(HAVE_NURSERY_P && nursery.size - ALLOT_BUFFER_ZONE > a)
347 /* If there is insufficient room, collect the nursery */
348 if(nursery.here + ALLOT_BUFFER_ZONE + a > nursery.end)
349 garbage_collection(NURSERY,false,0);
351 CELL h = nursery.here;
352 nursery.here = h + align8(a);
355 /* If the object is bigger than the nursery, allocate it in
359 F_ZONE *tenured = &data_heap->generations[TENURED];
361 /* If tenured space does not have enough room, collect */
362 if(tenured->here + a > tenured->end)
365 tenured = &data_heap->generations[TENURED];
368 /* If it still won't fit, grow the heap */
369 if(tenured->here + a > tenured->end)
371 garbage_collection(TENURED,true,a);
372 tenured = &data_heap->generations[TENURED];
375 object = allot_zone(tenured,a);
377 /* We have to do this */
378 allot_barrier((CELL)object);
380 /* Allows initialization code to store old->new pointers
381 without hitting the write barrier in the common case of
382 a nursery allocation */
383 write_barrier((CELL)object);
386 *object = tag_header(type);
390 void collect_next_loop(CELL scan, CELL *end);
392 void primitive_gc(void);
393 void primitive_gc_stats(void);
394 void primitive_gc_reset(void);
395 void primitive_become(void);
397 CELL find_all_words(void);