1999-05-22 Ben Elliston <bje@cygnus.com>
[official-gcc.git] / boehm-gc / gc_priv.h
blob888e46e1e607f73582f07ab29a990a9aa867fcc5
1 /*
2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
14 /* Boehm, February 16, 1996 2:30 pm PST */
17 # ifndef GC_PRIVATE_H
18 # define GC_PRIVATE_H
20 #if defined(mips) && defined(SYSTYPE_BSD) && defined(sony_news)
21 /* sony RISC NEWS, NEWSOS 4 */
22 # define BSD_TIME
23 /* typedef long ptrdiff_t; -- necessary on some really old systems */
24 #endif
26 #if defined(mips) && defined(SYSTYPE_BSD43)
27 /* MIPS RISCOS 4 */
28 # define BSD_TIME
29 #endif
31 #ifdef BSD_TIME
32 # include <sys/types.h>
33 # include <sys/time.h>
34 # include <sys/resource.h>
35 #endif /* BSD_TIME */
37 # ifndef GC_H
38 # include "gc.h"
39 # endif
41 typedef GC_word word;
42 typedef GC_signed_word signed_word;
44 # ifndef CONFIG_H
45 # include "config.h"
46 # endif
48 # ifndef HEADERS_H
49 # include "gc_hdrs.h"
50 # endif
52 typedef int GC_bool;
53 # define TRUE 1
54 # define FALSE 0
56 typedef char * ptr_t; /* A generic pointer to which we can add */
57 /* byte displacements. */
58 /* Preferably identical to caddr_t, if it */
59 /* exists. */
61 #if defined(__STDC__)
62 # include <stdlib.h>
63 # if !(defined( sony_news ) )
64 # include <stddef.h>
65 # endif
66 # define VOLATILE volatile
67 # define CONST const
68 #else
69 # ifdef MSWIN32
70 # include <stdlib.h>
71 # endif
72 # define VOLATILE
73 # define CONST
74 #endif
76 #ifdef AMIGA
77 # define GC_FAR __far
78 #else
79 # define GC_FAR
80 #endif
82 /*********************************/
83 /* */
84 /* Definitions for conservative */
85 /* collector */
86 /* */
87 /*********************************/
89 /*********************************/
90 /* */
91 /* Easily changeable parameters */
92 /* */
93 /*********************************/
95 #define STUBBORN_ALLOC /* Define stubborn allocation primitives */
96 #if defined(SRC_M3) || defined(SMALL_CONFIG)
97 # undef STUBBORN_ALLOC
98 #endif
101 /* #define ALL_INTERIOR_POINTERS */
102 /* Forces all pointers into the interior of an */
103 /* object to be considered valid. Also causes the */
104 /* sizes of all objects to be inflated by at least */
105 /* one byte. This should suffice to guarantee */
106 /* that in the presence of a compiler that does */
107 /* not perform garbage-collector-unsafe */
108 /* optimizations, all portable, strictly ANSI */
109 /* conforming C programs should be safely usable */
110 /* with malloc replaced by GC_malloc and free */
111 /* calls removed. There are several disadvantages: */
112 /* 1. There are probably no interesting, portable, */
113 /* strictly ANSI conforming C programs. */
114 /* 2. This option makes it hard for the collector */
115 /* to allocate space that is not ``pointed to'' */
116 /* by integers, etc. Under SunOS 4.X with a */
117 /* statically linked libc, we empiricaly */
118 /* observed that it would be difficult to */
119 /* allocate individual objects larger than 100K. */
120 /* Even if only smaller objects are allocated, */
121 /* more swap space is likely to be needed. */
122 /* Fortunately, much of this will never be */
123 /* touched. */
124 /* If you can easily avoid using this option, do. */
125 /* If not, try to keep individual objects small. */
127 #define PRINTSTATS /* Print garbage collection statistics */
128 /* For less verbose output, undefine in reclaim.c */
130 #define PRINTTIMES /* Print the amount of time consumed by each garbage */
131 /* collection. */
133 #define PRINTBLOCKS /* Print object sizes associated with heap blocks, */
134 /* whether the objects are atomic or composite, and */
135 /* whether or not the block was found to be empty */
136 /* during the reclaim phase. Typically generates */
137 /* about one screenful per garbage collection. */
138 #undef PRINTBLOCKS
140 #ifdef SILENT
141 # ifdef PRINTSTATS
142 # undef PRINTSTATS
143 # endif
144 # ifdef PRINTTIMES
145 # undef PRINTTIMES
146 # endif
147 # ifdef PRINTNBLOCKS
148 # undef PRINTNBLOCKS
149 # endif
150 #endif
152 #if defined(PRINTSTATS) && !defined(GATHERSTATS)
153 # define GATHERSTATS
154 #endif
156 #ifdef FINALIZE_ON_DEMAND
157 # define GC_INVOKE_FINALIZERS()
158 #else
159 # define GC_INVOKE_FINALIZERS() (void)GC_invoke_finalizers()
160 #endif
162 #define MERGE_SIZES /* Round up some object sizes, so that fewer distinct */
163 /* free lists are actually maintained. This applies */
164 /* only to the top level routines in misc.c, not to */
165 /* user generated code that calls GC_allocobj and */
166 /* GC_allocaobj directly. */
167 /* Slows down average programs slightly. May however */
168 /* substantially reduce fragmentation if allocation */
169 /* request sizes are widely scattered. */
170 /* May save significant amounts of space for obj_map */
171 /* entries. */
173 #ifndef OLD_BLOCK_ALLOC
174 /* Macros controlling large block allocation strategy. */
175 # define EXACT_FIRST /* Make a complete pass through the large object */
176 /* free list before splitting a block */
177 # define PRESERVE_LAST /* Do not divide last allocated heap segment */
178 /* unless we would otherwise need to expand the */
179 /* heap. */
180 #endif
182 /* ALIGN_DOUBLE requires MERGE_SIZES at present. */
183 # if defined(ALIGN_DOUBLE) && !defined(MERGE_SIZES)
184 # define MERGE_SIZES
185 # endif
187 #if defined(ALL_INTERIOR_POINTERS) && !defined(DONT_ADD_BYTE_AT_END)
188 # define ADD_BYTE_AT_END
189 #endif
192 # ifndef LARGE_CONFIG
193 # define MINHINCR 16 /* Minimum heap increment, in blocks of HBLKSIZE */
194 /* Must be multiple of largest page size. */
195 # define MAXHINCR 512 /* Maximum heap increment, in blocks */
196 # else
197 # define MINHINCR 64
198 # define MAXHINCR 4096
199 # endif
201 # define TIME_LIMIT 50 /* We try to keep pause times from exceeding */
202 /* this by much. In milliseconds. */
204 # define BL_LIMIT GC_black_list_spacing
205 /* If we need a block of N bytes, and we have */
206 /* a block of N + BL_LIMIT bytes available, */
207 /* and N > BL_LIMIT, */
208 /* but all possible positions in it are */
209 /* blacklisted, we just use it anyway (and */
210 /* print a warning, if warnings are enabled). */
211 /* This risks subsequently leaking the block */
212 /* due to a false reference. But not using */
213 /* the block risks unreasonable immediate */
214 /* heap growth. */
216 /*********************************/
217 /* */
218 /* Stack saving for debugging */
219 /* */
220 /*********************************/
222 #ifdef SAVE_CALL_CHAIN
225 * Number of frames and arguments to save in objects allocated by
226 * debugging allocator.
228 # define NFRAMES 6 /* Number of frames to save. Even for */
229 /* alignment reasons. */
230 # define NARGS 2 /* Mumber of arguments to save for each call. */
232 # define NEED_CALLINFO
234 /* Fill in the pc and argument information for up to NFRAMES of my */
235 /* callers. Ignore my frame and my callers frame. */
236 void GC_save_callers (/* struct callinfo info[NFRAMES] */);
238 void GC_print_callers (/* struct callinfo info[NFRAMES] */);
240 #else
242 # ifdef GC_ADD_CALLER
243 # define NFRAMES 1
244 # define NARGS 0
245 # define NEED_CALLINFO
246 # endif
248 #endif
250 #ifdef NEED_CALLINFO
251 struct callinfo {
252 word ci_pc;
253 # if NARGS > 0
254 word ci_arg[NARGS]; /* bit-wise complement to avoid retention */
255 # endif
256 # if defined(ALIGN_DOUBLE) && (NFRAMES * (NARGS + 1)) % 2 == 1
257 /* Likely alignment problem. */
258 word ci_dummy;
259 # endif
261 #endif
264 /*********************************/
265 /* */
266 /* OS interface routines */
267 /* */
268 /*********************************/
270 #ifdef BSD_TIME
271 # undef CLOCK_TYPE
272 # undef GET_TIME
273 # undef MS_TIME_DIFF
274 # define CLOCK_TYPE struct timeval
275 # define GET_TIME(x) { struct rusage rusage; \
276 getrusage (RUSAGE_SELF, &rusage); \
277 x = rusage.ru_utime; }
278 # define MS_TIME_DIFF(a,b) ((double) (a.tv_sec - b.tv_sec) * 1000.0 \
279 + (double) (a.tv_usec - b.tv_usec) / 1000.0)
280 #else /* !BSD_TIME */
281 # include <time.h>
282 # if !defined(__STDC__) && defined(SPARC) && defined(SUNOS4)
283 clock_t clock(); /* Not in time.h, where it belongs */
284 # endif
285 # if defined(FREEBSD) && !defined(CLOCKS_PER_SEC)
286 # include <machine/limits.h>
287 # define CLOCKS_PER_SEC CLK_TCK
288 # endif
289 # if !defined(CLOCKS_PER_SEC)
290 # define CLOCKS_PER_SEC 1000000
292 * This is technically a bug in the implementation. ANSI requires that
293 * CLOCKS_PER_SEC be defined. But at least under SunOS4.1.1, it isn't.
294 * Also note that the combination of ANSI C and POSIX is incredibly gross
295 * here. The type clock_t is used by both clock() and times(). But on
296 * some machines these use different notions of a clock tick, CLOCKS_PER_SEC
297 * seems to apply only to clock. Hence we use it here. On many machines,
298 * including SunOS, clock actually uses units of microseconds (which are
299 * not really clock ticks).
301 # endif
302 # define CLOCK_TYPE clock_t
303 # define GET_TIME(x) x = clock()
304 # define MS_TIME_DIFF(a,b) ((unsigned long) \
305 (1000.0*(double)((a)-(b))/(double)CLOCKS_PER_SEC))
306 #endif /* !BSD_TIME */
308 /* We use bzero and bcopy internally. They may not be available. */
309 # if defined(SPARC) && defined(SUNOS4)
310 # define BCOPY_EXISTS
311 # endif
312 # if defined(M68K) && defined(AMIGA)
313 # define BCOPY_EXISTS
314 # endif
315 # if defined(M68K) && defined(NEXT)
316 # define BCOPY_EXISTS
317 # endif
318 # if defined(VAX)
319 # define BCOPY_EXISTS
320 # endif
321 # if defined(AMIGA)
322 # include <string.h>
323 # define BCOPY_EXISTS
324 # endif
326 # ifndef BCOPY_EXISTS
327 # include <string.h>
328 # define BCOPY(x,y,n) memcpy(y, x, (size_t)(n))
329 # define BZERO(x,n) memset(x, 0, (size_t)(n))
330 # else
331 # define BCOPY(x,y,n) bcopy((char *)(x),(char *)(y),(int)(n))
332 # define BZERO(x,n) bzero((char *)(x),(int)(n))
333 # endif
335 /* HBLKSIZE aligned allocation. 0 is taken to mean failure */
336 /* space is assumed to be cleared. */
337 /* In the case os USE_MMAP, the argument must also be a */
338 /* physical page size. */
339 # ifdef PCR
340 char * real_malloc();
341 # define GET_MEM(bytes) HBLKPTR(real_malloc((size_t)bytes + GC_page_size) \
342 + GC_page_size-1)
343 # else
344 # ifdef OS2
345 void * os2_alloc(size_t bytes);
346 # define GET_MEM(bytes) HBLKPTR((ptr_t)os2_alloc((size_t)bytes \
347 + GC_page_size) \
348 + GC_page_size-1)
349 # else
350 # if defined(AMIGA) || defined(NEXT) || defined(DOS4GW)
351 # define GET_MEM(bytes) HBLKPTR((size_t) \
352 calloc(1, (size_t)bytes + GC_page_size) \
353 + GC_page_size-1)
354 # else
355 # ifdef MSWIN32
356 extern ptr_t GC_win32_get_mem();
357 # define GET_MEM(bytes) (struct hblk *)GC_win32_get_mem(bytes)
358 # else
359 # ifdef MACOS
360 # if defined(USE_TEMPORARY_MEMORY)
361 extern Ptr GC_MacTemporaryNewPtr(size_t size,
362 Boolean clearMemory);
363 # define GET_MEM(bytes) HBLKPTR( \
364 GC_MacTemporaryNewPtr(bytes + GC_page_size, true) \
365 + GC_page_size-1)
366 # else
367 # define GET_MEM(bytes) HBLKPTR( \
368 NewPtrClear(bytes + GC_page_size) + GC_page_size-1)
369 # endif
370 # else
371 extern ptr_t GC_unix_get_mem();
372 # define GET_MEM(bytes) (struct hblk *)GC_unix_get_mem(bytes)
373 # endif
374 # endif
375 # endif
376 # endif
377 # endif
380 * Mutual exclusion between allocator/collector routines.
381 * Needed if there is more than one allocator thread.
382 * FASTLOCK() is assumed to try to acquire the lock in a cheap and
383 * dirty way that is acceptable for a few instructions, e.g. by
384 * inhibiting preemption. This is assumed to have succeeded only
385 * if a subsequent call to FASTLOCK_SUCCEEDED() returns TRUE.
386 * FASTUNLOCK() is called whether or not FASTLOCK_SUCCEEDED().
387 * If signals cannot be tolerated with the FASTLOCK held, then
388 * FASTLOCK should disable signals. The code executed under
389 * FASTLOCK is otherwise immune to interruption, provided it is
390 * not restarted.
391 * DCL_LOCK_STATE declares any local variables needed by LOCK and UNLOCK
392 * and/or DISABLE_SIGNALS and ENABLE_SIGNALS and/or FASTLOCK.
393 * (There is currently no equivalent for FASTLOCK.)
395 # ifdef THREADS
396 # ifdef PCR_OBSOLETE /* Faster, but broken with multiple lwp's */
397 # include "th/PCR_Th.h"
398 # include "th/PCR_ThCrSec.h"
399 extern struct PCR_Th_MLRep GC_allocate_ml;
400 # define DCL_LOCK_STATE PCR_sigset_t GC_old_sig_mask
401 # define LOCK() PCR_Th_ML_Acquire(&GC_allocate_ml)
402 # define UNLOCK() PCR_Th_ML_Release(&GC_allocate_ml)
403 # define FASTLOCK() PCR_ThCrSec_EnterSys()
404 /* Here we cheat (a lot): */
405 # define FASTLOCK_SUCCEEDED() (*(int *)(&GC_allocate_ml) == 0)
406 /* TRUE if nobody currently holds the lock */
407 # define FASTUNLOCK() PCR_ThCrSec_ExitSys()
408 # endif
409 # ifdef PCR
410 # include <base/PCR_Base.h>
411 # include <th/PCR_Th.h>
412 extern PCR_Th_ML GC_allocate_ml;
413 # define DCL_LOCK_STATE \
414 PCR_ERes GC_fastLockRes; PCR_sigset_t GC_old_sig_mask
415 # define LOCK() PCR_Th_ML_Acquire(&GC_allocate_ml)
416 # define UNLOCK() PCR_Th_ML_Release(&GC_allocate_ml)
417 # define FASTLOCK() (GC_fastLockRes = PCR_Th_ML_Try(&GC_allocate_ml))
418 # define FASTLOCK_SUCCEEDED() (GC_fastLockRes == PCR_ERes_okay)
419 # define FASTUNLOCK() {\
420 if( FASTLOCK_SUCCEEDED() ) PCR_Th_ML_Release(&GC_allocate_ml); }
421 # endif
422 # ifdef SRC_M3
423 extern word RT0u__inCritical;
424 # define LOCK() RT0u__inCritical++
425 # define UNLOCK() RT0u__inCritical--
426 # endif
427 # ifdef SOLARIS_THREADS
428 # include <thread.h>
429 # include <signal.h>
430 extern mutex_t GC_allocate_ml;
431 # define LOCK() mutex_lock(&GC_allocate_ml);
432 # define UNLOCK() mutex_unlock(&GC_allocate_ml);
433 # endif
434 # ifdef LINUX_THREADS
435 # include <pthread.h>
436 # ifdef __i386__
437 inline static int GC_test_and_set(volatile unsigned int *addr) {
438 int oldval;
439 /* Note: the "xchg" instruction does not need a "lock" prefix */
440 __asm__ __volatile__("xchgl %0, %1"
441 : "=r"(oldval), "=m"(*(addr))
442 : "0"(1), "m"(*(addr)));
443 return oldval;
445 # else
446 -- > Need implementation of GC_test_and_set()
447 # endif
448 # define GC_clear(addr) (*(addr) = 0)
450 extern volatile unsigned int GC_allocate_lock;
451 /* This is not a mutex because mutexes that obey the (optional) */
452 /* POSIX scheduling rules are subject to convoys in high contention */
453 /* applications. This is basically a spin lock. */
454 extern pthread_t GC_lock_holder;
455 extern void GC_lock(void);
456 /* Allocation lock holder. Only set if acquired by client through */
457 /* GC_call_with_alloc_lock. */
458 # define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
459 # define NO_THREAD (pthread_t)(-1)
460 # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
461 # define I_HOLD_LOCK() (pthread_equal(GC_lock_holder, pthread_self()))
462 # ifdef UNDEFINED
463 # define LOCK() pthread_mutex_lock(&GC_allocate_ml)
464 # define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
465 # else
466 # define LOCK() \
467 { if (GC_test_and_set(&GC_allocate_lock)) GC_lock(); }
468 # define UNLOCK() \
469 GC_clear(&GC_allocate_lock)
470 # endif
471 extern GC_bool GC_collecting;
472 # define ENTER_GC() \
474 GC_collecting = 1; \
476 # define EXIT_GC() GC_collecting = 0;
477 # endif /* LINUX_THREADS */
478 # ifdef IRIX_THREADS
479 # include <pthread.h>
480 # include <mutex.h>
482 # if __mips < 3 || !(defined (_ABIN32) || defined(_ABI64))
483 # define GC_test_and_set(addr, v) test_and_set(addr,v)
484 # else
485 # define GC_test_and_set(addr, v) __test_and_set(addr,v)
486 # endif
487 extern unsigned long GC_allocate_lock;
488 /* This is not a mutex because mutexes that obey the (optional) */
489 /* POSIX scheduling rules are subject to convoys in high contention */
490 /* applications. This is basically a spin lock. */
491 extern pthread_t GC_lock_holder;
492 extern void GC_lock(void);
493 /* Allocation lock holder. Only set if acquired by client through */
494 /* GC_call_with_alloc_lock. */
495 # define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
496 # define NO_THREAD (pthread_t)(-1)
497 # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
498 # define I_HOLD_LOCK() (pthread_equal(GC_lock_holder, pthread_self()))
499 # ifdef UNDEFINED
500 # define LOCK() pthread_mutex_lock(&GC_allocate_ml)
501 # define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
502 # else
503 # define LOCK() { if (GC_test_and_set(&GC_allocate_lock, 1)) GC_lock(); }
504 # if __mips >= 3 && (defined (_ABIN32) || defined(_ABI64))
505 # define UNLOCK() __lock_release(&GC_allocate_lock)
506 # else
507 # define UNLOCK() GC_allocate_lock = 0
508 # endif
509 # endif
510 extern GC_bool GC_collecting;
511 # define ENTER_GC() \
513 GC_collecting = 1; \
515 # define EXIT_GC() GC_collecting = 0;
516 # endif /* IRIX_THREADS */
517 # ifdef WIN32_THREADS
518 # include <windows.h>
519 GC_API CRITICAL_SECTION GC_allocate_ml;
520 # define LOCK() EnterCriticalSection(&GC_allocate_ml);
521 # define UNLOCK() LeaveCriticalSection(&GC_allocate_ml);
522 # endif
523 # ifdef QUICK_THREADS
524 # define LOCK()
525 # define UNLOCK()
526 # endif
527 # ifndef SET_LOCK_HOLDER
528 # define SET_LOCK_HOLDER()
529 # define UNSET_LOCK_HOLDER()
530 # define I_HOLD_LOCK() FALSE
531 /* Used on platforms were locks can be reacquired, */
532 /* so it doesn't matter if we lie. */
533 # endif
534 # else
535 # define LOCK()
536 # define UNLOCK()
537 # endif
538 # ifndef SET_LOCK_HOLDER
539 # define SET_LOCK_HOLDER()
540 # define UNSET_LOCK_HOLDER()
541 # define I_HOLD_LOCK() FALSE
542 /* Used on platforms were locks can be reacquired, */
543 /* so it doesn't matter if we lie. */
544 # endif
545 # ifndef ENTER_GC
546 # define ENTER_GC()
547 # define EXIT_GC()
548 # endif
550 # ifndef DCL_LOCK_STATE
551 # define DCL_LOCK_STATE
552 # endif
553 # ifndef FASTLOCK
554 # define FASTLOCK() LOCK()
555 # define FASTLOCK_SUCCEEDED() TRUE
556 # define FASTUNLOCK() UNLOCK()
557 # endif
559 /* Delay any interrupts or signals that may abort this thread. Data */
560 /* structures are in a consistent state outside this pair of calls. */
561 /* ANSI C allows both to be empty (though the standard isn't very */
562 /* clear on that point). Standard malloc implementations are usually */
563 /* neither interruptable nor thread-safe, and thus correspond to */
564 /* empty definitions. */
565 # ifdef PCR
566 # define DISABLE_SIGNALS() \
567 PCR_Th_SetSigMask(PCR_allSigsBlocked,&GC_old_sig_mask)
568 # define ENABLE_SIGNALS() \
569 PCR_Th_SetSigMask(&GC_old_sig_mask, NIL)
570 # else
571 # if defined(SRC_M3) || defined(AMIGA) || defined(SOLARIS_THREADS) \
572 || defined(MSWIN32) || defined(MACOS) || defined(DJGPP) \
573 || defined(NO_SIGNALS) || defined(IRIX_THREADS) \
574 || defined(LINUX_THREADS)
575 /* Also useful for debugging. */
576 /* Should probably use thr_sigsetmask for SOLARIS_THREADS. */
577 # define DISABLE_SIGNALS()
578 # define ENABLE_SIGNALS()
579 # else
580 # define DISABLE_SIGNALS() GC_disable_signals()
581 void GC_disable_signals();
582 # define ENABLE_SIGNALS() GC_enable_signals()
583 void GC_enable_signals();
584 # endif
585 # endif
588 * Stop and restart mutator threads.
590 # ifdef PCR
591 # include "th/PCR_ThCtl.h"
592 # define STOP_WORLD() \
593 PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_stopNormal, \
594 PCR_allSigsBlocked, \
595 PCR_waitForever)
596 # define START_WORLD() \
597 PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_null, \
598 PCR_allSigsBlocked, \
599 PCR_waitForever);
600 # else
601 # if defined(SOLARIS_THREADS) || defined(WIN32_THREADS) \
602 || defined(IRIX_THREADS) || defined(LINUX_THREADS)
603 void GC_stop_world();
604 void GC_start_world();
605 # define STOP_WORLD() GC_stop_world()
606 # define START_WORLD() GC_start_world()
607 # else
608 # define STOP_WORLD()
609 # define START_WORLD()
610 # endif
611 # endif
613 /* Abandon ship */
614 # ifdef PCR
615 # define ABORT(s) PCR_Base_Panic(s)
616 # else
617 # ifdef SMALL_CONFIG
618 # define ABORT(msg) abort();
619 # else
620 GC_API void GC_abort();
621 # define ABORT(msg) GC_abort(msg);
622 # endif
623 # endif
625 /* Exit abnormally, but without making a mess (e.g. out of memory) */
626 # ifdef PCR
627 # define EXIT() PCR_Base_Exit(1,PCR_waitForever)
628 # else
629 # define EXIT() (void)exit(1)
630 # endif
632 /* Print warning message, e.g. almost out of memory. */
633 # define WARN(msg,arg) (*GC_current_warn_proc)(msg, (GC_word)(arg))
634 extern GC_warn_proc GC_current_warn_proc;
636 /*********************************/
637 /* */
638 /* Word-size-dependent defines */
639 /* */
640 /*********************************/
642 #if CPP_WORDSZ == 32
643 # define WORDS_TO_BYTES(x) ((x)<<2)
644 # define BYTES_TO_WORDS(x) ((x)>>2)
645 # define LOGWL ((word)5) /* log[2] of CPP_WORDSZ */
646 # define modWORDSZ(n) ((n) & 0x1f) /* n mod size of word */
647 # if ALIGNMENT != 4
648 # define UNALIGNED
649 # endif
650 #endif
652 #if CPP_WORDSZ == 64
653 # define WORDS_TO_BYTES(x) ((x)<<3)
654 # define BYTES_TO_WORDS(x) ((x)>>3)
655 # define LOGWL ((word)6) /* log[2] of CPP_WORDSZ */
656 # define modWORDSZ(n) ((n) & 0x3f) /* n mod size of word */
657 # if ALIGNMENT != 8
658 # define UNALIGNED
659 # endif
660 #endif
662 #define WORDSZ ((word)CPP_WORDSZ)
663 #define SIGNB ((word)1 << (WORDSZ-1))
664 #define BYTES_PER_WORD ((word)(sizeof (word)))
665 #define ONES ((word)(-1))
666 #define divWORDSZ(n) ((n) >> LOGWL) /* divide n by size of word */
668 /*********************/
669 /* */
670 /* Size Parameters */
671 /* */
672 /*********************/
674 /* heap block size, bytes. Should be power of 2 */
676 #ifndef HBLKSIZE
677 # ifdef SMALL_CONFIG
678 # define CPP_LOG_HBLKSIZE 10
679 # else
680 # if CPP_WORDSZ == 32
681 # define CPP_LOG_HBLKSIZE 12
682 # else
683 # define CPP_LOG_HBLKSIZE 13
684 # endif
685 # endif
686 #else
687 # if HBLKSIZE == 512
688 # define CPP_LOG_HBLKSIZE 9
689 # endif
690 # if HBLKSIZE == 1024
691 # define CPP_LOG_HBLKSIZE 10
692 # endif
693 # if HBLKSIZE == 2048
694 # define CPP_LOG_HBLKSIZE 11
695 # endif
696 # if HBLKSIZE == 4096
697 # define CPP_LOG_HBLKSIZE 12
698 # endif
699 # if HBLKSIZE == 8192
700 # define CPP_LOG_HBLKSIZE 13
701 # endif
702 # if HBLKSIZE == 16384
703 # define CPP_LOG_HBLKSIZE 14
704 # endif
705 # ifndef CPP_LOG_HBLKSIZE
706 --> fix HBLKSIZE
707 # endif
708 # undef HBLKSIZE
709 #endif
710 # define CPP_HBLKSIZE (1 << CPP_LOG_HBLKSIZE)
711 # define LOG_HBLKSIZE ((word)CPP_LOG_HBLKSIZE)
712 # define HBLKSIZE ((word)CPP_HBLKSIZE)
715 /* max size objects supported by freelist (larger objects may be */
716 /* allocated, but less efficiently) */
718 #define CPP_MAXOBJSZ BYTES_TO_WORDS(CPP_HBLKSIZE/2)
719 #define MAXOBJSZ ((word)CPP_MAXOBJSZ)
721 # define divHBLKSZ(n) ((n) >> LOG_HBLKSIZE)
723 # define HBLK_PTR_DIFF(p,q) divHBLKSZ((ptr_t)p - (ptr_t)q)
724 /* Equivalent to subtracting 2 hblk pointers. */
725 /* We do it this way because a compiler should */
726 /* find it hard to use an integer division */
727 /* instead of a shift. The bundled SunOS 4.1 */
728 /* o.w. sometimes pessimizes the subtraction to */
729 /* involve a call to .div. */
731 # define modHBLKSZ(n) ((n) & (HBLKSIZE-1))
733 # define HBLKPTR(objptr) ((struct hblk *)(((word) (objptr)) & ~(HBLKSIZE-1)))
735 # define HBLKDISPL(objptr) (((word) (objptr)) & (HBLKSIZE-1))
737 /* Round up byte allocation requests to integral number of words, etc. */
738 # ifdef ADD_BYTE_AT_END
739 # define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + WORDS_TO_BYTES(1))
740 # ifdef ALIGN_DOUBLE
741 # define ALIGNED_WORDS(n) (BYTES_TO_WORDS((n) + WORDS_TO_BYTES(2)) & ~1)
742 # else
743 # define ALIGNED_WORDS(n) ROUNDED_UP_WORDS(n)
744 # endif
745 # define SMALL_OBJ(bytes) ((bytes) < WORDS_TO_BYTES(MAXOBJSZ))
746 # define ADD_SLOP(bytes) ((bytes)+1)
747 # else
748 # define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + (WORDS_TO_BYTES(1) - 1))
749 # ifdef ALIGN_DOUBLE
750 # define ALIGNED_WORDS(n) \
751 (BYTES_TO_WORDS((n) + WORDS_TO_BYTES(2) - 1) & ~1)
752 # else
753 # define ALIGNED_WORDS(n) ROUNDED_UP_WORDS(n)
754 # endif
755 # define SMALL_OBJ(bytes) ((bytes) <= WORDS_TO_BYTES(MAXOBJSZ))
756 # define ADD_SLOP(bytes) (bytes)
757 # endif
761 * Hash table representation of sets of pages. This assumes it is
762 * OK to add spurious entries to sets.
763 * Used by black-listing code, and perhaps by dirty bit maintenance code.
766 # ifdef LARGE_CONFIG
767 # define LOG_PHT_ENTRIES 17
768 # else
769 # define LOG_PHT_ENTRIES 14 /* Collisions are likely if heap grows */
770 /* to more than 16K hblks = 64MB. */
771 /* Each hash table occupies 2K bytes. */
772 # endif
773 # define PHT_ENTRIES ((word)1 << LOG_PHT_ENTRIES)
774 # define PHT_SIZE (PHT_ENTRIES >> LOGWL)
775 typedef word page_hash_table[PHT_SIZE];
777 # define PHT_HASH(addr) ((((word)(addr)) >> LOG_HBLKSIZE) & (PHT_ENTRIES - 1))
779 # define get_pht_entry_from_index(bl, index) \
780 (((bl)[divWORDSZ(index)] >> modWORDSZ(index)) & 1)
781 # define set_pht_entry_from_index(bl, index) \
782 (bl)[divWORDSZ(index)] |= (word)1 << modWORDSZ(index)
783 # define clear_pht_entry_from_index(bl, index) \
784 (bl)[divWORDSZ(index)] &= ~((word)1 << modWORDSZ(index))
788 /********************************************/
789 /* */
790 /* H e a p B l o c k s */
791 /* */
792 /********************************************/
794 /* heap block header */
795 #define HBLKMASK (HBLKSIZE-1)
797 #define BITS_PER_HBLK (HBLKSIZE * 8)
799 #define MARK_BITS_PER_HBLK (BITS_PER_HBLK/CPP_WORDSZ)
800 /* upper bound */
801 /* We allocate 1 bit/word. Only the first word */
802 /* in each object is actually marked. */
804 # ifdef ALIGN_DOUBLE
805 # define MARK_BITS_SZ (((MARK_BITS_PER_HBLK + 2*CPP_WORDSZ - 1) \
806 / (2*CPP_WORDSZ))*2)
807 # else
808 # define MARK_BITS_SZ ((MARK_BITS_PER_HBLK + CPP_WORDSZ - 1)/CPP_WORDSZ)
809 # endif
810 /* Upper bound on number of mark words per heap block */
812 struct hblkhdr {
813 word hb_sz; /* If in use, size in words, of objects in the block. */
814 /* if free, the size in bytes of the whole block */
815 struct hblk * hb_next; /* Link field for hblk free list */
816 /* and for lists of chunks waiting to be */
817 /* reclaimed. */
818 word hb_descr; /* object descriptor for marking. See */
819 /* mark.h. */
820 char* hb_map; /* A pointer to a pointer validity map of the block. */
821 /* See GC_obj_map. */
822 /* Valid for all blocks with headers. */
823 /* Free blocks point to GC_invalid_map. */
824 unsigned char hb_obj_kind;
825 /* Kind of objects in the block. Each kind */
826 /* identifies a mark procedure and a set of */
827 /* list headers. Sometimes called regions. */
828 unsigned char hb_flags;
829 # define IGNORE_OFF_PAGE 1 /* Ignore pointers that do not */
830 /* point to the first page of */
831 /* this object. */
832 unsigned short hb_last_reclaimed;
833 /* Value of GC_gc_no when block was */
834 /* last allocated or swept. May wrap. */
835 word hb_marks[MARK_BITS_SZ];
836 /* Bit i in the array refers to the */
837 /* object starting at the ith word (header */
838 /* INCLUDED) in the heap block. */
839 /* The lsb of word 0 is numbered 0. */
842 /* heap block body */
844 # define DISCARD_WORDS 0
845 /* Number of words to be dropped at the beginning of each block */
846 /* Must be a multiple of WORDSZ. May reasonably be nonzero */
847 /* on machines that don't guarantee longword alignment of */
848 /* pointers, so that the number of false hits is minimized. */
849 /* 0 and WORDSZ are probably the only reasonable values. */
851 # define BODY_SZ ((HBLKSIZE-WORDS_TO_BYTES(DISCARD_WORDS))/sizeof(word))
853 struct hblk {
854 # if (DISCARD_WORDS != 0)
855 word garbage[DISCARD_WORDS];
856 # endif
857 word hb_body[BODY_SZ];
860 # define HDR_WORDS ((word)DISCARD_WORDS)
861 # define HDR_BYTES ((word)WORDS_TO_BYTES(DISCARD_WORDS))
863 # define OBJ_SZ_TO_BLOCKS(sz) \
864 divHBLKSZ(HDR_BYTES + WORDS_TO_BYTES(sz) + HBLKSIZE-1)
865 /* Size of block (in units of HBLKSIZE) needed to hold objects of */
866 /* given sz (in words). */
868 /* Object free list link */
869 # define obj_link(p) (*(ptr_t *)(p))
871 /* lists of all heap blocks and free lists */
872 /* These are grouped together in a struct */
873 /* so that they can be easily skipped by the */
874 /* GC_mark routine. */
875 /* The ordering is weird to make GC_malloc */
876 /* faster by keeping the important fields */
877 /* sufficiently close together that a */
878 /* single load of a base register will do. */
879 /* Scalars that could easily appear to */
880 /* be pointers are also put here. */
881 /* The main fields should precede any */
882 /* conditionally included fields, so that */
883 /* gc_inl.h will work even if a different set */
884 /* of macros is defined when the client is */
885 /* compiled. */
887 struct _GC_arrays {
888 word _heapsize;
889 word _max_heapsize;
890 ptr_t _last_heap_addr;
891 ptr_t _prev_heap_addr;
892 word _words_allocd_before_gc;
893 /* Number of words allocated before this */
894 /* collection cycle. */
895 word _words_allocd;
896 /* Number of words allocated during this collection cycle */
897 word _words_wasted;
898 /* Number of words wasted due to internal fragmentation */
899 /* in large objects, or due to dropping blacklisted */
900 /* blocks, since last gc. Approximate. */
901 word _words_finalized;
902 /* Approximate number of words in objects (and headers) */
903 /* That became ready for finalization in the last */
904 /* collection. */
905 word _non_gc_bytes_at_gc;
906 /* Number of explicitly managed bytes of storage */
907 /* at last collection. */
908 word _mem_freed;
909 /* Number of explicitly deallocated words of memory */
910 /* since last collection. */
912 ptr_t _objfreelist[MAXOBJSZ+1];
913 /* free list for objects */
914 ptr_t _aobjfreelist[MAXOBJSZ+1];
915 /* free list for atomic objs */
917 ptr_t _uobjfreelist[MAXOBJSZ+1];
918 /* uncollectable but traced objs */
919 /* objects on this and auobjfreelist */
920 /* are always marked, except during */
921 /* garbage collections. */
922 # ifdef ATOMIC_UNCOLLECTABLE
923 ptr_t _auobjfreelist[MAXOBJSZ+1];
924 # endif
925 /* uncollectable but traced objs */
927 # ifdef GATHERSTATS
928 word _composite_in_use;
929 /* Number of words in accessible composite */
930 /* objects. */
931 word _atomic_in_use;
932 /* Number of words in accessible atomic */
933 /* objects. */
934 # endif
935 # ifdef MERGE_SIZES
936 unsigned _size_map[WORDS_TO_BYTES(MAXOBJSZ+1)];
937 /* Number of words to allocate for a given allocation request in */
938 /* bytes. */
939 # endif
941 # ifdef STUBBORN_ALLOC
942 ptr_t _sobjfreelist[MAXOBJSZ+1];
943 # endif
944 /* free list for immutable objects */
945 ptr_t _obj_map[MAXOBJSZ+1];
946 /* If not NIL, then a pointer to a map of valid */
947 /* object addresses. _obj_map[sz][i] is j if the */
948 /* address block_start+i is a valid pointer */
949 /* to an object at */
950 /* block_start+i&~3 - WORDS_TO_BYTES(j). */
951 /* (If ALL_INTERIOR_POINTERS is defined, then */
952 /* instead ((short *)(hbh_map[sz])[i] is j if */
953 /* block_start+WORDS_TO_BYTES(i) is in the */
954 /* interior of an object starting at */
955 /* block_start+WORDS_TO_BYTES(i-j)). */
956 /* It is OBJ_INVALID if */
957 /* block_start+WORDS_TO_BYTES(i) is not */
958 /* valid as a pointer to an object. */
959 /* We assume all values of j <= OBJ_INVALID. */
960 /* The zeroth entry corresponds to large objects.*/
961 # ifdef ALL_INTERIOR_POINTERS
962 # define map_entry_type short
963 # define OBJ_INVALID 0x7fff
964 # define MAP_ENTRY(map, bytes) \
965 (((map_entry_type *)(map))[BYTES_TO_WORDS(bytes)])
966 # define MAP_ENTRIES BYTES_TO_WORDS(HBLKSIZE)
967 # define MAP_SIZE (MAP_ENTRIES * sizeof(map_entry_type))
968 # define OFFSET_VALID(displ) TRUE
969 # define CPP_MAX_OFFSET (HBLKSIZE - HDR_BYTES - 1)
970 # define MAX_OFFSET ((word)CPP_MAX_OFFSET)
971 # else
972 # define map_entry_type char
973 # define OBJ_INVALID 0x7f
974 # define MAP_ENTRY(map, bytes) \
975 (map)[bytes]
976 # define MAP_ENTRIES HBLKSIZE
977 # define MAP_SIZE MAP_ENTRIES
978 # define CPP_MAX_OFFSET (WORDS_TO_BYTES(OBJ_INVALID) - 1)
979 # define MAX_OFFSET ((word)CPP_MAX_OFFSET)
980 # define VALID_OFFSET_SZ \
981 (CPP_MAX_OFFSET > WORDS_TO_BYTES(CPP_MAXOBJSZ)? \
982 CPP_MAX_OFFSET+1 \
983 : WORDS_TO_BYTES(CPP_MAXOBJSZ)+1)
984 char _valid_offsets[VALID_OFFSET_SZ];
985 /* GC_valid_offsets[i] == TRUE ==> i */
986 /* is registered as a displacement. */
987 # define OFFSET_VALID(displ) GC_valid_offsets[displ]
988 char _modws_valid_offsets[sizeof(word)];
989 /* GC_valid_offsets[i] ==> */
990 /* GC_modws_valid_offsets[i%sizeof(word)] */
991 # endif
992 # ifdef STUBBORN_ALLOC
993 page_hash_table _changed_pages;
994 /* Stubborn object pages that were changes since last call to */
995 /* GC_read_changed. */
996 page_hash_table _prev_changed_pages;
997 /* Stubborn object pages that were changes before last call to */
998 /* GC_read_changed. */
999 # endif
1000 # if defined(PROC_VDB) || defined(MPROTECT_VDB)
1001 page_hash_table _grungy_pages; /* Pages that were dirty at last */
1002 /* GC_read_dirty. */
1003 # endif
1004 # ifdef LARGE_CONFIG
1005 # if CPP_WORDSZ > 32
1006 # define MAX_HEAP_SECTS 4096 /* overflows at roughly 64 GB */
1007 # else
1008 # define MAX_HEAP_SECTS 768 /* Separately added heap sections. */
1009 # endif
1010 # else
1011 # define MAX_HEAP_SECTS 256
1012 # endif
1013 struct HeapSect {
1014 ptr_t hs_start; word hs_bytes;
1015 } _heap_sects[MAX_HEAP_SECTS];
1016 # ifdef MSWIN32
1017 ptr_t _heap_bases[MAX_HEAP_SECTS];
1018 /* Start address of memory regions obtained from kernel. */
1019 # endif
1020 /* Block header index; see gc_headers.h */
1021 bottom_index * _all_nils;
1022 bottom_index * _top_index [TOP_SZ];
1023 #ifdef SAVE_CALL_CHAIN
1024 struct callinfo _last_stack[NFRAMES]; /* Stack at last garbage collection.*/
1025 /* Useful for debugging mysterious */
1026 /* object disappearances. */
1027 /* In the multithreaded case, we */
1028 /* currently only save the calling */
1029 /* stack. */
1030 #endif
1033 GC_API GC_FAR struct _GC_arrays GC_arrays;
1035 # define GC_objfreelist GC_arrays._objfreelist
1036 # define GC_aobjfreelist GC_arrays._aobjfreelist
1037 # define GC_uobjfreelist GC_arrays._uobjfreelist
1038 # ifdef ATOMIC_UNCOLLECTABLE
1039 # define GC_auobjfreelist GC_arrays._auobjfreelist
1040 # endif
1041 # define GC_sobjfreelist GC_arrays._sobjfreelist
1042 # define GC_valid_offsets GC_arrays._valid_offsets
1043 # define GC_modws_valid_offsets GC_arrays._modws_valid_offsets
1044 # ifdef STUBBORN_ALLOC
1045 # define GC_changed_pages GC_arrays._changed_pages
1046 # define GC_prev_changed_pages GC_arrays._prev_changed_pages
1047 # endif
1048 # define GC_obj_map GC_arrays._obj_map
1049 # define GC_last_heap_addr GC_arrays._last_heap_addr
1050 # define GC_prev_heap_addr GC_arrays._prev_heap_addr
1051 # define GC_words_allocd GC_arrays._words_allocd
1052 # define GC_words_wasted GC_arrays._words_wasted
1053 # define GC_words_finalized GC_arrays._words_finalized
1054 # define GC_non_gc_bytes_at_gc GC_arrays._non_gc_bytes_at_gc
1055 # define GC_mem_freed GC_arrays._mem_freed
1056 # define GC_heapsize GC_arrays._heapsize
1057 # define GC_max_heapsize GC_arrays._max_heapsize
1058 # define GC_words_allocd_before_gc GC_arrays._words_allocd_before_gc
1059 # define GC_heap_sects GC_arrays._heap_sects
1060 # define GC_last_stack GC_arrays._last_stack
1061 # ifdef MSWIN32
1062 # define GC_heap_bases GC_arrays._heap_bases
1063 # endif
1064 # define GC_all_nils GC_arrays._all_nils
1065 # define GC_top_index GC_arrays._top_index
1066 # if defined(PROC_VDB) || defined(MPROTECT_VDB)
1067 # define GC_grungy_pages GC_arrays._grungy_pages
1068 # endif
1069 # ifdef GATHERSTATS
1070 # define GC_composite_in_use GC_arrays._composite_in_use
1071 # define GC_atomic_in_use GC_arrays._atomic_in_use
1072 # endif
1073 # ifdef MERGE_SIZES
1074 # define GC_size_map GC_arrays._size_map
1075 # endif
1077 # define beginGC_arrays ((ptr_t)(&GC_arrays))
1078 # define endGC_arrays (((ptr_t)(&GC_arrays)) + (sizeof GC_arrays))
1080 GC_API word GC_fo_entries;
1082 # define MAXOBJKINDS 16
1084 /* Object kinds: */
1085 extern struct obj_kind {
1086 ptr_t *ok_freelist; /* Array of free listheaders for this kind of object */
1087 /* Point either to GC_arrays or to storage allocated */
1088 /* with GC_scratch_alloc. */
1089 struct hblk **ok_reclaim_list;
1090 /* List headers for lists of blocks waiting to be */
1091 /* swept. */
1092 word ok_descriptor; /* Descriptor template for objects in this */
1093 /* block. */
1094 GC_bool ok_relocate_descr;
1095 /* Add object size in bytes to descriptor */
1096 /* template to obtain descriptor. Otherwise */
1097 /* template is used as is. */
1098 GC_bool ok_init; /* Clear objects before putting them on the free list. */
1099 } GC_obj_kinds[MAXOBJKINDS];
1100 /* Predefined kinds: */
1101 # define PTRFREE 0
1102 # define NORMAL 1
1103 # define UNCOLLECTABLE 2
1104 # ifdef ATOMIC_UNCOLLECTABLE
1105 # define AUNCOLLECTABLE 3
1106 # define STUBBORN 4
1107 # define IS_UNCOLLECTABLE(k) (((k) & ~1) == UNCOLLECTABLE)
1108 # else
1109 # define STUBBORN 3
1110 # define IS_UNCOLLECTABLE(k) ((k) == UNCOLLECTABLE)
1111 # endif
1113 extern int GC_n_kinds;
1115 extern word GC_n_heap_sects; /* Number of separately added heap */
1116 /* sections. */
1118 extern word GC_page_size;
1120 # ifdef MSWIN32
1121 extern word GC_n_heap_bases; /* See GC_heap_bases. */
1122 # endif
1124 extern word GC_total_stack_black_listed;
1125 /* Number of bytes on stack blacklist. */
1127 extern word GC_black_list_spacing;
1128 /* Average number of bytes between blacklisted */
1129 /* blocks. Approximate. */
1130 /* Counts only blocks that are */
1131 /* "stack-blacklisted", i.e. that are */
1132 /* problematic in the interior of an object. */
1134 extern char * GC_invalid_map;
1135 /* Pointer to the nowhere valid hblk map */
1136 /* Blocks pointing to this map are free. */
1138 extern struct hblk * GC_hblkfreelist;
1139 /* List of completely empty heap blocks */
1140 /* Linked through hb_next field of */
1141 /* header structure associated with */
1142 /* block. */
1144 extern GC_bool GC_is_initialized; /* GC_init() has been run. */
1146 extern GC_bool GC_objects_are_marked; /* There are marked objects in */
1147 /* the heap. */
1149 extern GC_bool GC_incremental; /* Using incremental/generational collection. */
1151 extern GC_bool GC_dirty_maintained;
1152 /* Dirty bits are being maintained, */
1153 /* either for incremental collection, */
1154 /* or to limit the root set. */
1156 # ifndef PCR
1157 extern ptr_t GC_stackbottom; /* Cool end of user stack */
1158 # endif
1160 extern word GC_root_size; /* Total size of registered root sections */
1162 extern GC_bool GC_debugging_started; /* GC_debug_malloc has been called. */
1164 extern ptr_t GC_least_plausible_heap_addr;
1165 extern ptr_t GC_greatest_plausible_heap_addr;
1166 /* Bounds on the heap. Guaranteed valid */
1167 /* Likely to include future heap expansion. */
1169 /* Operations */
1170 # ifndef abs
1171 # define abs(x) ((x) < 0? (-(x)) : (x))
1172 # endif
1175 /* Marks are in a reserved area in */
1176 /* each heap block. Each word has one mark bit associated */
1177 /* with it. Only those corresponding to the beginning of an */
1178 /* object are used. */
1181 /* Mark bit operations */
1184 * Retrieve, set, clear the mark bit corresponding
1185 * to the nth word in a given heap block.
1187 * (Recall that bit n corresponds to object beginning at word n
1188 * relative to the beginning of the block, including unused words)
1191 # define mark_bit_from_hdr(hhdr,n) (((hhdr)->hb_marks[divWORDSZ(n)] \
1192 >> (modWORDSZ(n))) & (word)1)
1193 # define set_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1194 |= (word)1 << modWORDSZ(n)
1196 # define clear_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1197 &= ~((word)1 << modWORDSZ(n))
1199 /* Important internal collector routines */
1201 ptr_t GC_approx_sp();
1203 GC_bool GC_should_collect();
1204 #ifdef PRESERVE_LAST
1205 GC_bool GC_in_last_heap_sect(/* ptr_t */);
1206 /* In last added heap section? If so, avoid breaking up. */
1207 #endif
1208 void GC_apply_to_all_blocks(/*fn, client_data*/);
1209 /* Invoke fn(hbp, client_data) for each */
1210 /* allocated heap block. */
1211 struct hblk * GC_next_block(/* struct hblk * h */);
1212 void GC_mark_init();
1213 void GC_clear_marks(); /* Clear mark bits for all heap objects. */
1214 void GC_invalidate_mark_state(); /* Tell the marker that marked */
1215 /* objects may point to unmarked */
1216 /* ones, and roots may point to */
1217 /* unmarked objects. */
1218 /* Reset mark stack. */
1219 void GC_mark_from_mark_stack(); /* Mark from everything on the mark stack. */
1220 /* Return after about one pages worth of */
1221 /* work. */
1222 GC_bool GC_mark_stack_empty();
1223 GC_bool GC_mark_some(); /* Perform about one pages worth of marking */
1224 /* work of whatever kind is needed. Returns */
1225 /* quickly if no collection is in progress. */
1226 /* Return TRUE if mark phase finished. */
1227 void GC_initiate_gc(); /* initiate collection. */
1228 /* If the mark state is invalid, this */
1229 /* becomes full colleection. Otherwise */
1230 /* it's partial. */
1231 void GC_push_all(/*b,t*/); /* Push everything in a range */
1232 /* onto mark stack. */
1233 void GC_push_dirty(/*b,t*/); /* Push all possibly changed */
1234 /* subintervals of [b,t) onto */
1235 /* mark stack. */
1236 #ifndef SMALL_CONFIG
1237 void GC_push_conditional(/* ptr_t b, ptr_t t, GC_bool all*/);
1238 #else
1239 # define GC_push_conditional(b, t, all) GC_push_all(b, t)
1240 #endif
1241 /* Do either of the above, depending */
1242 /* on the third arg. */
1243 void GC_push_all_stack(/*b,t*/); /* As above, but consider */
1244 /* interior pointers as valid */
1245 void GC_push_roots(/* GC_bool all */); /* Push all or dirty roots. */
1246 extern void (*GC_push_other_roots)();
1247 /* Push system or application specific roots */
1248 /* onto the mark stack. In some environments */
1249 /* (e.g. threads environments) this is */
1250 /* predfined to be non-zero. A client supplied */
1251 /* replacement should also call the original */
1252 /* function. */
1253 extern void (*GC_start_call_back)(/* void */);
1254 /* Called at start of full collections. */
1255 /* Not called if 0. Called with allocation */
1256 /* lock held. */
1257 /* 0 by default. */
1258 void GC_push_regs(); /* Push register contents onto mark stack. */
1259 void GC_remark(); /* Mark from all marked objects. Used */
1260 /* only if we had to drop something. */
1261 # if defined(MSWIN32)
1262 void __cdecl GC_push_one();
1263 # else
1264 void GC_push_one(/*p*/); /* If p points to an object, mark it */
1265 /* and push contents on the mark stack */
1266 # endif
1267 void GC_push_one_checked(/*p*/); /* Ditto, omits plausibility test */
1268 void GC_push_marked(/* struct hblk h, hdr * hhdr */);
1269 /* Push contents of all marked objects in h onto */
1270 /* mark stack. */
1271 #ifdef SMALL_CONFIG
1272 # define GC_push_next_marked_dirty(h) GC_push_next_marked(h)
1273 #else
1274 struct hblk * GC_push_next_marked_dirty(/* h */);
1275 /* Invoke GC_push_marked on next dirty block above h. */
1276 /* Return a pointer just past the end of this block. */
1277 #endif /* !SMALL_CONFIG */
1278 struct hblk * GC_push_next_marked(/* h */);
1279 /* Ditto, but also mark from clean pages. */
1280 struct hblk * GC_push_next_marked_uncollectable(/* h */);
1281 /* Ditto, but mark only from uncollectable pages. */
1282 GC_bool GC_stopped_mark(); /* Stop world and mark from all roots */
1283 /* and rescuers. */
1284 void GC_clear_hdr_marks(/* hhdr */); /* Clear the mark bits in a header */
1285 void GC_set_hdr_marks(/* hhdr */); /* Set the mark bits in a header */
1286 void GC_add_roots_inner();
1287 GC_bool GC_is_static_root(/* ptr_t p */);
1288 /* Is the address p in one of the registered static */
1289 /* root sections? */
1290 void GC_register_dynamic_libraries();
1291 /* Add dynamic library data sections to the root set. */
1293 /* Machine dependent startup routines */
1294 ptr_t GC_get_stack_base();
1295 void GC_register_data_segments();
1297 /* Black listing: */
1298 void GC_bl_init();
1299 # ifndef ALL_INTERIOR_POINTERS
1300 void GC_add_to_black_list_normal(/* bits, maybe source */);
1301 /* Register bits as a possible future false */
1302 /* reference from the heap or static data */
1303 # ifdef PRINT_BLACK_LIST
1304 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1305 GC_add_to_black_list_normal(bits, source)
1306 # else
1307 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1308 GC_add_to_black_list_normal(bits)
1309 # endif
1310 # else
1311 # ifdef PRINT_BLACK_LIST
1312 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1313 GC_add_to_black_list_stack(bits, source)
1314 # else
1315 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1316 GC_add_to_black_list_stack(bits)
1317 # endif
1318 # endif
1320 void GC_add_to_black_list_stack(/* bits, maybe source */);
1321 struct hblk * GC_is_black_listed(/* h, len */);
1322 /* If there are likely to be false references */
1323 /* to a block starting at h of the indicated */
1324 /* length, then return the next plausible */
1325 /* starting location for h that might avoid */
1326 /* these false references. */
1327 void GC_promote_black_lists();
1328 /* Declare an end to a black listing phase. */
1329 void GC_unpromote_black_lists();
1330 /* Approximately undo the effect of the above. */
1331 /* This actually loses some information, but */
1332 /* only in a reasonably safe way. */
1333 word GC_number_stack_black_listed(/*struct hblk *start, struct hblk *endp1 */);
1334 /* Return the number of (stack) blacklisted */
1335 /* blocks in the range for statistical */
1336 /* purposes. */
1338 ptr_t GC_scratch_alloc(/*bytes*/);
1339 /* GC internal memory allocation for */
1340 /* small objects. Deallocation is not */
1341 /* possible. */
1343 /* Heap block layout maps: */
1344 void GC_invalidate_map(/* hdr */);
1345 /* Remove the object map associated */
1346 /* with the block. This identifies */
1347 /* the block as invalid to the mark */
1348 /* routines. */
1349 GC_bool GC_add_map_entry(/*sz*/);
1350 /* Add a heap block map for objects of */
1351 /* size sz to obj_map. */
1352 /* Return FALSE on failure. */
1353 void GC_register_displacement_inner(/*offset*/);
1354 /* Version of GC_register_displacement */
1355 /* that assumes lock is already held */
1356 /* and signals are already disabled. */
1358 /* hblk allocation: */
1359 void GC_new_hblk(/*size_in_words, kind*/);
1360 /* Allocate a new heap block, and build */
1361 /* a free list in it. */
1362 struct hblk * GC_allochblk(/*size_in_words, kind*/);
1363 /* Allocate a heap block, clear it if */
1364 /* for composite objects, inform */
1365 /* the marker that block is valid */
1366 /* for objects of indicated size. */
1367 /* sz < 0 ==> atomic. */
1368 void GC_freehblk(); /* Deallocate a heap block and mark it */
1369 /* as invalid. */
1371 /* Misc GC: */
1372 void GC_init_inner();
1373 GC_bool GC_expand_hp_inner();
1374 void GC_start_reclaim(/*abort_if_found*/);
1375 /* Restore unmarked objects to free */
1376 /* lists, or (if abort_if_found is */
1377 /* TRUE) report them. */
1378 /* Sweeping of small object pages is */
1379 /* largely deferred. */
1380 void GC_continue_reclaim(/*size, kind*/);
1381 /* Sweep pages of the given size and */
1382 /* kind, as long as possible, and */
1383 /* as long as the corr. free list is */
1384 /* empty. */
1385 void GC_reclaim_or_delete_all();
1386 /* Arrange for all reclaim lists to be */
1387 /* empty. Judiciously choose between */
1388 /* sweeping and discarding each page. */
1389 GC_bool GC_reclaim_all(/* GC_stop_func f*/);
1390 /* Reclaim all blocks. Abort (in a */
1391 /* consistent state) if f returns TRUE. */
1392 GC_bool GC_block_empty(/* hhdr */); /* Block completely unmarked? */
1393 GC_bool GC_never_stop_func(); /* Returns FALSE. */
1394 GC_bool GC_try_to_collect_inner(/* GC_stop_func f */);
1395 /* Collect; caller must have acquired */
1396 /* lock and disabled signals. */
1397 /* Collection is aborted if f returns */
1398 /* TRUE. Returns TRUE if it completes */
1399 /* successfully. */
1400 # define GC_gcollect_inner() \
1401 (void) GC_try_to_collect_inner(GC_never_stop_func)
1402 void GC_finish_collection(); /* Finish collection. Mark bits are */
1403 /* consistent and lock is still held. */
1404 GC_bool GC_collect_or_expand(/* needed_blocks */);
1405 /* Collect or expand heap in an attempt */
1406 /* make the indicated number of free */
1407 /* blocks available. Should be called */
1408 /* until the blocks are available or */
1409 /* until it fails by returning FALSE. */
1410 void GC_init(); /* Initialize collector. */
1411 void GC_collect_a_little_inner(/* int n */);
1412 /* Do n units worth of garbage */
1413 /* collection work, if appropriate. */
1414 /* A unit is an amount appropriate for */
1415 /* HBLKSIZE bytes of allocation. */
1416 ptr_t GC_generic_malloc(/* bytes, kind */);
1417 /* Allocate an object of the given */
1418 /* kind. By default, there are only */
1419 /* a few kinds: composite(pointerfree), */
1420 /* atomic, uncollectable, etc. */
1421 /* We claim it's possible for clever */
1422 /* client code that understands GC */
1423 /* internals to add more, e.g. to */
1424 /* communicate object layout info */
1425 /* to the collector. */
1426 ptr_t GC_generic_malloc_ignore_off_page(/* bytes, kind */);
1427 /* As above, but pointers past the */
1428 /* first page of the resulting object */
1429 /* are ignored. */
1430 ptr_t GC_generic_malloc_inner(/* bytes, kind */);
1431 /* Ditto, but I already hold lock, etc. */
1432 ptr_t GC_generic_malloc_words_small GC_PROTO((size_t words, int kind));
1433 /* As above, but size in units of words */
1434 /* Bypasses MERGE_SIZES. Assumes */
1435 /* words <= MAXOBJSZ. */
1436 ptr_t GC_generic_malloc_inner_ignore_off_page(/* bytes, kind */);
1437 /* Allocate an object, where */
1438 /* the client guarantees that there */
1439 /* will always be a pointer to the */
1440 /* beginning of the object while the */
1441 /* object is live. */
1442 ptr_t GC_allocobj(/* sz_inn_words, kind */);
1443 /* Make the indicated */
1444 /* free list nonempty, and return its */
1445 /* head. */
1447 void GC_init_headers();
1448 GC_bool GC_install_header(/*h*/);
1449 /* Install a header for block h. */
1450 /* Return FALSE on failure. */
1451 GC_bool GC_install_counts(/*h, sz*/);
1452 /* Set up forwarding counts for block */
1453 /* h of size sz. */
1454 /* Return FALSE on failure. */
1455 void GC_remove_header(/*h*/);
1456 /* Remove the header for block h. */
1457 void GC_remove_counts(/*h, sz*/);
1458 /* Remove forwarding counts for h. */
1459 hdr * GC_find_header(/*p*/); /* Debugging only. */
1461 void GC_finalize(); /* Perform all indicated finalization actions */
1462 /* on unmarked objects. */
1463 /* Unreachable finalizable objects are enqueued */
1464 /* for processing by GC_invoke_finalizers. */
1465 /* Invoked with lock. */
1467 void GC_add_to_heap(/*p, bytes*/);
1468 /* Add a HBLKSIZE aligned chunk to the heap. */
1470 void GC_print_obj(/* ptr_t p */);
1471 /* P points to somewhere inside an object with */
1472 /* debugging info. Print a human readable */
1473 /* description of the object to stderr. */
1474 ptr_t GC_debug_object_start(/* ptr_t p */);
1475 /* P points to the start of an object that may */
1476 /* have debug info at its head. Return the */
1477 /* start of the real data. */
1478 extern void (*GC_check_heap)();
1479 /* Check that all objects in the heap with */
1480 /* debugging info are intact. Print */
1481 /* descriptions of any that are not. */
1482 extern void (*GC_print_heap_obj)(/* ptr_t p */);
1483 /* If possible print s followed by a more */
1484 /* detailed description of the object */
1485 /* referred to by p. */
1487 /* Virtual dirty bit implementation: */
1488 /* Each implementation exports the following: */
1489 void GC_read_dirty(); /* Retrieve dirty bits. */
1490 GC_bool GC_page_was_dirty(/* struct hblk * h */);
1491 /* Read retrieved dirty bits. */
1492 GC_bool GC_page_was_ever_dirty(/* struct hblk * h */);
1493 /* Could the page contain valid heap pointers? */
1494 void GC_is_fresh(/* struct hblk * h, word number_of_blocks */);
1495 /* Assert the region currently contains no */
1496 /* valid pointers. */
1497 void GC_write_hint(/* struct hblk * h */);
1498 /* h is about to be written. */
1499 void GC_dirty_init();
1501 /* Slow/general mark bit manipulation: */
1502 GC_bool GC_is_marked();
1503 void GC_clear_mark_bit();
1504 void GC_set_mark_bit();
1506 /* Stubborn objects: */
1507 void GC_read_changed(); /* Analogous to GC_read_dirty */
1508 GC_bool GC_page_was_changed(/* h */); /* Analogous to GC_page_was_dirty */
1509 void GC_clean_changing_list(); /* Collect obsolete changing list entries */
1510 void GC_stubborn_init();
1512 /* Debugging print routines: */
1513 void GC_print_block_list();
1514 void GC_print_hblkfreelist();
1515 void GC_print_heap_sects();
1516 void GC_print_static_roots();
1517 void GC_dump();
1519 /* Make arguments appear live to compiler */
1520 # ifdef __WATCOMC__
1521 void GC_noop(void*, ...);
1522 # else
1523 GC_API void GC_noop();
1524 # endif
1526 void GC_noop1(/* word arg */);
1528 /* Logging and diagnostic output: */
1529 GC_API void GC_printf GC_PROTO((char * format, long, long, long, long, long, long));
1530 /* A version of printf that doesn't allocate, */
1531 /* is restricted to long arguments, and */
1532 /* (unfortunately) doesn't use varargs for */
1533 /* portability. Restricted to 6 args and */
1534 /* 1K total output length. */
1535 /* (We use sprintf. Hopefully that doesn't */
1536 /* allocate for long arguments.) */
1537 # define GC_printf0(f) GC_printf(f, 0l, 0l, 0l, 0l, 0l, 0l)
1538 # define GC_printf1(f,a) GC_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1539 # define GC_printf2(f,a,b) GC_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1540 # define GC_printf3(f,a,b,c) GC_printf(f, (long)a, (long)b, (long)c, 0l, 0l, 0l)
1541 # define GC_printf4(f,a,b,c,d) GC_printf(f, (long)a, (long)b, (long)c, \
1542 (long)d, 0l, 0l)
1543 # define GC_printf5(f,a,b,c,d,e) GC_printf(f, (long)a, (long)b, (long)c, \
1544 (long)d, (long)e, 0l)
1545 # define GC_printf6(f,a,b,c,d,e,g) GC_printf(f, (long)a, (long)b, (long)c, \
1546 (long)d, (long)e, (long)g)
1548 void GC_err_printf(/* format, a, b, c, d, e, f */);
1549 # define GC_err_printf0(f) GC_err_puts(f)
1550 # define GC_err_printf1(f,a) GC_err_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1551 # define GC_err_printf2(f,a,b) GC_err_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1552 # define GC_err_printf3(f,a,b,c) GC_err_printf(f, (long)a, (long)b, (long)c, \
1553 0l, 0l, 0l)
1554 # define GC_err_printf4(f,a,b,c,d) GC_err_printf(f, (long)a, (long)b, \
1555 (long)c, (long)d, 0l, 0l)
1556 # define GC_err_printf5(f,a,b,c,d,e) GC_err_printf(f, (long)a, (long)b, \
1557 (long)c, (long)d, \
1558 (long)e, 0l)
1559 # define GC_err_printf6(f,a,b,c,d,e,g) GC_err_printf(f, (long)a, (long)b, \
1560 (long)c, (long)d, \
1561 (long)e, (long)g)
1562 /* Ditto, writes to stderr. */
1564 void GC_err_puts(/* char *s */);
1565 /* Write s to stderr, don't buffer, don't add */
1566 /* newlines, don't ... */
1569 # endif /* GC_PRIVATE_H */