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) 1996-1999 by Silicon Graphics.  All rights reserved.
5  * Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved.
6  *
7  *
8  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
10  *
11  * Permission is hereby granted to use or copy this program
12  * for any purpose,  provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  */
17 
18 
19 # ifndef GC_PRIVATE_H
20 # define GC_PRIVATE_H
21 
22 #if defined(mips) && defined(SYSTYPE_BSD) && defined(sony_news)
23     /* sony RISC NEWS, NEWSOS 4 */
24 #   define BSD_TIME
25 /*    typedef long ptrdiff_t;   -- necessary on some really old systems	*/
26 #endif
27 
28 #if defined(mips) && defined(SYSTYPE_BSD43)
29     /* MIPS RISCOS 4 */
30 #   define BSD_TIME
31 #endif
32 
33 #ifdef DGUX
34 #   include <sys/types.h>
35 #   include <sys/time.h>
36 #   include <sys/resource.h>
37 #endif /* DGUX */
38 
39 #ifdef BSD_TIME
40 #   include <sys/types.h>
41 #   include <sys/time.h>
42 #   include <sys/resource.h>
43 #endif /* BSD_TIME */
44 
45 # ifndef _GC_H
46 #   include "../gc.h"
47 # endif
48 
49 # ifndef GC_MARK_H
50 #   include "../gc_mark.h"
51 # endif
52 
53 typedef GC_word word;
54 typedef GC_signed_word signed_word;
55 
56 typedef int GC_bool;
57 # define TRUE 1
58 # define FALSE 0
59 
60 typedef char * ptr_t;	/* A generic pointer to which we can add	*/
61 			/* byte displacements.				*/
62 			/* Preferably identical to caddr_t, if it 	*/
63 			/* exists.					*/
64 
65 # ifndef GCCONFIG_H
66 #   include "gcconfig.h"
67 # endif
68 
69 # ifndef HEADERS_H
70 #   include "gc_hdrs.h"
71 # endif
72 
73 #if defined(__STDC__)
74 #   include <stdlib.h>
75 #   if !(defined( sony_news ) )
76 #       include <stddef.h>
77 #   endif
78 #   define VOLATILE volatile
79 #else
80 #   ifdef MSWIN32
81 #   	include <stdlib.h>
82 #   endif
83 #   define VOLATILE
84 #endif
85 
86 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
87 /* This doesn't work in some earlier gcc versions */
88 # define EXPECT(expr, outcome) __builtin_expect(expr,outcome)
89   /* Equivalent to (expr), but predict that usually (expr)==outcome. */
90 #else
91 # define EXPECT(expr, outcome) (expr)
92 #endif
93 
94 # ifndef GC_LOCKS_H
95 #   include "gc_locks.h"
96 # endif
97 
98 # ifdef STACK_GROWS_DOWN
99 #   define COOLER_THAN >
100 #   define HOTTER_THAN <
101 #   define MAKE_COOLER(x,y) if ((word)(x)+(y) > (word)(x)) {(x) += (y);} \
102 			    else {(x) = (word)ONES;}
103 #   define MAKE_HOTTER(x,y) (x) -= (y)
104 # else
105 #   define COOLER_THAN <
106 #   define HOTTER_THAN >
107 #   define MAKE_COOLER(x,y) if ((word)(x)-(y) < (word)(x)) {(x) -= (y);} else {(x) = 0;}
108 #   define MAKE_HOTTER(x,y) (x) += (y)
109 # endif
110 
111 #if defined(AMIGA) && defined(__SASC)
112 #   define GC_FAR __far
113 #else
114 #   define GC_FAR
115 #endif
116 
117 
118 /*********************************/
119 /*                               */
120 /* Definitions for conservative  */
121 /* collector                     */
122 /*                               */
123 /*********************************/
124 
125 /*********************************/
126 /*                               */
127 /* Easily changeable parameters  */
128 /*                               */
129 /*********************************/
130 
131 /* #define STUBBORN_ALLOC */
132 		    /* Enable stubborm allocation, and thus a limited	*/
133 		    /* form of incremental collection w/o dirty bits.	*/
134 
135 /* #define ALL_INTERIOR_POINTERS */
136 		    /* Forces all pointers into the interior of an 	*/
137 		    /* object to be considered valid.  Also causes the	*/
138 		    /* sizes of all objects to be inflated by at least 	*/
139 		    /* one byte.  This should suffice to guarantee	*/
140 		    /* that in the presence of a compiler that does	*/
141 		    /* not perform garbage-collector-unsafe		*/
142 		    /* optimizations, all portable, strictly ANSI	*/
143 		    /* conforming C programs should be safely usable	*/
144 		    /* with malloc replaced by GC_malloc and free	*/
145 		    /* calls removed.  There are several disadvantages: */
146 		    /* 1. There are probably no interesting, portable,	*/
147 		    /*    strictly ANSI	conforming C programs.		*/
148 		    /* 2. This option makes it hard for the collector	*/
149 		    /*    to allocate space that is not ``pointed to''  */
150 		    /*    by integers, etc.  Under SunOS 4.X with a 	*/
151 		    /*    statically linked libc, we empiricaly		*/
152 		    /*    observed that it would be difficult to 	*/
153 		    /*	  allocate individual objects larger than 100K.	*/
154 		    /* 	  Even if only smaller objects are allocated,	*/
155 		    /*    more swap space is likely to be needed.       */
156 		    /*    Fortunately, much of this will never be	*/
157 		    /*    touched.					*/
158 		    /* If you can easily avoid using this option, do.	*/
159 		    /* If not, try to keep individual objects small.	*/
160 		    /* This is now really controlled at startup,	*/
161 		    /* through GC_all_interior_pointers.		*/
162 
163 #define PRINTSTATS  /* Print garbage collection statistics          	*/
164 		    /* For less verbose output, undefine in reclaim.c 	*/
165 
166 #define PRINTTIMES  /* Print the amount of time consumed by each garbage   */
167 		    /* collection.                                         */
168 
169 #define PRINTBLOCKS /* Print object sizes associated with heap blocks,     */
170 		    /* whether the objects are atomic or composite, and    */
171 		    /* whether or not the block was found to be empty      */
172 		    /* during the reclaim phase.  Typically generates       */
173 		    /* about one screenful per garbage collection.         */
174 #undef PRINTBLOCKS
175 
176 #ifdef SILENT
177 #  ifdef PRINTSTATS
178 #    undef PRINTSTATS
179 #  endif
180 #  ifdef PRINTTIMES
181 #    undef PRINTTIMES
182 #  endif
183 #  ifdef PRINTNBLOCKS
184 #    undef PRINTNBLOCKS
185 #  endif
186 #endif
187 
188 #if defined(PRINTSTATS) && !defined(GATHERSTATS)
189 #   define GATHERSTATS
190 #endif
191 
192 #if defined(PRINTSTATS) || !defined(SMALL_CONFIG)
193 #   define CONDPRINT  /* Print some things if GC_print_stats is set */
194 #endif
195 
196 #define GC_INVOKE_FINALIZERS() GC_notify_or_invoke_finalizers()
197 
198 #define MERGE_SIZES /* Round up some object sizes, so that fewer distinct */
199 		    /* free lists are actually maintained.  This applies  */
200 		    /* only to the top level routines in misc.c, not to   */
201 		    /* user generated code that calls GC_allocobj and     */
202 		    /* GC_allocaobj directly.                             */
203 		    /* Slows down average programs slightly.  May however */
204 		    /* substantially reduce fragmentation if allocation   */
205 		    /* request sizes are widely scattered.                */
206 		    /* May save significant amounts of space for obj_map  */
207 		    /* entries.						  */
208 
209 #if defined(USE_MARK_BYTES) && !defined(ALIGN_DOUBLE)
210 #  define ALIGN_DOUBLE
211    /* We use one byte for every 2 words, which doesn't allow for	*/
212    /* odd numbered words to have mark bits.				*/
213 #endif
214 
215 #if defined(GC_GCJ_SUPPORT) && ALIGNMENT < 8 && !defined(ALIGN_DOUBLE)
216    /* GCJ's Hashtable synchronization code requires 64-bit alignment.  */
217 #  define ALIGN_DOUBLE
218 #endif
219 
220 /* ALIGN_DOUBLE requires MERGE_SIZES at present. */
221 # if defined(ALIGN_DOUBLE) && !defined(MERGE_SIZES)
222 #   define MERGE_SIZES
223 # endif
224 
225 #if !defined(DONT_ADD_BYTE_AT_END)
226 # define EXTRA_BYTES GC_all_interior_pointers
227 #else
228 # define EXTRA_BYTES 0
229 #endif
230 
231 
232 # ifndef LARGE_CONFIG
233 #   define MINHINCR 16	 /* Minimum heap increment, in blocks of HBLKSIZE  */
234 			 /* Must be multiple of largest page size.	   */
235 #   define MAXHINCR 2048 /* Maximum heap increment, in blocks              */
236 # else
237 #   define MINHINCR 64
238 #   define MAXHINCR 4096
239 # endif
240 
241 # define TIME_LIMIT 50	   /* We try to keep pause times from exceeding	 */
242 			   /* this by much. In milliseconds.		 */
243 
244 # define BL_LIMIT GC_black_list_spacing
245 			   /* If we need a block of N bytes, and we have */
246 			   /* a block of N + BL_LIMIT bytes available, 	 */
247 			   /* and N > BL_LIMIT,				 */
248 			   /* but all possible positions in it are 	 */
249 			   /* blacklisted, we just use it anyway (and	 */
250 			   /* print a warning, if warnings are enabled). */
251 			   /* This risks subsequently leaking the block	 */
252 			   /* due to a false reference.  But not using	 */
253 			   /* the block risks unreasonable immediate	 */
254 			   /* heap growth.				 */
255 
256 /*********************************/
257 /*                               */
258 /* Stack saving for debugging	 */
259 /*                               */
260 /*********************************/
261 
262 #ifdef NEED_CALLINFO
263     struct callinfo {
264 	word ci_pc;  	/* Caller, not callee, pc	*/
265 #	if NARGS > 0
266 	    word ci_arg[NARGS];	/* bit-wise complement to avoid retention */
267 #	endif
268 #	if defined(ALIGN_DOUBLE) && (NFRAMES * (NARGS + 1)) % 2 == 1
269 	    /* Likely alignment problem. */
270 	    word ci_dummy;
271 #	endif
272     };
273 #endif
274 
275 #ifdef SAVE_CALL_CHAIN
276 
277 /* Fill in the pc and argument information for up to NFRAMES of my	*/
278 /* callers.  Ignore my frame and my callers frame.			*/
279 void GC_save_callers GC_PROTO((struct callinfo info[NFRAMES]));
280 
281 void GC_print_callers GC_PROTO((struct callinfo info[NFRAMES]));
282 
283 #endif
284 
285 
286 /*********************************/
287 /*                               */
288 /* OS interface routines	 */
289 /*                               */
290 /*********************************/
291 
292 #ifdef BSD_TIME
293 #   undef CLOCK_TYPE
294 #   undef GET_TIME
295 #   undef MS_TIME_DIFF
296 #   define CLOCK_TYPE struct timeval
297 #   define GET_TIME(x) { struct rusage rusage; \
298 			 getrusage (RUSAGE_SELF,  &rusage); \
299 			 x = rusage.ru_utime; }
300 #   define MS_TIME_DIFF(a,b) ((double) (a.tv_sec - b.tv_sec) * 1000.0 \
301                                + (double) (a.tv_usec - b.tv_usec) / 1000.0)
302 #else /* !BSD_TIME */
303 # if defined(MSWIN32) || defined(MSWINCE)
304 #   include <windows.h>
305 #   include <winbase.h>
306 #   define CLOCK_TYPE DWORD
307 #   define GET_TIME(x) x = GetTickCount()
308 #   define MS_TIME_DIFF(a,b) ((long)((a)-(b)))
309 # else /* !MSWIN32, !MSWINCE, !BSD_TIME */
310 #   include <time.h>
311 #   if !defined(__STDC__) && defined(SPARC) && defined(SUNOS4)
312       clock_t clock();	/* Not in time.h, where it belongs	*/
313 #   endif
314 #   if defined(FREEBSD) && !defined(CLOCKS_PER_SEC)
315 #     include <machine/limits.h>
316 #     define CLOCKS_PER_SEC CLK_TCK
317 #   endif
318 #   if !defined(CLOCKS_PER_SEC)
319 #     define CLOCKS_PER_SEC 1000000
320 /*
321  * This is technically a bug in the implementation.  ANSI requires that
322  * CLOCKS_PER_SEC be defined.  But at least under SunOS4.1.1, it isn't.
323  * Also note that the combination of ANSI C and POSIX is incredibly gross
324  * here. The type clock_t is used by both clock() and times().  But on
325  * some machines these use different notions of a clock tick,  CLOCKS_PER_SEC
326  * seems to apply only to clock.  Hence we use it here.  On many machines,
327  * including SunOS, clock actually uses units of microseconds (which are
328  * not really clock ticks).
329  */
330 #   endif
331 #   define CLOCK_TYPE clock_t
332 #   define GET_TIME(x) x = clock()
333 #   define MS_TIME_DIFF(a,b) ((unsigned long) \
334 		(1000.0*(double)((a)-(b))/(double)CLOCKS_PER_SEC))
335 # endif /* !MSWIN32 */
336 #endif /* !BSD_TIME */
337 
338 /* We use bzero and bcopy internally.  They may not be available.	*/
339 # if defined(SPARC) && defined(SUNOS4)
340 #   define BCOPY_EXISTS
341 # endif
342 # if defined(M68K) && defined(AMIGA)
343 #   define BCOPY_EXISTS
344 # endif
345 # if defined(M68K) && defined(NEXT)
346 #   define BCOPY_EXISTS
347 # endif
348 # if defined(VAX)
349 #   define BCOPY_EXISTS
350 # endif
351 # if defined(AMIGA)
352 #   include <string.h>
353 #   define BCOPY_EXISTS
354 # endif
355 # if defined(DARWIN)
356 #   include <string.h>
357 #   define BCOPY_EXISTS
358 # endif
359 
360 # ifndef BCOPY_EXISTS
361 #   include <string.h>
362 #   define BCOPY(x,y,n) memcpy(y, x, (size_t)(n))
363 #   define BZERO(x,n)  memset(x, 0, (size_t)(n))
364 # else
365 #   define BCOPY(x,y,n) bcopy((char *)(x),(char *)(y),(int)(n))
366 #   define BZERO(x,n) bzero((char *)(x),(int)(n))
367 # endif
368 
369 #if defined(DARWIN)
370 #	if defined(POWERPC)
371 #		define GC_MACH_THREAD_STATE_FLAVOR PPC_THREAD_STATE
372 #	elif defined(I386)
373 #		define GC_MACH_THREAD_STATE_FLAVOR i386_THREAD_STATE
374 #	elif defined(X86_64)
375 #		define GC_MACH_THREAD_STATE_FLAVOR x86_THREAD_STATE64
376 #	else
377 #		define GC_MACH_THREAD_STATE_FLAVOR MACHINE_THREAD_STATE
378 #	endif
379 #endif
380 
381 /* Delay any interrupts or signals that may abort this thread.  Data	*/
382 /* structures are in a consistent state outside this pair of calls.	*/
383 /* ANSI C allows both to be empty (though the standard isn't very	*/
384 /* clear on that point).  Standard malloc implementations are usually	*/
385 /* neither interruptable nor thread-safe, and thus correspond to	*/
386 /* empty definitions.							*/
387 /* It probably doesn't make any sense to declare these to be nonempty	*/
388 /* if the code is being optimized, since signal safety relies on some	*/
389 /* ordering constraints that are typically not obeyed by optimizing	*/
390 /* compilers.								*/
391 # ifdef PCR
392 #   define DISABLE_SIGNALS() \
393 		 PCR_Th_SetSigMask(PCR_allSigsBlocked,&GC_old_sig_mask)
394 #   define ENABLE_SIGNALS() \
395 		PCR_Th_SetSigMask(&GC_old_sig_mask, NIL)
396 # else
397 #   if defined(THREADS) || defined(AMIGA)  \
398 	|| defined(MSWIN32) || defined(MSWINCE) || defined(MACOS) \
399 	|| defined(DJGPP) || defined(NO_SIGNALS)
400 			/* Also useful for debugging.		*/
401 	/* Should probably use thr_sigsetmask for GC_SOLARIS_THREADS. */
402 #     define DISABLE_SIGNALS()
403 #     define ENABLE_SIGNALS()
404 #   else
405 #     define DISABLE_SIGNALS() GC_disable_signals()
406 	void GC_disable_signals(void);
407 #     define ENABLE_SIGNALS() GC_enable_signals()
408 	void GC_enable_signals(void);
409 #   endif
410 # endif
411 
412 /*
413  * Stop and restart mutator threads.
414  */
415 # ifdef PCR
416 #     include "th/PCR_ThCtl.h"
417 #     define STOP_WORLD() \
418  	PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_stopNormal, \
419  				   PCR_allSigsBlocked, \
420  				   PCR_waitForever)
421 #     define START_WORLD() \
422 	PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_null, \
423  				   PCR_allSigsBlocked, \
424  				   PCR_waitForever);
425 # else
426 #   if defined(GC_SOLARIS_THREADS) || defined(GC_WIN32_THREADS) \
427 	|| defined(GC_PTHREADS)
428       void GC_stop_world(void);
429       void GC_start_world(void);
430 #     define STOP_WORLD() GC_stop_world()
431 #     define START_WORLD() GC_start_world()
432 #   else
433 #     define STOP_WORLD()
434 #     define START_WORLD()
435 #   endif
436 # endif
437 
438 /* Abandon ship */
439 # ifdef PCR
440 #   define ABORT(s) PCR_Base_Panic(s)
441 # else
442 #   ifdef SMALL_CONFIG
443 #	define ABORT(msg) abort();
444 #   else
445 	GC_API void GC_abort GC_PROTO((GC_CONST char * msg));
446 #       define ABORT(msg) GC_abort(msg);
447 #   endif
448 # endif
449 
450 /* Exit abnormally, but without making a mess (e.g. out of memory) */
451 # ifdef PCR
452 #   define EXIT() PCR_Base_Exit(1,PCR_waitForever)
453 # else
454 #   define EXIT() (void)exit(1)
455 # endif
456 
457 /* Print warning message, e.g. almost out of memory.	*/
458 # define WARN(msg,arg) (*GC_current_warn_proc)("GC Warning: " msg, (GC_word)(arg))
459 extern GC_warn_proc GC_current_warn_proc;
460 
461 /* Get environment entry */
462 #if !defined(NO_GETENV)
463 #   if defined(EMPTY_GETENV_RESULTS)
464 	/* Workaround for a reputed Wine bug.	*/
fixed_getenv(const char * name)465 	static inline char * fixed_getenv(const char *name)
466 	{
467 	  char * tmp = getenv(name);
468 	  if (tmp == 0 || strlen(tmp) == 0)
469 	    return 0;
470 	  return tmp;
471 	}
472 #       define GETENV(name) fixed_getenv(name)
473 #   else
474 #       define GETENV(name) getenv(name)
475 #   endif
476 #else
477 #   define GETENV(name) 0
478 #endif
479 
480 /*********************************/
481 /*                               */
482 /* Word-size-dependent defines   */
483 /*                               */
484 /*********************************/
485 
486 #if CPP_WORDSZ == 32
487 #  define WORDS_TO_BYTES(x)   ((x)<<2)
488 #  define BYTES_TO_WORDS(x)   ((x)>>2)
489 #  define LOGWL               ((word)5)    /* log[2] of CPP_WORDSZ */
490 #  define modWORDSZ(n) ((n) & 0x1f)        /* n mod size of word	    */
491 #  if ALIGNMENT != 4
492 #	define UNALIGNED
493 #  endif
494 #endif
495 
496 #if CPP_WORDSZ == 64
497 #  define WORDS_TO_BYTES(x)   ((x)<<3)
498 #  define BYTES_TO_WORDS(x)   ((x)>>3)
499 #  define LOGWL               ((word)6)    /* log[2] of CPP_WORDSZ */
500 #  define modWORDSZ(n) ((n) & 0x3f)        /* n mod size of word	    */
501 #  if ALIGNMENT != 8
502 #	define UNALIGNED
503 #  endif
504 #endif
505 
506 #define WORDSZ ((word)CPP_WORDSZ)
507 #define SIGNB  ((word)1 << (WORDSZ-1))
508 #define BYTES_PER_WORD      ((word)(sizeof (word)))
509 #define ONES                ((word)(signed_word)(-1))
510 #define divWORDSZ(n) ((n) >> LOGWL)	   /* divide n by size of word      */
511 
512 /*********************/
513 /*                   */
514 /*  Size Parameters  */
515 /*                   */
516 /*********************/
517 
518 /*  heap block size, bytes. Should be power of 2 */
519 
520 #ifndef HBLKSIZE
521 # ifdef SMALL_CONFIG
522 #   define CPP_LOG_HBLKSIZE 10
523 # else
524 #   if (CPP_WORDSZ == 32) || (defined(HPUX) && defined(HP_PA))
525       /* HPUX/PA seems to use 4K pages with the 64 bit ABI */
526 #     define CPP_LOG_HBLKSIZE 12
527 #   else
528 #     define CPP_LOG_HBLKSIZE 13
529 #   endif
530 # endif
531 #else
532 # if HBLKSIZE == 512
533 #   define CPP_LOG_HBLKSIZE 9
534 # endif
535 # if HBLKSIZE == 1024
536 #   define CPP_LOG_HBLKSIZE 10
537 # endif
538 # if HBLKSIZE == 2048
539 #   define CPP_LOG_HBLKSIZE 11
540 # endif
541 # if HBLKSIZE == 4096
542 #   define CPP_LOG_HBLKSIZE 12
543 # endif
544 # if HBLKSIZE == 8192
545 #   define CPP_LOG_HBLKSIZE 13
546 # endif
547 # if HBLKSIZE == 16384
548 #   define CPP_LOG_HBLKSIZE 14
549 # endif
550 # ifndef CPP_LOG_HBLKSIZE
551     --> fix HBLKSIZE
552 # endif
553 # undef HBLKSIZE
554 #endif
555 # define CPP_HBLKSIZE (1 << CPP_LOG_HBLKSIZE)
556 # define LOG_HBLKSIZE   ((word)CPP_LOG_HBLKSIZE)
557 # define HBLKSIZE ((word)CPP_HBLKSIZE)
558 
559 
560 /*  max size objects supported by freelist (larger objects may be   */
561 /*  allocated, but less efficiently)                                */
562 
563 #define CPP_MAXOBJBYTES (CPP_HBLKSIZE/2)
564 #define MAXOBJBYTES ((word)CPP_MAXOBJBYTES)
565 #define CPP_MAXOBJSZ    BYTES_TO_WORDS(CPP_MAXOBJBYTES)
566 #define MAXOBJSZ ((word)CPP_MAXOBJSZ)
567 
568 # define divHBLKSZ(n) ((n) >> LOG_HBLKSIZE)
569 
570 # define HBLK_PTR_DIFF(p,q) divHBLKSZ((ptr_t)p - (ptr_t)q)
571 	/* Equivalent to subtracting 2 hblk pointers.	*/
572 	/* We do it this way because a compiler should	*/
573 	/* find it hard to use an integer division	*/
574 	/* instead of a shift.  The bundled SunOS 4.1	*/
575 	/* o.w. sometimes pessimizes the subtraction to	*/
576 	/* involve a call to .div.			*/
577 
578 # define modHBLKSZ(n) ((n) & (HBLKSIZE-1))
579 
580 # define HBLKPTR(objptr) ((struct hblk *)(((word) (objptr)) & ~(HBLKSIZE-1)))
581 
582 # define HBLKDISPL(objptr) (((word) (objptr)) & (HBLKSIZE-1))
583 
584 /* Round up byte allocation requests to integral number of words, etc. */
585 # define ROUNDED_UP_WORDS(n) \
586 	BYTES_TO_WORDS((n) + (WORDS_TO_BYTES(1) - 1 + EXTRA_BYTES))
587 # ifdef ALIGN_DOUBLE
588 #       define ALIGNED_WORDS(n) \
589 	    (BYTES_TO_WORDS((n) + WORDS_TO_BYTES(2) - 1 + EXTRA_BYTES) & ~1)
590 # else
591 #       define ALIGNED_WORDS(n) ROUNDED_UP_WORDS(n)
592 # endif
593 # define SMALL_OBJ(bytes) ((bytes) <= (MAXOBJBYTES - EXTRA_BYTES))
594 # define ADD_SLOP(bytes) ((bytes) + EXTRA_BYTES)
595 # ifndef MIN_WORDS
596     /* MIN_WORDS is the size of the smallest allocated object.	*/
597     /* 1 and 2 are the only valid values.			*/
598     /* 2 must be used if:					*/
599     /* - GC_gcj_malloc can be used for objects of requested 	*/
600     /*   size  smaller than 2 words, or				*/
601     /* - USE_MARK_BYTES is defined.				*/
602 #   if defined(USE_MARK_BYTES) || defined(GC_GCJ_SUPPORT)
603 #     define MIN_WORDS 2   	/* Smallest allocated object.	*/
604 #   else
605 #     define MIN_WORDS 1
606 #   endif
607 # endif
608 
609 
610 /*
611  * Hash table representation of sets of pages.  This assumes it is
612  * OK to add spurious entries to sets.
613  * Used by black-listing code, and perhaps by dirty bit maintenance code.
614  */
615 
616 # ifdef LARGE_CONFIG
617 #   define LOG_PHT_ENTRIES  20  /* Collisions likely at 1M blocks,	*/
618 				/* which is >= 4GB.  Each table takes	*/
619 				/* 128KB, some of which may never be	*/
620 				/* touched.				*/
621 # else
622 #   ifdef SMALL_CONFIG
623 #     define LOG_PHT_ENTRIES  14 /* Collisions are likely if heap grows	*/
624 				 /* to more than 16K hblks = 64MB.	*/
625 				 /* Each hash table occupies 2K bytes.   */
626 #   else /* default "medium" configuration */
627 #     define LOG_PHT_ENTRIES  16 /* Collisions are likely if heap grows	*/
628 				 /* to more than 64K hblks >= 256MB.	*/
629 				 /* Each hash table occupies 8K bytes.  */
630 				 /* Even for somewhat smaller heaps, 	*/
631 				 /* say half that, collisions may be an	*/
632 				 /* issue because we blacklist 		*/
633 				 /* addresses outside the heap.		*/
634 #   endif
635 # endif
636 # define PHT_ENTRIES ((word)1 << LOG_PHT_ENTRIES)
637 # define PHT_SIZE (PHT_ENTRIES >> LOGWL)
638 typedef word page_hash_table[PHT_SIZE];
639 
640 # define PHT_HASH(addr) ((((word)(addr)) >> LOG_HBLKSIZE) & (PHT_ENTRIES - 1))
641 
642 # define get_pht_entry_from_index(bl, index) \
643 		(((bl)[divWORDSZ(index)] >> modWORDSZ(index)) & 1)
644 # define set_pht_entry_from_index(bl, index) \
645 		(bl)[divWORDSZ(index)] |= (word)1 << modWORDSZ(index)
646 # define clear_pht_entry_from_index(bl, index) \
647 		(bl)[divWORDSZ(index)] &= ~((word)1 << modWORDSZ(index))
648 /* And a dumb but thread-safe version of set_pht_entry_from_index.	*/
649 /* This sets (many) extra bits.						*/
650 # define set_pht_entry_from_index_safe(bl, index) \
651 		(bl)[divWORDSZ(index)] = ONES
652 
653 
654 
655 /********************************************/
656 /*                                          */
657 /*    H e a p   B l o c k s                 */
658 /*                                          */
659 /********************************************/
660 
661 /*  heap block header */
662 #define HBLKMASK   (HBLKSIZE-1)
663 
664 #define BITS_PER_HBLK (CPP_HBLKSIZE * 8)
665 
666 #define MARK_BITS_PER_HBLK (BITS_PER_HBLK/CPP_WORDSZ)
667 	   /* upper bound                                    */
668 	   /* We allocate 1 bit/word, unless USE_MARK_BYTES  */
669 	   /* is defined.  Only the first word   	     */
670 	   /* in each object is actually marked.             */
671 
672 # ifdef USE_MARK_BYTES
673 #   define MARK_BITS_SZ (MARK_BITS_PER_HBLK/2)
674 	/* Unlike the other case, this is in units of bytes.		*/
675 	/* We actually allocate only every second mark bit, since we	*/
676 	/* force all objects to be doubleword aligned.			*/
677 	/* However, each mark bit is allocated as a byte.		*/
678 # else
679 #   define MARK_BITS_SZ (MARK_BITS_PER_HBLK/CPP_WORDSZ)
680 # endif
681 
682 /* We maintain layout maps for heap blocks containing objects of a given */
683 /* size.  Each entry in this map describes a byte offset and has the	 */
684 /* following type.							 */
685 typedef unsigned char map_entry_type;
686 
687 struct hblkhdr {
688     word hb_sz;  /* If in use, size in words, of objects in the block. */
689 		 /* if free, the size in bytes of the whole block      */
690     struct hblk * hb_next; 	/* Link field for hblk free list	 */
691     				/* and for lists of chunks waiting to be */
692     				/* reclaimed.				 */
693     struct hblk * hb_prev;	/* Backwards link for free list.	*/
694     word hb_descr;   		/* object descriptor for marking.  See	*/
695     				/* mark.h.				*/
696     map_entry_type * hb_map;
697     			/* A pointer to a pointer validity map of the block. */
698     		      	/* See GC_obj_map.				     */
699     		     	/* Valid for all blocks with headers.		     */
700     		     	/* Free blocks point to GC_invalid_map.		     */
701     unsigned char hb_obj_kind;
702     			 /* Kind of objects in the block.  Each kind 	*/
703     			 /* identifies a mark procedure and a set of 	*/
704     			 /* list headers.  Sometimes called regions.	*/
705     unsigned char hb_flags;
706 #	define IGNORE_OFF_PAGE	1	/* Ignore pointers that do not	*/
707 					/* point to the first page of 	*/
708 					/* this object.			*/
709 #	define WAS_UNMAPPED 2	/* This is a free block, which has	*/
710 				/* been unmapped from the address 	*/
711 				/* space.				*/
712 				/* GC_remap must be invoked on it	*/
713 				/* before it can be reallocated.	*/
714 				/* Only set with USE_MUNMAP.		*/
715     unsigned short hb_last_reclaimed;
716     				/* Value of GC_gc_no when block was	*/
717     				/* last allocated or swept. May wrap.   */
718 				/* For a free block, this is maintained */
719 				/* only for USE_MUNMAP, and indicates	*/
720 				/* when the header was allocated, or	*/
721 				/* when the size of the block last	*/
722 				/* changed.				*/
723 #   ifdef USE_MARK_BYTES
724       union {
725         char _hb_marks[MARK_BITS_SZ];
726 			    /* The i'th byte is 1 if the object 	*/
727 			    /* starting at word 2i is marked, 0 o.w.	*/
728 	word dummy;	/* Force word alignment of mark bytes. */
729       } _mark_byte_union;
730 #     define hb_marks _mark_byte_union._hb_marks
731 #   else
732       word hb_marks[MARK_BITS_SZ];
733 			    /* Bit i in the array refers to the             */
734 			    /* object starting at the ith word (header      */
735 			    /* INCLUDED) in the heap block.                 */
736 			    /* The lsb of word 0 is numbered 0.		    */
737 			    /* Unused bits are invalid, and are 	    */
738 			    /* occasionally set, e.g for uncollectable	    */
739 			    /* objects.					    */
740 #   endif /* !USE_MARK_BYTES */
741 };
742 
743 /*  heap block body */
744 
745 # define BODY_SZ (HBLKSIZE/sizeof(word))
746 
747 struct hblk {
748     word hb_body[BODY_SZ];
749 };
750 
751 # define HBLK_IS_FREE(hdr) ((hdr) -> hb_map == GC_invalid_map)
752 
753 # define OBJ_SZ_TO_BLOCKS(sz) \
754     divHBLKSZ(WORDS_TO_BYTES(sz) + HBLKSIZE-1)
755     /* Size of block (in units of HBLKSIZE) needed to hold objects of	*/
756     /* given sz (in words).						*/
757 
758 /* Object free list link */
759 # define obj_link(p) (*(ptr_t *)(p))
760 
761 # define LOG_MAX_MARK_PROCS 6
762 # define MAX_MARK_PROCS (1 << LOG_MAX_MARK_PROCS)
763 
764 /* Root sets.  Logically private to mark_rts.c.  But we don't want the	*/
765 /* tables scanned, so we put them here.					*/
766 /* MAX_ROOT_SETS is the maximum number of ranges that can be 	*/
767 /* registered as static roots. 					*/
768 # ifdef LARGE_CONFIG
769 #   define MAX_ROOT_SETS 4096
770 # else
771 #   ifdef PCR
772 #     define MAX_ROOT_SETS 1024
773 #   else
774 #     if defined(MSWIN32) || defined(MSWINCE)
775 #	define MAX_ROOT_SETS 1024
776 	    /* Under NT, we add only written pages, which can result 	*/
777 	    /* in many small root sets.					*/
778 #     else
779 #       define MAX_ROOT_SETS 1024
780 #     endif
781 #   endif
782 # endif
783 
784 # define MAX_EXCLUSIONS (MAX_ROOT_SETS/4)
785 /* Maximum number of segments that can be excluded from root sets.	*/
786 
787 /*
788  * Data structure for excluded static roots.
789  */
790 struct exclusion {
791     ptr_t e_start;
792     ptr_t e_end;
793 };
794 
795 /* Data structure for list of root sets.				*/
796 /* We keep a hash table, so that we can filter out duplicate additions.	*/
797 /* Under Win32, we need to do a better job of filtering overlaps, so	*/
798 /* we resort to sequential search, and pay the price.			*/
799 struct roots {
800 	ptr_t r_start;
801 	ptr_t r_end;
802 #	if !defined(MSWIN32) && !defined(MSWINCE)
803 	  struct roots * r_next;
804 #	endif
805 	GC_bool r_tmp;
806 	  	/* Delete before registering new dynamic libraries */
807 };
808 
809 #if !defined(MSWIN32) && !defined(MSWINCE)
810     /* Size of hash table index to roots.	*/
811 #   define LOG_RT_SIZE 6
812 #   define RT_SIZE (1 << LOG_RT_SIZE) /* Power of 2, may be != MAX_ROOT_SETS */
813 #endif
814 
815 /* Lists of all heap blocks and free lists	*/
816 /* as well as other random data structures	*/
817 /* that should not be scanned by the		*/
818 /* collector.					*/
819 /* These are grouped together in a struct	*/
820 /* so that they can be easily skipped by the	*/
821 /* GC_mark routine.				*/
822 /* The ordering is weird to make GC_malloc	*/
823 /* faster by keeping the important fields	*/
824 /* sufficiently close together that a		*/
825 /* single load of a base register will do.	*/
826 /* Scalars that could easily appear to		*/
827 /* be pointers are also put here.		*/
828 /* The main fields should precede any 		*/
829 /* conditionally included fields, so that	*/
830 /* gc_inl.h will work even if a different set	*/
831 /* of macros is defined when the client is	*/
832 /* compiled.					*/
833 
834 struct _GC_arrays {
835   word _heapsize;
836   word _max_heapsize;
837   word _requested_heapsize;	/* Heap size due to explicit expansion */
838   ptr_t _last_heap_addr;
839   ptr_t _prev_heap_addr;
840   word _large_free_bytes;
841 	/* Total bytes contained in blocks on large object free */
842 	/* list.						*/
843   word _large_allocd_bytes;
844   	/* Total number of bytes in allocated large objects blocks.	*/
845   	/* For the purposes of this counter and the next one only, a 	*/
846   	/* large object is one that occupies a block of at least	*/
847   	/* 2*HBLKSIZE.							*/
848   word _max_large_allocd_bytes;
849   	/* Maximum number of bytes that were ever allocated in		*/
850   	/* large object blocks.  This is used to help decide when it	*/
851   	/* is safe to split up a large block.				*/
852   word _words_allocd_before_gc;
853 		/* Number of words allocated before this	*/
854 		/* collection cycle.				*/
855 # ifndef SEPARATE_GLOBALS
856     word _words_allocd;
857   	/* Number of words allocated during this collection cycle */
858 # endif
859   word _words_wasted;
860   	/* Number of words wasted due to internal fragmentation	*/
861   	/* in large objects, or due to dropping blacklisted     */
862 	/* blocks, since last gc.  Approximate.                 */
863   word _words_finalized;
864   	/* Approximate number of words in objects (and headers)	*/
865   	/* That became ready for finalization in the last 	*/
866   	/* collection.						*/
867   word _non_gc_bytes_at_gc;
868   	/* Number of explicitly managed bytes of storage 	*/
869   	/* at last collection.					*/
870   word _mem_freed;
871   	/* Number of explicitly deallocated words of memory	*/
872   	/* since last collection.				*/
873   word _finalizer_mem_freed;
874   	/* Words of memory explicitly deallocated while 	*/
875   	/* finalizers were running.  Used to approximate mem.	*/
876   	/* explicitly deallocated by finalizers.		*/
877   ptr_t _scratch_end_ptr;
878   ptr_t _scratch_last_end_ptr;
879 	/* Used by headers.c, and can easily appear to point to	*/
880 	/* heap.						*/
881   GC_mark_proc _mark_procs[MAX_MARK_PROCS];
882   	/* Table of user-defined mark procedures.  There is	*/
883 	/* a small number of these, which can be referenced	*/
884 	/* by DS_PROC mark descriptors.  See gc_mark.h.		*/
885 
886 # ifndef SEPARATE_GLOBALS
887     ptr_t _objfreelist[MAXOBJSZ+1];
888 			  /* free list for objects */
889     ptr_t _aobjfreelist[MAXOBJSZ+1];
890 			  /* free list for atomic objs 	*/
891 # endif
892 
893   ptr_t _uobjfreelist[MAXOBJSZ+1];
894 			  /* uncollectable but traced objs 	*/
895 			  /* objects on this and auobjfreelist  */
896 			  /* are always marked, except during   */
897 			  /* garbage collections.		*/
898 # ifdef ATOMIC_UNCOLLECTABLE
899     ptr_t _auobjfreelist[MAXOBJSZ+1];
900 # endif
901 			  /* uncollectable but traced objs 	*/
902 
903 # ifdef GATHERSTATS
904     word _composite_in_use;
905    		/* Number of words in accessible composite	*/
906 		/* objects.					*/
907     word _atomic_in_use;
908    		/* Number of words in accessible atomic		*/
909 		/* objects.					*/
910 # endif
911 # ifdef USE_MUNMAP
912     word _unmapped_bytes;
913 # endif
914 # ifdef MERGE_SIZES
915     unsigned _size_map[WORDS_TO_BYTES(MAXOBJSZ+1)];
916     	/* Number of words to allocate for a given allocation request in */
917     	/* bytes.							 */
918 # endif
919 
920 # ifdef STUBBORN_ALLOC
921     ptr_t _sobjfreelist[MAXOBJSZ+1];
922 # endif
923   			  /* free list for immutable objects	*/
924   map_entry_type * _obj_map[MAXOBJSZ+1];
925                        /* If not NIL, then a pointer to a map of valid  */
926     		       /* object addresses. _obj_map[sz][i] is j if the	*/
927     		       /* address block_start+i is a valid pointer      */
928     		       /* to an object at block_start +			*/
929  		       /* WORDS_TO_BYTES(BYTES_TO_WORDS(i) - j)		*/
930   		       /* I.e. j is a word displacement from the	*/
931   		       /* object beginning.				*/
932   		       /* The entry is OBJ_INVALID if the corresponding	*/
933   		       /* address is not a valid pointer.  It is 	*/
934   		       /* OFFSET_TOO_BIG if the value j would be too 	*/
935   		       /* large to fit in the entry.  (Note that the	*/
936   		       /* size of these entries matters, both for 	*/
937   		       /* space consumption and for cache utilization.)	*/
938 #   define OFFSET_TOO_BIG 0xfe
939 #   define OBJ_INVALID 0xff
940 #   define MAP_ENTRY(map, bytes) (map)[bytes]
941 #   define MAP_ENTRIES HBLKSIZE
942 #   define MAP_SIZE MAP_ENTRIES
943 #   define CPP_MAX_OFFSET (OFFSET_TOO_BIG - 1)
944 #   define MAX_OFFSET ((word)CPP_MAX_OFFSET)
945 #   define INIT_MAP(map) memset((map), OBJ_INVALID, MAP_SIZE)
946     /* The following are used only if GC_all_interior_ptrs != 0 */
947 # 	define VALID_OFFSET_SZ \
948 	  (CPP_MAX_OFFSET > WORDS_TO_BYTES(CPP_MAXOBJSZ)? \
949 	   CPP_MAX_OFFSET+1 \
950 	   : WORDS_TO_BYTES(CPP_MAXOBJSZ)+1)
951   	char _valid_offsets[VALID_OFFSET_SZ];
952 				/* GC_valid_offsets[i] == TRUE ==> i 	*/
953 				/* is registered as a displacement.	*/
954   	char _modws_valid_offsets[sizeof(word)];
955 				/* GC_valid_offsets[i] ==>		  */
956 				/* GC_modws_valid_offsets[i%sizeof(word)] */
957 #   define OFFSET_VALID(displ) \
958 	  (GC_all_interior_pointers || GC_valid_offsets[displ])
959 # ifdef STUBBORN_ALLOC
960     page_hash_table _changed_pages;
961         /* Stubborn object pages that were changes since last call to	*/
962 	/* GC_read_changed.						*/
963     page_hash_table _prev_changed_pages;
964         /* Stubborn object pages that were changes before last call to	*/
965 	/* GC_read_changed.						*/
966 # endif
967 # if defined(PROC_VDB) || defined(MPROTECT_VDB)
968     page_hash_table _grungy_pages; /* Pages that were dirty at last 	   */
969 				     /* GC_read_dirty.			   */
970 # endif
971 # ifdef MPROTECT_VDB
972     VOLATILE page_hash_table _dirty_pages;
973 			/* Pages dirtied since last GC_read_dirty. */
974 # endif
975 # ifdef PROC_VDB
976     page_hash_table _written_pages;	/* Pages ever dirtied	*/
977 # endif
978 # ifdef LARGE_CONFIG
979 #   if CPP_WORDSZ > 32
980 #     define MAX_HEAP_SECTS 4096 	/* overflows at roughly 64 GB	   */
981 #   else
982 #     define MAX_HEAP_SECTS 768		/* Separately added heap sections. */
983 #   endif
984 # else
985 #   ifdef SMALL_CONFIG
986 #     define MAX_HEAP_SECTS 128		/* Roughly 256MB (128*2048*1K)	*/
987 #   else
988 #     define MAX_HEAP_SECTS (384+128)		/* Roughly 4GB			*/
989 #   endif
990 # endif
991   struct HeapSect {
992       ptr_t hs_start; word hs_bytes;
993   } _heap_sects[MAX_HEAP_SECTS];
994 # if defined(MSWIN32) || defined(MSWINCE)
995     ptr_t _heap_bases[MAX_HEAP_SECTS];
996     		/* Start address of memory regions obtained from kernel. */
997 # endif
998 # ifdef MSWINCE
999     word _heap_lengths[MAX_HEAP_SECTS];
1000     		/* Commited lengths of memory regions obtained from kernel. */
1001 # endif
1002   struct roots _static_roots[MAX_ROOT_SETS];
1003 # if !defined(MSWIN32) && !defined(MSWINCE)
1004     struct roots * _root_index[RT_SIZE];
1005 # endif
1006   struct exclusion _excl_table[MAX_EXCLUSIONS];
1007   /* Block header index; see gc_headers.h */
1008   bottom_index * _all_nils;
1009   bottom_index * _top_index [TOP_SZ];
1010 #ifdef SAVE_CALL_CHAIN
1011   struct callinfo _last_stack[NFRAMES];	/* Stack at last garbage collection.*/
1012   					/* Useful for debugging	mysterious  */
1013   					/* object disappearances.	    */
1014   					/* In the multithreaded case, we    */
1015   					/* currently only save the calling  */
1016   					/* stack.			    */
1017 #endif
1018 };
1019 
1020 GC_API GC_FAR struct _GC_arrays GC_arrays;
1021 
1022 # ifndef SEPARATE_GLOBALS
1023 #   define GC_objfreelist GC_arrays._objfreelist
1024 #   define GC_aobjfreelist GC_arrays._aobjfreelist
1025 #   define GC_words_allocd GC_arrays._words_allocd
1026 # endif
1027 # define GC_uobjfreelist GC_arrays._uobjfreelist
1028 # ifdef ATOMIC_UNCOLLECTABLE
1029 #   define GC_auobjfreelist GC_arrays._auobjfreelist
1030 # endif
1031 # define GC_sobjfreelist GC_arrays._sobjfreelist
1032 # define GC_valid_offsets GC_arrays._valid_offsets
1033 # define GC_modws_valid_offsets GC_arrays._modws_valid_offsets
1034 # ifdef STUBBORN_ALLOC
1035 #    define GC_changed_pages GC_arrays._changed_pages
1036 #    define GC_prev_changed_pages GC_arrays._prev_changed_pages
1037 # endif
1038 # define GC_obj_map GC_arrays._obj_map
1039 # define GC_last_heap_addr GC_arrays._last_heap_addr
1040 # define GC_prev_heap_addr GC_arrays._prev_heap_addr
1041 # define GC_words_wasted GC_arrays._words_wasted
1042 # define GC_large_free_bytes GC_arrays._large_free_bytes
1043 # define GC_large_allocd_bytes GC_arrays._large_allocd_bytes
1044 # define GC_max_large_allocd_bytes GC_arrays._max_large_allocd_bytes
1045 # define GC_words_finalized GC_arrays._words_finalized
1046 # define GC_non_gc_bytes_at_gc GC_arrays._non_gc_bytes_at_gc
1047 # define GC_mem_freed GC_arrays._mem_freed
1048 # define GC_finalizer_mem_freed GC_arrays._finalizer_mem_freed
1049 # define GC_scratch_end_ptr GC_arrays._scratch_end_ptr
1050 # define GC_scratch_last_end_ptr GC_arrays._scratch_last_end_ptr
1051 # define GC_mark_procs GC_arrays._mark_procs
1052 # define GC_heapsize GC_arrays._heapsize
1053 # define GC_max_heapsize GC_arrays._max_heapsize
1054 # define GC_requested_heapsize GC_arrays._requested_heapsize
1055 # define GC_words_allocd_before_gc GC_arrays._words_allocd_before_gc
1056 # define GC_heap_sects GC_arrays._heap_sects
1057 # define GC_last_stack GC_arrays._last_stack
1058 # ifdef USE_MUNMAP
1059 #   define GC_unmapped_bytes GC_arrays._unmapped_bytes
1060 # endif
1061 # if defined(MSWIN32) || defined(MSWINCE)
1062 #   define GC_heap_bases GC_arrays._heap_bases
1063 # endif
1064 # ifdef MSWINCE
1065 #   define GC_heap_lengths GC_arrays._heap_lengths
1066 # endif
1067 # define GC_static_roots GC_arrays._static_roots
1068 # define GC_root_index GC_arrays._root_index
1069 # define GC_excl_table GC_arrays._excl_table
1070 # define GC_all_nils GC_arrays._all_nils
1071 # define GC_top_index GC_arrays._top_index
1072 # if defined(PROC_VDB) || defined(MPROTECT_VDB)
1073 #   define GC_grungy_pages GC_arrays._grungy_pages
1074 # endif
1075 # ifdef MPROTECT_VDB
1076 #   define GC_dirty_pages GC_arrays._dirty_pages
1077 # endif
1078 # ifdef PROC_VDB
1079 #   define GC_written_pages GC_arrays._written_pages
1080 # endif
1081 # ifdef GATHERSTATS
1082 #   define GC_composite_in_use GC_arrays._composite_in_use
1083 #   define GC_atomic_in_use GC_arrays._atomic_in_use
1084 # endif
1085 # ifdef MERGE_SIZES
1086 #   define GC_size_map GC_arrays._size_map
1087 # endif
1088 
1089 # define beginGC_arrays ((ptr_t)(&GC_arrays))
1090 # define endGC_arrays (((ptr_t)(&GC_arrays)) + (sizeof GC_arrays))
1091 
1092 #define USED_HEAP_SIZE (GC_heapsize - GC_large_free_bytes)
1093 
1094 /* Object kinds: */
1095 # define MAXOBJKINDS 16
1096 
1097 extern struct obj_kind {
1098    ptr_t *ok_freelist;	/* Array of free listheaders for this kind of object */
1099    			/* Point either to GC_arrays or to storage allocated */
1100    			/* with GC_scratch_alloc.			     */
1101    struct hblk **ok_reclaim_list;
1102    			/* List headers for lists of blocks waiting to be */
1103    			/* swept.					  */
1104    word ok_descriptor;  /* Descriptor template for objects in this	*/
1105    			/* block.					*/
1106    GC_bool ok_relocate_descr;
1107    			/* Add object size in bytes to descriptor 	*/
1108    			/* template to obtain descriptor.  Otherwise	*/
1109    			/* template is used as is.			*/
1110    GC_bool ok_init;   /* Clear objects before putting them on the free list. */
1111 } GC_obj_kinds[MAXOBJKINDS];
1112 
1113 # define beginGC_obj_kinds ((ptr_t)(&GC_obj_kinds))
1114 # define endGC_obj_kinds (beginGC_obj_kinds + (sizeof GC_obj_kinds))
1115 
1116 /* Variables that used to be in GC_arrays, but need to be accessed by 	*/
1117 /* inline allocation code.  If they were in GC_arrays, the inlined 	*/
1118 /* allocation code would include GC_arrays offsets (as it did), which	*/
1119 /* introduce maintenance problems.					*/
1120 
1121 #ifdef SEPARATE_GLOBALS
1122   word GC_words_allocd;
1123   	/* Number of words allocated during this collection cycle */
1124   ptr_t GC_objfreelist[MAXOBJSZ+1];
1125 			  /* free list for NORMAL objects */
1126 # define beginGC_objfreelist ((ptr_t)(&GC_objfreelist))
1127 # define endGC_objfreelist (beginGC_objfreelist + sizeof(GC_objfreelist))
1128 
1129   ptr_t GC_aobjfreelist[MAXOBJSZ+1];
1130 			  /* free list for atomic (PTRFREE) objs 	*/
1131 # define beginGC_aobjfreelist ((ptr_t)(&GC_aobjfreelist))
1132 # define endGC_aobjfreelist (beginGC_aobjfreelist + sizeof(GC_aobjfreelist))
1133 #endif
1134 
1135 /* Predefined kinds: */
1136 # define PTRFREE 0
1137 # define NORMAL  1
1138 # define UNCOLLECTABLE 2
1139 # ifdef ATOMIC_UNCOLLECTABLE
1140 #   define AUNCOLLECTABLE 3
1141 #   define STUBBORN 4
1142 #   define IS_UNCOLLECTABLE(k) (((k) & ~1) == UNCOLLECTABLE)
1143 # else
1144 #   define STUBBORN 3
1145 #   define IS_UNCOLLECTABLE(k) ((k) == UNCOLLECTABLE)
1146 # endif
1147 
1148 extern int GC_n_kinds;
1149 
1150 GC_API word GC_fo_entries;
1151 
1152 extern word GC_n_heap_sects;	/* Number of separately added heap	*/
1153 				/* sections.				*/
1154 
1155 extern word GC_page_size;
1156 
1157 # if defined(MSWIN32) || defined(MSWINCE)
1158   struct _SYSTEM_INFO;
1159   extern struct _SYSTEM_INFO GC_sysinfo;
1160   extern word GC_n_heap_bases;	/* See GC_heap_bases.	*/
1161 # endif
1162 
1163 extern word GC_total_stack_black_listed;
1164 			/* Number of bytes on stack blacklist. 	*/
1165 
1166 extern word GC_black_list_spacing;
1167 			/* Average number of bytes between blacklisted	*/
1168 			/* blocks. Approximate.				*/
1169 			/* Counts only blocks that are 			*/
1170 			/* "stack-blacklisted", i.e. that are 		*/
1171 			/* problematic in the interior of an object.	*/
1172 
1173 extern map_entry_type * GC_invalid_map;
1174 			/* Pointer to the nowhere valid hblk map */
1175 			/* Blocks pointing to this map are free. */
1176 
1177 extern struct hblk * GC_hblkfreelist[];
1178 				/* List of completely empty heap blocks	*/
1179 				/* Linked through hb_next field of 	*/
1180 				/* header structure associated with	*/
1181 				/* block.				*/
1182 
1183 extern GC_bool GC_objects_are_marked;	/* There are marked objects in  */
1184 					/* the heap.			*/
1185 
1186 #ifndef SMALL_CONFIG
1187   extern GC_bool GC_incremental;
1188 			/* Using incremental/generational collection. */
1189 # define TRUE_INCREMENTAL \
1190 	(GC_incremental && GC_time_limit != GC_TIME_UNLIMITED)
1191 	/* True incremental, not just generational, mode */
1192 #else
1193 # define GC_incremental FALSE
1194 			/* Hopefully allow optimizer to remove some code. */
1195 # define TRUE_INCREMENTAL FALSE
1196 #endif
1197 
1198 extern GC_bool GC_dirty_maintained;
1199 				/* Dirty bits are being maintained, 	*/
1200 				/* either for incremental collection,	*/
1201 				/* or to limit the root set.		*/
1202 
1203 extern word GC_root_size;	/* Total size of registered root sections */
1204 
1205 extern GC_bool GC_debugging_started;	/* GC_debug_malloc has been called. */
1206 
1207 extern long GC_large_alloc_warn_interval;
1208 	/* Interval between unsuppressed warnings.	*/
1209 
1210 extern long GC_large_alloc_warn_suppressed;
1211 	/* Number of warnings suppressed so far.	*/
1212 
1213 #ifdef THREADS
1214   extern GC_bool GC_world_stopped;
1215 #endif
1216 
1217 extern void (*GC_notify_event) GC_PROTO((GC_EventType));
1218 
1219 /* Operations */
1220 # ifndef abs
1221 #   define abs(x)  ((x) < 0? (-(x)) : (x))
1222 # endif
1223 
1224 
1225 /*  Marks are in a reserved area in                          */
1226 /*  each heap block.  Each word has one mark bit associated  */
1227 /*  with it. Only those corresponding to the beginning of an */
1228 /*  object are used.                                         */
1229 
1230 /* Set mark bit correctly, even if mark bits may be concurrently 	*/
1231 /* accessed.								*/
1232 #ifdef PARALLEL_MARK
1233 # define OR_WORD(addr, bits) \
1234 	{ word old; \
1235 	  do { \
1236 	    old = *((volatile word *)addr); \
1237 	  } while (!GC_compare_and_exchange((addr), old, old | (bits))); \
1238 	}
1239 # define OR_WORD_EXIT_IF_SET(addr, bits, exit_label) \
1240 	{ word old; \
1241 	  word my_bits = (bits); \
1242 	  do { \
1243 	    old = *((volatile word *)addr); \
1244 	    if (old & my_bits) goto exit_label; \
1245 	  } while (!GC_compare_and_exchange((addr), old, old | my_bits)); \
1246 	}
1247 #else
1248 # define OR_WORD(addr, bits) *(addr) |= (bits)
1249 # define OR_WORD_EXIT_IF_SET(addr, bits, exit_label) \
1250 	{ \
1251 	  word old = *(addr); \
1252 	  word my_bits = (bits); \
1253 	  if (old & my_bits) goto exit_label; \
1254 	  *(addr) = (old | my_bits); \
1255 	}
1256 #endif
1257 
1258 /* Mark bit operations */
1259 
1260 /*
1261  * Retrieve, set, clear the mark bit corresponding
1262  * to the nth word in a given heap block.
1263  *
1264  * (Recall that bit n corresponds to object beginning at word n
1265  * relative to the beginning of the block, including unused words)
1266  */
1267 
1268 #ifdef USE_MARK_BYTES
1269 # define mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[(n) >> 1])
1270 # define set_mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[(n)>>1]) = 1
1271 # define clear_mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[(n)>>1]) = 0
1272 #else /* !USE_MARK_BYTES */
1273 # define mark_bit_from_hdr(hhdr,n) (((hhdr)->hb_marks[divWORDSZ(n)] \
1274 			    >> (modWORDSZ(n))) & (word)1)
1275 # define set_mark_bit_from_hdr(hhdr,n) \
1276 			    OR_WORD((hhdr)->hb_marks+divWORDSZ(n), \
1277 				    (word)1 << modWORDSZ(n))
1278 # define clear_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1279 				&= ~((word)1 << modWORDSZ(n))
1280 #endif /* !USE_MARK_BYTES */
1281 
1282 /* Important internal collector routines */
1283 
1284 ptr_t GC_approx_sp GC_PROTO((void));
1285 
1286 GC_bool GC_should_collect GC_PROTO((void));
1287 
1288 void GC_apply_to_all_blocks GC_PROTO(( \
1289     void (*fn) GC_PROTO((struct hblk *h, word client_data)), \
1290     word client_data));
1291   			/* Invoke fn(hbp, client_data) for each 	*/
1292   			/* allocated heap block.			*/
1293 struct hblk * GC_next_used_block GC_PROTO((struct hblk * h));
1294   			/* Return first in-use block >= h	*/
1295 struct hblk * GC_prev_block GC_PROTO((struct hblk * h));
1296   			/* Return last block <= h.  Returned block	*/
1297   			/* is managed by GC, but may or may not be in	*/
1298 			/* use.						*/
1299 void GC_mark_init GC_PROTO((void));
1300 void GC_clear_marks GC_PROTO((void));	/* Clear mark bits for all heap objects. */
1301 void GC_invalidate_mark_state GC_PROTO((void));
1302 					/* Tell the marker that	marked 	   */
1303   					/* objects may point to	unmarked   */
1304   					/* ones, and roots may point to	   */
1305   					/* unmarked objects.		   */
1306   					/* Reset mark stack.		   */
1307 GC_bool GC_mark_stack_empty GC_PROTO((void));
1308 GC_bool GC_mark_some GC_PROTO((ptr_t cold_gc_frame));
1309   			/* Perform about one pages worth of marking	*/
1310   			/* work of whatever kind is needed.  Returns	*/
1311   			/* quickly if no collection is in progress.	*/
1312   			/* Return TRUE if mark phase finished.		*/
1313 void GC_initiate_gc GC_PROTO((void));
1314 				/* initiate collection.			*/
1315   				/* If the mark state is invalid, this	*/
1316   				/* becomes full colleection.  Otherwise */
1317   				/* it's partial.			*/
1318 void GC_push_all GC_PROTO((ptr_t bottom, ptr_t top));
1319 				/* Push everything in a range 		*/
1320   				/* onto mark stack.			*/
1321 void GC_push_selected GC_PROTO(( \
1322     ptr_t bottom, \
1323     ptr_t top, \
1324     int (*dirty_fn) GC_PROTO((struct hblk *h)), \
1325     void (*push_fn) GC_PROTO((ptr_t bottom, ptr_t top)) ));
1326 				  /* Push all pages h in [b,t) s.t. 	*/
1327 				  /* select_fn(h) != 0 onto mark stack. */
1328 #ifndef SMALL_CONFIG
1329   void GC_push_conditional GC_PROTO((ptr_t b, ptr_t t, GC_bool all));
1330 #else
1331 # define GC_push_conditional(b, t, all) GC_push_all(b, t)
1332 #endif
1333                                 /* Do either of the above, depending	*/
1334 				/* on the third arg.			*/
1335 void GC_push_all_stack GC_PROTO((ptr_t b, ptr_t t));
1336 				    /* As above, but consider		*/
1337 				    /*  interior pointers as valid  	*/
1338 void GC_push_all_eager GC_PROTO((ptr_t b, ptr_t t));
1339 				    /* Same as GC_push_all_stack, but   */
1340 				    /* ensures that stack is scanned	*/
1341 				    /* immediately, not just scheduled  */
1342 				    /* for scanning.			*/
1343 #ifndef THREADS
1344   void GC_push_all_stack_partially_eager GC_PROTO(( \
1345       ptr_t bottom, ptr_t top, ptr_t cold_gc_frame ));
1346 			/* Similar to GC_push_all_eager, but only the	*/
1347 			/* part hotter than cold_gc_frame is scanned	*/
1348 			/* immediately.  Needed to ensure that callee-	*/
1349 			/* save registers are not missed.		*/
1350 #else
1351   /* In the threads case, we push part of the current thread stack	*/
1352   /* with GC_push_all_eager when we push the registers.  This gets the  */
1353   /* callee-save registers that may disappear.  The remainder of the	*/
1354   /* stacks are scheduled for scanning in *GC_push_other_roots, which	*/
1355   /* is thread-package-specific.					*/
1356 #endif
1357 void GC_push_current_stack GC_PROTO((ptr_t cold_gc_frame));
1358   			/* Push enough of the current stack eagerly to	*/
1359   			/* ensure that callee-save registers saved in	*/
1360   			/* GC frames are scanned.			*/
1361   			/* In the non-threads case, schedule entire	*/
1362   			/* stack for scanning.				*/
1363 void GC_push_roots GC_PROTO((GC_bool all, ptr_t cold_gc_frame));
1364   			/* Push all or dirty roots.	*/
1365 extern void (*GC_push_other_roots) GC_PROTO((void));
1366   			/* Push system or application specific roots	*/
1367   			/* onto the mark stack.  In some environments	*/
1368   			/* (e.g. threads environments) this is		*/
1369   			/* predfined to be non-zero.  A client supplied */
1370   			/* replacement should also call the original	*/
1371   			/* function.					*/
1372 extern void GC_push_gc_structures GC_PROTO((void));
1373 			/* Push GC internal roots.  These are normally	*/
1374 			/* included in the static data segment, and 	*/
1375 			/* Thus implicitly pushed.  But we must do this	*/
1376 			/* explicitly if normal root processing is 	*/
1377 			/* disabled.  Calls the following:		*/
1378 	extern void GC_push_finalizer_structures GC_PROTO((void));
1379 	extern void GC_push_stubborn_structures GC_PROTO((void));
1380 #	ifdef THREADS
1381 	  extern void GC_push_thread_structures GC_PROTO((void));
1382 #	endif
1383 extern void (*GC_start_call_back) GC_PROTO((void));
1384   			/* Called at start of full collections.		*/
1385   			/* Not called if 0.  Called with allocation 	*/
1386   			/* lock held.					*/
1387   			/* 0 by default.				*/
1388 # if defined(USE_GENERIC_PUSH_REGS)
1389   void GC_generic_push_regs GC_PROTO((ptr_t cold_gc_frame));
1390 # else
1391   void GC_push_regs GC_PROTO((void));
1392 # endif
1393 # if defined(SPARC) || defined(IA64)
1394   /* Cause all stacked registers to be saved in memory.  Return a	*/
1395   /* pointer to the top of the corresponding memory stack.		*/
1396   word GC_save_regs_in_stack GC_PROTO((void));
1397 # endif
1398 			/* Push register contents onto mark stack.	*/
1399   			/* If NURSERY is defined, the default push	*/
1400   			/* action can be overridden with GC_push_proc	*/
1401 
1402 # ifdef NURSERY
1403     extern void (*GC_push_proc)(ptr_t);
1404 # endif
1405 # if defined(MSWIN32) || defined(MSWINCE)
1406   void __cdecl GC_push_one GC_PROTO((word p));
1407 # else
1408   void GC_push_one GC_PROTO((word p));
1409 			      /* If p points to an object, mark it    */
1410                               /* and push contents on the mark stack  */
1411   			      /* Pointer recognition test always      */
1412   			      /* accepts interior pointers, i.e. this */
1413   			      /* is appropriate for pointers found on */
1414   			      /* stack.				      */
1415 # endif
1416 # if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
1417   void GC_mark_and_push_stack GC_PROTO((word p, ptr_t source));
1418 				/* Ditto, omits plausibility test	*/
1419 # else
1420   void GC_mark_and_push_stack GC_PROTO((word p));
1421 # endif
1422 void GC_push_marked GC_PROTO((struct hblk * h, hdr * hhdr));
1423 		/* Push contents of all marked objects in h onto	*/
1424 		/* mark stack.						*/
1425 #ifdef SMALL_CONFIG
1426 # define GC_push_next_marked_dirty(h) GC_push_next_marked(h)
1427 #else
1428   struct hblk * GC_push_next_marked_dirty GC_PROTO((struct hblk * h));
1429 		/* Invoke GC_push_marked on next dirty block above h.	*/
1430 		/* Return a pointer just past the end of this block.	*/
1431 #endif /* !SMALL_CONFIG */
1432 struct hblk * GC_push_next_marked GC_PROTO((struct hblk * h));
1433   		/* Ditto, but also mark from clean pages.	*/
1434 struct hblk * GC_push_next_marked_uncollectable GC_PROTO((struct hblk * h));
1435   		/* Ditto, but mark only from uncollectable pages.	*/
1436 GC_bool GC_stopped_mark GC_PROTO((GC_stop_func stop_func));
1437  			/* Stop world and mark from all roots	*/
1438   			/* and rescuers.			*/
1439 void GC_clear_hdr_marks GC_PROTO((hdr * hhdr));
1440 				    /* Clear the mark bits in a header */
1441 void GC_set_hdr_marks GC_PROTO((hdr * hhdr));
1442  				    /* Set the mark bits in a header */
1443 void GC_set_fl_marks GC_PROTO((ptr_t p));
1444 				    /* Set all mark bits associated with */
1445 				    /* a free list.			 */
1446 void GC_add_roots_inner GC_PROTO((char * b, char * e, GC_bool tmp));
1447 void GC_remove_roots_inner GC_PROTO((char * b, char * e));
1448 GC_bool GC_is_static_root GC_PROTO((ptr_t p));
1449   		/* Is the address p in one of the registered static	*/
1450   		/* root sections?					*/
1451 # if defined(MSWIN32) || defined(_WIN32_WCE_EMULATION)
1452 GC_bool GC_is_tmp_root GC_PROTO((ptr_t p));
1453 		/* Is the address p in one of the temporary static	*/
1454 		/* root sections?					*/
1455 # endif
1456 void GC_register_dynamic_libraries GC_PROTO((void));
1457   		/* Add dynamic library data sections to the root set. */
1458 
1459 void GC_cond_register_dynamic_libraries GC_PROTO((void));
1460 		/* Remove and reregister dynamic libraries if we're     */
1461 		/* configured to do that at each GC.                    */
1462 
1463 GC_bool GC_register_main_static_data GC_PROTO((void));
1464 		/* We need to register the main data segment.  Returns	*/
1465 		/* TRUE unless this is done implicitly as part of	*/
1466 		/* dynamic library registration.			*/
1467 
1468 /* Machine dependent startup routines */
1469 ptr_t GC_get_stack_base GC_PROTO((void));	/* Cold end of stack */
1470 #ifdef IA64
1471   ptr_t GC_get_register_stack_base GC_PROTO((void));
1472   					/* Cold end of register stack.	*/
1473 #endif
1474 void GC_register_data_segments GC_PROTO((void));
1475 
1476 /* Black listing: */
1477 void GC_bl_init GC_PROTO((void));
1478 # ifdef PRINT_BLACK_LIST
1479       void GC_add_to_black_list_normal GC_PROTO((word p, ptr_t source));
1480 			/* Register bits as a possible future false	*/
1481 			/* reference from the heap or static data	*/
1482 #     define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1483       		if (GC_all_interior_pointers) { \
1484 		  GC_add_to_black_list_stack(bits, (ptr_t)(source)); \
1485 		} else { \
1486   		  GC_add_to_black_list_normal(bits, (ptr_t)(source)); \
1487 		}
1488 # else
1489       void GC_add_to_black_list_normal GC_PROTO((word p));
1490 #     define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1491       		if (GC_all_interior_pointers) { \
1492 		  GC_add_to_black_list_stack(bits); \
1493 		} else { \
1494   		  GC_add_to_black_list_normal(bits); \
1495 		}
1496 # endif
1497 
1498 # ifdef PRINT_BLACK_LIST
1499     void GC_add_to_black_list_stack GC_PROTO((word p, ptr_t source));
1500 # else
1501     void GC_add_to_black_list_stack GC_PROTO((word p));
1502 # endif
1503 struct hblk * GC_is_black_listed GC_PROTO((struct hblk * h, word len));
1504   			/* If there are likely to be false references	*/
1505   			/* to a block starting at h of the indicated    */
1506   			/* length, then return the next plausible	*/
1507   			/* starting location for h that might avoid	*/
1508   			/* these false references.			*/
1509 void GC_promote_black_lists GC_PROTO((void));
1510   			/* Declare an end to a black listing phase.	*/
1511 void GC_unpromote_black_lists GC_PROTO((void));
1512   			/* Approximately undo the effect of the above.	*/
1513   			/* This actually loses some information, but	*/
1514   			/* only in a reasonably safe way.		*/
1515 word GC_number_stack_black_listed GC_PROTO(( \
1516 	struct hblk *start, struct hblk *endp1));
1517   			/* Return the number of (stack) blacklisted	*/
1518   			/* blocks in the range for statistical		*/
1519   			/* purposes.					*/
1520 
1521 ptr_t GC_scratch_alloc GC_PROTO((word bytes));
1522   				/* GC internal memory allocation for	*/
1523   				/* small objects.  Deallocation is not  */
1524   				/* possible.				*/
1525 
1526 /* Heap block layout maps: */
1527 void GC_invalidate_map GC_PROTO((hdr * hhdr));
1528   				/* Remove the object map associated	*/
1529   				/* with the block.  This identifies	*/
1530   				/* the block as invalid to the mark	*/
1531   				/* routines.				*/
1532 GC_bool GC_add_map_entry GC_PROTO((word sz));
1533   				/* Add a heap block map for objects of	*/
1534   				/* size sz to obj_map.			*/
1535   				/* Return FALSE on failure.		*/
1536 void GC_register_displacement_inner GC_PROTO((word offset));
1537   				/* Version of GC_register_displacement	*/
1538   				/* that assumes lock is already held	*/
1539   				/* and signals are already disabled.	*/
1540 
1541 /*  hblk allocation: */
1542 void GC_new_hblk GC_PROTO((word size_in_words, int kind));
1543   				/* Allocate a new heap block, and build */
1544   				/* a free list in it.			*/
1545 
1546 ptr_t GC_build_fl GC_PROTO((struct hblk *h, word sz,
1547 			   GC_bool clear,  ptr_t list));
1548 				/* Build a free list for objects of 	*/
1549 				/* size sz in block h.  Append list to	*/
1550 				/* end of the free lists.  Possibly	*/
1551 				/* clear objects on the list.  Normally	*/
1552 				/* called by GC_new_hblk, but also	*/
1553 				/* called explicitly without GC lock.	*/
1554 
1555 struct hblk * GC_allochblk GC_PROTO(( \
1556 	word size_in_words, int kind, unsigned flags));
1557 				/* Allocate a heap block, inform	*/
1558 				/* the marker that block is valid	*/
1559 				/* for objects of indicated size.	*/
1560 
1561 ptr_t GC_alloc_large GC_PROTO((word lw, int k, unsigned flags));
1562 			/* Allocate a large block of size lw words.	*/
1563 			/* The block is not cleared.			*/
1564 			/* Flags is 0 or IGNORE_OFF_PAGE.		*/
1565 			/* Calls GC_allchblk to do the actual 		*/
1566 			/* allocation, but also triggers GC and/or	*/
1567 			/* heap expansion as appropriate.		*/
1568 			/* Does not update GC_words_allocd, but does	*/
1569 			/* other accounting.				*/
1570 
1571 ptr_t GC_alloc_large_and_clear GC_PROTO((word lw, int k, unsigned flags));
1572 			/* As above, but clear block if appropriate	*/
1573 			/* for kind k.					*/
1574 
1575 void GC_freehblk GC_PROTO((struct hblk * p));
1576 				/* Deallocate a heap block and mark it  */
1577   				/* as invalid.				*/
1578 
1579 /*  Misc GC: */
1580 void GC_init_inner GC_PROTO((void));
1581 GC_bool GC_expand_hp_inner GC_PROTO((word n));
1582 void GC_start_reclaim GC_PROTO((int abort_if_found));
1583   				/* Restore unmarked objects to free	*/
1584   				/* lists, or (if abort_if_found is	*/
1585   				/* TRUE) report them.			*/
1586   				/* Sweeping of small object pages is	*/
1587   				/* largely deferred.			*/
1588 void GC_continue_reclaim GC_PROTO((word sz, int kind));
1589   				/* Sweep pages of the given size and	*/
1590   				/* kind, as long as possible, and	*/
1591   				/* as long as the corr. free list is    */
1592   				/* empty.				*/
1593 void GC_reclaim_or_delete_all GC_PROTO((void));
1594   				/* Arrange for all reclaim lists to be	*/
1595   				/* empty.  Judiciously choose between	*/
1596   				/* sweeping and discarding each page.	*/
1597 GC_bool GC_reclaim_all GC_PROTO((GC_stop_func stop_func, GC_bool ignore_old));
1598   				/* Reclaim all blocks.  Abort (in a	*/
1599   				/* consistent state) if f returns TRUE. */
1600 GC_bool GC_block_empty GC_PROTO((hdr * hhdr));
1601  				/* Block completely unmarked? 	*/
1602 GC_bool GC_never_stop_func GC_PROTO((void));
1603 				/* Returns FALSE.		*/
1604 GC_bool GC_try_to_collect_inner GC_PROTO((GC_stop_func f));
1605 
1606 				/* Collect; caller must have acquired	*/
1607 				/* lock and disabled signals.		*/
1608 				/* Collection is aborted if f returns	*/
1609 				/* TRUE.  Returns TRUE if it completes	*/
1610 				/* successfully.			*/
1611 # define GC_gcollect_inner() \
1612 	(void) GC_try_to_collect_inner(GC_never_stop_func)
1613 void GC_finish_collection GC_PROTO((void));
1614  				/* Finish collection.  Mark bits are	*/
1615   				/* consistent and lock is still held.	*/
1616 GC_bool GC_collect_or_expand GC_PROTO(( \
1617 	word needed_blocks, GC_bool ignore_off_page));
1618   				/* Collect or expand heap in an attempt */
1619   				/* make the indicated number of free	*/
1620   				/* blocks available.  Should be called	*/
1621   				/* until the blocks are available or	*/
1622   				/* until it fails by returning FALSE.	*/
1623 
1624 extern GC_bool GC_is_initialized;	/* GC_init() has been run.	*/
1625 
1626 #if defined(MSWIN32) || defined(MSWINCE)
1627   void GC_deinit GC_PROTO((void));
1628                                 /* Free any resources allocated by      */
1629                                 /* GC_init                              */
1630 #endif
1631 
1632 void GC_collect_a_little_inner GC_PROTO((int n));
1633   				/* Do n units worth of garbage 		*/
1634   				/* collection work, if appropriate.	*/
1635   				/* A unit is an amount appropriate for  */
1636   				/* HBLKSIZE bytes of allocation.	*/
1637 /* ptr_t GC_generic_malloc GC_PROTO((word lb, int k)); */
1638   				/* Allocate an object of the given	*/
1639   				/* kind.  By default, there are only	*/
1640   				/* a few kinds: composite(pointerfree), */
1641 				/* atomic, uncollectable, etc.		*/
1642 				/* We claim it's possible for clever	*/
1643 				/* client code that understands GC	*/
1644 				/* internals to add more, e.g. to	*/
1645 				/* communicate object layout info	*/
1646 				/* to the collector.			*/
1647 				/* The actual decl is in gc_mark.h.	*/
1648 ptr_t GC_generic_malloc_ignore_off_page GC_PROTO((size_t b, int k));
1649   				/* As above, but pointers past the 	*/
1650   				/* first page of the resulting object	*/
1651   				/* are ignored.				*/
1652 ptr_t GC_generic_malloc_inner GC_PROTO((word lb, int k));
1653   				/* Ditto, but I already hold lock, etc.	*/
1654 ptr_t GC_generic_malloc_words_small_inner GC_PROTO((word lw, int k));
1655 				/* Analogous to the above, but assumes	*/
1656 				/* a small object size, and bypasses	*/
1657 				/* MERGE_SIZES mechanism.		*/
1658 ptr_t GC_generic_malloc_words_small GC_PROTO((size_t lw, int k));
1659   				/* As above, but size in units of words */
1660   				/* Bypasses MERGE_SIZES.  Assumes	*/
1661   				/* words <= MAXOBJSZ.			*/
1662 ptr_t GC_generic_malloc_inner_ignore_off_page GC_PROTO((size_t lb, int k));
1663   				/* Allocate an object, where		*/
1664   				/* the client guarantees that there	*/
1665   				/* will always be a pointer to the 	*/
1666   				/* beginning of the object while the	*/
1667   				/* object is live.			*/
1668 ptr_t GC_allocobj GC_PROTO((word sz, int kind));
1669   				/* Make the indicated 			*/
1670   				/* free list nonempty, and return its	*/
1671   				/* head.				*/
1672 
1673 void GC_free_inner(GC_PTR p);
1674 
1675 void GC_init_headers GC_PROTO((void));
1676 struct hblkhdr * GC_install_header GC_PROTO((struct hblk *h));
1677   				/* Install a header for block h.	*/
1678   				/* Return 0 on failure, or the header	*/
1679   				/* otherwise.				*/
1680 GC_bool GC_install_counts GC_PROTO((struct hblk * h, word sz));
1681   				/* Set up forwarding counts for block	*/
1682   				/* h of size sz.			*/
1683   				/* Return FALSE on failure.		*/
1684 void GC_remove_header GC_PROTO((struct hblk * h));
1685   				/* Remove the header for block h.	*/
1686 void GC_remove_counts GC_PROTO((struct hblk * h, word sz));
1687   				/* Remove forwarding counts for h.	*/
1688 hdr * GC_find_header GC_PROTO((ptr_t h)); /* Debugging only.		*/
1689 
1690 void GC_finalize GC_PROTO((void));
1691  			/* Perform all indicated finalization actions	*/
1692   			/* on unmarked objects.				*/
1693   			/* Unreachable finalizable objects are enqueued	*/
1694   			/* for processing by GC_invoke_finalizers.	*/
1695   			/* Invoked with lock.				*/
1696 
1697 void GC_process_togglerefs (void);
1698 	/*Process the togglerefs before GC starts */
1699 
1700 void GC_notify_or_invoke_finalizers GC_PROTO((void));
1701 			/* If GC_finalize_on_demand is not set, invoke	*/
1702 			/* eligible finalizers. Otherwise:		*/
1703 			/* Call *GC_finalizer_notifier if there are	*/
1704 			/* finalizers to be run, and we haven't called	*/
1705 			/* this procedure yet this GC cycle.		*/
1706 
1707 GC_API GC_PTR GC_make_closure GC_PROTO((GC_finalization_proc fn, GC_PTR data));
1708 GC_API void GC_debug_invoke_finalizer GC_PROTO((GC_PTR obj, GC_PTR data));
1709 			/* Auxiliary fns to make finalization work	*/
1710 			/* correctly with displaced pointers introduced	*/
1711 			/* by the debugging allocators.			*/
1712 
1713 void GC_add_to_heap GC_PROTO((struct hblk *p, word bytes));
1714   			/* Add a HBLKSIZE aligned chunk to the heap.	*/
1715 
1716 void GC_print_obj GC_PROTO((ptr_t p));
1717   			/* P points to somewhere inside an object with	*/
1718   			/* debugging info.  Print a human readable	*/
1719   			/* description of the object to stderr.		*/
1720 extern void (*GC_check_heap) GC_PROTO((void));
1721   			/* Check that all objects in the heap with 	*/
1722   			/* debugging info are intact.  			*/
1723   			/* Add any that are not to GC_smashed list.	*/
1724 extern void (*GC_print_all_smashed) GC_PROTO((void));
1725 			/* Print GC_smashed if it's not empty.		*/
1726 			/* Clear GC_smashed list.			*/
1727 extern void GC_print_all_errors GC_PROTO((void));
1728 			/* Print smashed and leaked objects, if any.	*/
1729 			/* Clear the lists of such objects.		*/
1730 extern void (*GC_print_heap_obj) GC_PROTO((ptr_t p));
1731   			/* If possible print s followed by a more	*/
1732   			/* detailed description of the object 		*/
1733   			/* referred to by p.				*/
1734 #if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
1735   void GC_print_address_map GC_PROTO((void));
1736   			/* Print an address map of the process.		*/
1737 #endif
1738 
1739 extern GC_bool GC_have_errors;  /* We saw a smashed or leaked object.	*/
1740 				/* Call error printing routine 		*/
1741 				/* occasionally.			*/
1742 extern GC_bool GC_print_stats;	/* Produce at least some logging output	*/
1743 				/* Set from environment variable.	*/
1744 
1745 #ifndef NO_DEBUGGING
1746   extern GC_bool GC_dump_regularly;  /* Generate regular debugging dumps. */
1747 # define COND_DUMP if (GC_dump_regularly) GC_dump();
1748 #else
1749 # define COND_DUMP
1750 #endif
1751 
1752 #ifdef KEEP_BACK_PTRS
1753   extern long GC_backtraces;
1754   void GC_generate_random_backtrace_no_gc(void);
1755 #endif
1756 
1757 extern GC_bool GC_print_back_height;
1758 
1759 #ifdef MAKE_BACK_GRAPH
1760   void GC_print_back_graph_stats(void);
1761 #endif
1762 
1763 /* Macros used for collector internal allocation.	*/
1764 /* These assume the collector lock is held.		*/
1765 #ifdef DBG_HDRS_ALL
1766     extern GC_PTR GC_debug_generic_malloc_inner(size_t lb, int k);
1767     extern GC_PTR GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
1768 								int k);
1769 #   define GC_INTERNAL_MALLOC GC_debug_generic_malloc_inner
1770 #   define GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE \
1771 		 GC_debug_generic_malloc_inner_ignore_off_page
1772 #   ifdef THREADS
1773 #       define GC_INTERNAL_FREE GC_debug_free_inner
1774 #   else
1775 #       define GC_INTERNAL_FREE GC_debug_free
1776 #   endif
1777 #else
1778 #   define GC_INTERNAL_MALLOC GC_generic_malloc_inner
1779 #   define GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE \
1780 		 GC_generic_malloc_inner_ignore_off_page
1781 #   ifdef THREADS
1782 #       define GC_INTERNAL_FREE GC_free_inner
1783 #   else
1784 #       define GC_INTERNAL_FREE GC_free
1785 #   endif
1786 #endif
1787 
1788 /* Memory unmapping: */
1789 #ifdef USE_MUNMAP
1790   void GC_unmap_old(void);
1791   void GC_merge_unmapped(void);
1792   void GC_unmap(ptr_t start, word bytes);
1793   void GC_remap(ptr_t start, word bytes);
1794   void GC_unmap_gap(ptr_t start1, word bytes1, ptr_t start2, word bytes2);
1795 #endif
1796 
1797 /* Virtual dirty bit implementation:		*/
1798 /* Each implementation exports the following:	*/
1799 void GC_read_dirty GC_PROTO((void));
1800 			/* Retrieve dirty bits.	*/
1801 GC_bool GC_page_was_dirty GC_PROTO((struct hblk *h));
1802   			/* Read retrieved dirty bits.	*/
1803 GC_bool GC_page_was_ever_dirty GC_PROTO((struct hblk *h));
1804   			/* Could the page contain valid heap pointers?	*/
1805 void GC_is_fresh GC_PROTO((struct hblk *h, word n));
1806   			/* Assert the region currently contains no	*/
1807   			/* valid pointers.				*/
1808 void GC_remove_protection GC_PROTO((struct hblk *h, word nblocks,
1809 			   	    GC_bool pointerfree));
1810   			/* h is about to be writteni or allocated.  Ensure  */
1811 			/* that it's not write protected by the virtual	    */
1812 			/* dirty bit implementation.			    */
1813 
1814 void GC_dirty_init GC_PROTO((void));
1815 
1816 /* Slow/general mark bit manipulation: */
1817 GC_API GC_bool GC_is_marked GC_PROTO((ptr_t p));
1818 void GC_clear_mark_bit GC_PROTO((ptr_t p));
1819 void GC_set_mark_bit GC_PROTO((ptr_t p));
1820 
1821 /* Stubborn objects: */
1822 void GC_read_changed GC_PROTO((void));	/* Analogous to GC_read_dirty */
1823 GC_bool GC_page_was_changed GC_PROTO((struct hblk * h));
1824  				/* Analogous to GC_page_was_dirty */
1825 void GC_clean_changing_list GC_PROTO((void));
1826  				/* Collect obsolete changing list entries */
1827 void GC_stubborn_init GC_PROTO((void));
1828 
1829 /* Debugging print routines: */
1830 void GC_print_block_list GC_PROTO((void));
1831 void GC_print_hblkfreelist GC_PROTO((void));
1832 void GC_print_heap_sects GC_PROTO((void));
1833 void GC_print_static_roots GC_PROTO((void));
1834 void GC_print_finalization_stats GC_PROTO((void));
1835 void GC_dump GC_PROTO((void));
1836 
1837 #ifdef KEEP_BACK_PTRS
1838    void GC_store_back_pointer(ptr_t source, ptr_t dest);
1839    void GC_marked_for_finalization(ptr_t dest);
1840 #  define GC_STORE_BACK_PTR(source, dest) GC_store_back_pointer(source, dest)
1841 #  define GC_MARKED_FOR_FINALIZATION(dest) GC_marked_for_finalization(dest)
1842 #else
1843 #  define GC_STORE_BACK_PTR(source, dest)
1844 #  define GC_MARKED_FOR_FINALIZATION(dest)
1845 #endif
1846 
1847 /* Make arguments appear live to compiler */
1848 # ifdef __WATCOMC__
1849     void GC_noop(void*, ...);
1850 # else
1851 #   ifdef __DMC__
1852       GC_API void GC_noop(...);
1853 #   else
1854       GC_API void GC_noop(void*,...);
1855 #   endif
1856 # endif
1857 
1858 void GC_noop1 GC_PROTO((word));
1859 
1860 /* Logging and diagnostic output: 	*/
1861 GC_API void GC_printf GC_PROTO((GC_CONST char * format, long, long, long, long, long, long));
1862 			/* A version of printf that doesn't allocate,	*/
1863 			/* is restricted to long arguments, and		*/
1864 			/* (unfortunately) doesn't use varargs for	*/
1865 			/* portability.  Restricted to 6 args and	*/
1866 			/* 1K total output length.			*/
1867 			/* (We use sprintf.  Hopefully that doesn't	*/
1868 			/* allocate for long arguments.)  		*/
1869 # define GC_printf0(f) GC_printf(f, 0l, 0l, 0l, 0l, 0l, 0l)
1870 # define GC_printf1(f,a) GC_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1871 # define GC_printf2(f,a,b) GC_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1872 # define GC_printf3(f,a,b,c) GC_printf(f, (long)a, (long)b, (long)c, 0l, 0l, 0l)
1873 # define GC_printf4(f,a,b,c,d) GC_printf(f, (long)a, (long)b, (long)c, \
1874 					    (long)d, 0l, 0l)
1875 # define GC_printf5(f,a,b,c,d,e) GC_printf(f, (long)a, (long)b, (long)c, \
1876 					      (long)d, (long)e, 0l)
1877 # define GC_printf6(f,a,b,c,d,e,g) GC_printf(f, (long)a, (long)b, (long)c, \
1878 						(long)d, (long)e, (long)g)
1879 
1880 GC_API void GC_err_printf GC_PROTO((GC_CONST char * format, long, long, long, long, long, long));
1881 # define GC_err_printf0(f) GC_err_puts(f)
1882 # define GC_err_printf1(f,a) GC_err_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1883 # define GC_err_printf2(f,a,b) GC_err_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1884 # define GC_err_printf3(f,a,b,c) GC_err_printf(f, (long)a, (long)b, (long)c, \
1885 						  0l, 0l, 0l)
1886 # define GC_err_printf4(f,a,b,c,d) GC_err_printf(f, (long)a, (long)b, \
1887 						    (long)c, (long)d, 0l, 0l)
1888 # define GC_err_printf5(f,a,b,c,d,e) GC_err_printf(f, (long)a, (long)b, \
1889 						      (long)c, (long)d, \
1890 						      (long)e, 0l)
1891 # define GC_err_printf6(f,a,b,c,d,e,g) GC_err_printf(f, (long)a, (long)b, \
1892 							(long)c, (long)d, \
1893 							(long)e, (long)g)
1894 			/* Ditto, writes to stderr.			*/
1895 
1896 void GC_err_puts GC_PROTO((GC_CONST char *s));
1897 			/* Write s to stderr, don't buffer, don't add	*/
1898 			/* newlines, don't ...				*/
1899 
1900 #if defined(LINUX) && !defined(SMALL_CONFIG)
1901   void GC_err_write GC_PROTO((GC_CONST char *buf, size_t len));
1902  			/* Write buf to stderr, don't buffer, don't add	*/
1903   			/* newlines, don't ...				*/
1904 #endif
1905 
1906 
1907 # ifdef GC_ASSERTIONS
1908 #	define GC_ASSERT(expr) if(!(expr)) {\
1909 		GC_err_printf2("Assertion failure: %s:%ld\n", \
1910 				__FILE__, (unsigned long)__LINE__); \
1911 		ABORT("assertion failure"); }
1912 # else
1913 #	define GC_ASSERT(expr)
1914 # endif
1915 
1916 /* Check a compile time assertion at compile time.  The error	*/
1917 /* message for failure is a bit baroque, but ...		*/
1918 #if defined(mips) && !defined(__GNUC__)
1919 /* DOB: MIPSPro C gets an internal error taking the sizeof an array type.
1920    This code works correctly (ugliness is to avoid "unused var" warnings) */
1921 # define GC_STATIC_ASSERT(expr) do { if (0) { char j[(expr)? 1 : -1]; j[0]='\0'; j[0]=j[0]; } } while(0)
1922 #else
1923 # define GC_STATIC_ASSERT(expr) sizeof(char[(expr)? 1 : -1])
1924 #endif
1925 
1926 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
1927     /* We need additional synchronization facilities from the thread	*/
1928     /* support.  We believe these are less performance critical		*/
1929     /* than the main garbage collector lock; standard pthreads-based	*/
1930     /* implementations should be sufficient.				*/
1931 
1932     /* The mark lock and condition variable.  If the GC lock is also 	*/
1933     /* acquired, the GC lock must be acquired first.  The mark lock is	*/
1934     /* used to both protect some variables used by the parallel		*/
1935     /* marker, and to protect GC_fl_builder_count, below.		*/
1936     /* GC_notify_all_marker() is called when				*/
1937     /* the state of the parallel marker changes				*/
1938     /* in some significant way (see gc_mark.h for details).  The	*/
1939     /* latter set of events includes incrementing GC_mark_no.		*/
1940     /* GC_notify_all_builder() is called when GC_fl_builder_count	*/
1941     /* reaches 0.							*/
1942 
1943      extern void GC_acquire_mark_lock(void);
1944      extern void GC_release_mark_lock(void);
1945      extern void GC_notify_all_builder(void);
1946      /* extern void GC_wait_builder(); */
1947      extern void GC_wait_for_reclaim(void);
1948 
1949      extern word GC_fl_builder_count;	/* Protected by mark lock.	*/
1950 # endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
1951 # ifdef PARALLEL_MARK
1952      extern void GC_notify_all_marker(void);
1953      extern void GC_wait_marker(void);
1954      extern word GC_mark_no;		/* Protected by mark lock.	*/
1955 
1956      extern void GC_help_marker(word my_mark_no);
1957 		/* Try to help out parallel marker for mark cycle 	*/
1958 		/* my_mark_no.  Returns if the mark cycle finishes or	*/
1959 		/* was already done, or there was nothing to do for	*/
1960 		/* some other reason.					*/
1961 # endif /* PARALLEL_MARK */
1962 
1963 # if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS)
1964   /* We define the thread suspension signal here, so that we can refer	*/
1965   /* to it in the dirty bit implementation, if necessary.  Ideally we	*/
1966   /* would allocate a (real-time ?) signal using the standard mechanism.*/
1967   /* unfortunately, there is no standard mechanism.  (There is one 	*/
1968   /* in Linux glibc, but it's not exported.)  Thus we continue to use	*/
1969   /* the same hard-coded signals we've always used.			*/
1970 #  if !defined(SIG_SUSPEND)
1971 #   if defined(GC_LINUX_THREADS) || defined(GC_DGUX386_THREADS) || defined(GC_NETBSD_THREADS)
1972 #    if defined(SPARC) && !defined(SIGPWR)
1973        /* SPARC/Linux doesn't properly define SIGPWR in <signal.h>.
1974         * It is aliased to SIGLOST in asm/signal.h, though.		*/
1975 #      define SIG_SUSPEND SIGLOST
1976 #    elif defined(NACL)
1977 #	define SIG_SUSPEND 0
1978 #    else
1979        /* Linuxthreads itself uses SIGUSR1 and SIGUSR2.			*/
1980 #      define SIG_SUSPEND SIGPWR
1981 #    endif
1982 #   else  /* !GC_LINUX_THREADS */
1983 #     if defined(_SIGRTMIN)
1984 #       define SIG_SUSPEND _SIGRTMIN + 6
1985 #     else
1986 #       define SIG_SUSPEND SIGRTMIN + 6
1987 #     endif
1988 #   endif
1989 #  endif /* !SIG_SUSPEND */
1990 
1991 # endif
1992 
1993 # endif /* GC_PRIVATE_H */
1994