2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
18 # include "private/gc_priv.h"
20 /* Data structure for list of root sets. */
21 /* We keep a hash table, so that we can filter out duplicate additions. */
22 /* Under Win32, we need to do a better job of filtering overlaps, so */
23 /* we resort to sequential search, and pay the price. */
24 /* This is really declared in gc_priv.h:
28 # if !defined(MSWIN32) && !defined(MSWINCE)
29 struct roots * r_next;
32 -- Delete before registering new dynamic libraries
35 struct roots GC_static_roots[MAX_ROOT_SETS];
38 int GC_no_dls = 0; /* Register dynamic library data segments. */
40 static int n_root_sets = 0;
42 /* GC_static_roots[0..n_root_sets) contains the valid root sets. */
44 # if !defined(NO_DEBUGGING)
46 void GC_print_static_roots()
51 for (i = 0; i < n_root_sets; i++) {
52 GC_printf2("From 0x%lx to 0x%lx ",
53 (unsigned long) GC_static_roots[i].r_start,
54 (unsigned long) GC_static_roots[i].r_end);
55 if (GC_static_roots[i].r_tmp) {
56 GC_printf0(" (temporary)\n");
60 total += GC_static_roots[i].r_end - GC_static_roots[i].r_start;
62 GC_printf1("Total size: %ld\n", (unsigned long) total);
63 if (GC_root_size != total) {
64 GC_printf1("GC_root_size incorrect: %ld!!\n",
65 (unsigned long) GC_root_size);
68 # endif /* NO_DEBUGGING */
70 /* Primarily for debugging support: */
71 /* Is the address p in one of the registered static */
73 GC_bool GC_is_static_root(p)
76 static int last_root_set = MAX_ROOT_SETS;
80 if (last_root_set < n_root_sets
81 && p >= GC_static_roots[last_root_set].r_start
82 && p < GC_static_roots[last_root_set].r_end) return(TRUE);
83 for (i = 0; i < n_root_sets; i++) {
84 if (p >= GC_static_roots[i].r_start
85 && p < GC_static_roots[i].r_end) {
93 #if !defined(MSWIN32) && !defined(MSWINCE)
95 # define LOG_RT_SIZE 6
96 # define RT_SIZE (1 << LOG_RT_SIZE) -- Power of 2, may be != MAX_ROOT_SETS
98 struct roots * GC_root_index[RT_SIZE];
99 -- Hash table header. Used only to check whether a range is
101 -- really defined in gc_priv.h
104 static int rt_hash(addr)
107 word result = (word) addr;
108 # if CPP_WORDSZ > 8*LOG_RT_SIZE
109 result ^= result >> 8*LOG_RT_SIZE;
111 # if CPP_WORDSZ > 4*LOG_RT_SIZE
112 result ^= result >> 4*LOG_RT_SIZE;
114 result ^= result >> 2*LOG_RT_SIZE;
115 result ^= result >> LOG_RT_SIZE;
116 result &= (RT_SIZE-1);
120 /* Is a range starting at b already in the table? If so return a */
121 /* pointer to it, else NIL. */
122 struct roots * GC_roots_present(b)
125 register int h = rt_hash(b);
126 register struct roots *p = GC_root_index[h];
129 if (p -> r_start == (ptr_t)b) return(p);
135 /* Add the given root structure to the index. */
136 static void add_roots_to_index(p)
139 register int h = rt_hash(p -> r_start);
141 p -> r_next = GC_root_index[h];
142 GC_root_index[h] = p;
145 # else /* MSWIN32 || MSWINCE */
147 # define add_roots_to_index(p)
154 word GC_root_size = 0;
156 void GC_add_roots(b, e)
163 GC_add_roots_inner(b, e, FALSE);
169 /* Add [b,e) to the root set. Adding the same interval a second time */
170 /* is a moderately fast noop, and hence benign. We do not handle */
171 /* different but overlapping intervals efficiently. (We do handle */
172 /* them correctly.) */
173 /* Tmp specifies that the interval may be deleted before */
174 /* reregistering dynamic libraries. */
175 void GC_add_roots_inner(b, e, tmp)
181 # if defined(MSWIN32) || defined(MSWINCE)
182 /* Spend the time to ensure that there are no overlapping */
183 /* or adjacent intervals. */
184 /* This could be done faster with e.g. a */
185 /* balanced tree. But the execution time here is */
186 /* virtually guaranteed to be dominated by the time it */
187 /* takes to scan the roots. */
191 for (i = 0; i < n_root_sets; i++) {
192 old = GC_static_roots + i;
193 if ((ptr_t)b <= old -> r_end && (ptr_t)e >= old -> r_start) {
194 if ((ptr_t)b < old -> r_start) {
195 old -> r_start = (ptr_t)b;
196 GC_root_size += (old -> r_start - (ptr_t)b);
198 if ((ptr_t)e > old -> r_end) {
199 old -> r_end = (ptr_t)e;
200 GC_root_size += ((ptr_t)e - old -> r_end);
206 if (i < n_root_sets) {
207 /* merge other overlapping intervals */
210 for (i++; i < n_root_sets; i++) {
211 other = GC_static_roots + i;
212 b = (char *)(other -> r_start);
213 e = (char *)(other -> r_end);
214 if ((ptr_t)b <= old -> r_end && (ptr_t)e >= old -> r_start) {
215 if ((ptr_t)b < old -> r_start) {
216 old -> r_start = (ptr_t)b;
217 GC_root_size += (old -> r_start - (ptr_t)b);
219 if ((ptr_t)e > old -> r_end) {
220 old -> r_end = (ptr_t)e;
221 GC_root_size += ((ptr_t)e - old -> r_end);
223 old -> r_tmp &= other -> r_tmp;
224 /* Delete this entry. */
225 GC_root_size -= (other -> r_end - other -> r_start);
226 other -> r_start = GC_static_roots[n_root_sets-1].r_start;
227 other -> r_end = GC_static_roots[n_root_sets-1].r_end;
235 old = GC_roots_present(b);
237 if ((ptr_t)e <= old -> r_end) /* already there */ return;
239 GC_root_size += (ptr_t)e - old -> r_end;
240 old -> r_end = (ptr_t)e;
244 if (n_root_sets == MAX_ROOT_SETS) {
245 ABORT("Too many root sets\n");
247 GC_static_roots[n_root_sets].r_start = (ptr_t)b;
248 GC_static_roots[n_root_sets].r_end = (ptr_t)e;
249 GC_static_roots[n_root_sets].r_tmp = tmp;
250 # if !defined(MSWIN32) && !defined(MSWINCE)
251 GC_static_roots[n_root_sets].r_next = 0;
253 add_roots_to_index(GC_static_roots + n_root_sets);
254 GC_root_size += (ptr_t)e - (ptr_t)b;
258 static GC_bool roots_were_cleared = FALSE;
260 void GC_clear_roots GC_PROTO((void))
266 roots_were_cleared = TRUE;
269 # if !defined(MSWIN32) && !defined(MSWINCE)
273 for (i = 0; i < RT_SIZE; i++) GC_root_index[i] = 0;
280 /* Internal use only; lock held. */
281 static void GC_remove_root_at_pos(i)
284 GC_root_size -= (GC_static_roots[i].r_end - GC_static_roots[i].r_start);
285 GC_static_roots[i].r_start = GC_static_roots[n_root_sets-1].r_start;
286 GC_static_roots[i].r_end = GC_static_roots[n_root_sets-1].r_end;
287 GC_static_roots[i].r_tmp = GC_static_roots[n_root_sets-1].r_tmp;
291 #if !defined(MSWIN32) && !defined(MSWINCE)
292 static void GC_rebuild_root_index()
296 for (i = 0; i < RT_SIZE; i++) GC_root_index[i] = 0;
297 for (i = 0; i < n_root_sets; i++)
298 add_roots_to_index(GC_static_roots + i);
302 /* Internal use only; lock held. */
303 void GC_remove_tmp_roots()
307 for (i = 0; i < n_root_sets; ) {
308 if (GC_static_roots[i].r_tmp) {
309 GC_remove_root_at_pos(i);
314 #if !defined(MSWIN32) && !defined(MSWINCE)
315 GC_rebuild_root_index();
319 #if !defined(MSWIN32) && !defined(MSWINCE)
320 void GC_remove_roots(b, e)
327 GC_remove_roots_inner(b, e);
332 /* Should only be called when the lock is held */
333 void GC_remove_roots_inner(b,e)
337 for (i = 0; i < n_root_sets; ) {
338 if (GC_static_roots[i].r_start >= (ptr_t)b && GC_static_roots[i].r_end <= (ptr_t)e) {
339 GC_remove_root_at_pos(i);
344 GC_rebuild_root_index();
346 #endif /* !defined(MSWIN32) && !defined(MSWINCE) */
348 #if defined(MSWIN32) || defined(_WIN32_WCE_EMULATION)
349 /* Workaround for the OS mapping and unmapping behind our back: */
350 /* Is the address p in one of the temporary static root sections? */
351 GC_bool GC_is_tmp_root(p)
354 static int last_root_set = MAX_ROOT_SETS;
357 if (last_root_set < n_root_sets
358 && p >= GC_static_roots[last_root_set].r_start
359 && p < GC_static_roots[last_root_set].r_end)
360 return GC_static_roots[last_root_set].r_tmp;
361 for (i = 0; i < n_root_sets; i++) {
362 if (p >= GC_static_roots[i].r_start
363 && p < GC_static_roots[i].r_end) {
365 return GC_static_roots[i].r_tmp;
370 #endif /* MSWIN32 || _WIN32_WCE_EMULATION */
377 # pragma warning(disable:4172)
379 return((ptr_t)(&dummy));
381 # pragma warning(default:4172)
386 * Data structure for excluded static roots.
387 * Real declaration is in gc_priv.h.
394 struct exclusion GC_excl_table[MAX_EXCLUSIONS];
395 -- Array of exclusions, ascending
399 size_t GC_excl_table_entries = 0; /* Number of entries in use. */
401 /* Return the first exclusion range that includes an address >= start_addr */
402 /* Assumes the exclusion table contains at least one entry (namely the */
403 /* GC data structures). */
404 struct exclusion * GC_next_exclusion(start_addr)
408 size_t high = GC_excl_table_entries - 1;
412 mid = (low + high) >> 1;
413 /* low <= mid < high */
414 if ((word) GC_excl_table[mid].e_end <= (word) start_addr) {
420 if ((word) GC_excl_table[low].e_end <= (word) start_addr) return 0;
421 return GC_excl_table + low;
424 void GC_exclude_static_roots(start, finish)
428 struct exclusion * next;
429 size_t next_index, i;
431 if (0 == GC_excl_table_entries) {
434 next = GC_next_exclusion(start);
437 if ((word)(next -> e_start) < (word) finish) {
438 /* incomplete error check. */
439 ABORT("exclusion ranges overlap");
441 if ((word)(next -> e_start) == (word) finish) {
442 /* extend old range backwards */
443 next -> e_start = (ptr_t)start;
446 next_index = next - GC_excl_table;
447 for (i = GC_excl_table_entries; i > next_index; --i) {
448 GC_excl_table[i] = GC_excl_table[i-1];
451 next_index = GC_excl_table_entries;
453 if (GC_excl_table_entries == MAX_EXCLUSIONS) ABORT("Too many exclusions");
454 GC_excl_table[next_index].e_start = (ptr_t)start;
455 GC_excl_table[next_index].e_end = (ptr_t)finish;
456 ++GC_excl_table_entries;
459 /* Invoke push_conditional on ranges that are not excluded. */
460 void GC_push_conditional_with_exclusions(bottom, top, all)
465 struct exclusion * next;
468 while (bottom < top) {
469 next = GC_next_exclusion(bottom);
470 if (0 == next || (excl_start = next -> e_start) >= top) {
471 GC_push_conditional(bottom, top, all);
474 if (excl_start > bottom) GC_push_conditional(bottom, excl_start, all);
475 bottom = next -> e_end;
480 * In the absence of threads, push the stack contents.
481 * In the presence of threads, push enough of the current stack
482 * to ensure that callee-save registers saved in collector frames have been
485 void GC_push_current_stack(cold_gc_frame)
488 # if defined(THREADS)
489 if (0 == cold_gc_frame) return;
490 # ifdef STACK_GROWS_DOWN
491 GC_push_all_eager(GC_approx_sp(), cold_gc_frame);
492 /* For IA64, the register stack backing store is handled */
493 /* in the thread-specific code. */
495 GC_push_all_eager( cold_gc_frame, GC_approx_sp() );
498 # ifdef STACK_GROWS_DOWN
499 GC_push_all_stack_partially_eager( GC_approx_sp(), GC_stackbottom,
502 /* We also need to push the register stack backing store. */
503 /* This should really be done in the same way as the */
504 /* regular stack. For now we fudge it a bit. */
505 /* Note that the backing store grows up, so we can't use */
506 /* GC_push_all_stack_partially_eager. */
508 extern word GC_save_regs_ret_val;
509 /* Previously set to backing store pointer. */
510 ptr_t bsp = (ptr_t) GC_save_regs_ret_val;
511 ptr_t cold_gc_bs_pointer;
512 if (GC_all_interior_pointers) {
513 cold_gc_bs_pointer = bsp - 2048;
514 if (cold_gc_bs_pointer < BACKING_STORE_BASE) {
515 cold_gc_bs_pointer = BACKING_STORE_BASE;
517 GC_push_all_stack(BACKING_STORE_BASE, cold_gc_bs_pointer);
520 cold_gc_bs_pointer = BACKING_STORE_BASE;
522 GC_push_all_eager(cold_gc_bs_pointer, bsp);
523 /* All values should be sufficiently aligned that we */
524 /* dont have to worry about the boundary. */
528 GC_push_all_stack_partially_eager( GC_stackbottom, GC_approx_sp(),
531 # endif /* !THREADS */
535 * Push GC internal roots. Only called if there is some reason to believe
536 * these would not otherwise get registered.
538 void GC_push_gc_structures GC_PROTO((void))
540 GC_push_finalizer_structures();
541 GC_push_stubborn_structures();
542 # if defined(THREADS)
543 GC_push_thread_structures();
547 #ifdef THREAD_LOCAL_ALLOC
548 void GC_mark_thread_local_free_lists();
551 void GC_cond_register_dynamic_libraries()
553 # if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
554 || defined(PCR)) && !defined(SRC_M3)
555 GC_remove_tmp_roots();
556 if (!GC_no_dls) GC_register_dynamic_libraries();
563 * Call the mark routines (GC_tl_push for a single pointer, GC_push_conditional
564 * on groups of pointers) on every top level accessible pointer.
565 * If all is FALSE, arrange to push only possibly altered values.
566 * Cold_gc_frame is an address inside a GC frame that
567 * remains valid until all marking is complete.
568 * A zero value indicates that it's OK to miss some
571 void GC_push_roots(all, cold_gc_frame)
579 * Next push static data. This must happen early on, since it's
580 * not robust against mark stack overflow.
582 /* Reregister dynamic libraries, in case one got added. */
583 /* There is some argument for doing this as late as possible, */
584 /* especially on win32, where it can change asynchronously. */
585 /* In those cases, we do it here. But on other platforms, it's */
586 /* not safe with the world stopped, so we do it earlier. */
587 # if !defined(REGISTER_LIBRARIES_EARLY)
588 GC_cond_register_dynamic_libraries();
591 /* Mark everything in static data areas */
592 for (i = 0; i < n_root_sets; i++) {
593 GC_push_conditional_with_exclusions(
594 GC_static_roots[i].r_start,
595 GC_static_roots[i].r_end, all);
598 /* Mark all free list header blocks, if those were allocated from */
599 /* the garbage collected heap. This makes sure they don't */
600 /* disappear if we are not marking from static data. It also */
601 /* saves us the trouble of scanning them, and possibly that of */
602 /* marking the freelists. */
603 for (kind = 0; kind < GC_n_kinds; kind++) {
604 GC_PTR base = GC_base(GC_obj_kinds[kind].ok_freelist);
606 GC_set_mark_bit(base);
610 /* Mark from GC internal roots if those might otherwise have */
612 if (GC_no_dls || roots_were_cleared) {
613 GC_push_gc_structures();
616 /* Mark thread local free lists, even if their mark */
617 /* descriptor excludes the link field. */
618 /* If the world is not stopped, this is unsafe. It is */
619 /* also unnecessary, since we will do this again with the */
621 # ifdef THREAD_LOCAL_ALLOC
622 if (GC_world_stopped) GC_mark_thread_local_free_lists();
626 * Now traverse stacks, and mark from register contents.
627 * These must be done last, since they can legitimately overflow
630 # ifdef USE_GENERIC_PUSH_REGS
631 GC_generic_push_regs(cold_gc_frame);
632 /* Also pushes stack, so that we catch callee-save registers */
633 /* saved inside the GC_push_regs frame. */
636 * push registers - i.e., call GC_push_one(r) for each
637 * register contents r.
639 GC_push_regs(); /* usually defined in machine_dep.c */
640 GC_push_current_stack(cold_gc_frame);
641 /* In the threads case, this only pushes collector frames. */
642 /* In the case of linux threads on IA64, the hot section of */
643 /* the main stack is marked here, but the register stack */
644 /* backing store is handled in the threads-specific code. */
646 if (GC_push_other_roots != 0) (*GC_push_other_roots)();
647 /* In the threads case, this also pushes thread stacks. */
648 /* Note that without interior pointer recognition lots */
649 /* of stuff may have been pushed already, and this */
650 /* should be careful about mark stack overflows. */