Import boehm-gc snapshot, taken from
[official-gcc.git] / boehm-gc / mark_rts.c
blob7ffe82a6df1bd64d416459576f63b459b05c85af
1 /*
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.
15 #include "private/gc_priv.h"
17 #include <stdio.h>
19 /* Data structure for list of root sets. */
20 /* We keep a hash table, so that we can filter out duplicate additions. */
21 /* Under Win32, we need to do a better job of filtering overlaps, so */
22 /* we resort to sequential search, and pay the price. */
23 /* This is really declared in gc_priv.h:
24 struct roots {
25 ptr_t r_start;
26 ptr_t r_end;
27 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
28 struct roots * r_next;
29 # endif
30 GC_bool r_tmp;
31 -- Delete before registering new dynamic libraries
34 struct roots GC_static_roots[MAX_ROOT_SETS];
37 int GC_no_dls = 0; /* Register dynamic library data segments. */
39 static int n_root_sets = 0;
40 /* GC_static_roots[0..n_root_sets) contains the valid root sets. */
42 #if !defined(NO_DEBUGGING) || defined(GC_ASSERTIONS)
43 /* Should return the same value as GC_root_size. */
44 GC_INNER word GC_compute_root_size(void)
46 int i;
47 word size = 0;
49 for (i = 0; i < n_root_sets; i++) {
50 size += GC_static_roots[i].r_end - GC_static_roots[i].r_start;
52 return size;
54 #endif /* !NO_DEBUGGING || GC_ASSERTIONS */
56 #if !defined(NO_DEBUGGING)
57 /* For debugging: */
58 void GC_print_static_roots(void)
60 int i;
61 word size;
63 for (i = 0; i < n_root_sets; i++) {
64 GC_printf("From %p to %p%s\n",
65 GC_static_roots[i].r_start, GC_static_roots[i].r_end,
66 GC_static_roots[i].r_tmp ? " (temporary)" : "");
68 GC_printf("GC_root_size: %lu\n", (unsigned long)GC_root_size);
70 if ((size = GC_compute_root_size()) != GC_root_size)
71 GC_err_printf("GC_root_size incorrect!! Should be: %lu\n",
72 (unsigned long)size);
74 #endif /* !NO_DEBUGGING */
76 #ifndef THREADS
77 /* Primarily for debugging support: */
78 /* Is the address p in one of the registered static root sections? */
79 GC_INNER GC_bool GC_is_static_root(ptr_t p)
81 static int last_root_set = MAX_ROOT_SETS;
82 int i;
84 if (last_root_set < n_root_sets
85 && (word)p >= (word)GC_static_roots[last_root_set].r_start
86 && (word)p < (word)GC_static_roots[last_root_set].r_end)
87 return(TRUE);
88 for (i = 0; i < n_root_sets; i++) {
89 if ((word)p >= (word)GC_static_roots[i].r_start
90 && (word)p < (word)GC_static_roots[i].r_end) {
91 last_root_set = i;
92 return(TRUE);
95 return(FALSE);
97 #endif /* !THREADS */
99 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
101 # define LOG_RT_SIZE 6
102 # define RT_SIZE (1 << LOG_RT_SIZE) -- Power of 2, may be != MAX_ROOT_SETS
104 struct roots * GC_root_index[RT_SIZE];
105 -- Hash table header. Used only to check whether a range is
106 -- already present.
107 -- really defined in gc_priv.h
110 GC_INLINE int rt_hash(ptr_t addr)
112 word result = (word) addr;
113 # if CPP_WORDSZ > 8*LOG_RT_SIZE
114 result ^= result >> 8*LOG_RT_SIZE;
115 # endif
116 # if CPP_WORDSZ > 4*LOG_RT_SIZE
117 result ^= result >> 4*LOG_RT_SIZE;
118 # endif
119 result ^= result >> 2*LOG_RT_SIZE;
120 result ^= result >> LOG_RT_SIZE;
121 result &= (RT_SIZE-1);
122 return(result);
125 /* Is a range starting at b already in the table? If so return a */
126 /* pointer to it, else NULL. */
127 GC_INNER void * GC_roots_present(ptr_t b)
129 int h = rt_hash(b);
130 struct roots *p = GC_root_index[h];
132 while (p != 0) {
133 if (p -> r_start == (ptr_t)b) return(p);
134 p = p -> r_next;
136 return NULL;
139 /* Add the given root structure to the index. */
140 GC_INLINE void add_roots_to_index(struct roots *p)
142 int h = rt_hash(p -> r_start);
144 p -> r_next = GC_root_index[h];
145 GC_root_index[h] = p;
147 #endif /* !MSWIN32 && !MSWINCE && !CYGWIN32 */
149 GC_INNER word GC_root_size = 0;
151 GC_API void GC_CALL GC_add_roots(void *b, void *e)
153 DCL_LOCK_STATE;
155 if (!EXPECT(GC_is_initialized, TRUE)) GC_init();
156 LOCK();
157 GC_add_roots_inner((ptr_t)b, (ptr_t)e, FALSE);
158 UNLOCK();
162 /* Add [b,e) to the root set. Adding the same interval a second time */
163 /* is a moderately fast no-op, and hence benign. We do not handle */
164 /* different but overlapping intervals efficiently. (We do handle */
165 /* them correctly.) */
166 /* Tmp specifies that the interval may be deleted before */
167 /* re-registering dynamic libraries. */
168 void GC_add_roots_inner(ptr_t b, ptr_t e, GC_bool tmp)
170 struct roots * old;
172 GC_ASSERT((word)b <= (word)e);
173 b = (ptr_t)(((word)b + (sizeof(word) - 1)) & ~(sizeof(word) - 1));
174 /* round b up to word boundary */
175 e = (ptr_t)((word)e & ~(sizeof(word) - 1));
176 /* round e down to word boundary */
177 if ((word)b >= (word)e) return; /* nothing to do */
179 # if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
180 /* Spend the time to ensure that there are no overlapping */
181 /* or adjacent intervals. */
182 /* This could be done faster with e.g. a */
183 /* balanced tree. But the execution time here is */
184 /* virtually guaranteed to be dominated by the time it */
185 /* takes to scan the roots. */
187 register int i;
188 old = 0; /* initialized to prevent warning. */
189 for (i = 0; i < n_root_sets; i++) {
190 old = GC_static_roots + i;
191 if ((word)b <= (word)old->r_end
192 && (word)e >= (word)old->r_start) {
193 if ((word)b < (word)old->r_start) {
194 GC_root_size += old->r_start - b;
195 old -> r_start = b;
197 if ((word)e > (word)old->r_end) {
198 GC_root_size += e - old->r_end;
199 old -> r_end = e;
201 old -> r_tmp &= tmp;
202 break;
205 if (i < n_root_sets) {
206 /* merge other overlapping intervals */
207 struct roots *other;
209 for (i++; i < n_root_sets; i++) {
210 other = GC_static_roots + i;
211 b = other -> r_start;
212 e = other -> r_end;
213 if ((word)b <= (word)old->r_end
214 && (word)e >= (word)old->r_start) {
215 if ((word)b < (word)old->r_start) {
216 GC_root_size += old->r_start - b;
217 old -> r_start = b;
219 if ((word)e > (word)old->r_end) {
220 GC_root_size += e - old->r_end;
221 old -> r_end = e;
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;
228 n_root_sets--;
231 return;
234 # else
235 old = (struct roots *)GC_roots_present(b);
236 if (old != 0) {
237 if ((word)e <= (word)old->r_end) /* already there */ return;
238 /* else extend */
239 GC_root_size += e - old -> r_end;
240 old -> r_end = e;
241 return;
243 # endif
244 if (n_root_sets == MAX_ROOT_SETS) {
245 ABORT("Too many root sets");
248 # ifdef DEBUG_ADD_DEL_ROOTS
249 GC_log_printf("Adding data root section %d: %p .. %p\n",
250 n_root_sets, b, e);
251 # endif
252 GC_static_roots[n_root_sets].r_start = (ptr_t)b;
253 GC_static_roots[n_root_sets].r_end = (ptr_t)e;
254 GC_static_roots[n_root_sets].r_tmp = tmp;
255 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
256 GC_static_roots[n_root_sets].r_next = 0;
257 add_roots_to_index(GC_static_roots + n_root_sets);
258 # endif
259 GC_root_size += e - b;
260 n_root_sets++;
263 static GC_bool roots_were_cleared = FALSE;
265 GC_API void GC_CALL GC_clear_roots(void)
267 DCL_LOCK_STATE;
269 if (!EXPECT(GC_is_initialized, TRUE)) GC_init();
270 LOCK();
271 roots_were_cleared = TRUE;
272 n_root_sets = 0;
273 GC_root_size = 0;
274 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
275 BZERO(GC_root_index, RT_SIZE * sizeof(void *));
276 # endif
277 # ifdef DEBUG_ADD_DEL_ROOTS
278 GC_log_printf("Clear all data root sections\n");
279 # endif
280 UNLOCK();
283 /* Internal use only; lock held. */
284 STATIC void GC_remove_root_at_pos(int i)
286 # ifdef DEBUG_ADD_DEL_ROOTS
287 GC_log_printf("Remove data root section %d: %p .. %p\n",
288 i, GC_static_roots[i].r_start, GC_static_roots[i].r_end);
289 # endif
290 GC_root_size -= (GC_static_roots[i].r_end - GC_static_roots[i].r_start);
291 GC_static_roots[i].r_start = GC_static_roots[n_root_sets-1].r_start;
292 GC_static_roots[i].r_end = GC_static_roots[n_root_sets-1].r_end;
293 GC_static_roots[i].r_tmp = GC_static_roots[n_root_sets-1].r_tmp;
294 n_root_sets--;
297 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
298 STATIC void GC_rebuild_root_index(void)
300 int i;
301 BZERO(GC_root_index, RT_SIZE * sizeof(void *));
302 for (i = 0; i < n_root_sets; i++)
303 add_roots_to_index(GC_static_roots + i);
305 #endif
307 #if defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
308 || defined(PCR) || defined(CYGWIN32)
309 /* Internal use only; lock held. */
310 STATIC void GC_remove_tmp_roots(void)
312 int i;
314 for (i = 0; i < n_root_sets; ) {
315 if (GC_static_roots[i].r_tmp) {
316 GC_remove_root_at_pos(i);
317 } else {
318 i++;
321 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
322 GC_rebuild_root_index();
323 # endif
325 #endif
327 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
328 STATIC void GC_remove_roots_inner(ptr_t b, ptr_t e);
330 GC_API void GC_CALL GC_remove_roots(void *b, void *e)
332 DCL_LOCK_STATE;
334 /* Quick check whether has nothing to do */
335 if ((((word)b + (sizeof(word) - 1)) & ~(sizeof(word) - 1)) >=
336 ((word)e & ~(sizeof(word) - 1)))
337 return;
339 LOCK();
340 GC_remove_roots_inner((ptr_t)b, (ptr_t)e);
341 UNLOCK();
344 /* Should only be called when the lock is held */
345 STATIC void GC_remove_roots_inner(ptr_t b, ptr_t e)
347 int i;
348 for (i = 0; i < n_root_sets; ) {
349 if ((word)GC_static_roots[i].r_start >= (word)b
350 && (word)GC_static_roots[i].r_end <= (word)e) {
351 GC_remove_root_at_pos(i);
352 } else {
353 i++;
356 GC_rebuild_root_index();
358 #endif /* !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32) */
360 #if (defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)) \
361 && !defined(NO_DEBUGGING)
362 /* Not used at present (except for, may be, debugging purpose). */
363 /* Workaround for the OS mapping and unmapping behind our back: */
364 /* Is the address p in one of the temporary static root sections? */
365 GC_bool GC_is_tmp_root(ptr_t p)
367 static int last_root_set = MAX_ROOT_SETS;
368 register int i;
370 if (last_root_set < n_root_sets
371 && (word)p >= (word)GC_static_roots[last_root_set].r_start
372 && (word)p < (word)GC_static_roots[last_root_set].r_end)
373 return GC_static_roots[last_root_set].r_tmp;
374 for (i = 0; i < n_root_sets; i++) {
375 if ((word)p >= (word)GC_static_roots[i].r_start
376 && (word)p < (word)GC_static_roots[i].r_end) {
377 last_root_set = i;
378 return GC_static_roots[i].r_tmp;
381 return(FALSE);
383 #endif /* MSWIN32 || MSWINCE || CYGWIN32 */
385 GC_INNER ptr_t GC_approx_sp(void)
387 volatile word sp;
388 sp = (word)&sp;
389 /* Also force stack to grow if necessary. Otherwise the */
390 /* later accesses might cause the kernel to think we're */
391 /* doing something wrong. */
392 return((ptr_t)sp);
393 /* GNU C: alternatively, we may return the value of */
394 /*__builtin_frame_address(0). */
398 * Data structure for excluded static roots.
399 * Real declaration is in gc_priv.h.
401 struct exclusion {
402 ptr_t e_start;
403 ptr_t e_end;
406 struct exclusion GC_excl_table[MAX_EXCLUSIONS];
407 -- Array of exclusions, ascending
408 -- address order.
411 STATIC size_t GC_excl_table_entries = 0;/* Number of entries in use. */
413 /* Return the first exclusion range that includes an address >= start_addr */
414 /* Assumes the exclusion table contains at least one entry (namely the */
415 /* GC data structures). */
416 STATIC struct exclusion * GC_next_exclusion(ptr_t start_addr)
418 size_t low = 0;
419 size_t high = GC_excl_table_entries - 1;
420 size_t mid;
422 while (high > low) {
423 mid = (low + high) >> 1;
424 /* low <= mid < high */
425 if ((word) GC_excl_table[mid].e_end <= (word) start_addr) {
426 low = mid + 1;
427 } else {
428 high = mid;
431 if ((word) GC_excl_table[low].e_end <= (word) start_addr) return 0;
432 return GC_excl_table + low;
435 /* Should only be called when the lock is held. The range boundaries */
436 /* should be properly aligned and valid. */
437 GC_INNER void GC_exclude_static_roots_inner(void *start, void *finish)
439 struct exclusion * next;
440 size_t next_index, i;
442 GC_ASSERT((word)start % sizeof(word) == 0);
443 GC_ASSERT((word)start < (word)finish);
445 if (0 == GC_excl_table_entries) {
446 next = 0;
447 } else {
448 next = GC_next_exclusion(start);
450 if (0 != next) {
451 if ((word)(next -> e_start) < (word) finish) {
452 /* incomplete error check. */
453 ABORT("Exclusion ranges overlap");
455 if ((word)(next -> e_start) == (word) finish) {
456 /* extend old range backwards */
457 next -> e_start = (ptr_t)start;
458 return;
460 next_index = next - GC_excl_table;
461 for (i = GC_excl_table_entries; i > next_index; --i) {
462 GC_excl_table[i] = GC_excl_table[i-1];
464 } else {
465 next_index = GC_excl_table_entries;
467 if (GC_excl_table_entries == MAX_EXCLUSIONS) ABORT("Too many exclusions");
468 GC_excl_table[next_index].e_start = (ptr_t)start;
469 GC_excl_table[next_index].e_end = (ptr_t)finish;
470 ++GC_excl_table_entries;
473 GC_API void GC_CALL GC_exclude_static_roots(void *b, void *e)
475 DCL_LOCK_STATE;
477 if (b == e) return; /* nothing to exclude? */
479 /* Round boundaries (in direction reverse to that of GC_add_roots). */
480 b = (void *)((word)b & ~(sizeof(word) - 1));
481 e = (void *)(((word)e + (sizeof(word) - 1)) & ~(sizeof(word) - 1));
482 if (0 == e) e = (void *)(word)(~(sizeof(word) - 1)); /* handle overflow */
484 LOCK();
485 GC_exclude_static_roots_inner(b, e);
486 UNLOCK();
489 /* Invoke push_conditional on ranges that are not excluded. */
490 STATIC void GC_push_conditional_with_exclusions(ptr_t bottom, ptr_t top,
491 GC_bool all GC_ATTR_UNUSED)
493 struct exclusion * next;
494 ptr_t excl_start;
496 while ((word)bottom < (word)top) {
497 next = GC_next_exclusion(bottom);
498 if (0 == next || (word)(excl_start = next -> e_start) >= (word)top) {
499 GC_PUSH_CONDITIONAL(bottom, top, all);
500 return;
502 if ((word)excl_start > (word)bottom)
503 GC_PUSH_CONDITIONAL(bottom, excl_start, all);
504 bottom = next -> e_end;
508 #ifdef IA64
509 /* Similar to GC_push_all_stack_sections() but for IA-64 registers store. */
510 GC_INNER void GC_push_all_register_sections(ptr_t bs_lo, ptr_t bs_hi,
511 int eager, struct GC_traced_stack_sect_s *traced_stack_sect)
513 while (traced_stack_sect != NULL) {
514 ptr_t frame_bs_lo = traced_stack_sect -> backing_store_end;
515 GC_ASSERT((word)frame_bs_lo <= (word)bs_hi);
516 if (eager) {
517 GC_push_all_eager(frame_bs_lo, bs_hi);
518 } else {
519 GC_push_all_stack(frame_bs_lo, bs_hi);
521 bs_hi = traced_stack_sect -> saved_backing_store_ptr;
522 traced_stack_sect = traced_stack_sect -> prev;
524 GC_ASSERT((word)bs_lo <= (word)bs_hi);
525 if (eager) {
526 GC_push_all_eager(bs_lo, bs_hi);
527 } else {
528 GC_push_all_stack(bs_lo, bs_hi);
531 #endif /* IA64 */
533 #ifdef THREADS
535 GC_INNER void GC_push_all_stack_sections(ptr_t lo, ptr_t hi,
536 struct GC_traced_stack_sect_s *traced_stack_sect)
538 while (traced_stack_sect != NULL) {
539 GC_ASSERT((word)lo HOTTER_THAN (word)traced_stack_sect);
540 # ifdef STACK_GROWS_UP
541 GC_push_all_stack((ptr_t)traced_stack_sect, lo);
542 # else /* STACK_GROWS_DOWN */
543 GC_push_all_stack(lo, (ptr_t)traced_stack_sect);
544 # endif
545 lo = traced_stack_sect -> saved_stack_ptr;
546 GC_ASSERT(lo != NULL);
547 traced_stack_sect = traced_stack_sect -> prev;
549 GC_ASSERT(!((word)hi HOTTER_THAN (word)lo));
550 # ifdef STACK_GROWS_UP
551 /* We got them backwards! */
552 GC_push_all_stack(hi, lo);
553 # else /* STACK_GROWS_DOWN */
554 GC_push_all_stack(lo, hi);
555 # endif
558 #else /* !THREADS */
560 # ifdef TRACE_BUF
561 /* Defined in mark.c. */
562 void GC_add_trace_entry(char *kind, word arg1, word arg2);
563 # endif
565 /* Similar to GC_push_all_eager, but only the */
566 /* part hotter than cold_gc_frame is scanned */
567 /* immediately. Needed to ensure that callee- */
568 /* save registers are not missed. */
570 * A version of GC_push_all that treats all interior pointers as valid
571 * and scans part of the area immediately, to make sure that saved
572 * register values are not lost.
573 * Cold_gc_frame delimits the stack section that must be scanned
574 * eagerly. A zero value indicates that no eager scanning is needed.
575 * We don't need to worry about the MANUAL_VDB case here, since this
576 * is only called in the single-threaded case. We assume that we
577 * cannot collect between an assignment and the corresponding
578 * GC_dirty() call.
580 STATIC void GC_push_all_stack_partially_eager(ptr_t bottom, ptr_t top,
581 ptr_t cold_gc_frame)
583 if (!NEED_FIXUP_POINTER && GC_all_interior_pointers) {
584 /* Push the hot end of the stack eagerly, so that register values */
585 /* saved inside GC frames are marked before they disappear. */
586 /* The rest of the marking can be deferred until later. */
587 if (0 == cold_gc_frame) {
588 GC_push_all_stack(bottom, top);
589 return;
591 GC_ASSERT((word)bottom <= (word)cold_gc_frame
592 && (word)cold_gc_frame <= (word)top);
593 # ifdef STACK_GROWS_DOWN
594 GC_push_all(cold_gc_frame - sizeof(ptr_t), top);
595 GC_push_all_eager(bottom, cold_gc_frame);
596 # else /* STACK_GROWS_UP */
597 GC_push_all(bottom, cold_gc_frame + sizeof(ptr_t));
598 GC_push_all_eager(cold_gc_frame, top);
599 # endif /* STACK_GROWS_UP */
600 } else {
601 GC_push_all_eager(bottom, top);
603 # ifdef TRACE_BUF
604 GC_add_trace_entry("GC_push_all_stack", bottom, top);
605 # endif
608 /* Similar to GC_push_all_stack_sections() but also uses cold_gc_frame. */
609 STATIC void GC_push_all_stack_part_eager_sections(ptr_t lo, ptr_t hi,
610 ptr_t cold_gc_frame, struct GC_traced_stack_sect_s *traced_stack_sect)
612 GC_ASSERT(traced_stack_sect == NULL || cold_gc_frame == NULL ||
613 (word)cold_gc_frame HOTTER_THAN (word)traced_stack_sect);
615 while (traced_stack_sect != NULL) {
616 GC_ASSERT((word)lo HOTTER_THAN (word)traced_stack_sect);
617 # ifdef STACK_GROWS_UP
618 GC_push_all_stack_partially_eager((ptr_t)traced_stack_sect, lo,
619 cold_gc_frame);
620 # else /* STACK_GROWS_DOWN */
621 GC_push_all_stack_partially_eager(lo, (ptr_t)traced_stack_sect,
622 cold_gc_frame);
623 # endif
624 lo = traced_stack_sect -> saved_stack_ptr;
625 GC_ASSERT(lo != NULL);
626 traced_stack_sect = traced_stack_sect -> prev;
627 cold_gc_frame = NULL; /* Use at most once. */
630 GC_ASSERT(!((word)hi HOTTER_THAN (word)lo));
631 # ifdef STACK_GROWS_UP
632 /* We got them backwards! */
633 GC_push_all_stack_partially_eager(hi, lo, cold_gc_frame);
634 # else /* STACK_GROWS_DOWN */
635 GC_push_all_stack_partially_eager(lo, hi, cold_gc_frame);
636 # endif
639 #endif /* !THREADS */
641 /* Push enough of the current stack eagerly to */
642 /* ensure that callee-save registers saved in */
643 /* GC frames are scanned. */
644 /* In the non-threads case, schedule entire */
645 /* stack for scanning. */
646 /* The second argument is a pointer to the */
647 /* (possibly null) thread context, for */
648 /* (currently hypothetical) more precise */
649 /* stack scanning. */
651 * In the absence of threads, push the stack contents.
652 * In the presence of threads, push enough of the current stack
653 * to ensure that callee-save registers saved in collector frames have been
654 * seen.
655 * FIXME: Merge with per-thread stuff.
657 STATIC void GC_push_current_stack(ptr_t cold_gc_frame,
658 void * context GC_ATTR_UNUSED)
660 # if defined(THREADS)
661 if (0 == cold_gc_frame) return;
662 # ifdef STACK_GROWS_DOWN
663 GC_push_all_eager(GC_approx_sp(), cold_gc_frame);
664 /* For IA64, the register stack backing store is handled */
665 /* in the thread-specific code. */
666 # else
667 GC_push_all_eager(cold_gc_frame, GC_approx_sp());
668 # endif
669 # else
670 GC_push_all_stack_part_eager_sections(GC_approx_sp(), GC_stackbottom,
671 cold_gc_frame, GC_traced_stack_sect);
672 # ifdef IA64
673 /* We also need to push the register stack backing store. */
674 /* This should really be done in the same way as the */
675 /* regular stack. For now we fudge it a bit. */
676 /* Note that the backing store grows up, so we can't use */
677 /* GC_push_all_stack_partially_eager. */
679 ptr_t bsp = GC_save_regs_ret_val;
680 ptr_t cold_gc_bs_pointer = bsp - 2048;
681 if (GC_all_interior_pointers
682 && (word)cold_gc_bs_pointer > (word)BACKING_STORE_BASE) {
683 /* Adjust cold_gc_bs_pointer if below our innermost */
684 /* "traced stack section" in backing store. */
685 if (GC_traced_stack_sect != NULL
686 && (word)cold_gc_bs_pointer
687 < (word)GC_traced_stack_sect->backing_store_end)
688 cold_gc_bs_pointer =
689 GC_traced_stack_sect->backing_store_end;
690 GC_push_all_register_sections(BACKING_STORE_BASE,
691 cold_gc_bs_pointer, FALSE, GC_traced_stack_sect);
692 GC_push_all_eager(cold_gc_bs_pointer, bsp);
693 } else {
694 GC_push_all_register_sections(BACKING_STORE_BASE, bsp,
695 TRUE /* eager */, GC_traced_stack_sect);
697 /* All values should be sufficiently aligned that we */
698 /* don't have to worry about the boundary. */
700 # endif
701 # endif /* !THREADS */
704 GC_INNER void (*GC_push_typed_structures)(void) = 0;
706 /* Push GC internal roots. These are normally */
707 /* included in the static data segment, and */
708 /* Thus implicitly pushed. But we must do this */
709 /* explicitly if normal root processing is */
710 /* disabled. */
712 * Push GC internal roots. Only called if there is some reason to believe
713 * these would not otherwise get registered.
715 STATIC void GC_push_gc_structures(void)
717 # ifndef GC_NO_FINALIZATION
718 GC_push_finalizer_structures();
719 # endif
720 # if defined(THREADS)
721 GC_push_thread_structures();
722 # endif
723 if( GC_push_typed_structures )
724 GC_push_typed_structures();
727 GC_INNER void GC_cond_register_dynamic_libraries(void)
729 # if defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
730 || defined(CYGWIN32) || defined(PCR)
731 GC_remove_tmp_roots();
732 if (!GC_no_dls) GC_register_dynamic_libraries();
733 # else
734 GC_no_dls = TRUE;
735 # endif
738 STATIC void GC_push_regs_and_stack(ptr_t cold_gc_frame)
740 GC_with_callee_saves_pushed(GC_push_current_stack, cold_gc_frame);
744 * Call the mark routines (GC_tl_push for a single pointer,
745 * GC_push_conditional on groups of pointers) on every top level
746 * accessible pointer.
747 * If all is FALSE, arrange to push only possibly altered values.
748 * Cold_gc_frame is an address inside a GC frame that
749 * remains valid until all marking is complete.
750 * A zero value indicates that it's OK to miss some
751 * register values.
753 GC_INNER void GC_push_roots(GC_bool all, ptr_t cold_gc_frame)
755 int i;
756 unsigned kind;
759 * Next push static data. This must happen early on, since it's
760 * not robust against mark stack overflow.
762 /* Re-register dynamic libraries, in case one got added. */
763 /* There is some argument for doing this as late as possible, */
764 /* especially on win32, where it can change asynchronously. */
765 /* In those cases, we do it here. But on other platforms, it's */
766 /* not safe with the world stopped, so we do it earlier. */
767 # if !defined(REGISTER_LIBRARIES_EARLY)
768 GC_cond_register_dynamic_libraries();
769 # endif
771 /* Mark everything in static data areas */
772 for (i = 0; i < n_root_sets; i++) {
773 GC_push_conditional_with_exclusions(
774 GC_static_roots[i].r_start,
775 GC_static_roots[i].r_end, all);
778 /* Mark all free list header blocks, if those were allocated from */
779 /* the garbage collected heap. This makes sure they don't */
780 /* disappear if we are not marking from static data. It also */
781 /* saves us the trouble of scanning them, and possibly that of */
782 /* marking the freelists. */
783 for (kind = 0; kind < GC_n_kinds; kind++) {
784 void *base = GC_base(GC_obj_kinds[kind].ok_freelist);
785 if (0 != base) {
786 GC_set_mark_bit(base);
790 /* Mark from GC internal roots if those might otherwise have */
791 /* been excluded. */
792 if (GC_no_dls || roots_were_cleared) {
793 GC_push_gc_structures();
796 /* Mark thread local free lists, even if their mark */
797 /* descriptor excludes the link field. */
798 /* If the world is not stopped, this is unsafe. It is */
799 /* also unnecessary, since we will do this again with the */
800 /* world stopped. */
801 # if defined(THREAD_LOCAL_ALLOC)
802 if (GC_world_stopped) GC_mark_thread_local_free_lists();
803 # endif
806 * Now traverse stacks, and mark from register contents.
807 * These must be done last, since they can legitimately overflow
808 * the mark stack.
809 * This is usually done by saving the current context on the
810 * stack, and then just tracing from the stack.
812 GC_push_regs_and_stack(cold_gc_frame);
814 if (GC_push_other_roots != 0) (*GC_push_other_roots)();
815 /* In the threads case, this also pushes thread stacks. */
816 /* Note that without interior pointer recognition lots */
817 /* of stuff may have been pushed already, and this */
818 /* should be careful about mark stack overflows. */