Debian Codegraph @

gc/mark_rts.c

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