]> gcc.gnu.org Git - gcc.git/blame - boehm-gc/mark.c
Initial revision
[gcc.git] / boehm-gc / mark.c
CommitLineData
18a4bc4e
TT
1
2/*
3 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
4 * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
5 *
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 *
9 * Permission is hereby granted to use or copy this program
10 * for any purpose, provided the above notices are retained on all copies.
11 * Permission to modify the code and to distribute modified code is granted,
12 * provided the above notices are retained, and a notice that the code was
13 * modified is included with the above copyright notice.
14 *
15 */
16
17
18# include <stdio.h>
19# include "gc_priv.h"
20# include "gc_mark.h"
21
22/* We put this here to minimize the risk of inlining. */
23/*VARARGS*/
24void GC_noop() {}
25
26/* Single argument version, robust against whole program analysis. */
27void GC_noop1(x)
28word x;
29{
30 static VOLATILE word sink;
31
32 sink = x;
33}
34
35mark_proc GC_mark_procs[MAX_MARK_PROCS] = {0};
36word GC_n_mark_procs = 0;
37
38/* Initialize GC_obj_kinds properly and standard free lists properly. */
39/* This must be done statically since they may be accessed before */
40/* GC_init is called. */
41/* It's done here, since we need to deal with mark descriptors. */
42struct obj_kind GC_obj_kinds[MAXOBJKINDS] = {
43/* PTRFREE */ { &GC_aobjfreelist[0], 0 /* filled in dynamically */,
44 0 | DS_LENGTH, FALSE, FALSE },
45/* NORMAL */ { &GC_objfreelist[0], 0,
46# if defined(ADD_BYTE_AT_END) && ALIGNMENT > DS_TAGS
47 (word)(-ALIGNMENT) | DS_LENGTH,
48# else
49 0 | DS_LENGTH,
50# endif
51 TRUE /* add length to descr */, TRUE },
52/* UNCOLLECTABLE */
53 { &GC_uobjfreelist[0], 0,
54 0 | DS_LENGTH, TRUE /* add length to descr */, TRUE },
55# ifdef ATOMIC_UNCOLLECTABLE
56 /* AUNCOLLECTABLE */
57 { &GC_auobjfreelist[0], 0,
58 0 | DS_LENGTH, FALSE /* add length to descr */, FALSE },
59# endif
60# ifdef STUBBORN_ALLOC
61/*STUBBORN*/ { &GC_sobjfreelist[0], 0,
62 0 | DS_LENGTH, TRUE /* add length to descr */, TRUE },
63# endif
64};
65
66# ifdef ATOMIC_UNCOLLECTABLE
67# ifdef STUBBORN_ALLOC
68 int GC_n_kinds = 5;
69# else
70 int GC_n_kinds = 4;
71# endif
72# else
73# ifdef STUBBORN_ALLOC
74 int GC_n_kinds = 4;
75# else
76 int GC_n_kinds = 3;
77# endif
78# endif
79
80
81# ifndef INITIAL_MARK_STACK_SIZE
82# define INITIAL_MARK_STACK_SIZE (1*HBLKSIZE)
83 /* INITIAL_MARK_STACK_SIZE * sizeof(mse) should be a */
84 /* multiple of HBLKSIZE. */
85# endif
86
87/*
88 * Limits of stack for GC_mark routine.
89 * All ranges between GC_mark_stack(incl.) and GC_mark_stack_top(incl.) still
90 * need to be marked from.
91 */
92
93word GC_n_rescuing_pages; /* Number of dirty pages we marked from */
94 /* excludes ptrfree pages, etc. */
95
96mse * GC_mark_stack;
97
98word GC_mark_stack_size = 0;
99
100mse * GC_mark_stack_top;
101
102static struct hblk * scan_ptr;
103
104mark_state_t GC_mark_state = MS_NONE;
105
106GC_bool GC_mark_stack_too_small = FALSE;
107
108GC_bool GC_objects_are_marked = FALSE; /* Are there collectable marked */
109 /* objects in the heap? */
110
111GC_bool GC_collection_in_progress()
112{
113 return(GC_mark_state != MS_NONE);
114}
115
116/* clear all mark bits in the header */
117void GC_clear_hdr_marks(hhdr)
118register hdr * hhdr;
119{
120 BZERO(hhdr -> hb_marks, MARK_BITS_SZ*sizeof(word));
121}
122
123/* Set all mark bits in the header. Used for uncollectable blocks. */
124void GC_set_hdr_marks(hhdr)
125register hdr * hhdr;
126{
127 register int i;
128
129 for (i = 0; i < MARK_BITS_SZ; ++i) {
130 hhdr -> hb_marks[i] = ONES;
131 }
132}
133
134/*
135 * Clear all mark bits associated with block h.
136 */
137/*ARGSUSED*/
138static void clear_marks_for_block(h, dummy)
139struct hblk *h;
140word dummy;
141{
142 register hdr * hhdr = HDR(h);
143
144 if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) return;
145 /* Mark bit for these is cleared only once the object is */
146 /* explicitly deallocated. This either frees the block, or */
147 /* the bit is cleared once the object is on the free list. */
148 GC_clear_hdr_marks(hhdr);
149}
150
151/* Slow but general routines for setting/clearing/asking about mark bits */
152void GC_set_mark_bit(p)
153ptr_t p;
154{
155 register struct hblk *h = HBLKPTR(p);
156 register hdr * hhdr = HDR(h);
157 register int word_no = (word *)p - (word *)h;
158
159 set_mark_bit_from_hdr(hhdr, word_no);
160}
161
162void GC_clear_mark_bit(p)
163ptr_t p;
164{
165 register struct hblk *h = HBLKPTR(p);
166 register hdr * hhdr = HDR(h);
167 register int word_no = (word *)p - (word *)h;
168
169 clear_mark_bit_from_hdr(hhdr, word_no);
170}
171
172GC_bool GC_is_marked(p)
173ptr_t p;
174{
175 register struct hblk *h = HBLKPTR(p);
176 register hdr * hhdr = HDR(h);
177 register int word_no = (word *)p - (word *)h;
178
179 return(mark_bit_from_hdr(hhdr, word_no));
180}
181
182
183/*
184 * Clear mark bits in all allocated heap blocks. This invalidates
185 * the marker invariant, and sets GC_mark_state to reflect this.
186 * (This implicitly starts marking to reestablish the invariant.)
187 */
188void GC_clear_marks()
189{
190 GC_apply_to_all_blocks(clear_marks_for_block, (word)0);
191 GC_objects_are_marked = FALSE;
192 GC_mark_state = MS_INVALID;
193 scan_ptr = 0;
194# ifdef GATHERSTATS
195 /* Counters reflect currently marked objects: reset here */
196 GC_composite_in_use = 0;
197 GC_atomic_in_use = 0;
198# endif
199
200}
201
202/* Initiate a garbage collection. Initiates a full collection if the */
203/* mark state is invalid. */
204/*ARGSUSED*/
205void GC_initiate_gc()
206{
207 if (GC_dirty_maintained) GC_read_dirty();
208# ifdef STUBBORN_ALLOC
209 GC_read_changed();
210# endif
211# ifdef CHECKSUMS
212 {
213 extern void GC_check_dirty();
214
215 if (GC_dirty_maintained) GC_check_dirty();
216 }
217# endif
218# ifdef GATHERSTATS
219 GC_n_rescuing_pages = 0;
220# endif
221 if (GC_mark_state == MS_NONE) {
222 GC_mark_state = MS_PUSH_RESCUERS;
223 } else if (GC_mark_state != MS_INVALID) {
224 ABORT("unexpected state");
225 } /* else this is really a full collection, and mark */
226 /* bits are invalid. */
227 scan_ptr = 0;
228}
229
230
231static void alloc_mark_stack();
232
233/* Perform a small amount of marking. */
234/* We try to touch roughly a page of memory. */
235/* Return TRUE if we just finished a mark phase. */
236GC_bool GC_mark_some()
237{
238 switch(GC_mark_state) {
239 case MS_NONE:
240 return(FALSE);
241
242 case MS_PUSH_RESCUERS:
243 if (GC_mark_stack_top
244 >= GC_mark_stack + INITIAL_MARK_STACK_SIZE/4) {
245 GC_mark_from_mark_stack();
246 return(FALSE);
247 } else {
248 scan_ptr = GC_push_next_marked_dirty(scan_ptr);
249 if (scan_ptr == 0) {
250# ifdef PRINTSTATS
251 GC_printf1("Marked from %lu dirty pages\n",
252 (unsigned long)GC_n_rescuing_pages);
253# endif
254 GC_push_roots(FALSE);
255 GC_objects_are_marked = TRUE;
256 if (GC_mark_state != MS_INVALID) {
257 GC_mark_state = MS_ROOTS_PUSHED;
258 }
259 }
260 }
261 return(FALSE);
262
263 case MS_PUSH_UNCOLLECTABLE:
264 if (GC_mark_stack_top
265 >= GC_mark_stack + INITIAL_MARK_STACK_SIZE/4) {
266 GC_mark_from_mark_stack();
267 return(FALSE);
268 } else {
269 scan_ptr = GC_push_next_marked_uncollectable(scan_ptr);
270 if (scan_ptr == 0) {
271 GC_push_roots(TRUE);
272 GC_objects_are_marked = TRUE;
273 if (GC_mark_state != MS_INVALID) {
274 GC_mark_state = MS_ROOTS_PUSHED;
275 }
276 }
277 }
278 return(FALSE);
279
280 case MS_ROOTS_PUSHED:
281 if (GC_mark_stack_top >= GC_mark_stack) {
282 GC_mark_from_mark_stack();
283 return(FALSE);
284 } else {
285 GC_mark_state = MS_NONE;
286 if (GC_mark_stack_too_small) {
287 alloc_mark_stack(2*GC_mark_stack_size);
288 }
289 return(TRUE);
290 }
291
292 case MS_INVALID:
293 case MS_PARTIALLY_INVALID:
294 if (!GC_objects_are_marked) {
295 GC_mark_state = MS_PUSH_UNCOLLECTABLE;
296 return(FALSE);
297 }
298 if (GC_mark_stack_top >= GC_mark_stack) {
299 GC_mark_from_mark_stack();
300 return(FALSE);
301 }
302 if (scan_ptr == 0
303 && (GC_mark_state == MS_INVALID || GC_mark_stack_too_small)) {
304 alloc_mark_stack(2*GC_mark_stack_size);
305 GC_mark_state = MS_PARTIALLY_INVALID;
306 }
307 scan_ptr = GC_push_next_marked(scan_ptr);
308 if (scan_ptr == 0 && GC_mark_state == MS_PARTIALLY_INVALID) {
309 GC_push_roots(TRUE);
310 GC_objects_are_marked = TRUE;
311 if (GC_mark_state != MS_INVALID) {
312 GC_mark_state = MS_ROOTS_PUSHED;
313 }
314 }
315 return(FALSE);
316 default:
317 ABORT("GC_mark_some: bad state");
318 return(FALSE);
319 }
320}
321
322
323GC_bool GC_mark_stack_empty()
324{
325 return(GC_mark_stack_top < GC_mark_stack);
326}
327
328#ifdef PROF_MARKER
329 word GC_prof_array[10];
330# define PROF(n) GC_prof_array[n]++
331#else
332# define PROF(n)
333#endif
334
335/* Given a pointer to someplace other than a small object page or the */
336/* first page of a large object, return a pointer either to the */
337/* start of the large object or NIL. */
338/* In the latter case black list the address current. */
339/* Returns NIL without black listing if current points to a block */
340/* with IGNORE_OFF_PAGE set. */
341/*ARGSUSED*/
342# ifdef PRINT_BLACK_LIST
343 word GC_find_start(current, hhdr, source)
344 word source;
345# else
346 word GC_find_start(current, hhdr)
347# define source 0
348# endif
349register word current;
350register hdr * hhdr;
351{
352# ifdef ALL_INTERIOR_POINTERS
353 if (hhdr != 0) {
354 register word orig = current;
355
356 current = (word)HBLKPTR(current) + HDR_BYTES;
357 do {
358 current = current - HBLKSIZE*(word)hhdr;
359 hhdr = HDR(current);
360 } while(IS_FORWARDING_ADDR_OR_NIL(hhdr));
361 /* current points to the start of the large object */
362 if (hhdr -> hb_flags & IGNORE_OFF_PAGE) return(0);
363 if ((word *)orig - (word *)current
364 >= (ptrdiff_t)(hhdr->hb_sz)) {
365 /* Pointer past the end of the block */
366 GC_ADD_TO_BLACK_LIST_NORMAL(orig, source);
367 return(0);
368 }
369 return(current);
370 } else {
371 GC_ADD_TO_BLACK_LIST_NORMAL(current, source);
372 return(0);
373 }
374# else
375 GC_ADD_TO_BLACK_LIST_NORMAL(current, source);
376 return(0);
377# endif
378# undef source
379}
380
381void GC_invalidate_mark_state()
382{
383 GC_mark_state = MS_INVALID;
384 GC_mark_stack_top = GC_mark_stack-1;
385}
386
387mse * GC_signal_mark_stack_overflow(msp)
388mse * msp;
389{
390 GC_mark_state = MS_INVALID;
391# ifdef PRINTSTATS
392 GC_printf1("Mark stack overflow; current size = %lu entries\n",
393 GC_mark_stack_size);
394# endif
395 return(msp-INITIAL_MARK_STACK_SIZE/8);
396}
397
398
399/*
400 * Mark objects pointed to by the regions described by
401 * mark stack entries between GC_mark_stack and GC_mark_stack_top,
402 * inclusive. Assumes the upper limit of a mark stack entry
403 * is never 0. A mark stack entry never has size 0.
404 * We try to traverse on the order of a hblk of memory before we return.
405 * Caller is responsible for calling this until the mark stack is empty.
406 */
407void GC_mark_from_mark_stack()
408{
409 mse * GC_mark_stack_reg = GC_mark_stack;
410 mse * GC_mark_stack_top_reg = GC_mark_stack_top;
411 mse * mark_stack_limit = &(GC_mark_stack[GC_mark_stack_size]);
412 int credit = HBLKSIZE; /* Remaining credit for marking work */
413 register word * current_p; /* Pointer to current candidate ptr. */
414 register word current; /* Candidate pointer. */
415 register word * limit; /* (Incl) limit of current candidate */
416 /* range */
417 register word descr;
418 register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
419 register ptr_t least_ha = GC_least_plausible_heap_addr;
420# define SPLIT_RANGE_WORDS 128 /* Must be power of 2. */
421
422 GC_objects_are_marked = TRUE;
423# ifdef OS2 /* Use untweaked version to circumvent compiler problem */
424 while (GC_mark_stack_top_reg >= GC_mark_stack_reg && credit >= 0) {
425# else
426 while ((((ptr_t)GC_mark_stack_top_reg - (ptr_t)GC_mark_stack_reg) | credit)
427 >= 0) {
428# endif
429 current_p = GC_mark_stack_top_reg -> mse_start;
430 retry:
431 descr = GC_mark_stack_top_reg -> mse_descr;
432 if (descr & ((~(WORDS_TO_BYTES(SPLIT_RANGE_WORDS) - 1)) | DS_TAGS)) {
433 word tag = descr & DS_TAGS;
434
435 switch(tag) {
436 case DS_LENGTH:
437 /* Large length. */
438 /* Process part of the range to avoid pushing too much on the */
439 /* stack. */
440 GC_mark_stack_top_reg -> mse_start =
441 limit = current_p + SPLIT_RANGE_WORDS-1;
442 GC_mark_stack_top_reg -> mse_descr -=
443 WORDS_TO_BYTES(SPLIT_RANGE_WORDS-1);
444 /* Make sure that pointers overlapping the two ranges are */
445 /* considered. */
446 limit = (word *)((char *)limit + sizeof(word) - ALIGNMENT);
447 break;
448 case DS_BITMAP:
449 GC_mark_stack_top_reg--;
450 descr &= ~DS_TAGS;
451 credit -= WORDS_TO_BYTES(WORDSZ/2); /* guess */
452 while (descr != 0) {
453 if ((signed_word)descr < 0) {
454 current = *current_p;
455 if ((ptr_t)current >= least_ha && (ptr_t)current < greatest_ha) {
456 PUSH_CONTENTS(current, GC_mark_stack_top_reg, mark_stack_limit,
457 current_p, exit1);
458 }
459 }
460 descr <<= 1;
461 ++ current_p;
462 }
463 continue;
464 case DS_PROC:
465 GC_mark_stack_top_reg--;
466 credit -= PROC_BYTES;
467 GC_mark_stack_top_reg =
468 (*PROC(descr))
469 (current_p, GC_mark_stack_top_reg,
470 mark_stack_limit, ENV(descr));
471 continue;
472 case DS_PER_OBJECT:
473 GC_mark_stack_top_reg -> mse_descr =
474 *(word *)((ptr_t)current_p + descr - tag);
475 goto retry;
476 }
477 } else {
478 GC_mark_stack_top_reg--;
479 limit = (word *)(((ptr_t)current_p) + (word)descr);
480 }
481 /* The simple case in which we're scanning a range. */
482 credit -= (ptr_t)limit - (ptr_t)current_p;
483 limit -= 1;
484 while (current_p <= limit) {
485 current = *current_p;
486 if ((ptr_t)current >= least_ha && (ptr_t)current < greatest_ha) {
487 PUSH_CONTENTS(current, GC_mark_stack_top_reg,
488 mark_stack_limit, current_p, exit2);
489 }
490 current_p = (word *)((char *)current_p + ALIGNMENT);
491 }
492 }
493 GC_mark_stack_top = GC_mark_stack_top_reg;
494}
495
496/* Allocate or reallocate space for mark stack of size s words */
497/* May silently fail. */
498static void alloc_mark_stack(n)
499word n;
500{
501 mse * new_stack = (mse *)GC_scratch_alloc(n * sizeof(struct ms_entry));
502
503 GC_mark_stack_too_small = FALSE;
504 if (GC_mark_stack_size != 0) {
505 if (new_stack != 0) {
506 word displ = (word)GC_mark_stack & (GC_page_size - 1);
507 word size = GC_mark_stack_size * sizeof(struct ms_entry);
508
509 /* Recycle old space */
510 if (0 != displ) displ = GC_page_size - displ;
511 size = (size - displ) & ~(GC_page_size - 1);
512 GC_add_to_heap((struct hblk *)
513 ((word)GC_mark_stack + displ), size);
514 GC_mark_stack = new_stack;
515 GC_mark_stack_size = n;
516# ifdef PRINTSTATS
517 GC_printf1("Grew mark stack to %lu frames\n",
518 (unsigned long) GC_mark_stack_size);
519# endif
520 } else {
521# ifdef PRINTSTATS
522 GC_printf1("Failed to grow mark stack to %lu frames\n",
523 (unsigned long) n);
524# endif
525 }
526 } else {
527 if (new_stack == 0) {
528 GC_err_printf0("No space for mark stack\n");
529 EXIT();
530 }
531 GC_mark_stack = new_stack;
532 GC_mark_stack_size = n;
533 }
534 GC_mark_stack_top = GC_mark_stack-1;
535}
536
537void GC_mark_init()
538{
539 alloc_mark_stack(INITIAL_MARK_STACK_SIZE);
540}
541
542/*
543 * Push all locations between b and t onto the mark stack.
544 * b is the first location to be checked. t is one past the last
545 * location to be checked.
546 * Should only be used if there is no possibility of mark stack
547 * overflow.
548 */
549void GC_push_all(bottom, top)
550ptr_t bottom;
551ptr_t top;
552{
553 register word length;
554
555 bottom = (ptr_t)(((word) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
556 top = (ptr_t)(((word) top) & ~(ALIGNMENT-1));
557 if (top == 0 || bottom == top) return;
558 GC_mark_stack_top++;
559 if (GC_mark_stack_top >= GC_mark_stack + GC_mark_stack_size) {
560 ABORT("unexpected mark stack overflow");
561 }
562 length = top - bottom;
563# if DS_TAGS > ALIGNMENT - 1
564 length += DS_TAGS;
565 length &= ~DS_TAGS;
566# endif
567 GC_mark_stack_top -> mse_start = (word *)bottom;
568 GC_mark_stack_top -> mse_descr = length;
569}
570
571/*
572 * Analogous to the above, but push only those pages that may have been
573 * dirtied. A block h is assumed dirty if dirty_fn(h) != 0.
574 * We use push_fn to actually push the block.
575 * Will not overflow mark stack if push_fn pushes a small fixed number
576 * of entries. (This is invoked only if push_fn pushes a single entry,
577 * or if it marks each object before pushing it, thus ensuring progress
578 * in the event of a stack overflow.)
579 */
580void GC_push_dirty(bottom, top, dirty_fn, push_fn)
581ptr_t bottom;
582ptr_t top;
583int (*dirty_fn)(/* struct hblk * h */);
584void (*push_fn)(/* ptr_t bottom, ptr_t top */);
585{
586 register struct hblk * h;
587
588 bottom = (ptr_t)(((long) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
589 top = (ptr_t)(((long) top) & ~(ALIGNMENT-1));
590
591 if (top == 0 || bottom == top) return;
592 h = HBLKPTR(bottom + HBLKSIZE);
593 if (top <= (ptr_t) h) {
594 if ((*dirty_fn)(h-1)) {
595 (*push_fn)(bottom, top);
596 }
597 return;
598 }
599 if ((*dirty_fn)(h-1)) {
600 (*push_fn)(bottom, (ptr_t)h);
601 }
602 while ((ptr_t)(h+1) <= top) {
603 if ((*dirty_fn)(h)) {
604 if ((word)(GC_mark_stack_top - GC_mark_stack)
605 > 3 * GC_mark_stack_size / 4) {
606 /* Danger of mark stack overflow */
607 (*push_fn)((ptr_t)h, top);
608 return;
609 } else {
610 (*push_fn)((ptr_t)h, (ptr_t)(h+1));
611 }
612 }
613 h++;
614 }
615 if ((ptr_t)h != top) {
616 if ((*dirty_fn)(h)) {
617 (*push_fn)((ptr_t)h, top);
618 }
619 }
620 if (GC_mark_stack_top >= GC_mark_stack + GC_mark_stack_size) {
621 ABORT("unexpected mark stack overflow");
622 }
623}
624
625# ifndef SMALL_CONFIG
626void GC_push_conditional(bottom, top, all)
627ptr_t bottom;
628ptr_t top;
629int all;
630{
631 if (all) {
632 if (GC_dirty_maintained) {
633# ifdef PROC_VDB
634 /* Pages that were never dirtied cannot contain pointers */
635 GC_push_dirty(bottom, top, GC_page_was_ever_dirty, GC_push_all);
636# else
637 GC_push_all(bottom, top);
638# endif
639 } else {
640 GC_push_all(bottom, top);
641 }
642 } else {
643 GC_push_dirty(bottom, top, GC_page_was_dirty, GC_push_all);
644 }
645}
646#endif
647
648# ifdef MSWIN32
649 void __cdecl GC_push_one(p)
650# else
651 void GC_push_one(p)
652# endif
653word p;
654{
655 GC_PUSH_ONE_STACK(p);
656}
657
658# ifdef __STDC__
659# define BASE(p) (word)GC_base((void *)(p))
660# else
661# define BASE(p) (word)GC_base((char *)(p))
662# endif
663
664/* As above, but argument passed preliminary test. */
665# ifdef PRINT_BLACK_LIST
666 void GC_push_one_checked(p, interior_ptrs, source)
667 ptr_t source;
668# else
669 void GC_push_one_checked(p, interior_ptrs)
670# define source 0
671# endif
672register word p;
673register GC_bool interior_ptrs;
674{
675 register word r;
676 register hdr * hhdr;
677 register int displ;
678
679 GET_HDR(p, hhdr);
680 if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
681 if (hhdr != 0 && interior_ptrs) {
682 r = BASE(p);
683 hhdr = HDR(r);
684 displ = BYTES_TO_WORDS(HBLKDISPL(r));
685 } else {
686 hhdr = 0;
687 }
688 } else {
689 register map_entry_type map_entry;
690
691 displ = HBLKDISPL(p);
692 map_entry = MAP_ENTRY((hhdr -> hb_map), displ);
693 if (map_entry == OBJ_INVALID) {
694 if (interior_ptrs) {
695 r = BASE(p);
696 displ = BYTES_TO_WORDS(HBLKDISPL(r));
697 if (r == 0) hhdr = 0;
698 } else {
699 hhdr = 0;
700 }
701 } else {
702 displ = BYTES_TO_WORDS(displ);
703 displ -= map_entry;
704 r = (word)((word *)(HBLKPTR(p)) + displ);
705 }
706 }
707 /* If hhdr != 0 then r == GC_base(p), only we did it faster. */
708 /* displ is the word index within the block. */
709 if (hhdr == 0) {
710 if (interior_ptrs) {
711# ifdef PRINT_BLACK_LIST
712 GC_add_to_black_list_stack(p, source);
713# else
714 GC_add_to_black_list_stack(p);
715# endif
716 } else {
717 GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
718# undef source /* In case we had to define it. */
719 }
720 } else {
721 if (!mark_bit_from_hdr(hhdr, displ)) {
722 set_mark_bit_from_hdr(hhdr, displ);
723 PUSH_OBJ((word *)r, hhdr, GC_mark_stack_top,
724 &(GC_mark_stack[GC_mark_stack_size]));
725 }
726 }
727}
728
729# ifdef TRACE_BUF
730
731# define TRACE_ENTRIES 1000
732
733struct trace_entry {
734 char * kind;
735 word gc_no;
736 word words_allocd;
737 word arg1;
738 word arg2;
739} GC_trace_buf[TRACE_ENTRIES];
740
741int GC_trace_buf_ptr = 0;
742
743void GC_add_trace_entry(char *kind, word arg1, word arg2)
744{
745 GC_trace_buf[GC_trace_buf_ptr].kind = kind;
746 GC_trace_buf[GC_trace_buf_ptr].gc_no = GC_gc_no;
747 GC_trace_buf[GC_trace_buf_ptr].words_allocd = GC_words_allocd;
748 GC_trace_buf[GC_trace_buf_ptr].arg1 = arg1 ^ 0x80000000;
749 GC_trace_buf[GC_trace_buf_ptr].arg2 = arg2 ^ 0x80000000;
750 GC_trace_buf_ptr++;
751 if (GC_trace_buf_ptr >= TRACE_ENTRIES) GC_trace_buf_ptr = 0;
752}
753
754void GC_print_trace(word gc_no, GC_bool lock)
755{
756 int i;
757 struct trace_entry *p;
758
759 if (lock) LOCK();
760 for (i = GC_trace_buf_ptr-1; i != GC_trace_buf_ptr; i--) {
761 if (i < 0) i = TRACE_ENTRIES-1;
762 p = GC_trace_buf + i;
763 if (p -> gc_no < gc_no || p -> kind == 0) return;
764 printf("Trace:%s (gc:%d,words:%d) 0x%X, 0x%X\n",
765 p -> kind, p -> gc_no, p -> words_allocd,
766 (p -> arg1) ^ 0x80000000, (p -> arg2) ^ 0x80000000);
767 }
768 printf("Trace incomplete\n");
769 if (lock) UNLOCK();
770}
771
772# endif /* TRACE_BUF */
773
774/*
775 * A version of GC_push_all that treats all interior pointers as valid
776 */
777void GC_push_all_stack(bottom, top)
778ptr_t bottom;
779ptr_t top;
780{
781# ifdef ALL_INTERIOR_POINTERS
782 GC_push_all(bottom, top);
783# ifdef TRACE_BUF
784 GC_add_trace_entry("GC_push_all_stack", bottom, top);
785# endif
786# else
787 word * b = (word *)(((long) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
788 word * t = (word *)(((long) top) & ~(ALIGNMENT-1));
789 register word *p;
790 register word q;
791 register word *lim;
792 register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
793 register ptr_t least_ha = GC_least_plausible_heap_addr;
794# define GC_greatest_plausible_heap_addr greatest_ha
795# define GC_least_plausible_heap_addr least_ha
796
797 if (top == 0) return;
798 /* check all pointers in range and put in push if they appear */
799 /* to be valid. */
800 lim = t - 1 /* longword */;
801 for (p = b; p <= lim; p = (word *)(((char *)p) + ALIGNMENT)) {
802 q = *p;
803 GC_PUSH_ONE_STACK(q);
804 }
805# undef GC_greatest_plausible_heap_addr
806# undef GC_least_plausible_heap_addr
807# endif
808}
809
810#ifndef SMALL_CONFIG
811/* Push all objects reachable from marked objects in the given block */
812/* of size 1 objects. */
813void GC_push_marked1(h, hhdr)
814struct hblk *h;
815register hdr * hhdr;
816{
817 word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
818 register word *p;
819 word *plim;
820 register int i;
821 register word q;
822 register word mark_word;
823 register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
824 register ptr_t least_ha = GC_least_plausible_heap_addr;
825# define GC_greatest_plausible_heap_addr greatest_ha
826# define GC_least_plausible_heap_addr least_ha
827
828 p = (word *)(h->hb_body);
829 plim = (word *)(((word)h) + HBLKSIZE);
830
831 /* go through all words in block */
832 while( p < plim ) {
833 mark_word = *mark_word_addr++;
834 i = 0;
835 while(mark_word != 0) {
836 if (mark_word & 1) {
837 q = p[i];
838 GC_PUSH_ONE_HEAP(q);
839 }
840 i++;
841 mark_word >>= 1;
842 }
843 p += WORDSZ;
844 }
845# undef GC_greatest_plausible_heap_addr
846# undef GC_least_plausible_heap_addr
847}
848
849
850#ifndef UNALIGNED
851
852/* Push all objects reachable from marked objects in the given block */
853/* of size 2 objects. */
854void GC_push_marked2(h, hhdr)
855struct hblk *h;
856register hdr * hhdr;
857{
858 word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
859 register word *p;
860 word *plim;
861 register int i;
862 register word q;
863 register word mark_word;
864 register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
865 register ptr_t least_ha = GC_least_plausible_heap_addr;
866# define GC_greatest_plausible_heap_addr greatest_ha
867# define GC_least_plausible_heap_addr least_ha
868
869 p = (word *)(h->hb_body);
870 plim = (word *)(((word)h) + HBLKSIZE);
871
872 /* go through all words in block */
873 while( p < plim ) {
874 mark_word = *mark_word_addr++;
875 i = 0;
876 while(mark_word != 0) {
877 if (mark_word & 1) {
878 q = p[i];
879 GC_PUSH_ONE_HEAP(q);
880 q = p[i+1];
881 GC_PUSH_ONE_HEAP(q);
882 }
883 i += 2;
884 mark_word >>= 2;
885 }
886 p += WORDSZ;
887 }
888# undef GC_greatest_plausible_heap_addr
889# undef GC_least_plausible_heap_addr
890}
891
892/* Push all objects reachable from marked objects in the given block */
893/* of size 4 objects. */
894/* There is a risk of mark stack overflow here. But we handle that. */
895/* And only unmarked objects get pushed, so it's not very likely. */
896void GC_push_marked4(h, hhdr)
897struct hblk *h;
898register hdr * hhdr;
899{
900 word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
901 register word *p;
902 word *plim;
903 register int i;
904 register word q;
905 register word mark_word;
906 register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
907 register ptr_t least_ha = GC_least_plausible_heap_addr;
908# define GC_greatest_plausible_heap_addr greatest_ha
909# define GC_least_plausible_heap_addr least_ha
910
911 p = (word *)(h->hb_body);
912 plim = (word *)(((word)h) + HBLKSIZE);
913
914 /* go through all words in block */
915 while( p < plim ) {
916 mark_word = *mark_word_addr++;
917 i = 0;
918 while(mark_word != 0) {
919 if (mark_word & 1) {
920 q = p[i];
921 GC_PUSH_ONE_HEAP(q);
922 q = p[i+1];
923 GC_PUSH_ONE_HEAP(q);
924 q = p[i+2];
925 GC_PUSH_ONE_HEAP(q);
926 q = p[i+3];
927 GC_PUSH_ONE_HEAP(q);
928 }
929 i += 4;
930 mark_word >>= 4;
931 }
932 p += WORDSZ;
933 }
934# undef GC_greatest_plausible_heap_addr
935# undef GC_least_plausible_heap_addr
936}
937
938#endif /* UNALIGNED */
939
940#endif /* SMALL_CONFIG */
941
942/* Push all objects reachable from marked objects in the given block */
943void GC_push_marked(h, hhdr)
944struct hblk *h;
945register hdr * hhdr;
946{
947 register int sz = hhdr -> hb_sz;
948 register word * p;
949 register int word_no;
950 register word * lim;
951 register mse * GC_mark_stack_top_reg;
952 register mse * mark_stack_limit = &(GC_mark_stack[GC_mark_stack_size]);
953
954 /* Some quick shortcuts: */
955 {
956 struct obj_kind *ok = &(GC_obj_kinds[hhdr -> hb_obj_kind]);
957 if ((0 | DS_LENGTH) == ok -> ok_descriptor
958 && FALSE == ok -> ok_relocate_descr)
959 return;
960 }
961 if (GC_block_empty(hhdr)/* nothing marked */) return;
962# ifdef GATHERSTATS
963 GC_n_rescuing_pages++;
964# endif
965 GC_objects_are_marked = TRUE;
966 if (sz > MAXOBJSZ) {
967 lim = (word *)(h + 1);
968 } else {
969 lim = (word *)(h + 1) - sz;
970 }
971
972 switch(sz) {
973# if !defined(SMALL_CONFIG)
974 case 1:
975 GC_push_marked1(h, hhdr);
976 break;
977# endif
978# if !defined(SMALL_CONFIG) && !defined(UNALIGNED)
979 case 2:
980 GC_push_marked2(h, hhdr);
981 break;
982 case 4:
983 GC_push_marked4(h, hhdr);
984 break;
985# endif
986 default:
987 GC_mark_stack_top_reg = GC_mark_stack_top;
988 for (p = (word *)h + HDR_WORDS, word_no = HDR_WORDS; p <= lim;
989 p += sz, word_no += sz) {
990 /* This ignores user specified mark procs. This currently */
991 /* doesn't matter, since marking from the whole object */
992 /* is always sufficient, and we will eventually use the user */
993 /* mark proc to avoid any bogus pointers. */
994 if (mark_bit_from_hdr(hhdr, word_no)) {
995 /* Mark from fields inside the object */
996 PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top_reg, mark_stack_limit);
997# ifdef GATHERSTATS
998 /* Subtract this object from total, since it was */
999 /* added in twice. */
1000 GC_composite_in_use -= sz;
1001# endif
1002 }
1003 }
1004 GC_mark_stack_top = GC_mark_stack_top_reg;
1005 }
1006}
1007
1008#ifndef SMALL_CONFIG
1009/* Test whether any page in the given block is dirty */
1010GC_bool GC_block_was_dirty(h, hhdr)
1011struct hblk *h;
1012register hdr * hhdr;
1013{
1014 register int sz = hhdr -> hb_sz;
1015
1016 if (sz < MAXOBJSZ) {
1017 return(GC_page_was_dirty(h));
1018 } else {
1019 register ptr_t p = (ptr_t)h;
1020 sz += HDR_WORDS;
1021 sz = WORDS_TO_BYTES(sz);
1022 while (p < (ptr_t)h + sz) {
1023 if (GC_page_was_dirty((struct hblk *)p)) return(TRUE);
1024 p += HBLKSIZE;
1025 }
1026 return(FALSE);
1027 }
1028}
1029#endif /* SMALL_CONFIG */
1030
1031/* Similar to GC_push_next_marked, but return address of next block */
1032struct hblk * GC_push_next_marked(h)
1033struct hblk *h;
1034{
1035 register hdr * hhdr;
1036
1037 h = GC_next_block(h);
1038 if (h == 0) return(0);
1039 hhdr = HDR(h);
1040 GC_push_marked(h, hhdr);
1041 return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
1042}
1043
1044#ifndef SMALL_CONFIG
1045/* Identical to above, but mark only from dirty pages */
1046struct hblk * GC_push_next_marked_dirty(h)
1047struct hblk *h;
1048{
1049 register hdr * hhdr = HDR(h);
1050
1051 if (!GC_dirty_maintained) { ABORT("dirty bits not set up"); }
1052 for (;;) {
1053 h = GC_next_block(h);
1054 if (h == 0) return(0);
1055 hhdr = HDR(h);
1056# ifdef STUBBORN_ALLOC
1057 if (hhdr -> hb_obj_kind == STUBBORN) {
1058 if (GC_page_was_changed(h) && GC_block_was_dirty(h, hhdr)) {
1059 break;
1060 }
1061 } else {
1062 if (GC_block_was_dirty(h, hhdr)) break;
1063 }
1064# else
1065 if (GC_block_was_dirty(h, hhdr)) break;
1066# endif
1067 h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
1068 }
1069 GC_push_marked(h, hhdr);
1070 return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
1071}
1072#endif
1073
1074/* Similar to above, but for uncollectable pages. Needed since we */
1075/* do not clear marks for such pages, even for full collections. */
1076struct hblk * GC_push_next_marked_uncollectable(h)
1077struct hblk *h;
1078{
1079 register hdr * hhdr = HDR(h);
1080
1081 for (;;) {
1082 h = GC_next_block(h);
1083 if (h == 0) return(0);
1084 hhdr = HDR(h);
1085 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) break;
1086 h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
1087 }
1088 GC_push_marked(h, hhdr);
1089 return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
1090}
1091
1092
This page took 0.110819 seconds and 5 git commands to generate.