Newer
Older
/*
* Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
* Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
* Copyright (c) 2000 by Hewlett-Packard Company. All rights reserved.
*
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
*
* Permission is hereby granted to use or copy this program
* for any purpose, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*
*/
# include <stdio.h>
/* We put this here to minimize the risk of inlining. */
/*VARARGS*/
#ifdef __WATCOMC__
void GC_noop(void *p, ...) {}
#else
void GC_noop() {}
#endif
/* Single argument version, robust against whole program analysis. */
void GC_noop1(x)
word x;
{
static VOLATILE word sink;
sink = x;
}
/* mark_proc GC_mark_procs[MAX_MARK_PROCS] = {0} -- declared in gc_priv.h */
word GC_n_mark_procs = GC_RESERVED_MARK_PROCS;
/* Initialize GC_obj_kinds properly and standard free lists properly. */
/* This must be done statically since they may be accessed before */
/* GC_init is called. */
/* It's done here, since we need to deal with mark descriptors. */
struct obj_kind GC_obj_kinds[MAXOBJKINDS] = {
/* PTRFREE */ { &GC_aobjfreelist[0], 0 /* filled in dynamically */,
0 | GC_DS_LENGTH, /* Adjusted in GC_init_inner for EXTRA_BYTES */
TRUE /* add length to descr */, TRUE },
/* UNCOLLECTABLE */
{ &GC_uobjfreelist[0], 0,
0 | GC_DS_LENGTH, TRUE /* add length to descr */, TRUE },
# ifdef ATOMIC_UNCOLLECTABLE
/* AUNCOLLECTABLE */
{ &GC_auobjfreelist[0], 0,
0 | GC_DS_LENGTH, FALSE /* add length to descr */, FALSE },
# endif
# ifdef STUBBORN_ALLOC
/*STUBBORN*/ { &GC_sobjfreelist[0], 0,
0 | GC_DS_LENGTH, TRUE /* add length to descr */, TRUE },
# endif
};
# ifdef ATOMIC_UNCOLLECTABLE
# ifdef STUBBORN_ALLOC
int GC_n_kinds = 5;
# else
int GC_n_kinds = 4;
# endif
# else
# ifdef STUBBORN_ALLOC
int GC_n_kinds = 4;
# else
int GC_n_kinds = 3;
# endif
# endif
# ifndef INITIAL_MARK_STACK_SIZE
# define INITIAL_MARK_STACK_SIZE (1*HBLKSIZE)
/* INITIAL_MARK_STACK_SIZE * sizeof(mse) should be a */
/* multiple of HBLKSIZE. */
/* The incremental collector actually likes a larger */
/* size, since it want to push all marked dirty objs */
/* before marking anything new. Currently we let it */
/* grow dynamically. */
# endif
/*
* Limits of stack for GC_mark routine.
* All ranges between GC_mark_stack(incl.) and GC_mark_stack_top(incl.) still
* need to be marked from.
*/
word GC_n_rescuing_pages; /* Number of dirty pages we marked from */
/* excludes ptrfree pages, etc. */
mse * GC_mark_stack;
#ifdef PARALLEL_MARK
mse * VOLATILE GC_mark_stack_top;
#else
mse * GC_mark_stack_top;
#endif
static struct hblk * scan_ptr;
mark_state_t GC_mark_state = MS_NONE;
GC_bool GC_mark_stack_too_small = FALSE;
GC_bool GC_objects_are_marked = FALSE; /* Are there collectable marked */
/* objects in the heap? */
/* Is a collection in progress? Note that this can return true in the */
/* nonincremental case, if a collection has been abandoned and the */
/* mark state is now MS_INVALID. */
GC_bool GC_collection_in_progress()
{
return(GC_mark_state != MS_NONE);
}
/* clear all mark bits in the header */
void GC_clear_hdr_marks(hhdr)
register hdr * hhdr;
{
# ifdef USE_MARK_BYTES
BZERO(hhdr -> hb_marks, MARK_BITS_SZ);
# else
BZERO(hhdr -> hb_marks, MARK_BITS_SZ*sizeof(word));
# endif
}
/* Set all mark bits in the header. Used for uncollectable blocks. */
void GC_set_hdr_marks(hhdr)
register hdr * hhdr;
{
register int i;
for (i = 0; i < MARK_BITS_SZ; ++i) {
# ifdef USE_MARK_BYTES
hhdr -> hb_marks[i] = 1;
# else
}
}
/*
* Clear all mark bits associated with block h.
*/
/*ARGSUSED*/
# if defined(__STDC__) || defined(__cplusplus)
static void clear_marks_for_block(struct hblk *h, word dummy)
# else
static void clear_marks_for_block(h, dummy)
struct hblk *h;
word dummy;
# endif
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
{
register hdr * hhdr = HDR(h);
if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) return;
/* Mark bit for these is cleared only once the object is */
/* explicitly deallocated. This either frees the block, or */
/* the bit is cleared once the object is on the free list. */
GC_clear_hdr_marks(hhdr);
}
/* Slow but general routines for setting/clearing/asking about mark bits */
void GC_set_mark_bit(p)
ptr_t p;
{
register struct hblk *h = HBLKPTR(p);
register hdr * hhdr = HDR(h);
register int word_no = (word *)p - (word *)h;
set_mark_bit_from_hdr(hhdr, word_no);
}
void GC_clear_mark_bit(p)
ptr_t p;
{
register struct hblk *h = HBLKPTR(p);
register hdr * hhdr = HDR(h);
register int word_no = (word *)p - (word *)h;
clear_mark_bit_from_hdr(hhdr, word_no);
}
GC_bool GC_is_marked(p)
ptr_t p;
{
register struct hblk *h = HBLKPTR(p);
register hdr * hhdr = HDR(h);
register int word_no = (word *)p - (word *)h;
return(mark_bit_from_hdr(hhdr, word_no));
}
/*
* Clear mark bits in all allocated heap blocks. This invalidates
* the marker invariant, and sets GC_mark_state to reflect this.
* (This implicitly starts marking to reestablish the invariant.)
*/
void GC_clear_marks()
{
GC_apply_to_all_blocks(clear_marks_for_block, (word)0);
GC_objects_are_marked = FALSE;
GC_mark_state = MS_INVALID;
scan_ptr = 0;
# ifdef GATHERSTATS
/* Counters reflect currently marked objects: reset here */
GC_composite_in_use = 0;
GC_atomic_in_use = 0;
# endif
}
/* Initiate a garbage collection. Initiates a full collection if the */
/* mark state is invalid. */
/*ARGSUSED*/
void GC_initiate_gc()
{
if (GC_dirty_maintained) GC_read_dirty();
# ifdef STUBBORN_ALLOC
GC_read_changed();
# endif
# ifdef CHECKSUMS
{
extern void GC_check_dirty();
if (GC_dirty_maintained) GC_check_dirty();
}
# endif
if (GC_mark_state == MS_NONE) {
GC_mark_state = MS_PUSH_RESCUERS;
} else if (GC_mark_state != MS_INVALID) {
ABORT("unexpected state");
} /* else this is really a full collection, and mark */
/* bits are invalid. */
scan_ptr = 0;
}
static void alloc_mark_stack();
/* Perform a small amount of marking. */
/* We try to touch roughly a page of memory. */
/* Return TRUE if we just finished a mark phase. */
/* Cold_gc_frame is an address inside a GC frame that */
/* remains valid until all marking is complete. */
/* A zero value indicates that it's OK to miss some */
/* register values. */
GC_bool GC_mark_some(cold_gc_frame)
ptr_t cold_gc_frame;
#if defined(MSWIN32) && !defined(__GNUC__)
/* Windows 98 appears to asynchronously create and remove writable */
/* memory mappings, for reasons we haven't yet understood. Since */
/* we look for writable regions to determine the root set, we may */
/* try to mark from an address range that disappeared since we */
/* started the collection. Thus we have to recover from faults here. */
/* This code does not appear to be necessary for Windows 95/NT/2000. */
/* Note that this code should never generate an incremental GC write */
/* fault. */
__try {
#endif /* defined(MSWIN32) && !defined(__GNUC__) */
switch(GC_mark_state) {
case MS_NONE:
return(FALSE);
case MS_PUSH_RESCUERS:
if (GC_mark_stack_top
>= GC_mark_stack_limit - INITIAL_MARK_STACK_SIZE/2) {
/* Go ahead and mark, even though that might cause us to */
/* see more marked dirty objects later on. Avoid this */
/* in the future. */
GC_mark_stack_too_small = TRUE;
return(FALSE);
} else {
scan_ptr = GC_push_next_marked_dirty(scan_ptr);
if (scan_ptr == 0) {
GC_printf1("Marked from %lu dirty pages\n",
(unsigned long)GC_n_rescuing_pages);
GC_push_roots(FALSE, cold_gc_frame);
GC_objects_are_marked = TRUE;
if (GC_mark_state != MS_INVALID) {
GC_mark_state = MS_ROOTS_PUSHED;
}
}
}
return(FALSE);
case MS_PUSH_UNCOLLECTABLE:
if (GC_mark_stack_top
>= GC_mark_stack + GC_mark_stack_size/4) {
# ifdef PARALLEL_MARK
/* Avoid this, since we don't parallelize the marker */
/* here. */
if (GC_parallel) GC_mark_stack_too_small = TRUE;
# endif
MARK_FROM_MARK_STACK();
return(FALSE);
} else {
scan_ptr = GC_push_next_marked_uncollectable(scan_ptr);
if (scan_ptr == 0) {
GC_push_roots(TRUE, cold_gc_frame);
GC_objects_are_marked = TRUE;
if (GC_mark_state != MS_INVALID) {
GC_mark_state = MS_ROOTS_PUSHED;
}
}
}
return(FALSE);
case MS_ROOTS_PUSHED:
# ifdef PARALLEL_MARK
/* In the incremental GC case, this currently doesn't */
/* quite do the right thing, since it runs to */
/* completion. On the other hand, starting a */
/* parallel marker is expensive, so perhaps it is */
/* the right thing? */
/* Eventually, incremental marking should run */
/* asynchronously in multiple threads, without grabbing */
/* the allocation lock. */
if (GC_parallel) {
GC_do_parallel_mark();
GC_ASSERT(GC_mark_stack_top < GC_first_nonempty);
GC_mark_stack_top = GC_mark_stack - 1;
if (GC_mark_stack_too_small) {
alloc_mark_stack(2*GC_mark_stack_size);
}
if (GC_mark_state == MS_ROOTS_PUSHED) {
GC_mark_state = MS_NONE;
return(TRUE);
} else {
return(FALSE);
}
}
# endif
return(FALSE);
} else {
GC_mark_state = MS_NONE;
if (GC_mark_stack_too_small) {
alloc_mark_stack(2*GC_mark_stack_size);
}
return(TRUE);
}
case MS_INVALID:
case MS_PARTIALLY_INVALID:
if (!GC_objects_are_marked) {
GC_mark_state = MS_PUSH_UNCOLLECTABLE;
return(FALSE);
}
if (GC_mark_stack_top >= GC_mark_stack) {
if (scan_ptr == 0 && GC_mark_state == MS_INVALID) {
/* About to start a heap scan for marked objects. */
/* Mark stack is empty. OK to reallocate. */
if (GC_mark_stack_too_small) {
alloc_mark_stack(2*GC_mark_stack_size);
}
GC_mark_state = MS_PARTIALLY_INVALID;
}
scan_ptr = GC_push_next_marked(scan_ptr);
if (scan_ptr == 0 && GC_mark_state == MS_PARTIALLY_INVALID) {
GC_push_roots(TRUE, cold_gc_frame);
GC_objects_are_marked = TRUE;
if (GC_mark_state != MS_INVALID) {
GC_mark_state = MS_ROOTS_PUSHED;
}
}
return(FALSE);
default:
ABORT("GC_mark_some: bad state");
return(FALSE);
}
#if defined(MSWIN32) && !defined(__GNUC__)
} __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
GC_printf0("Caught ACCESS_VIOLATION in marker. "
"Memory mapping disappeared.\n");
/* We have bad roots on the stack. Discard mark stack. */
/* Rescan from marked objects. Redetermine roots. */
GC_invalidate_mark_state();
scan_ptr = 0;
return FALSE;
}
#endif /* defined(MSWIN32) && !defined(__GNUC__) */
}
GC_bool GC_mark_stack_empty()
{
return(GC_mark_stack_top < GC_mark_stack);
}
#ifdef PROF_MARKER
word GC_prof_array[10];
# define PROF(n) GC_prof_array[n]++
#else
# define PROF(n)
#endif
/* Given a pointer to someplace other than a small object page or the */
/* first page of a large object, either: */
/* - return a pointer to somewhere in the first page of the large */
/* object, if current points to a large object. */
/* In this case *hhdr is replaced with a pointer to the header */
/* for the large object. */
/* - just return current if it does not point to a large object. */
ptr_t GC_find_start(current, hhdr, new_hdr_p, source)
ptr_t source;
ptr_t GC_find_start(current, hhdr, new_hdr_p)
register hdr *hhdr, **new_hdr_p;
do {
current = current - HBLKSIZE*(word)hhdr;
hhdr = HDR(current);
} while(IS_FORWARDING_ADDR_OR_NIL(hhdr));
/* current points to the start of the large object */
if (hhdr -> hb_flags & IGNORE_OFF_PAGE) return(0);
if ((word *)orig - (word *)current
>= (ptrdiff_t)(hhdr->hb_sz)) {
/* Pointer past the end of the block */
*new_hdr_p = hhdr;
return(current);
return(current);
# undef source
}
void GC_invalidate_mark_state()
{
GC_mark_state = MS_INVALID;
GC_mark_stack_top = GC_mark_stack-1;
}
mse * GC_signal_mark_stack_overflow(msp)
mse * msp;
{
GC_mark_state = MS_INVALID;
GC_printf1("Mark stack overflow; current size = %lu entries\n",
GC_mark_stack_size);
}
# endif
return(msp - GC_MARK_STACK_DISCARDS);
}
/*
* Mark objects pointed to by the regions described by
* mark stack entries between GC_mark_stack and GC_mark_stack_top,
* inclusive. Assumes the upper limit of a mark stack entry
* is never 0. A mark stack entry never has size 0.
* We try to traverse on the order of a hblk of memory before we return.
* Caller is responsible for calling this until the mark stack is empty.
* Note that this is the most performance critical routine in the
* collector. Hence it contains all sorts of ugly hacks to speed
* things up. In particular, we avoid procedure calls on the common
* path, we take advantage of peculiarities of the mark descriptor
* encoding, we optionally maintain a cache for the block address to
* header mapping, we prefetch when an object is "grayed", etc.
mse * GC_mark_from(mark_stack_top, mark_stack, mark_stack_limit)
mse * mark_stack_top;
mse * mark_stack;
mse * mark_stack_limit;
{
int credit = HBLKSIZE; /* Remaining credit for marking work */
register word * current_p; /* Pointer to current candidate ptr. */
register word current; /* Candidate pointer. */
register word * limit; /* (Incl) limit of current candidate */
/* range */
register word descr;
register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
register ptr_t least_ha = GC_least_plausible_heap_addr;
# define SPLIT_RANGE_WORDS 128 /* Must be power of 2. */
GC_objects_are_marked = TRUE;
# ifdef OS2 /* Use untweaked version to circumvent compiler problem */
while (mark_stack_top >= mark_stack && credit >= 0) {
while ((((ptr_t)mark_stack_top - (ptr_t)mark_stack) | credit)
current_p = mark_stack_top -> mse_start;
descr = mark_stack_top -> mse_descr;
retry:
/* current_p and descr describe the current object. */
/* The following is 0 only for small objects described by a simple */
/* length descriptor. For many applications this is the common */
/* case, so we try to detect it quickly. */
if (descr & ((~(WORDS_TO_BYTES(SPLIT_RANGE_WORDS) - 1)) | GC_DS_TAGS)) {
word tag = descr & GC_DS_TAGS;
/* Large length. */
/* Process part of the range to avoid pushing too much on the */
/* stack. */
GC_ASSERT(descr < GC_greatest_plausible_heap_addr
- GC_least_plausible_heap_addr);
# ifdef PARALLEL_MARK
# define SHARE_BYTES 2048
if (descr > SHARE_BYTES && GC_parallel
&& mark_stack_top < mark_stack_limit - 1) {
int new_size = (descr/2) & ~(sizeof(word)-1);
mark_stack_top -> mse_start = current_p;
mark_stack_top -> mse_descr = new_size + sizeof(word);
/* makes sure we handle */
/* misaligned pointers. */
mark_stack_top++;
current_p = (word *) ((char *)current_p + new_size);
descr -= new_size;
goto retry;
}
# endif /* PARALLEL_MARK */
mark_stack_top -> mse_start =
descr - WORDS_TO_BYTES(SPLIT_RANGE_WORDS-1);
/* Make sure that pointers overlapping the two ranges are */
/* considered. */
limit = (word *)((char *)limit + sizeof(word) - ALIGNMENT);
break;
case GC_DS_BITMAP:
mark_stack_top--;
descr &= ~GC_DS_TAGS;
credit -= WORDS_TO_BYTES(WORDSZ/2); /* guess */
while (descr != 0) {
if ((signed_word)descr < 0) {
current = *current_p;
if ((ptr_t)current >= least_ha && (ptr_t)current < greatest_ha) {
HC_PUSH_CONTENTS((ptr_t)current, mark_stack_top,
}
}
descr <<= 1;
++ current_p;
}
continue;
case GC_DS_PROC:
mark_stack_top--;
credit -= GC_PROC_BYTES;
mark_stack_top =
if ((signed_word)descr >= 0) {
/* Descriptor is in the object. */
descr = *(word *)((ptr_t)current_p + descr - GC_DS_PER_OBJECT);
} else {
/* Descriptor is in type descriptor pointed to by first */
/* word in object. */
ptr_t type_descr = *(ptr_t *)current_p;
/* type_descr is either a valid pointer to the descriptor */
/* structure, or this object was on a free list. If it */
/* it was anything but the last object on the free list, */
/* we will misinterpret the next object on the free list as */
/* the type descriptor, and get a 0 GC descriptor, which */
/* is ideal. Unfortunately, we need to check for the last */
/* object case explicitly. */
if (0 == type_descr) {
/* Rarely executed. */
continue;
}
descr = *(word *)(type_descr
- (descr - (GC_DS_PER_OBJECT
- GC_INDIR_PER_OBJ_BIAS)));
}
if (0 == descr) {
/* Can happen either because we generated a 0 descriptor */
/* or we saw a pointer to a free object. */
mark_stack_top--;
continue;
} else /* Small object with length descriptor */ {
limit = (word *)(((ptr_t)current_p) + (word)descr);
}
/* The simple case in which we're scanning a range. */
GC_ASSERT(!((word)current_p & (ALIGNMENT-1)));
{
# define PREF_DIST 4
# ifndef SMALL_CONFIG
word deferred;
/* Try to prefetch the next pointer to be examined asap. */
/* Empirically, this also seems to help slightly without */
/* prefetches, at least on linux/X86. Presumably this loop */
/* ends up with less register pressure, and gcc thus ends up */
/* generating slightly better code. Overall gcc code quality */
/* for this loop is still not great. */
for(;;) {
PREFETCH((ptr_t)limit - PREF_DIST*CACHE_LINE_SIZE);
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
deferred = *limit;
limit = (word *)((char *)limit - ALIGNMENT);
if ((ptr_t)deferred >= least_ha && (ptr_t)deferred < greatest_ha) {
PREFETCH(deferred);
break;
}
if (current_p > limit) goto next_object;
/* Unroll once, so we don't do too many of the prefetches */
/* based on limit. */
deferred = *limit;
limit = (word *)((char *)limit - ALIGNMENT);
if ((ptr_t)deferred >= least_ha && (ptr_t)deferred < greatest_ha) {
PREFETCH(deferred);
break;
}
if (current_p > limit) goto next_object;
}
# endif
while (current_p <= limit) {
/* Empirically, unrolling this loop doesn't help a lot. */
/* Since HC_PUSH_CONTENTS expands to a lot of code, */
/* we don't. */
current = *current_p;
PREFETCH((ptr_t)current_p + PREF_DIST*CACHE_LINE_SIZE);
if ((ptr_t)current >= least_ha && (ptr_t)current < greatest_ha) {
/* Prefetch the contents of the object we just pushed. It's */
/* likely we will need them soon. */
PREFETCH(current);
HC_PUSH_CONTENTS((ptr_t)current, mark_stack_top,
mark_stack_limit, current_p, exit2);
}
current_p = (word *)((char *)current_p + ALIGNMENT);
# ifndef SMALL_CONFIG
/* We still need to mark the entry we previously prefetched. */
/* We alrady know that it passes the preliminary pointer */
/* validity test. */
HC_PUSH_CONTENTS((ptr_t)deferred, mark_stack_top,
mark_stack_limit, current_p, exit4);
next_object:;
# endif
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
return mark_stack_top;
}
#ifdef PARALLEL_MARK
/* We assume we have an ANSI C Compiler. */
GC_bool GC_help_wanted = FALSE;
unsigned GC_helper_count = 0;
unsigned GC_active_count = 0;
mse * VOLATILE GC_first_nonempty;
word GC_mark_no = 0;
#define LOCAL_MARK_STACK_SIZE HBLKSIZE
/* Under normal circumstances, this is big enough to guarantee */
/* We don't overflow half of it in a single call to */
/* GC_mark_from. */
/* Steal mark stack entries starting at mse low into mark stack local */
/* until we either steal mse high, or we have max entries. */
/* Return a pointer to the top of the local mark stack. */
/* *next is replaced by a pointer to the next unscanned mark stack */
/* entry. */
mse * GC_steal_mark_stack(mse * low, mse * high, mse * local,
unsigned max, mse **next)
{
mse *p;
mse *top = local - 1;
unsigned i = 0;
GC_ASSERT(high >= low-1 && high - low + 1 <= GC_mark_stack_size);
for (p = low; p <= high && i <= max; ++p) {
word descr = *(volatile word *) &(p -> mse_descr);
if (descr != 0) {
*(volatile word *) &(p -> mse_descr) = 0;
++top;
top -> mse_descr = descr;
top -> mse_start = p -> mse_start;
GC_ASSERT( top -> mse_descr & GC_DS_TAGS != GC_DS_LENGTH ||
top -> mse_descr < GC_greatest_plausible_heap_addr
- GC_least_plausible_heap_addr);
/* There is no synchronization here. We assume that at */
/* least one thread will see the original descriptor. */
/* Otherwise we need a barrier. */
/* More than one thread may get this entry, but that's only */
/* a minor performance problem. */
/* If this is a big object, count it as */
/* size/256 + 1 objects. */
++i;
if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) i += (descr >> 8);
}
}
*next = p;
return top;
}
/* Copy back a local mark stack. */
/* low and high are inclusive bounds. */
void GC_return_mark_stack(mse * low, mse * high)
{
mse * my_top;
mse * my_start;
size_t stack_size;
if (high < low) return;
stack_size = high - low + 1;
GC_acquire_mark_lock();
my_top = GC_mark_stack_top;
my_start = my_top + 1;
if (my_start - GC_mark_stack + stack_size > GC_mark_stack_size) {
# ifdef CONDPRINT
if (GC_print_stats) {
GC_printf0("No room to copy back mark stack.");
}
# endif
GC_mark_state = MS_INVALID;
GC_mark_stack_too_small = TRUE;
/* We drop the local mark stack. We'll fix things later. */
} else {
BCOPY(low, my_start, stack_size * sizeof(mse));
GC_ASSERT(GC_mark_stack_top = my_top);
# if !defined(IA64) && !defined(HP_PA)
GC_memory_write_barrier();
# endif
/* On IA64, the volatile write acts as a release barrier. */
GC_mark_stack_top = my_top + stack_size;
}
GC_release_mark_lock();
GC_notify_all_marker();
}
/* Mark from the local mark stack. */
/* On return, the local mark stack is empty. */
/* But this may be achieved by copying the */
/* local mark stack back into the global one. */
void GC_do_local_mark(mse *local_mark_stack, mse *local_top)
{
unsigned n;
# define N_LOCAL_ITERS 1
# ifdef GC_ASSERTIONS
/* Make sure we don't hold mark lock. */
GC_acquire_mark_lock();
GC_release_mark_lock();
# endif
for (;;) {
for (n = 0; n < N_LOCAL_ITERS; ++n) {
local_top = GC_mark_from(local_top, local_mark_stack,
local_mark_stack + LOCAL_MARK_STACK_SIZE);
if (local_top < local_mark_stack) return;
if (local_top - local_mark_stack >= LOCAL_MARK_STACK_SIZE/2) {
GC_return_mark_stack(local_mark_stack, local_top);
return;
}
}
if (GC_mark_stack_top < GC_first_nonempty &&
GC_active_count < GC_helper_count
&& local_top > local_mark_stack + 1) {
/* Try to share the load, since the main stack is empty, */
/* and helper threads are waiting for a refill. */
/* The entries near the bottom of the stack are likely */
/* to require more work. Thus we return those, eventhough */
/* it's harder. */
mse * p;
mse * new_bottom = local_mark_stack
+ (local_top - local_mark_stack)/2;
GC_ASSERT(new_bottom > local_mark_stack
&& new_bottom < local_top);
GC_return_mark_stack(local_mark_stack, new_bottom - 1);
memmove(local_mark_stack, new_bottom,
(local_top - new_bottom + 1) * sizeof(mse));
local_top -= (new_bottom - local_mark_stack);
}
}
}
#define ENTRIES_TO_GET 5
long GC_markers = 2; /* Normally changed by thread-library- */
/* -specific code. */
/* Mark using the local mark stack until the global mark stack is empty */
/* and there are no active workers. Update GC_first_nonempty to reflect */
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/* progress. */
/* Caller does not hold mark lock. */
/* Caller has already incremented GC_helper_count. We decrement it, */
/* and maintain GC_active_count. */
void GC_mark_local(mse *local_mark_stack, int id)
{
mse * my_first_nonempty;
GC_acquire_mark_lock();
GC_active_count++;
my_first_nonempty = GC_first_nonempty;
GC_ASSERT(GC_first_nonempty >= GC_mark_stack &&
GC_first_nonempty <= GC_mark_stack_top + 1);
# ifdef PRINTSTATS
GC_printf1("Starting mark helper %lu\n", (unsigned long)id);
# endif
GC_release_mark_lock();
for (;;) {
size_t n_on_stack;
size_t n_to_get;
mse *next;
mse * my_top;
mse * local_top;
mse * global_first_nonempty = GC_first_nonempty;
GC_ASSERT(my_first_nonempty >= GC_mark_stack &&
my_first_nonempty <= GC_mark_stack_top + 1);
GC_ASSERT(global_first_nonempty >= GC_mark_stack &&
global_first_nonempty <= GC_mark_stack_top + 1);
if (my_first_nonempty < global_first_nonempty) {
my_first_nonempty = global_first_nonempty;
} else if (global_first_nonempty < my_first_nonempty) {
GC_compare_and_exchange((word *)(&GC_first_nonempty),
(word) global_first_nonempty,
(word) my_first_nonempty);
/* If this fails, we just go ahead, without updating */
/* GC_first_nonempty. */
}
/* Perhaps we should also update GC_first_nonempty, if it */
/* is less. But that would require using atomic updates. */
my_top = GC_mark_stack_top;
n_on_stack = my_top - my_first_nonempty + 1;
if (0 == n_on_stack) {
GC_acquire_mark_lock();
my_top = GC_mark_stack_top;
n_on_stack = my_top - my_first_nonempty + 1;
if (0 == n_on_stack) {
GC_active_count--;
GC_ASSERT(GC_active_count <= GC_helper_count);
/* Other markers may redeposit objects */
/* on the stack. */
if (0 == GC_active_count) GC_notify_all_marker();
while (GC_active_count > 0
&& GC_first_nonempty > GC_mark_stack_top) {
/* We will be notified if either GC_active_count */
/* reaches zero, or if more objects are pushed on */
/* the global mark stack. */
GC_wait_marker();
}
if (GC_active_count == 0 &&
GC_first_nonempty > GC_mark_stack_top) {
GC_bool need_to_notify = FALSE;
/* The above conditions can't be falsified while we */
/* hold the mark lock, since neither */
/* GC_active_count nor GC_mark_stack_top can */
/* change. GC_first_nonempty can only be */
/* incremented asynchronously. Thus we know that */
/* both conditions actually held simultaneously. */
GC_helper_count--;
if (0 == GC_helper_count) need_to_notify = TRUE;
# ifdef PRINTSTATS
GC_printf1(
"Finished mark helper %lu\n", (unsigned long)id);
# endif
GC_release_mark_lock();
if (need_to_notify) GC_notify_all_marker();
return;
}
/* else there's something on the stack again, or */
GC_active_count++;
GC_ASSERT(GC_active_count > 0);
GC_release_mark_lock();
continue;
} else {
GC_release_mark_lock();
}
}
n_to_get = ENTRIES_TO_GET;
if (n_on_stack < 2 * ENTRIES_TO_GET) n_to_get = 1;
local_top = GC_steal_mark_stack(my_first_nonempty, my_top,
local_mark_stack, n_to_get,
&my_first_nonempty);
GC_ASSERT(my_first_nonempty >= GC_mark_stack &&
my_first_nonempty <= GC_mark_stack_top + 1);
GC_do_local_mark(local_mark_stack, local_top);
}
/* Perform Parallel mark. */
/* We hold the GC lock, not the mark lock. */
/* Currently runs until the mark stack is */
/* empty. */
void GC_do_parallel_mark()
{
mse local_mark_stack[LOCAL_MARK_STACK_SIZE];
mse * local_top;
mse * my_top;
GC_acquire_mark_lock();
GC_ASSERT(I_HOLD_LOCK());
/* This could be a GC_ASSERT, but it seems safer to keep it on */
/* all the time, especially since it's cheap. */
if (GC_help_wanted || GC_active_count != 0 || GC_helper_count != 0)
ABORT("Tried to start parallel mark in bad state");
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ifdef PRINTSTATS
GC_printf1("Starting marking for mark phase number %lu\n",
(unsigned long)GC_mark_no);
# endif
GC_first_nonempty = GC_mark_stack;
GC_active_count = 0;
GC_helper_count = 1;
GC_help_wanted = TRUE;
GC_release_mark_lock();
GC_notify_all_marker();
/* Wake up potential helpers. */
GC_mark_local(local_mark_stack, 0);
GC_acquire_mark_lock();
GC_help_wanted = FALSE;
/* Done; clean up. */
while (GC_helper_count > 0) GC_wait_marker();
/* GC_helper_count cannot be incremented while GC_help_wanted == FALSE */
# ifdef PRINTSTATS
GC_printf1(
"Finished marking for mark phase number %lu\n",
(unsigned long)GC_mark_no);
# endif
GC_mark_no++;
GC_release_mark_lock();
GC_notify_all_marker();
}
/* Try to help out the marker, if it's running. */
/* We do not hold the GC lock, but the requestor does. */
void GC_help_marker(word my_mark_no)
{
mse local_mark_stack[LOCAL_MARK_STACK_SIZE];
unsigned my_id;
mse * my_first_nonempty;
if (!GC_parallel) return;
GC_acquire_mark_lock();
while (GC_mark_no < my_mark_no
|| !GC_help_wanted && GC_mark_no == my_mark_no) {
GC_wait_marker();
}
my_id = GC_helper_count;
if (GC_mark_no != my_mark_no || my_id >= GC_markers) {