aboutsummaryrefslogtreecommitdiffstats
path: root/gc/mark_rts.c
blob: 251a0d1458f613539ffe6a1fe5a3c5cddbfa8ade (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/* 
 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
 * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
 *
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
 * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
 *
 * Permission is hereby granted to use or copy this program
 * for any purpose,  provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is granted,
 * provided the above notices are retained, and a notice that the code was
 * modified is included with the above copyright notice.
 */
# include <stdio.h>
# include "private/gc_priv.h"

/* Data structure for list of root sets.				*/
/* We keep a hash table, so that we can filter out duplicate additions.	*/
/* Under Win32, we need to do a better job of filtering overlaps, so	*/
/* we resort to sequential search, and pay the price.			*/
/* This is really declared in gc_priv.h:
struct roots {
	ptr_t r_start;
	ptr_t r_end;
 #	if !defined(MSWIN32) && !defined(MSWINCE)
	  struct roots * r_next;
 #	endif
	GC_bool r_tmp;
	  	-- Delete before registering new dynamic libraries
};

struct roots GC_static_roots[MAX_ROOT_SETS];
*/

int GC_no_dls = 0;	/* Register dynamic library data segments.	*/

static int n_root_sets = 0;

	/* GC_static_roots[0..n_root_sets) contains the valid root sets. */

# if !defined(NO_DEBUGGING)
/* For debugging:	*/
void GC_print_static_roots()
{
    register int i;
    size_t total = 0;
    
    for (i = 0; i < n_root_sets; i++) {
        GC_printf2("From 0x%lx to 0x%lx ",
        	   (unsigned long) GC_static_roots[i].r_start,
        	   (unsigned long) GC_static_roots[i].r_end);
        if (GC_static_roots[i].r_tmp) {
            GC_printf0(" (temporary)\n");
        } else {
            GC_printf0("\n");
        }
        total += GC_static_roots[i].r_end - GC_static_roots[i].r_start;
    }
    GC_printf1("Total size: %ld\n", (unsigned long) total);
    if (GC_root_size != total) {
     	GC_printf1("GC_root_size incorrect: %ld!!\n",
     		   (unsigned long) GC_root_size);
    }
}
# endif /* NO_DEBUGGING */

/* Primarily for debugging support:	*/
/* Is the address p in one of the registered static			*/
/* root sections?							*/
GC_bool GC_is_static_root(p)
ptr_t p;
{
    static int last_root_set = MAX_ROOT_SETS;
    register int i;
    
    
    if (last_root_set < n_root_sets
	&& p >= GC_static_roots[last_root_set].r_start
        && p < GC_static_roots[last_root_set].r_end) return(TRUE);
    for (i = 0; i < n_root_sets; i++) {
    	if (p >= GC_static_roots[i].r_start
            && p < GC_static_roots[i].r_end) {
            last_root_set = i;
            return(TRUE);
        }
    }
    return(FALSE);
}

#if !defined(MSWIN32) && !defined(MSWINCE)
/* 
#   define LOG_RT_SIZE 6
#   define RT_SIZE (1 << LOG_RT_SIZE)  -- Power of 2, may be != MAX_ROOT_SETS

    struct roots * GC_root_index[RT_SIZE];
	-- Hash table header.  Used only to check whether a range is
	-- already present.
	-- really defined in gc_priv.h
*/

static int rt_hash(addr)
char * addr;
{
    word result = (word) addr;
#   if CPP_WORDSZ > 8*LOG_RT_SIZE
	result ^= result >> 8*LOG_RT_SIZE;
#   endif
#   if CPP_WORDSZ > 4*LOG_RT_SIZE
	result ^= result >> 4*LOG_RT_SIZE;
#   endif
    result ^= result >> 2*LOG_RT_SIZE;
    result ^= result >> LOG_RT_SIZE;
    result &= (RT_SIZE-1);
    return(result);
}

/* Is a range starting at b already in the table? If so return a	*/
/* pointer to it, else NIL.						*/
struct roots * GC_roots_present(b)
char *b;
{
    register int h = rt_hash(b);
    register struct roots *p = GC_root_index[h];
    
    while (p != 0) {
        if (p -> r_start == (ptr_t)b) return(p);
        p = p -> r_next;
    }
    return(FALSE);
}

/* Add the given root structure to the index. */
static void add_roots_to_index(p)
struct roots *p;
{
    register int h = rt_hash(p -> r_start);
    
    p -> r_next = GC_root_index[h];
    GC_root_index[h] = p;
}

# else /* MSWIN32 || MSWINCE */

#   define add_roots_to_index(p)

# endif




word GC_root_size = 0;

void GC_add_roots(b, e)
char * b; char * e;
{
    DCL_LOCK_STATE;
    
    DISABLE_SIGNALS();
    LOCK();
    GC_add_roots_inner(b, e, FALSE);
    UNLOCK();
    ENABLE_SIGNALS();
}


/* Add [b,e) to the root set.  Adding the same interval a second time	*/
/* is a moderately fast noop, and hence benign.  We do not handle	*/
/* different but overlapping intervals efficiently.  (We do handle	*/
/* them correctly.)							*/
/* Tmp specifies that the interval may be deleted before 		*/
/* reregistering dynamic libraries.					*/ 
void GC_add_roots_inner(b, e, tmp)
char * b; char * e;
GC_bool tmp;
{
    struct roots * old;
    
#   if defined(MSWIN32) || defined(MSWINCE)
      /* Spend the time to ensure that there are no overlapping	*/
      /* or adjacent intervals.					*/
      /* This could be done faster with e.g. a			*/
      /* balanced tree.  But the execution time here is		*/
      /* virtually guaranteed to be dominated by the time it	*/
      /* takes to scan the roots.				*/
      {
        register int i;
        
        for (i = 0; i < n_root_sets; i++) {
            old = GC_static_roots + i;
            if ((ptr_t)b <= old -> r_end && (ptr_t)e >= old -> r_start) {
                if ((ptr_t)b < old -> r_start) {
                    old -> r_start = (ptr_t)b;
                    GC_root_size += (old -> r_start - (ptr_t)b);
                }
                if ((ptr_t)e > old -> r_end) {
                    old -> r_end = (ptr_t)e;
                    GC_root_size += ((ptr_t)e - old -> r_end);
                }
                old -> r_tmp &= tmp;
                break;
            }
        }
        if (i < n_root_sets) {
          /* merge other overlapping intervals */
            struct roots *other;
            
            for (i++; i < n_root_sets; i++) {
              other = GC_static_roots + i;
              b = (char *)(other -> r_start);
              e = (char *)(other -> r_end);
              if ((ptr_t)b <= old -> r_end && (ptr_t)e >= old -> r_start) {
                if ((ptr_t)b < old -> r_start) {
                    old -> r_start = (ptr_t)b;
                    GC_root_size += (old -> r_start - (ptr_t)b);
                }
                if ((ptr_t)e > old -> r_end) {
                    old -> r_end = (ptr_t)e;
                    GC_root_size += ((ptr_t)e - old -> r_end);
                }
                old -> r_tmp &= other -> r_tmp;
                /* Delete this entry. */
                  GC_root_size -= (other -> r_end - other -> r_start);
                  other -> r_start = GC_static_roots[n_root_sets-1].r_start;
                  other -> r_end = GC_static_roots[n_root_sets-1].r_end;
                                  n_root_sets--;
              }
            }
          return;
        }
      }
#   else
      old = GC_roots_present(b);
      if (old != 0) {
        if ((ptr_t)e <= old -> r_end) /* already there */ return;
        /* else extend */
        GC_root_size += (ptr_t)e - old -> r_end;
        old -> r_end = (ptr_t)e;
        return;
      }
#   endif
    if (n_root_sets == MAX_ROOT_SETS) {
        ABORT("Too many root sets\n");
    }
    GC_static_roots[n_root_sets].r_start = (ptr_t)b;
    GC_static_roots[n_root_sets].r_end = (ptr_t)e;
    GC_static_roots[n_root_sets].r_tmp = tmp;
#   if !defined(MSWIN32) && !defined(MSWINCE)
      GC_static_roots[n_root_sets].r_next = 0;
#   endif
    add_roots_to_index(GC_static_roots + n_root_sets);
    GC_root_size += (ptr_t)e - (ptr_t)b;
    n_root_sets++;
}

static roots_were_cleared = FALSE;

void GC_clear_roots GC_PROTO((void))
{
    DCL_LOCK_STATE;
    
    DISABLE_SIGNALS();
    LOCK();
    roots_were_cleared = TRUE;
    n_root_sets = 0;
    GC_root_size = 0;
#   if !defined(MSWIN32) && !defined(MSWINCE)
    {
    	register int i;
    	
    	for (i = 0; i < RT_SIZE; i++) GC_root_index[i] = 0;
    }
#   endif
    UNLOCK();
    ENABLE_SIGNALS();
}

/* Internal use only; lock held.	*/
void GC_remove_tmp_roots()
{
    register int i;
    
    for (i = 0; i < n_root_sets; ) {
    	if (GC_static_roots[i].r_tmp) {
    	    GC_root_size -=
		(GC_static_roots[i].r_end - GC_static_roots[i].r_start);
    	    GC_static_roots[i].r_start = GC_static_roots[n_root_sets-1].r_start;
    	    GC_static_roots[i].r_end = GC_static_roots[n_root_sets-1].r_end;
    	    GC_static_roots[i].r_tmp = GC_static_roots[n_root_sets-1].r_tmp;
    	    n_root_sets--;
    	} else {
    	    i++;
    	}
    }
#   if !defined(MSWIN32) && !defined(MSWINCE)
    {
    	register int i;
    	
    	for (i = 0; i < RT_SIZE; i++) GC_root_index[i] = 0;
    	for (i = 0; i < n_root_sets; i++)
		add_roots_to_index(GC_static_roots + i);
    }
#   endif
    
}

#if defined(MSWIN32) || defined(_WIN32_WCE_EMULATION)
/* Workaround for the OS mapping and unmapping behind our back:		*/
/* Is the address p in one of the temporary static root sections?	*/
GC_bool GC_is_tmp_root(p)
ptr_t p;
{
    static int last_root_set = MAX_ROOT_SETS;
    register int i;
    
    if (last_root_set < n_root_sets
	&& p >= GC_static_roots[last_root_set].r_start
        && p < GC_static_roots[last_root_set].r_end)
	return GC_static_roots[last_root_set].r_tmp;
    for (i = 0; i < n_root_sets; i++) {
    	if (p >= GC_static_roots[i].r_start
            && p < GC_static_roots[i].r_end) {
            last_root_set = i;
            return GC_static_roots[i].r_tmp;
        }
    }
    return(FALSE);
}
#endif /* MSWIN32 || _WIN32_WCE_EMULATION */

ptr_t GC_approx_sp()
{
    word dummy;

#   ifdef _MSC_VER
#     pragma warning(disable:4172)
#   endif
    return((ptr_t)(&dummy));
#   ifdef _MSC_VER
#     pragma warning(default:4172)
#   endif
}

/*
 * Data structure for excluded static roots.
 * Real declaration is in gc_priv.h.

struct exclusion {
    ptr_t e_start;
    ptr_t e_end;
};

struct exclusion GC_excl_table[MAX_EXCLUSIONS];
					-- Array of exclusions, ascending
					-- address order.
*/

size_t GC_excl_table_entries = 0;	/* Number of entries in use.	  */

/* Return the first exclusion range that includes an address >= start_addr */
/* Assumes the exclusion table contains at least one entry (namely the	   */
/* GC data structures).							   */
struct exclusion * GC_next_exclusion(start_addr)
ptr_t start_addr;
{
    size_t low = 0;
    size_t high = GC_excl_table_entries - 1;
    size_t mid;

    while (high > low) {
	mid = (low + high) >> 1;
	/* low <= mid < high	*/
	if ((word) GC_excl_table[mid].e_end <= (word) start_addr) {
	    low = mid + 1;
	} else {
	    high = mid;
	}
    }
    if ((word) GC_excl_table[low].e_end <= (word) start_addr) return 0;
    return GC_excl_table + low;
}

void GC_exclude_static_roots(start, finish)
GC_PTR start;
GC_PTR finish;
{
    struct exclusion * next;
    size_t next_index, i;

    if (0 == GC_excl_table_entries) {
	next = 0;
    } else {
	next = GC_next_exclusion(start);
    }
    if (0 != next) {
      if ((word)(next -> e_start) < (word) finish) {
	/* incomplete error check. */
	ABORT("exclusion ranges overlap");
      }  
      if ((word)(next -> e_start) == (word) finish) {
        /* extend old range backwards	*/
          next -> e_start = (ptr_t)start;
	  return;
      }
      next_index = next - GC_excl_table;
      for (i = GC_excl_table_entries; i > next_index; --i) {
	GC_excl_table[i] = GC_excl_table[i-1];
      }
    } else {
      next_index = GC_excl_table_entries;
    }
    if (GC_excl_table_entries == MAX_EXCLUSIONS) ABORT("Too many exclusions");
    GC_excl_table[next_index].e_start = (ptr_t)start;
    GC_excl_table[next_index].e_end = (ptr_t)finish;
    ++GC_excl_table_entries;
}

/* Invoke push_conditional on ranges that are not excluded. */
void GC_push_conditional_with_exclusions(bottom, top, all)
ptr_t bottom;
ptr_t top;
int all;
{
    struct exclusion * next;
    ptr_t excl_start;

    while (bottom < top) {
        next = GC_next_exclusion(bottom);
	if (0 == next || (excl_start = next -> e_start) >= top) {
	    GC_push_conditional(bottom, top, all);
	    return;
	}
	if (excl_start > bottom) GC_push_conditional(bottom, excl_start, all);
	bottom = next -> e_end;
    }
}

/*
 * In the absence of threads, push the stack contents.
 * In the presence of threads, push enough of the current stack
 * to ensure that callee-save registers saved in collector frames have been
 * seen.
 */
void GC_push_current_stack(cold_gc_frame)
ptr_t cold_gc_frame;
{
#   if defined(THREADS)
	if (0 == cold_gc_frame) return;
#       ifdef STACK_GROWS_DOWN
    	  GC_push_all_eager(GC_approx_sp(), cold_gc_frame);
	  /* For IA64, the register stack backing store is handled 	*/
	  /* in the thread-specific code.				*/
#       else
	  GC_push_all_eager( cold_gc_frame, GC_approx_sp() );
#       endif
#   else
#   	ifdef STACK_GROWS_DOWN
    	    GC_push_all_stack_partially_eager( GC_approx_sp(), GC_stackbottom,
					       cold_gc_frame );
#	    ifdef IA64
	      /* We also need to push the register stack backing store. */
	      /* This should really be done in the same way as the	*/
	      /* regular stack.  For now we fudge it a bit.		*/
	      /* Note that the backing store grows up, so we can't use	*/
	      /* GC_push_all_stack_partially_eager.			*/
	      {
		extern word GC_save_regs_ret_val;
			/* Previously set to backing store pointer.	*/
		ptr_t bsp = (ptr_t) GC_save_regs_ret_val;
	        ptr_t cold_gc_bs_pointer;
		if (GC_all_interior_pointers) {
	          cold_gc_bs_pointer = bsp - 2048;
		  if (cold_gc_bs_pointer < BACKING_STORE_BASE) {
		    cold_gc_bs_pointer = BACKING_STORE_BASE;
		  } else {
		    GC_push_all_stack(BACKING_STORE_BASE, cold_gc_bs_pointer);
		  }
		} else {
		  cold_gc_bs_pointer = BACKING_STORE_BASE;
		}
		GC_push_all_eager(cold_gc_bs_pointer, bsp);
		/* All values should be sufficiently aligned that we	*/
		/* dont have to worry about the boundary.		*/
	      }
#	    endif
#       else
	    GC_push_all_stack_partially_eager( GC_stackbottom, GC_approx_sp(),
					       cold_gc_frame );
#       endif
#   endif /* !THREADS */
}

/*
 * Push GC internal roots.  Only called if there is some reason to believe
 * these would not otherwise get registered.
 */
void GC_push_gc_structures GC_PROTO((void))
{
    GC_push_finalizer_structures();
    GC_push_stubborn_structures();
#   if defined(THREADS)
      GC_push_thread_structures();
#   endif
}

#ifdef THREAD_LOCAL_ALLOC
  void GC_mark_thread_local_free_lists();
#endif

/*
 * Call the mark routines (GC_tl_push for a single pointer, GC_push_conditional
 * on groups of pointers) on every top level accessible pointer.
 * If all is FALSE, arrange to push only possibly altered values.
 * Cold_gc_frame is an address inside a GC frame that
 * remains valid until all marking is complete.
 * A zero value indicates that it's OK to miss some
 * register values.
 */
void GC_push_roots(all, cold_gc_frame)
GC_bool all;
ptr_t cold_gc_frame;
{
    register int i;

    /*
     * push registers - i.e., call GC_push_one(r) for each
     * register contents r.
     */
#   ifdef USE_GENERIC_PUSH_REGS
	GC_generic_push_regs(cold_gc_frame);
#   else
        GC_push_regs(); /* usually defined in machine_dep.c */
#   endif
        
    /*
     * Next push static data.  This must happen early on, since it's
     * not robust against mark stack overflow.
     */
     /* Reregister dynamic libraries, in case one got added.	*/
#      if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
	   || defined(PCR)) && !defined(SRC_M3)
         GC_remove_tmp_roots();
         if (!GC_no_dls) GC_register_dynamic_libraries();
#      else
	 GC_no_dls = TRUE;
#      endif

     /* Mark everything in static data areas                             */
       for (i = 0; i < n_root_sets; i++) {
         GC_push_conditional_with_exclusions(
			     GC_static_roots[i].r_start,
			     GC_static_roots[i].r_end, all);
       }

     /* Mark from GC internal roots if those might otherwise have	*/
     /* been excluded.							*/
       if (GC_no_dls || roots_were_cleared) {
	   GC_push_gc_structures();
       }

     /* Mark thread local free lists, even if their mark 	*/
     /* descriptor excludes the link field.			*/
#      ifdef THREAD_LOCAL_ALLOC
         GC_mark_thread_local_free_lists();
#      endif

    /*
     * Now traverse stacks.
     */
#   if !defined(USE_GENERIC_PUSH_REGS)
	GC_push_current_stack(cold_gc_frame);
	/* IN the threads case, this only pushes collector frames.      */
	/* In the USE_GENERIC_PUSH_REGS case, this is done inside	*/
	/* GC_push_regs, so that we catch callee-save registers saved	*/
	/* inside the GC_push_regs frame.				*/
	/* In the case of linux threads on Ia64, the hot section of	*/
	/* the main stack is marked here, but the register stack	*/
	/* backing store is handled in the threads-specific code.	*/
#   endif
    if (GC_push_other_roots != 0) (*GC_push_other_roots)();
    	/* In the threads case, this also pushes thread stacks.	*/
}