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