1 /*
2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 *
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 *
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.
13 */
14
15 #include "private/gc_priv.h"
16
17 #include <stdio.h>
18
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
32 };
33
34 struct roots GC_static_roots[MAX_ROOT_SETS];
35 */
36
37 int GC_no_dls = 0; /* Register dynamic library data segments. */
38
39 static int n_root_sets = 0;
40 /* GC_static_roots[0..n_root_sets) contains the valid root sets. */
41
42 #if !defined(NO_DEBUGGING)
43 /* For debugging: */
GC_print_static_roots(void)44 void GC_print_static_roots(void)
45 {
46 int i;
47 size_t total = 0;
48
49 for (i = 0; i < n_root_sets; i++) {
50 GC_printf("From %p to %p%s\n",
51 GC_static_roots[i].r_start,
52 GC_static_roots[i].r_end,
53 GC_static_roots[i].r_tmp ? " (temporary)" : "");
54 total += GC_static_roots[i].r_end - GC_static_roots[i].r_start;
55 }
56 GC_printf("Total size: %ld\n", (unsigned long) total);
57 if (GC_root_size != total) {
58 GC_err_printf("GC_root_size incorrect: %ld!!\n",
59 (long) GC_root_size);
60 }
61 }
62 #endif /* !NO_DEBUGGING */
63
64 #ifndef THREADS
65 /* Primarily for debugging support: */
66 /* Is the address p in one of the registered static root sections? */
GC_is_static_root(ptr_t p)67 GC_INNER GC_bool GC_is_static_root(ptr_t p)
68 {
69 static int last_root_set = MAX_ROOT_SETS;
70 int i;
71
72 if (last_root_set < n_root_sets
73 && p >= GC_static_roots[last_root_set].r_start
74 && p < GC_static_roots[last_root_set].r_end) return(TRUE);
75 for (i = 0; i < n_root_sets; i++) {
76 if (p >= GC_static_roots[i].r_start
77 && p < GC_static_roots[i].r_end) {
78 last_root_set = i;
79 return(TRUE);
80 }
81 }
82 return(FALSE);
83 }
84 #endif /* !THREADS */
85
86 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
87 /*
88 # define LOG_RT_SIZE 6
89 # define RT_SIZE (1 << LOG_RT_SIZE) -- Power of 2, may be != MAX_ROOT_SETS
90
91 struct roots * GC_root_index[RT_SIZE];
92 -- Hash table header. Used only to check whether a range is
93 -- already present.
94 -- really defined in gc_priv.h
95 */
96
rt_hash(ptr_t addr)97 GC_INLINE int rt_hash(ptr_t addr)
98 {
99 word result = (word) addr;
100 # if CPP_WORDSZ > 8*LOG_RT_SIZE
101 result ^= result >> 8*LOG_RT_SIZE;
102 # endif
103 # if CPP_WORDSZ > 4*LOG_RT_SIZE
104 result ^= result >> 4*LOG_RT_SIZE;
105 # endif
106 result ^= result >> 2*LOG_RT_SIZE;
107 result ^= result >> LOG_RT_SIZE;
108 result &= (RT_SIZE-1);
109 return(result);
110 }
111
112 /* Is a range starting at b already in the table? If so return a */
113 /* pointer to it, else NULL. */
GC_roots_present(ptr_t b)114 GC_INNER struct roots * GC_roots_present(ptr_t b)
115 {
116 int h = rt_hash(b);
117 struct roots *p = GC_root_index[h];
118
119 while (p != 0) {
120 if (p -> r_start == (ptr_t)b) return(p);
121 p = p -> r_next;
122 }
123 return(FALSE);
124 }
125
126 /* Add the given root structure to the index. */
add_roots_to_index(struct roots * p)127 GC_INLINE void add_roots_to_index(struct roots *p)
128 {
129 int h = rt_hash(p -> r_start);
130
131 p -> r_next = GC_root_index[h];
132 GC_root_index[h] = p;
133 }
134 #endif /* !MSWIN32 && !MSWINCE && !CYGWIN32 */
135
136 GC_INNER word GC_root_size = 0;
137
GC_add_roots(void * b,void * e)138 GC_API void GC_CALL GC_add_roots(void *b, void *e)
139 {
140 DCL_LOCK_STATE;
141
142 if (!GC_is_initialized) GC_init();
143 LOCK();
144 GC_add_roots_inner((ptr_t)b, (ptr_t)e, FALSE);
145 UNLOCK();
146 }
147
148
149 /* Add [b,e) to the root set. Adding the same interval a second time */
150 /* is a moderately fast no-op, and hence benign. We do not handle */
151 /* different but overlapping intervals efficiently. (We do handle */
152 /* them correctly.) */
153 /* Tmp specifies that the interval may be deleted before */
154 /* re-registering dynamic libraries. */
GC_add_roots_inner(ptr_t b,ptr_t e,GC_bool tmp)155 void GC_add_roots_inner(ptr_t b, ptr_t e, GC_bool tmp)
156 {
157 struct roots * old;
158
159 /* Adjust and check range boundaries for safety */
160 GC_ASSERT((word)b % sizeof(word) == 0);
161 e = (ptr_t)((word)e & ~(sizeof(word) - 1));
162 GC_ASSERT(b <= e);
163 if (b == e) return; /* nothing to do? */
164
165 # if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
166 /* Spend the time to ensure that there are no overlapping */
167 /* or adjacent intervals. */
168 /* This could be done faster with e.g. a */
169 /* balanced tree. But the execution time here is */
170 /* virtually guaranteed to be dominated by the time it */
171 /* takes to scan the roots. */
172 {
173 register int i;
174 old = 0; /* initialized to prevent warning. */
175 for (i = 0; i < n_root_sets; i++) {
176 old = GC_static_roots + i;
177 if (b <= old -> r_end && e >= old -> r_start) {
178 if (b < old -> r_start) {
179 old -> r_start = b;
180 GC_root_size += (old -> r_start - b);
181 }
182 if (e > old -> r_end) {
183 old -> r_end = e;
184 GC_root_size += (e - old -> r_end);
185 }
186 old -> r_tmp &= tmp;
187 break;
188 }
189 }
190 if (i < n_root_sets) {
191 /* merge other overlapping intervals */
192 struct roots *other;
193
194 for (i++; i < n_root_sets; i++) {
195 other = GC_static_roots + i;
196 b = other -> r_start;
197 e = other -> r_end;
198 if (b <= old -> r_end && e >= old -> r_start) {
199 if (b < old -> r_start) {
200 old -> r_start = b;
201 GC_root_size += (old -> r_start - b);
202 }
203 if (e > old -> r_end) {
204 old -> r_end = e;
205 GC_root_size += (e - old -> r_end);
206 }
207 old -> r_tmp &= other -> r_tmp;
208 /* Delete this entry. */
209 GC_root_size -= (other -> r_end - other -> r_start);
210 other -> r_start = GC_static_roots[n_root_sets-1].r_start;
211 other -> r_end = GC_static_roots[n_root_sets-1].r_end;
212 n_root_sets--;
213 }
214 }
215 return;
216 }
217 }
218 # else
219 old = GC_roots_present(b);
220 if (old != 0) {
221 if (e <= old -> r_end) /* already there */ return;
222 /* else extend */
223 GC_root_size += e - old -> r_end;
224 old -> r_end = e;
225 return;
226 }
227 # endif
228 if (n_root_sets == MAX_ROOT_SETS) {
229 ABORT("Too many root sets");
230 }
231 GC_static_roots[n_root_sets].r_start = (ptr_t)b;
232 GC_static_roots[n_root_sets].r_end = (ptr_t)e;
233 GC_static_roots[n_root_sets].r_tmp = tmp;
234 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
235 GC_static_roots[n_root_sets].r_next = 0;
236 add_roots_to_index(GC_static_roots + n_root_sets);
237 # endif
238 GC_root_size += e - b;
239 n_root_sets++;
240 }
241
242 static GC_bool roots_were_cleared = FALSE;
243
GC_clear_roots(void)244 GC_API void GC_CALL GC_clear_roots(void)
245 {
246 DCL_LOCK_STATE;
247
248 if (!GC_is_initialized) GC_init();
249 LOCK();
250 roots_were_cleared = TRUE;
251 n_root_sets = 0;
252 GC_root_size = 0;
253 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
254 BZERO(GC_root_index, RT_SIZE * sizeof(void *));
255 # endif
256 UNLOCK();
257 }
258
259 /* Internal use only; lock held. */
GC_remove_root_at_pos(int i)260 STATIC void GC_remove_root_at_pos(int i)
261 {
262 GC_root_size -= (GC_static_roots[i].r_end - GC_static_roots[i].r_start);
263 GC_static_roots[i].r_start = GC_static_roots[n_root_sets-1].r_start;
264 GC_static_roots[i].r_end = GC_static_roots[n_root_sets-1].r_end;
265 GC_static_roots[i].r_tmp = GC_static_roots[n_root_sets-1].r_tmp;
266 n_root_sets--;
267 }
268
269 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
GC_rebuild_root_index(void)270 STATIC void GC_rebuild_root_index(void)
271 {
272 int i;
273 BZERO(GC_root_index, RT_SIZE * sizeof(void *));
274 for (i = 0; i < n_root_sets; i++)
275 add_roots_to_index(GC_static_roots + i);
276 }
277 #endif
278
279 #if defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
280 || defined(PCR) || defined(CYGWIN32)
281 /* Internal use only; lock held. */
GC_remove_tmp_roots(void)282 STATIC void GC_remove_tmp_roots(void)
283 {
284 int i;
285
286 for (i = 0; i < n_root_sets; ) {
287 if (GC_static_roots[i].r_tmp) {
288 GC_remove_root_at_pos(i);
289 } else {
290 i++;
291 }
292 }
293 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
294 GC_rebuild_root_index();
295 # endif
296 }
297 #endif
298
299 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
300 STATIC void GC_remove_roots_inner(ptr_t b, ptr_t e);
301
GC_remove_roots(void * b,void * e)302 GC_API void GC_CALL GC_remove_roots(void *b, void *e)
303 {
304 DCL_LOCK_STATE;
305
306 /* Quick check whether has nothing to do */
307 if ((((word)b + (sizeof(word) - 1)) & ~(sizeof(word) - 1)) >=
308 ((word)e & ~(sizeof(word) - 1)))
309 return;
310
311 LOCK();
312 GC_remove_roots_inner((ptr_t)b, (ptr_t)e);
313 UNLOCK();
314 }
315
316 /* Should only be called when the lock is held */
GC_remove_roots_inner(ptr_t b,ptr_t e)317 STATIC void GC_remove_roots_inner(ptr_t b, ptr_t e)
318 {
319 int i;
320 for (i = 0; i < n_root_sets; ) {
321 if (GC_static_roots[i].r_start >= b
322 && GC_static_roots[i].r_end <= e) {
323 GC_remove_root_at_pos(i);
324 } else {
325 i++;
326 }
327 }
328 GC_rebuild_root_index();
329 }
330 #endif /* !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32) */
331
332 #if (defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)) \
333 && !defined(NO_DEBUGGING)
334 /* Not used at present (except for, may be, debugging purpose). */
335 /* Workaround for the OS mapping and unmapping behind our back: */
336 /* Is the address p in one of the temporary static root sections? */
GC_is_tmp_root(ptr_t p)337 GC_bool GC_is_tmp_root(ptr_t p)
338 {
339 static int last_root_set = MAX_ROOT_SETS;
340 register int i;
341
342 if (last_root_set < n_root_sets
343 && p >= GC_static_roots[last_root_set].r_start
344 && p < GC_static_roots[last_root_set].r_end)
345 return GC_static_roots[last_root_set].r_tmp;
346 for (i = 0; i < n_root_sets; i++) {
347 if (p >= GC_static_roots[i].r_start
348 && p < GC_static_roots[i].r_end) {
349 last_root_set = i;
350 return GC_static_roots[i].r_tmp;
351 }
352 }
353 return(FALSE);
354 }
355 #endif /* MSWIN32 || MSWINCE || CYGWIN32 */
356
GC_approx_sp(void)357 GC_INNER ptr_t GC_approx_sp(void)
358 {
359 volatile word sp;
360 sp = (word)&sp;
361 /* Also force stack to grow if necessary. Otherwise the */
362 /* later accesses might cause the kernel to think we're */
363 /* doing something wrong. */
364 return((ptr_t)sp);
365 /* GNU C: alternatively, we may return the value of */
366 /*__builtin_frame_address(0). */
367 }
368
369 /*
370 * Data structure for excluded static roots.
371 * Real declaration is in gc_priv.h.
372
373 struct exclusion {
374 ptr_t e_start;
375 ptr_t e_end;
376 };
377
378 struct exclusion GC_excl_table[MAX_EXCLUSIONS];
379 -- Array of exclusions, ascending
380 -- address order.
381 */
382
383 STATIC size_t GC_excl_table_entries = 0;/* Number of entries in use. */
384
385 /* Return the first exclusion range that includes an address >= start_addr */
386 /* Assumes the exclusion table contains at least one entry (namely the */
387 /* GC data structures). */
GC_next_exclusion(ptr_t start_addr)388 STATIC struct exclusion * GC_next_exclusion(ptr_t start_addr)
389 {
390 size_t low = 0;
391 size_t high = GC_excl_table_entries - 1;
392 size_t mid;
393
394 while (high > low) {
395 mid = (low + high) >> 1;
396 /* low <= mid < high */
397 if ((word) GC_excl_table[mid].e_end <= (word) start_addr) {
398 low = mid + 1;
399 } else {
400 high = mid;
401 }
402 }
403 if ((word) GC_excl_table[low].e_end <= (word) start_addr) return 0;
404 return GC_excl_table + low;
405 }
406
407 /* Should only be called when the lock is held. The range boundaries */
408 /* should be properly aligned and valid. */
GC_exclude_static_roots_inner(void * start,void * finish)409 GC_INNER void GC_exclude_static_roots_inner(void *start, void *finish)
410 {
411 struct exclusion * next;
412 size_t next_index, i;
413
414 GC_ASSERT((word)start % sizeof(word) == 0);
415 GC_ASSERT(start < finish);
416
417 if (0 == GC_excl_table_entries) {
418 next = 0;
419 } else {
420 next = GC_next_exclusion(start);
421 }
422 if (0 != next) {
423 if ((word)(next -> e_start) < (word) finish) {
424 /* incomplete error check. */
425 ABORT("Exclusion ranges overlap");
426 }
427 if ((word)(next -> e_start) == (word) finish) {
428 /* extend old range backwards */
429 next -> e_start = (ptr_t)start;
430 return;
431 }
432 next_index = next - GC_excl_table;
433 for (i = GC_excl_table_entries; i > next_index; --i) {
434 GC_excl_table[i] = GC_excl_table[i-1];
435 }
436 } else {
437 next_index = GC_excl_table_entries;
438 }
439 if (GC_excl_table_entries == MAX_EXCLUSIONS) ABORT("Too many exclusions");
440 GC_excl_table[next_index].e_start = (ptr_t)start;
441 GC_excl_table[next_index].e_end = (ptr_t)finish;
442 ++GC_excl_table_entries;
443 }
444
GC_exclude_static_roots(void * b,void * e)445 GC_API void GC_CALL GC_exclude_static_roots(void *b, void *e)
446 {
447 DCL_LOCK_STATE;
448
449 /* Adjust the upper boundary for safety (round down) */
450 e = (void *)((word)e & ~(sizeof(word) - 1));
451
452 if (b == e) return; /* nothing to exclude? */
453
454 LOCK();
455 GC_exclude_static_roots_inner(b, e);
456 UNLOCK();
457 }
458
459 /* Invoke push_conditional on ranges that are not excluded. */
460 /*ARGSUSED*/
GC_push_conditional_with_exclusions(ptr_t bottom,ptr_t top,GC_bool all)461 STATIC void GC_push_conditional_with_exclusions(ptr_t bottom, ptr_t top,
462 GC_bool all)
463 {
464 struct exclusion * next;
465 ptr_t excl_start;
466
467 while (bottom < top) {
468 next = GC_next_exclusion(bottom);
469 if (0 == next || (excl_start = next -> e_start) >= top) {
470 GC_push_conditional(bottom, top, all);
471 return;
472 }
473 if (excl_start > bottom) GC_push_conditional(bottom, excl_start, all);
474 bottom = next -> e_end;
475 }
476 }
477
478 #ifdef IA64
479 /* Similar to GC_push_all_stack_sections() but for IA-64 registers store. */
GC_push_all_register_sections(ptr_t bs_lo,ptr_t bs_hi,int eager,struct GC_traced_stack_sect_s * traced_stack_sect)480 GC_INNER void GC_push_all_register_sections(ptr_t bs_lo, ptr_t bs_hi,
481 int eager, struct GC_traced_stack_sect_s *traced_stack_sect)
482 {
483 while (traced_stack_sect != NULL) {
484 ptr_t frame_bs_lo = traced_stack_sect -> backing_store_end;
485 GC_ASSERT(frame_bs_lo <= bs_hi);
486 if (eager) {
487 GC_push_all_eager(frame_bs_lo, bs_hi);
488 } else {
489 GC_push_all_stack(frame_bs_lo, bs_hi);
490 }
491 bs_hi = traced_stack_sect -> saved_backing_store_ptr;
492 traced_stack_sect = traced_stack_sect -> prev;
493 }
494 GC_ASSERT(bs_lo <= bs_hi);
495 if (eager) {
496 GC_push_all_eager(bs_lo, bs_hi);
497 } else {
498 GC_push_all_stack(bs_lo, bs_hi);
499 }
500 }
501 #endif /* IA64 */
502
503 #ifdef THREADS
504
GC_push_all_stack_sections(ptr_t lo,ptr_t hi,struct GC_traced_stack_sect_s * traced_stack_sect)505 GC_INNER void GC_push_all_stack_sections(ptr_t lo, ptr_t hi,
506 struct GC_traced_stack_sect_s *traced_stack_sect)
507 {
508 while (traced_stack_sect != NULL) {
509 GC_ASSERT(lo HOTTER_THAN (ptr_t)traced_stack_sect);
510 # ifdef STACK_GROWS_UP
511 GC_push_all_stack((ptr_t)traced_stack_sect, lo);
512 # else /* STACK_GROWS_DOWN */
513 GC_push_all_stack(lo, (ptr_t)traced_stack_sect);
514 # endif
515 lo = traced_stack_sect -> saved_stack_ptr;
516 GC_ASSERT(lo != NULL);
517 traced_stack_sect = traced_stack_sect -> prev;
518 }
519 GC_ASSERT(!(hi HOTTER_THAN lo));
520 # ifdef STACK_GROWS_UP
521 /* We got them backwards! */
522 GC_push_all_stack(hi, lo);
523 # else /* STACK_GROWS_DOWN */
524 GC_push_all_stack(lo, hi);
525 # endif
526 }
527
528 #else /* !THREADS */
529
530 # ifdef TRACE_BUF
531 /* Defined in mark.c. */
532 void GC_add_trace_entry(char *kind, word arg1, word arg2);
533 # endif
534
535 /* Similar to GC_push_all_eager, but only the */
536 /* part hotter than cold_gc_frame is scanned */
537 /* immediately. Needed to ensure that callee- */
538 /* save registers are not missed. */
539 /*
540 * A version of GC_push_all that treats all interior pointers as valid
541 * and scans part of the area immediately, to make sure that saved
542 * register values are not lost.
543 * Cold_gc_frame delimits the stack section that must be scanned
544 * eagerly. A zero value indicates that no eager scanning is needed.
545 * We don't need to worry about the MANUAL_VDB case here, since this
546 * is only called in the single-threaded case. We assume that we
547 * cannot collect between an assignment and the corresponding
548 * GC_dirty() call.
549 */
GC_push_all_stack_partially_eager(ptr_t bottom,ptr_t top,ptr_t cold_gc_frame)550 STATIC void GC_push_all_stack_partially_eager(ptr_t bottom, ptr_t top,
551 ptr_t cold_gc_frame)
552 {
553 if (!NEED_FIXUP_POINTER && GC_all_interior_pointers) {
554 /* Push the hot end of the stack eagerly, so that register values */
555 /* saved inside GC frames are marked before they disappear. */
556 /* The rest of the marking can be deferred until later. */
557 if (0 == cold_gc_frame) {
558 GC_push_all_stack(bottom, top);
559 return;
560 }
561 GC_ASSERT(bottom <= cold_gc_frame && cold_gc_frame <= top);
562 # ifdef STACK_GROWS_DOWN
563 GC_push_all(cold_gc_frame - sizeof(ptr_t), top);
564 GC_push_all_eager(bottom, cold_gc_frame);
565 # else /* STACK_GROWS_UP */
566 GC_push_all(bottom, cold_gc_frame + sizeof(ptr_t));
567 GC_push_all_eager(cold_gc_frame, top);
568 # endif /* STACK_GROWS_UP */
569 } else {
570 GC_push_all_eager(bottom, top);
571 }
572 # ifdef TRACE_BUF
573 GC_add_trace_entry("GC_push_all_stack", bottom, top);
574 # endif
575 }
576
577 /* Similar to GC_push_all_stack_sections() but also uses cold_gc_frame. */
GC_push_all_stack_part_eager_sections(ptr_t lo,ptr_t hi,ptr_t cold_gc_frame,struct GC_traced_stack_sect_s * traced_stack_sect)578 STATIC void GC_push_all_stack_part_eager_sections(ptr_t lo, ptr_t hi,
579 ptr_t cold_gc_frame, struct GC_traced_stack_sect_s *traced_stack_sect)
580 {
581 GC_ASSERT(traced_stack_sect == NULL || cold_gc_frame == NULL ||
582 cold_gc_frame HOTTER_THAN (ptr_t)traced_stack_sect);
583
584 while (traced_stack_sect != NULL) {
585 GC_ASSERT(lo HOTTER_THAN (ptr_t)traced_stack_sect);
586 # ifdef STACK_GROWS_UP
587 GC_push_all_stack_partially_eager((ptr_t)traced_stack_sect, lo,
588 cold_gc_frame);
589 # else /* STACK_GROWS_DOWN */
590 GC_push_all_stack_partially_eager(lo, (ptr_t)traced_stack_sect,
591 cold_gc_frame);
592 # endif
593 lo = traced_stack_sect -> saved_stack_ptr;
594 GC_ASSERT(lo != NULL);
595 traced_stack_sect = traced_stack_sect -> prev;
596 cold_gc_frame = NULL; /* Use at most once. */
597 }
598
599 GC_ASSERT(!(hi HOTTER_THAN lo));
600 # ifdef STACK_GROWS_UP
601 /* We got them backwards! */
602 GC_push_all_stack_partially_eager(hi, lo, cold_gc_frame);
603 # else /* STACK_GROWS_DOWN */
604 GC_push_all_stack_partially_eager(lo, hi, cold_gc_frame);
605 # endif
606 }
607
608 #endif /* !THREADS */
609
610 /* Push enough of the current stack eagerly to */
611 /* ensure that callee-save registers saved in */
612 /* GC frames are scanned. */
613 /* In the non-threads case, schedule entire */
614 /* stack for scanning. */
615 /* The second argument is a pointer to the */
616 /* (possibly null) thread context, for */
617 /* (currently hypothetical) more precise */
618 /* stack scanning. */
619 /*
620 * In the absence of threads, push the stack contents.
621 * In the presence of threads, push enough of the current stack
622 * to ensure that callee-save registers saved in collector frames have been
623 * seen.
624 * FIXME: Merge with per-thread stuff.
625 */
626 /*ARGSUSED*/
GC_push_current_stack(ptr_t cold_gc_frame,void * context)627 STATIC void GC_push_current_stack(ptr_t cold_gc_frame, void * context)
628 {
629 # if defined(THREADS)
630 if (0 == cold_gc_frame) return;
631 # ifdef STACK_GROWS_DOWN
632 GC_push_all_eager(GC_approx_sp(), cold_gc_frame);
633 /* For IA64, the register stack backing store is handled */
634 /* in the thread-specific code. */
635 # else
636 GC_push_all_eager(cold_gc_frame, GC_approx_sp());
637 # endif
638 # else
639 GC_push_all_stack_part_eager_sections(GC_approx_sp(), GC_stackbottom,
640 cold_gc_frame, GC_traced_stack_sect);
641 # ifdef IA64
642 /* We also need to push the register stack backing store. */
643 /* This should really be done in the same way as the */
644 /* regular stack. For now we fudge it a bit. */
645 /* Note that the backing store grows up, so we can't use */
646 /* GC_push_all_stack_partially_eager. */
647 {
648 ptr_t bsp = GC_save_regs_ret_val;
649 ptr_t cold_gc_bs_pointer = bsp - 2048;
650 if (GC_all_interior_pointers &&
651 cold_gc_bs_pointer > BACKING_STORE_BASE) {
652 /* Adjust cold_gc_bs_pointer if below our innermost */
653 /* "traced stack section" in backing store. */
654 if (GC_traced_stack_sect != NULL && cold_gc_bs_pointer <
655 GC_traced_stack_sect->backing_store_end)
656 cold_gc_bs_pointer =
657 GC_traced_stack_sect->backing_store_end;
658 GC_push_all_register_sections(BACKING_STORE_BASE,
659 cold_gc_bs_pointer, FALSE, GC_traced_stack_sect);
660 GC_push_all_eager(cold_gc_bs_pointer, bsp);
661 } else {
662 GC_push_all_register_sections(BACKING_STORE_BASE, bsp,
663 TRUE /* eager */, GC_traced_stack_sect);
664 }
665 /* All values should be sufficiently aligned that we */
666 /* don't have to worry about the boundary. */
667 }
668 # endif
669 # endif /* !THREADS */
670 }
671
672 GC_INNER void (*GC_push_typed_structures)(void) = 0;
673
674 /* Push GC internal roots. These are normally */
675 /* included in the static data segment, and */
676 /* Thus implicitly pushed. But we must do this */
677 /* explicitly if normal root processing is */
678 /* disabled. */
679 /*
680 * Push GC internal roots. Only called if there is some reason to believe
681 * these would not otherwise get registered.
682 */
GC_push_gc_structures(void)683 STATIC void GC_push_gc_structures(void)
684 {
685 GC_push_finalizer_structures();
686 # if defined(THREADS)
687 GC_push_thread_structures();
688 # endif
689 if( GC_push_typed_structures )
690 GC_push_typed_structures();
691 }
692
693 #ifdef THREAD_LOCAL_ALLOC
694 GC_INNER void GC_mark_thread_local_free_lists(void);
695 #endif
696
GC_cond_register_dynamic_libraries(void)697 GC_INNER void GC_cond_register_dynamic_libraries(void)
698 {
699 # if defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
700 || defined(CYGWIN32) || defined(PCR)
701 GC_remove_tmp_roots();
702 if (!GC_no_dls) GC_register_dynamic_libraries();
703 # else
704 GC_no_dls = TRUE;
705 # endif
706 }
707
GC_push_regs_and_stack(ptr_t cold_gc_frame)708 STATIC void GC_push_regs_and_stack(ptr_t cold_gc_frame)
709 {
710 GC_with_callee_saves_pushed(GC_push_current_stack, cold_gc_frame);
711 }
712
713 /*
714 * Call the mark routines (GC_tl_push for a single pointer, GC_push_conditional
715 * on groups of pointers) on every top level accessible pointer.
716 * If all is FALSE, arrange to push only possibly altered values.
717 * Cold_gc_frame is an address inside a GC frame that
718 * remains valid until all marking is complete.
719 * A zero value indicates that it's OK to miss some
720 * register values.
721 */
GC_push_roots(GC_bool all,ptr_t cold_gc_frame)722 GC_INNER void GC_push_roots(GC_bool all, ptr_t cold_gc_frame)
723 {
724 int i;
725 unsigned kind;
726
727 /*
728 * Next push static data. This must happen early on, since it's
729 * not robust against mark stack overflow.
730 */
731 /* Re-register dynamic libraries, in case one got added. */
732 /* There is some argument for doing this as late as possible, */
733 /* especially on win32, where it can change asynchronously. */
734 /* In those cases, we do it here. But on other platforms, it's */
735 /* not safe with the world stopped, so we do it earlier. */
736 # if !defined(REGISTER_LIBRARIES_EARLY)
737 GC_cond_register_dynamic_libraries();
738 # endif
739
740 /* Mark everything in static data areas */
741 for (i = 0; i < n_root_sets; i++) {
742 GC_push_conditional_with_exclusions(
743 GC_static_roots[i].r_start,
744 GC_static_roots[i].r_end, all);
745 }
746
747 /* Mark all free list header blocks, if those were allocated from */
748 /* the garbage collected heap. This makes sure they don't */
749 /* disappear if we are not marking from static data. It also */
750 /* saves us the trouble of scanning them, and possibly that of */
751 /* marking the freelists. */
752 for (kind = 0; kind < GC_n_kinds; kind++) {
753 void *base = GC_base(GC_obj_kinds[kind].ok_freelist);
754 if (0 != base) {
755 GC_set_mark_bit(base);
756 }
757 }
758
759 /* Mark from GC internal roots if those might otherwise have */
760 /* been excluded. */
761 if (GC_no_dls || roots_were_cleared) {
762 GC_push_gc_structures();
763 }
764
765 /* Mark thread local free lists, even if their mark */
766 /* descriptor excludes the link field. */
767 /* If the world is not stopped, this is unsafe. It is */
768 /* also unnecessary, since we will do this again with the */
769 /* world stopped. */
770 # if defined(THREAD_LOCAL_ALLOC)
771 if (GC_world_stopped) GC_mark_thread_local_free_lists();
772 # endif
773
774 /*
775 * Now traverse stacks, and mark from register contents.
776 * These must be done last, since they can legitimately overflow
777 * the mark stack.
778 * This is usually done by saving the current context on the
779 * stack, and then just tracing from the stack.
780 */
781 GC_push_regs_and_stack(cold_gc_frame);
782
783 if (GC_push_other_roots != 0) (*GC_push_other_roots)();
784 /* In the threads case, this also pushes thread stacks. */
785 /* Note that without interior pointer recognition lots */
786 /* of stuff may have been pushed already, and this */
787 /* should be careful about mark stack overflows. */
788 }
789