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 by Hewlett-Packard Company. All rights reserved.
8 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
17 /* Boehm, February 16, 1996 2:30 pm PST */
23 #if defined(mips) && defined(SYSTYPE_BSD) && defined(sony_news)
24 /* sony RISC NEWS, NEWSOS 4 */
26 /* typedef long ptrdiff_t; -- necessary on some really old systems */
29 #if defined(mips) && defined(SYSTYPE_BSD43)
35 # include <sys/types.h>
36 # include <sys/time.h>
37 # include <sys/resource.h>
45 typedef GC_signed_word signed_word
;
48 # include "gcconfig.h"
59 typedef char * ptr_t
; /* A generic pointer to which we can add */
60 /* byte displacements. */
61 /* Preferably identical to caddr_t, if it */
66 # if !(defined( sony_news ) )
69 # define VOLATILE volatile
77 #define CONST GC_CONST
79 #if 0 /* was once defined for AMIGA */
86 /*********************************/
88 /* Definitions for conservative */
91 /*********************************/
93 /*********************************/
95 /* Easily changeable parameters */
97 /*********************************/
99 #define STUBBORN_ALLOC /* Define stubborn allocation primitives */
100 #if defined(SRC_M3) || defined(SMALL_CONFIG)
101 # undef STUBBORN_ALLOC
105 /* #define ALL_INTERIOR_POINTERS */
106 /* Forces all pointers into the interior of an */
107 /* object to be considered valid. Also causes the */
108 /* sizes of all objects to be inflated by at least */
109 /* one byte. This should suffice to guarantee */
110 /* that in the presence of a compiler that does */
111 /* not perform garbage-collector-unsafe */
112 /* optimizations, all portable, strictly ANSI */
113 /* conforming C programs should be safely usable */
114 /* with malloc replaced by GC_malloc and free */
115 /* calls removed. There are several disadvantages: */
116 /* 1. There are probably no interesting, portable, */
117 /* strictly ANSI conforming C programs. */
118 /* 2. This option makes it hard for the collector */
119 /* to allocate space that is not ``pointed to'' */
120 /* by integers, etc. Under SunOS 4.X with a */
121 /* statically linked libc, we empiricaly */
122 /* observed that it would be difficult to */
123 /* allocate individual objects larger than 100K. */
124 /* Even if only smaller objects are allocated, */
125 /* more swap space is likely to be needed. */
126 /* Fortunately, much of this will never be */
128 /* If you can easily avoid using this option, do. */
129 /* If not, try to keep individual objects small. */
131 #define PRINTSTATS /* Print garbage collection statistics */
132 /* For less verbose output, undefine in reclaim.c */
134 #define PRINTTIMES /* Print the amount of time consumed by each garbage */
137 #define PRINTBLOCKS /* Print object sizes associated with heap blocks, */
138 /* whether the objects are atomic or composite, and */
139 /* whether or not the block was found to be empty */
140 /* during the reclaim phase. Typically generates */
141 /* about one screenful per garbage collection. */
156 #if defined(PRINTSTATS) && !defined(GATHERSTATS)
160 #ifdef FINALIZE_ON_DEMAND
161 # define GC_INVOKE_FINALIZERS()
163 # define GC_INVOKE_FINALIZERS() (void)GC_invoke_finalizers()
166 #define MERGE_SIZES /* Round up some object sizes, so that fewer distinct */
167 /* free lists are actually maintained. This applies */
168 /* only to the top level routines in misc.c, not to */
169 /* user generated code that calls GC_allocobj and */
170 /* GC_allocaobj directly. */
171 /* Slows down average programs slightly. May however */
172 /* substantially reduce fragmentation if allocation */
173 /* request sizes are widely scattered. */
174 /* May save significant amounts of space for obj_map */
177 /* ALIGN_DOUBLE requires MERGE_SIZES at present. */
178 # if defined(ALIGN_DOUBLE) && !defined(MERGE_SIZES)
182 #if defined(ALL_INTERIOR_POINTERS) && !defined(DONT_ADD_BYTE_AT_END)
183 # define ADD_BYTE_AT_END
187 # ifndef LARGE_CONFIG
188 # define MINHINCR 16 /* Minimum heap increment, in blocks of HBLKSIZE */
189 /* Must be multiple of largest page size. */
190 # define MAXHINCR 512 /* Maximum heap increment, in blocks */
193 # define MAXHINCR 4096
196 # define TIME_LIMIT 50 /* We try to keep pause times from exceeding */
197 /* this by much. In milliseconds. */
199 # define BL_LIMIT GC_black_list_spacing
200 /* If we need a block of N bytes, and we have */
201 /* a block of N + BL_LIMIT bytes available, */
202 /* and N > BL_LIMIT, */
203 /* but all possible positions in it are */
204 /* blacklisted, we just use it anyway (and */
205 /* print a warning, if warnings are enabled). */
206 /* This risks subsequently leaking the block */
207 /* due to a false reference. But not using */
208 /* the block risks unreasonable immediate */
211 /*********************************/
213 /* Stack saving for debugging */
215 /*********************************/
217 #ifdef SAVE_CALL_CHAIN
220 * Number of frames and arguments to save in objects allocated by
221 * debugging allocator.
223 # define NFRAMES 6 /* Number of frames to save. Even for */
224 /* alignment reasons. */
225 # define NARGS 2 /* Mumber of arguments to save for each call. */
227 # define NEED_CALLINFO
229 /* Fill in the pc and argument information for up to NFRAMES of my */
230 /* callers. Ignore my frame and my callers frame. */
231 void GC_save_callers (/* struct callinfo info[NFRAMES] */);
233 void GC_print_callers (/* struct callinfo info[NFRAMES] */);
237 # ifdef GC_ADD_CALLER
240 # define NEED_CALLINFO
249 word ci_arg
[NARGS
]; /* bit-wise complement to avoid retention */
251 # if defined(ALIGN_DOUBLE) && (NFRAMES * (NARGS + 1)) % 2 == 1
252 /* Likely alignment problem. */
259 /*********************************/
261 /* OS interface routines */
263 /*********************************/
269 # define CLOCK_TYPE struct timeval
270 # define GET_TIME(x) { struct rusage rusage; \
271 getrusage (RUSAGE_SELF, &rusage); \
272 x = rusage.ru_utime; }
273 # define MS_TIME_DIFF(a,b) ((double) (a.tv_sec - b.tv_sec) * 1000.0 \
274 + (double) (a.tv_usec - b.tv_usec) / 1000.0)
275 #else /* !BSD_TIME */
277 # include <windows.h>
278 # include <winbase.h>
279 # define CLOCK_TYPE DWORD
280 # define GET_TIME(x) x = GetTickCount()
281 # define MS_TIME_DIFF(a,b) ((long)((a)-(b)))
282 # else /* !MSWIN32, !BSD_TIME */
284 # if !defined(__STDC__) && defined(SPARC) && defined(SUNOS4)
285 clock_t clock(); /* Not in time.h, where it belongs */
287 # if defined(FREEBSD) && !defined(CLOCKS_PER_SEC)
288 # include <machine/limits.h>
289 # define CLOCKS_PER_SEC CLK_TCK
291 # if !defined(CLOCKS_PER_SEC)
292 # define CLOCKS_PER_SEC 1000000
294 * This is technically a bug in the implementation. ANSI requires that
295 * CLOCKS_PER_SEC be defined. But at least under SunOS4.1.1, it isn't.
296 * Also note that the combination of ANSI C and POSIX is incredibly gross
297 * here. The type clock_t is used by both clock() and times(). But on
298 * some machines these use different notions of a clock tick, CLOCKS_PER_SEC
299 * seems to apply only to clock. Hence we use it here. On many machines,
300 * including SunOS, clock actually uses units of microseconds (which are
301 * not really clock ticks).
304 # define CLOCK_TYPE clock_t
305 # define GET_TIME(x) x = clock()
306 # define MS_TIME_DIFF(a,b) ((unsigned long) \
307 (1000.0*(double)((a)-(b))/(double)CLOCKS_PER_SEC))
308 # endif /* !MSWIN32 */
309 #endif /* !BSD_TIME */
311 /* We use bzero and bcopy internally. They may not be available. */
312 # if defined(SPARC) && defined(SUNOS4)
313 # define BCOPY_EXISTS
315 # if defined(M68K) && defined(AMIGA)
316 # define BCOPY_EXISTS
318 # if defined(M68K) && defined(NEXT)
319 # define BCOPY_EXISTS
322 # define BCOPY_EXISTS
326 # define BCOPY_EXISTS
329 # ifndef BCOPY_EXISTS
331 # define BCOPY(x,y,n) memcpy(y, x, (size_t)(n))
332 # define BZERO(x,n) memset(x, 0, (size_t)(n))
334 # define BCOPY(x,y,n) bcopy((char *)(x),(char *)(y),(int)(n))
335 # define BZERO(x,n) bzero((char *)(x),(int)(n))
338 /* HBLKSIZE aligned allocation. 0 is taken to mean failure */
339 /* space is assumed to be cleared. */
340 /* In the case os USE_MMAP, the argument must also be a */
341 /* physical page size. */
342 /* GET_MEM is currently not assumed to retrieve 0 filled space, */
343 /* though we should perhaps take advantage of the case in which */
346 char * real_malloc();
347 # define GET_MEM(bytes) HBLKPTR(real_malloc((size_t)bytes + GC_page_size) \
351 void * os2_alloc(size_t bytes
);
352 # define GET_MEM(bytes) HBLKPTR((ptr_t)os2_alloc((size_t)bytes \
356 # if defined(AMIGA) || defined(NEXT) || defined(MACOSX) || defined(DOS4GW)
357 # define GET_MEM(bytes) HBLKPTR((size_t) \
358 calloc(1, (size_t)bytes + GC_page_size) \
362 extern ptr_t
GC_win32_get_mem();
363 # define GET_MEM(bytes) (struct hblk *)GC_win32_get_mem(bytes)
366 # if defined(USE_TEMPORARY_MEMORY)
367 extern Ptr
GC_MacTemporaryNewPtr(size_t size
,
368 Boolean clearMemory
);
369 # define GET_MEM(bytes) HBLKPTR( \
370 GC_MacTemporaryNewPtr(bytes + GC_page_size, true) \
373 # define GET_MEM(bytes) HBLKPTR( \
374 NewPtrClear(bytes + GC_page_size) + GC_page_size-1)
377 extern ptr_t
GC_unix_get_mem();
378 # define GET_MEM(bytes) (struct hblk *)GC_unix_get_mem(bytes)
386 * Mutual exclusion between allocator/collector routines.
387 * Needed if there is more than one allocator thread.
388 * FASTLOCK() is assumed to try to acquire the lock in a cheap and
389 * dirty way that is acceptable for a few instructions, e.g. by
390 * inhibiting preemption. This is assumed to have succeeded only
391 * if a subsequent call to FASTLOCK_SUCCEEDED() returns TRUE.
392 * FASTUNLOCK() is called whether or not FASTLOCK_SUCCEEDED().
393 * If signals cannot be tolerated with the FASTLOCK held, then
394 * FASTLOCK should disable signals. The code executed under
395 * FASTLOCK is otherwise immune to interruption, provided it is
397 * DCL_LOCK_STATE declares any local variables needed by LOCK and UNLOCK
398 * and/or DISABLE_SIGNALS and ENABLE_SIGNALS and/or FASTLOCK.
399 * (There is currently no equivalent for FASTLOCK.)
402 # ifdef PCR_OBSOLETE /* Faster, but broken with multiple lwp's */
403 # include "th/PCR_Th.h"
404 # include "th/PCR_ThCrSec.h"
405 extern struct PCR_Th_MLRep GC_allocate_ml
;
406 # define DCL_LOCK_STATE PCR_sigset_t GC_old_sig_mask
407 # define LOCK() PCR_Th_ML_Acquire(&GC_allocate_ml)
408 # define UNLOCK() PCR_Th_ML_Release(&GC_allocate_ml)
409 # define FASTLOCK() PCR_ThCrSec_EnterSys()
410 /* Here we cheat (a lot): */
411 # define FASTLOCK_SUCCEEDED() (*(int *)(&GC_allocate_ml) == 0)
412 /* TRUE if nobody currently holds the lock */
413 # define FASTUNLOCK() PCR_ThCrSec_ExitSys()
416 # include <base/PCR_Base.h>
417 # include <th/PCR_Th.h>
418 extern PCR_Th_ML GC_allocate_ml
;
419 # define DCL_LOCK_STATE \
420 PCR_ERes GC_fastLockRes; PCR_sigset_t GC_old_sig_mask
421 # define LOCK() PCR_Th_ML_Acquire(&GC_allocate_ml)
422 # define UNLOCK() PCR_Th_ML_Release(&GC_allocate_ml)
423 # define FASTLOCK() (GC_fastLockRes = PCR_Th_ML_Try(&GC_allocate_ml))
424 # define FASTLOCK_SUCCEEDED() (GC_fastLockRes == PCR_ERes_okay)
425 # define FASTUNLOCK() {\
426 if( FASTLOCK_SUCCEEDED() ) PCR_Th_ML_Release(&GC_allocate_ml); }
429 extern word RT0u__inCritical
;
430 # define LOCK() RT0u__inCritical++
431 # define UNLOCK() RT0u__inCritical--
433 # ifdef SOLARIS_THREADS
436 extern mutex_t GC_allocate_ml
;
437 # define LOCK() mutex_lock(&GC_allocate_ml);
438 # define UNLOCK() mutex_unlock(&GC_allocate_ml);
440 # if defined(LINUX_THREADS)
441 # if defined(I386)|| defined(POWERPC) || defined(ALPHA) || defined(IA64) \
442 || defined(M68K) || defined(SPARC)
443 # include <pthread.h>
444 # define USE_SPIN_LOCK
446 inline static int GC_test_and_set(volatile unsigned int *addr
) {
448 /* Note: the "xchg" instruction does not need a "lock" prefix */
449 __asm__
__volatile__("xchgl %0, %1"
450 : "=r"(oldval
), "=m"(*(addr
))
451 : "0"(1), "m"(*(addr
)));
456 inline static int GC_test_and_set(volatile unsigned int *addr
) {
458 __asm__
__volatile__("xchg4 %0=%1,%2"
459 : "=r"(oldval
), "=m"(*addr
)
460 : "r"(1), "1"(*addr
));
463 inline static void GC_clear(volatile unsigned int *addr
) {
464 __asm__
__volatile__("st4.rel %0=r0" : "=m" (*addr
));
466 # define GC_CLEAR_DEFINED
469 inline static int GC_test_and_set(volatile unsigned int *addr
) {
472 __asm__
__volatile__("ldstub %1,%0"
473 : "=r"(oldval
), "=m"(*addr
)
479 /* Contributed by Tony Mantler. I'm not sure how well it was */
481 inline static int GC_test_and_set(volatile unsigned int *addr
) {
482 char oldval
; /* this must be no longer than 8 bits */
484 /* The return value is semi-phony. */
485 /* 'tas' sets bit 7 while the return */
486 /* value pretends bit 0 was set */
487 __asm__
__volatile__(
488 "tas %1@; sne %0; negb %0"
494 # if defined(POWERPC)
495 inline static int GC_test_and_set(volatile unsigned int *addr
) {
497 int temp
= 1; // locked value
499 __asm__
__volatile__(
500 "1:\tlwarx %0,0,%3\n" // load and reserve
501 "\tcmpwi %0, 0\n" // if load is
502 "\tbne 2f\n" // non-zero, return already set
503 "\tstwcx. %2,0,%1\n" // else store conditional
504 "\tbne- 1b\n" // retry if lost reservation
505 "2:\t\n" // oldval is zero if we set
506 : "=&r"(oldval
), "=p"(addr
)
507 : "r"(temp
), "1"(addr
)
511 inline static void GC_clear(volatile unsigned int *addr
) {
512 __asm__
__volatile__("eieio");
515 # define GC_CLEAR_DEFINED
518 inline static int GC_test_and_set(volatile unsigned int * addr
)
520 unsigned long oldvalue
;
523 __asm__
__volatile__(
532 ".section .text2,\"ax\"\n"
535 :"=&r" (temp
), "=m" (*addr
), "=&r" (oldvalue
)
536 :"Ir" (1), "m" (*addr
));
540 /* Should probably also define GC_clear, since it needs */
541 /* a memory barrier ?? */
544 inline static int GC_test_and_set(volatile unsigned int *addr
) {
546 /* SWP on ARM is very similar to XCHG on x86. Doesn't lock the
547 * bus because there are no SMP ARM machines. If/when there are,
548 * this code will likely need to be updated. */
549 /* See linuxthreads/sysdeps/arm/pt-machine.h in glibc-2.1 */
550 __asm__
__volatile__("swp %0, %1, [%2]"
552 : "r"(1), "r"(addr
));
556 # ifndef GC_CLEAR_DEFINED
557 inline static void GC_clear(volatile unsigned int *addr
) {
558 /* Try to discourage gcc from moving anything past this. */
559 __asm__
__volatile__(" ");
564 extern volatile unsigned int GC_allocate_lock
;
565 extern pthread_t GC_lock_holder
;
566 extern void GC_lock(void);
567 /* Allocation lock holder. Only set if acquired by client through */
568 /* GC_call_with_alloc_lock. */
569 # define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
570 # define NO_THREAD (pthread_t)(-1)
571 # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
572 # define I_HOLD_LOCK() (pthread_equal(GC_lock_holder, pthread_self()))
574 { if (GC_test_and_set(&GC_allocate_lock)) GC_lock(); }
576 GC_clear(&GC_allocate_lock)
577 extern VOLATILE GC_bool GC_collecting
;
578 # define ENTER_GC() \
582 # define EXIT_GC() GC_collecting = 0;
583 # else /* LINUX_THREADS on hardware for which we don't know how */
584 /* to do test and set. */
585 # include <pthread.h>
586 extern pthread_mutex_t GC_allocate_ml
;
587 # define LOCK() pthread_mutex_lock(&GC_allocate_ml)
588 # define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
590 # endif /* LINUX_THREADS */
591 # if defined(HPUX_THREADS)
592 # include <pthread.h>
593 extern pthread_mutex_t GC_allocate_ml
;
594 # define LOCK() pthread_mutex_lock(&GC_allocate_ml)
595 # define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
597 # if defined(IRIX_THREADS) || defined(IRIX_JDK_THREADS)
598 /* This may also eventually be appropriate for HPUX_THREADS */
599 # include <pthread.h>
600 # ifndef HPUX_THREADS
601 /* This probably should never be included, but I can't test */
602 /* on Irix anymore. */
606 # ifndef HPUX_THREADS
607 # if __mips < 3 || !(defined (_ABIN32) || defined(_ABI64)) \
608 || !defined(_COMPILER_VERSION) || _COMPILER_VERSION < 700
609 # define GC_test_and_set(addr, v) test_and_set(addr,v)
611 # define GC_test_and_set(addr, v) __test_and_set(addr,v)
614 /* I couldn't find a way to do this inline on HP/UX */
616 extern unsigned long GC_allocate_lock
;
617 /* This is not a mutex because mutexes that obey the (optional) */
618 /* POSIX scheduling rules are subject to convoys in high contention */
619 /* applications. This is basically a spin lock. */
620 extern pthread_t GC_lock_holder
;
621 extern void GC_lock(void);
622 /* Allocation lock holder. Only set if acquired by client through */
623 /* GC_call_with_alloc_lock. */
624 # define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
625 # define NO_THREAD (pthread_t)(-1)
626 # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
627 # define I_HOLD_LOCK() (pthread_equal(GC_lock_holder, pthread_self()))
629 # define LOCK() { if (!GC_test_and_clear(&GC_allocate_lock)) GC_lock(); }
630 /* The following is INCORRECT, since the memory model is too weak. */
631 # define UNLOCK() { GC_noop1(&GC_allocate_lock); \
632 *(volatile unsigned long *)(&GC_allocate_lock) = 1; }
634 # define LOCK() { if (GC_test_and_set(&GC_allocate_lock, 1)) GC_lock(); }
635 # if __mips >= 3 && (defined (_ABIN32) || defined(_ABI64)) \
636 && defined(_COMPILER_VERSION) && _COMPILER_VERSION >= 700
637 # define UNLOCK() __lock_release(&GC_allocate_lock)
639 /* The function call in the following should prevent the */
640 /* compiler from moving assignments to below the UNLOCK. */
641 /* This is probably not necessary for ucode or gcc 2.8. */
642 /* It may be necessary for Ragnarok and future gcc */
644 # define UNLOCK() { GC_noop1(&GC_allocate_lock); \
645 *(volatile unsigned long *)(&GC_allocate_lock) = 0; }
648 extern VOLATILE GC_bool GC_collecting
;
649 # define ENTER_GC() \
653 # define EXIT_GC() GC_collecting = 0;
654 # endif /* IRIX_THREADS || IRIX_JDK_THREADS */
655 # ifdef WIN32_THREADS
656 # include <windows.h>
657 GC_API CRITICAL_SECTION GC_allocate_ml
;
658 # define LOCK() EnterCriticalSection(&GC_allocate_ml);
659 # define UNLOCK() LeaveCriticalSection(&GC_allocate_ml);
661 # ifndef SET_LOCK_HOLDER
662 # define SET_LOCK_HOLDER()
663 # define UNSET_LOCK_HOLDER()
664 # define I_HOLD_LOCK() FALSE
665 /* Used on platforms were locks can be reacquired, */
666 /* so it doesn't matter if we lie. */
672 # ifndef SET_LOCK_HOLDER
673 # define SET_LOCK_HOLDER()
674 # define UNSET_LOCK_HOLDER()
675 # define I_HOLD_LOCK() FALSE
676 /* Used on platforms were locks can be reacquired, */
677 /* so it doesn't matter if we lie. */
684 # ifndef DCL_LOCK_STATE
685 # define DCL_LOCK_STATE
688 # define FASTLOCK() LOCK()
689 # define FASTLOCK_SUCCEEDED() TRUE
690 # define FASTUNLOCK() UNLOCK()
693 /* Delay any interrupts or signals that may abort this thread. Data */
694 /* structures are in a consistent state outside this pair of calls. */
695 /* ANSI C allows both to be empty (though the standard isn't very */
696 /* clear on that point). Standard malloc implementations are usually */
697 /* neither interruptable nor thread-safe, and thus correspond to */
698 /* empty definitions. */
700 # define DISABLE_SIGNALS() \
701 PCR_Th_SetSigMask(PCR_allSigsBlocked,&GC_old_sig_mask)
702 # define ENABLE_SIGNALS() \
703 PCR_Th_SetSigMask(&GC_old_sig_mask, NIL)
705 # if defined(SRC_M3) || defined(AMIGA) || defined(SOLARIS_THREADS) \
706 || defined(MSWIN32) || defined(MACOS) || defined(DJGPP) \
707 || defined(NO_SIGNALS) || defined(IRIX_THREADS) \
708 || defined(IRIX_JDK_THREADS) || defined(LINUX_THREADS)
709 /* Also useful for debugging. */
710 /* Should probably use thr_sigsetmask for SOLARIS_THREADS. */
711 # define DISABLE_SIGNALS()
712 # define ENABLE_SIGNALS()
714 # define DISABLE_SIGNALS() GC_disable_signals()
715 void GC_disable_signals();
716 # define ENABLE_SIGNALS() GC_enable_signals()
717 void GC_enable_signals();
722 * Stop and restart mutator threads.
725 # include "th/PCR_ThCtl.h"
726 # define STOP_WORLD() \
727 PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_stopNormal, \
728 PCR_allSigsBlocked, \
730 # define START_WORLD() \
731 PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_null, \
732 PCR_allSigsBlocked, \
735 # if defined(SOLARIS_THREADS) || defined(WIN32_THREADS) \
736 || defined(IRIX_THREADS) || defined(LINUX_THREADS) \
737 || defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
738 void GC_stop_world();
739 void GC_start_world();
740 # define STOP_WORLD() GC_stop_world()
741 # define START_WORLD() GC_start_world()
743 # define STOP_WORLD()
744 # define START_WORLD()
750 # define ABORT(s) PCR_Base_Panic(s)
753 # define ABORT(msg) abort();
755 GC_API
void GC_abort();
756 # define ABORT(msg) GC_abort(msg);
760 /* Exit abnormally, but without making a mess (e.g. out of memory) */
762 # define EXIT() PCR_Base_Exit(1,PCR_waitForever)
764 # define EXIT() (void)exit(1)
767 /* Print warning message, e.g. almost out of memory. */
768 # define WARN(msg,arg) (*GC_current_warn_proc)(msg, (GC_word)(arg))
769 extern GC_warn_proc GC_current_warn_proc
;
771 /*********************************/
773 /* Word-size-dependent defines */
775 /*********************************/
778 # define WORDS_TO_BYTES(x) ((x)<<2)
779 # define BYTES_TO_WORDS(x) ((x)>>2)
780 # define LOGWL ((word)5) /* log[2] of CPP_WORDSZ */
781 # define modWORDSZ(n) ((n) & 0x1f) /* n mod size of word */
788 # define WORDS_TO_BYTES(x) ((x)<<3)
789 # define BYTES_TO_WORDS(x) ((x)>>3)
790 # define LOGWL ((word)6) /* log[2] of CPP_WORDSZ */
791 # define modWORDSZ(n) ((n) & 0x3f) /* n mod size of word */
797 #define WORDSZ ((word)CPP_WORDSZ)
798 #define SIGNB ((word)1 << (WORDSZ-1))
799 #define BYTES_PER_WORD ((word)(sizeof (word)))
800 #define ONES ((word)(-1))
801 #define divWORDSZ(n) ((n) >> LOGWL) /* divide n by size of word */
803 /*********************/
805 /* Size Parameters */
807 /*********************/
809 /* heap block size, bytes. Should be power of 2 */
813 # define CPP_LOG_HBLKSIZE 10
815 # if CPP_WORDSZ == 32
816 # define CPP_LOG_HBLKSIZE 12
818 # define CPP_LOG_HBLKSIZE 13
823 # define CPP_LOG_HBLKSIZE 9
825 # if HBLKSIZE == 1024
826 # define CPP_LOG_HBLKSIZE 10
828 # if HBLKSIZE == 2048
829 # define CPP_LOG_HBLKSIZE 11
831 # if HBLKSIZE == 4096
832 # define CPP_LOG_HBLKSIZE 12
834 # if HBLKSIZE == 8192
835 # define CPP_LOG_HBLKSIZE 13
837 # if HBLKSIZE == 16384
838 # define CPP_LOG_HBLKSIZE 14
840 # ifndef CPP_LOG_HBLKSIZE
845 # define CPP_HBLKSIZE (1 << CPP_LOG_HBLKSIZE)
846 # define LOG_HBLKSIZE ((word)CPP_LOG_HBLKSIZE)
847 # define HBLKSIZE ((word)CPP_HBLKSIZE)
850 /* max size objects supported by freelist (larger objects may be */
851 /* allocated, but less efficiently) */
853 #define CPP_MAXOBJSZ BYTES_TO_WORDS(CPP_HBLKSIZE/2)
854 #define MAXOBJSZ ((word)CPP_MAXOBJSZ)
856 # define divHBLKSZ(n) ((n) >> LOG_HBLKSIZE)
858 # define HBLK_PTR_DIFF(p,q) divHBLKSZ((ptr_t)p - (ptr_t)q)
859 /* Equivalent to subtracting 2 hblk pointers. */
860 /* We do it this way because a compiler should */
861 /* find it hard to use an integer division */
862 /* instead of a shift. The bundled SunOS 4.1 */
863 /* o.w. sometimes pessimizes the subtraction to */
864 /* involve a call to .div. */
866 # define modHBLKSZ(n) ((n) & (HBLKSIZE-1))
868 # define HBLKPTR(objptr) ((struct hblk *)(((word) (objptr)) & ~(HBLKSIZE-1)))
870 # define HBLKDISPL(objptr) (((word) (objptr)) & (HBLKSIZE-1))
872 /* Round up byte allocation requests to integral number of words, etc. */
873 # ifdef ADD_BYTE_AT_END
874 # define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + WORDS_TO_BYTES(1))
876 # define ALIGNED_WORDS(n) (BYTES_TO_WORDS((n) + WORDS_TO_BYTES(2)) & ~1)
878 # define ALIGNED_WORDS(n) ROUNDED_UP_WORDS(n)
880 # define SMALL_OBJ(bytes) ((bytes) < WORDS_TO_BYTES(MAXOBJSZ))
881 # define ADD_SLOP(bytes) ((bytes)+1)
883 # define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + (WORDS_TO_BYTES(1) - 1))
885 # define ALIGNED_WORDS(n) \
886 (BYTES_TO_WORDS((n) + WORDS_TO_BYTES(2) - 1) & ~1)
888 # define ALIGNED_WORDS(n) ROUNDED_UP_WORDS(n)
890 # define SMALL_OBJ(bytes) ((bytes) <= WORDS_TO_BYTES(MAXOBJSZ))
891 # define ADD_SLOP(bytes) (bytes)
896 * Hash table representation of sets of pages. This assumes it is
897 * OK to add spurious entries to sets.
898 * Used by black-listing code, and perhaps by dirty bit maintenance code.
902 # define LOG_PHT_ENTRIES 17
904 # define LOG_PHT_ENTRIES 14 /* Collisions are likely if heap grows */
905 /* to more than 16K hblks = 64MB. */
906 /* Each hash table occupies 2K bytes. */
908 # define PHT_ENTRIES ((word)1 << LOG_PHT_ENTRIES)
909 # define PHT_SIZE (PHT_ENTRIES >> LOGWL)
910 typedef word page_hash_table
[PHT_SIZE
];
912 # define PHT_HASH(addr) ((((word)(addr)) >> LOG_HBLKSIZE) & (PHT_ENTRIES - 1))
914 # define get_pht_entry_from_index(bl, index) \
915 (((bl)[divWORDSZ(index)] >> modWORDSZ(index)) & 1)
916 # define set_pht_entry_from_index(bl, index) \
917 (bl)[divWORDSZ(index)] |= (word)1 << modWORDSZ(index)
918 # define clear_pht_entry_from_index(bl, index) \
919 (bl)[divWORDSZ(index)] &= ~((word)1 << modWORDSZ(index))
923 /********************************************/
925 /* H e a p B l o c k s */
927 /********************************************/
929 /* heap block header */
930 #define HBLKMASK (HBLKSIZE-1)
932 #define BITS_PER_HBLK (HBLKSIZE * 8)
934 #define MARK_BITS_PER_HBLK (BITS_PER_HBLK/CPP_WORDSZ)
936 /* We allocate 1 bit/word. Only the first word */
937 /* in each object is actually marked. */
940 # define MARK_BITS_SZ (((MARK_BITS_PER_HBLK + 2*CPP_WORDSZ - 1) \
943 # define MARK_BITS_SZ ((MARK_BITS_PER_HBLK + CPP_WORDSZ - 1)/CPP_WORDSZ)
945 /* Upper bound on number of mark words per heap block */
948 word hb_sz
; /* If in use, size in words, of objects in the block. */
949 /* if free, the size in bytes of the whole block */
950 struct hblk
* hb_next
; /* Link field for hblk free list */
951 /* and for lists of chunks waiting to be */
953 struct hblk
* hb_prev
; /* Backwards link for free list. */
954 word hb_descr
; /* object descriptor for marking. See */
956 char* hb_map
; /* A pointer to a pointer validity map of the block. */
957 /* See GC_obj_map. */
958 /* Valid for all blocks with headers. */
959 /* Free blocks point to GC_invalid_map. */
960 unsigned char hb_obj_kind
;
961 /* Kind of objects in the block. Each kind */
962 /* identifies a mark procedure and a set of */
963 /* list headers. Sometimes called regions. */
964 unsigned char hb_flags
;
965 # define IGNORE_OFF_PAGE 1 /* Ignore pointers that do not */
966 /* point to the first page of */
968 # define WAS_UNMAPPED 2 /* This is a free block, which has */
969 /* been unmapped from the address */
971 /* GC_remap must be invoked on it */
972 /* before it can be reallocated. */
973 /* Only set with USE_MUNMAP. */
974 unsigned short hb_last_reclaimed
;
975 /* Value of GC_gc_no when block was */
976 /* last allocated or swept. May wrap. */
977 /* For a free block, this is maintained */
978 /* unly for USE_MUNMAP, and indicates */
979 /* when the header was allocated, or */
980 /* when the size of the block last */
982 word hb_marks
[MARK_BITS_SZ
];
983 /* Bit i in the array refers to the */
984 /* object starting at the ith word (header */
985 /* INCLUDED) in the heap block. */
986 /* The lsb of word 0 is numbered 0. */
987 /* Unused bits are invalid, and are */
988 /* occasionally set, e.g for uncollectable */
992 /* heap block body */
994 # define DISCARD_WORDS 0
995 /* Number of words to be dropped at the beginning of each block */
996 /* Must be a multiple of WORDSZ. May reasonably be nonzero */
997 /* on machines that don't guarantee longword alignment of */
998 /* pointers, so that the number of false hits is minimized. */
999 /* 0 and WORDSZ are probably the only reasonable values. */
1001 # define BODY_SZ ((HBLKSIZE-WORDS_TO_BYTES(DISCARD_WORDS))/sizeof(word))
1004 # if (DISCARD_WORDS != 0)
1005 word garbage
[DISCARD_WORDS
];
1007 word hb_body
[BODY_SZ
];
1010 # define HDR_WORDS ((word)DISCARD_WORDS)
1011 # define HDR_BYTES ((word)WORDS_TO_BYTES(DISCARD_WORDS))
1013 # define OBJ_SZ_TO_BLOCKS(sz) \
1014 divHBLKSZ(HDR_BYTES + WORDS_TO_BYTES(sz) + HBLKSIZE-1)
1015 /* Size of block (in units of HBLKSIZE) needed to hold objects of */
1016 /* given sz (in words). */
1018 /* Object free list link */
1019 # define obj_link(p) (*(ptr_t *)(p))
1021 /* The type of mark procedures. This really belongs in gc_mark.h. */
1022 /* But we put it here, so that we can avoid scanning the mark proc */
1024 typedef struct ms_entry
* (*mark_proc
)(/* word * addr,
1025 struct ms_entry *mark_stack_ptr,
1026 struct ms_entry *mark_stack_limit,
1028 # define LOG_MAX_MARK_PROCS 6
1029 # define MAX_MARK_PROCS (1 << LOG_MAX_MARK_PROCS)
1031 /* Root sets. Logically private to mark_rts.c. But we don't want the */
1032 /* tables scanned, so we put them here. */
1033 /* MAX_ROOT_SETS is the maximum number of ranges that can be */
1034 /* registered as static roots. */
1035 # ifdef LARGE_CONFIG
1036 # define MAX_ROOT_SETS 4096
1039 # define MAX_ROOT_SETS 1024
1042 # define MAX_ROOT_SETS 512
1043 /* Under NT, we add only written pages, which can result */
1044 /* in many small root sets. */
1046 # define MAX_ROOT_SETS 64
1051 # define MAX_EXCLUSIONS (MAX_ROOT_SETS/4)
1052 /* Maximum number of segments that can be excluded from root sets. */
1055 * Data structure for excluded static roots.
1062 /* Data structure for list of root sets. */
1063 /* We keep a hash table, so that we can filter out duplicate additions. */
1064 /* Under Win32, we need to do a better job of filtering overlaps, so */
1065 /* we resort to sequential search, and pay the price. */
1070 struct roots
* r_next
;
1073 /* Delete before registering new dynamic libraries */
1077 /* Size of hash table index to roots. */
1078 # define LOG_RT_SIZE 6
1079 # define RT_SIZE (1 << LOG_RT_SIZE) /* Power of 2, may be != MAX_ROOT_SETS */
1082 /* Lists of all heap blocks and free lists */
1083 /* as well as other random data structures */
1084 /* that should not be scanned by the */
1086 /* These are grouped together in a struct */
1087 /* so that they can be easily skipped by the */
1088 /* GC_mark routine. */
1089 /* The ordering is weird to make GC_malloc */
1090 /* faster by keeping the important fields */
1091 /* sufficiently close together that a */
1092 /* single load of a base register will do. */
1093 /* Scalars that could easily appear to */
1094 /* be pointers are also put here. */
1095 /* The main fields should precede any */
1096 /* conditionally included fields, so that */
1097 /* gc_inl.h will work even if a different set */
1098 /* of macros is defined when the client is */
1104 word _requested_heapsize
; /* Heap size due to explicit expansion */
1105 ptr_t _last_heap_addr
;
1106 ptr_t _prev_heap_addr
;
1107 word _large_free_bytes
;
1108 /* Total bytes contained in blocks on large object free */
1110 word _words_allocd_before_gc
;
1111 /* Number of words allocated before this */
1112 /* collection cycle. */
1114 /* Number of words allocated during this collection cycle */
1116 /* Number of words wasted due to internal fragmentation */
1117 /* in large objects, or due to dropping blacklisted */
1118 /* blocks, since last gc. Approximate. */
1119 word _words_finalized
;
1120 /* Approximate number of words in objects (and headers) */
1121 /* That became ready for finalization in the last */
1123 word _non_gc_bytes_at_gc
;
1124 /* Number of explicitly managed bytes of storage */
1125 /* at last collection. */
1127 /* Number of explicitly deallocated words of memory */
1128 /* since last collection. */
1129 ptr_t _scratch_end_ptr
;
1130 ptr_t _scratch_last_end_ptr
;
1131 /* Used by headers.c, and can easily appear to point to */
1133 mark_proc _mark_procs
[MAX_MARK_PROCS
];
1134 /* Table of user-defined mark procedures. There is */
1135 /* a small number of these, which can be referenced */
1136 /* by DS_PROC mark descriptors. See gc_mark.h. */
1137 ptr_t _objfreelist
[MAXOBJSZ
+1];
1138 /* free list for objects */
1139 ptr_t _aobjfreelist
[MAXOBJSZ
+1];
1140 /* free list for atomic objs */
1142 ptr_t _uobjfreelist
[MAXOBJSZ
+1];
1143 /* uncollectable but traced objs */
1144 /* objects on this and auobjfreelist */
1145 /* are always marked, except during */
1146 /* garbage collections. */
1147 # ifdef ATOMIC_UNCOLLECTABLE
1148 ptr_t _auobjfreelist
[MAXOBJSZ
+1];
1150 /* uncollectable but traced objs */
1153 word _composite_in_use
;
1154 /* Number of words in accessible composite */
1156 word _atomic_in_use
;
1157 /* Number of words in accessible atomic */
1161 word _unmapped_bytes
;
1164 unsigned _size_map
[WORDS_TO_BYTES(MAXOBJSZ
+1)];
1165 /* Number of words to allocate for a given allocation request in */
1169 # ifdef STUBBORN_ALLOC
1170 ptr_t _sobjfreelist
[MAXOBJSZ
+1];
1172 /* free list for immutable objects */
1173 ptr_t _obj_map
[MAXOBJSZ
+1];
1174 /* If not NIL, then a pointer to a map of valid */
1175 /* object addresses. _obj_map[sz][i] is j if the */
1176 /* address block_start+i is a valid pointer */
1177 /* to an object at */
1178 /* block_start+i&~3 - WORDS_TO_BYTES(j). */
1179 /* (If ALL_INTERIOR_POINTERS is defined, then */
1180 /* instead ((short *)(hb_map[sz])[i] is j if */
1181 /* block_start+WORDS_TO_BYTES(i) is in the */
1182 /* interior of an object starting at */
1183 /* block_start+WORDS_TO_BYTES(i-j)). */
1184 /* It is OBJ_INVALID if */
1185 /* block_start+WORDS_TO_BYTES(i) is not */
1186 /* valid as a pointer to an object. */
1187 /* We assume all values of j <= OBJ_INVALID. */
1188 /* The zeroth entry corresponds to large objects.*/
1189 # ifdef ALL_INTERIOR_POINTERS
1190 # define map_entry_type short
1191 # define OBJ_INVALID 0x7fff
1192 # define MAP_ENTRY(map, bytes) \
1193 (((map_entry_type *)(map))[BYTES_TO_WORDS(bytes)])
1194 # define MAP_ENTRIES BYTES_TO_WORDS(HBLKSIZE)
1195 # define MAP_SIZE (MAP_ENTRIES * sizeof(map_entry_type))
1196 # define OFFSET_VALID(displ) TRUE
1197 # define CPP_MAX_OFFSET (HBLKSIZE - HDR_BYTES - 1)
1198 # define MAX_OFFSET ((word)CPP_MAX_OFFSET)
1200 # define map_entry_type char
1201 # define OBJ_INVALID 0x7f
1202 # define MAP_ENTRY(map, bytes) \
1204 # define MAP_ENTRIES HBLKSIZE
1205 # define MAP_SIZE MAP_ENTRIES
1206 # define CPP_MAX_OFFSET (WORDS_TO_BYTES(OBJ_INVALID) - 1)
1207 # define MAX_OFFSET ((word)CPP_MAX_OFFSET)
1208 # define VALID_OFFSET_SZ \
1209 (CPP_MAX_OFFSET > WORDS_TO_BYTES(CPP_MAXOBJSZ)? \
1211 : WORDS_TO_BYTES(CPP_MAXOBJSZ)+1)
1212 char _valid_offsets
[VALID_OFFSET_SZ
];
1213 /* GC_valid_offsets[i] == TRUE ==> i */
1214 /* is registered as a displacement. */
1215 # define OFFSET_VALID(displ) GC_valid_offsets[displ]
1216 char _modws_valid_offsets
[sizeof(word
)];
1217 /* GC_valid_offsets[i] ==> */
1218 /* GC_modws_valid_offsets[i%sizeof(word)] */
1220 # ifdef STUBBORN_ALLOC
1221 page_hash_table _changed_pages
;
1222 /* Stubborn object pages that were changes since last call to */
1223 /* GC_read_changed. */
1224 page_hash_table _prev_changed_pages
;
1225 /* Stubborn object pages that were changes before last call to */
1226 /* GC_read_changed. */
1228 # if defined(PROC_VDB) || defined(MPROTECT_VDB)
1229 page_hash_table _grungy_pages
; /* Pages that were dirty at last */
1230 /* GC_read_dirty. */
1232 # ifdef MPROTECT_VDB
1233 VOLATILE page_hash_table _dirty_pages
;
1234 /* Pages dirtied since last GC_read_dirty. */
1237 page_hash_table _written_pages
; /* Pages ever dirtied */
1239 # ifdef LARGE_CONFIG
1240 # if CPP_WORDSZ > 32
1241 # define MAX_HEAP_SECTS 4096 /* overflows at roughly 64 GB */
1243 # define MAX_HEAP_SECTS 768 /* Separately added heap sections. */
1246 # define MAX_HEAP_SECTS 256
1249 ptr_t hs_start
; word hs_bytes
;
1250 } _heap_sects
[MAX_HEAP_SECTS
];
1252 ptr_t _heap_bases
[MAX_HEAP_SECTS
];
1253 /* Start address of memory regions obtained from kernel. */
1255 struct roots _static_roots
[MAX_ROOT_SETS
];
1257 struct roots
* _root_index
[RT_SIZE
];
1259 struct exclusion _excl_table
[MAX_EXCLUSIONS
];
1260 /* Block header index; see gc_headers.h */
1261 bottom_index
* _all_nils
;
1262 bottom_index
* _top_index
[TOP_SZ
];
1263 #ifdef SAVE_CALL_CHAIN
1264 struct callinfo _last_stack
[NFRAMES
]; /* Stack at last garbage collection.*/
1265 /* Useful for debugging mysterious */
1266 /* object disappearances. */
1267 /* In the multithreaded case, we */
1268 /* currently only save the calling */
1273 GC_API GC_FAR
struct _GC_arrays GC_arrays
;
1275 # define GC_objfreelist GC_arrays._objfreelist
1276 # define GC_aobjfreelist GC_arrays._aobjfreelist
1277 # define GC_uobjfreelist GC_arrays._uobjfreelist
1278 # ifdef ATOMIC_UNCOLLECTABLE
1279 # define GC_auobjfreelist GC_arrays._auobjfreelist
1281 # define GC_sobjfreelist GC_arrays._sobjfreelist
1282 # define GC_valid_offsets GC_arrays._valid_offsets
1283 # define GC_modws_valid_offsets GC_arrays._modws_valid_offsets
1284 # ifdef STUBBORN_ALLOC
1285 # define GC_changed_pages GC_arrays._changed_pages
1286 # define GC_prev_changed_pages GC_arrays._prev_changed_pages
1288 # define GC_obj_map GC_arrays._obj_map
1289 # define GC_last_heap_addr GC_arrays._last_heap_addr
1290 # define GC_prev_heap_addr GC_arrays._prev_heap_addr
1291 # define GC_words_allocd GC_arrays._words_allocd
1292 # define GC_words_wasted GC_arrays._words_wasted
1293 # define GC_large_free_bytes GC_arrays._large_free_bytes
1294 # define GC_words_finalized GC_arrays._words_finalized
1295 # define GC_non_gc_bytes_at_gc GC_arrays._non_gc_bytes_at_gc
1296 # define GC_mem_freed GC_arrays._mem_freed
1297 # define GC_scratch_end_ptr GC_arrays._scratch_end_ptr
1298 # define GC_scratch_last_end_ptr GC_arrays._scratch_last_end_ptr
1299 # define GC_mark_procs GC_arrays._mark_procs
1300 # define GC_heapsize GC_arrays._heapsize
1301 # define GC_max_heapsize GC_arrays._max_heapsize
1302 # define GC_requested_heapsize GC_arrays._requested_heapsize
1303 # define GC_words_allocd_before_gc GC_arrays._words_allocd_before_gc
1304 # define GC_heap_sects GC_arrays._heap_sects
1305 # define GC_last_stack GC_arrays._last_stack
1307 # define GC_unmapped_bytes GC_arrays._unmapped_bytes
1310 # define GC_heap_bases GC_arrays._heap_bases
1312 # define GC_static_roots GC_arrays._static_roots
1313 # define GC_root_index GC_arrays._root_index
1314 # define GC_excl_table GC_arrays._excl_table
1315 # define GC_all_nils GC_arrays._all_nils
1316 # define GC_top_index GC_arrays._top_index
1317 # if defined(PROC_VDB) || defined(MPROTECT_VDB)
1318 # define GC_grungy_pages GC_arrays._grungy_pages
1320 # ifdef MPROTECT_VDB
1321 # define GC_dirty_pages GC_arrays._dirty_pages
1324 # define GC_written_pages GC_arrays._written_pages
1327 # define GC_composite_in_use GC_arrays._composite_in_use
1328 # define GC_atomic_in_use GC_arrays._atomic_in_use
1331 # define GC_size_map GC_arrays._size_map
1334 # define beginGC_arrays ((ptr_t)(&GC_arrays))
1335 # define endGC_arrays (((ptr_t)(&GC_arrays)) + (sizeof GC_arrays))
1337 #define USED_HEAP_SIZE (GC_heapsize - GC_large_free_bytes)
1340 # define MAXOBJKINDS 16
1342 extern struct obj_kind
{
1343 ptr_t
*ok_freelist
; /* Array of free listheaders for this kind of object */
1344 /* Point either to GC_arrays or to storage allocated */
1345 /* with GC_scratch_alloc. */
1346 struct hblk
**ok_reclaim_list
;
1347 /* List headers for lists of blocks waiting to be */
1349 word ok_descriptor
; /* Descriptor template for objects in this */
1351 GC_bool ok_relocate_descr
;
1352 /* Add object size in bytes to descriptor */
1353 /* template to obtain descriptor. Otherwise */
1354 /* template is used as is. */
1355 GC_bool ok_init
; /* Clear objects before putting them on the free list. */
1356 } GC_obj_kinds
[MAXOBJKINDS
];
1358 # define endGC_obj_kinds (((ptr_t)(&GC_obj_kinds)) + (sizeof GC_obj_kinds))
1360 # define end_gc_area ((ptr_t)endGC_arrays == (ptr_t)(&GC_obj_kinds) ? \
1361 endGC_obj_kinds : endGC_arrays)
1363 /* Predefined kinds: */
1366 # define UNCOLLECTABLE 2
1367 # ifdef ATOMIC_UNCOLLECTABLE
1368 # define AUNCOLLECTABLE 3
1370 # define IS_UNCOLLECTABLE(k) (((k) & ~1) == UNCOLLECTABLE)
1373 # define IS_UNCOLLECTABLE(k) ((k) == UNCOLLECTABLE)
1376 extern int GC_n_kinds
;
1378 GC_API word GC_fo_entries
;
1380 extern word GC_n_heap_sects
; /* Number of separately added heap */
1383 extern word GC_page_size
;
1386 extern word GC_n_heap_bases
; /* See GC_heap_bases. */
1389 extern word GC_total_stack_black_listed
;
1390 /* Number of bytes on stack blacklist. */
1392 extern word GC_black_list_spacing
;
1393 /* Average number of bytes between blacklisted */
1394 /* blocks. Approximate. */
1395 /* Counts only blocks that are */
1396 /* "stack-blacklisted", i.e. that are */
1397 /* problematic in the interior of an object. */
1399 extern char * GC_invalid_map
;
1400 /* Pointer to the nowhere valid hblk map */
1401 /* Blocks pointing to this map are free. */
1403 extern struct hblk
* GC_hblkfreelist
[];
1404 /* List of completely empty heap blocks */
1405 /* Linked through hb_next field of */
1406 /* header structure associated with */
1409 extern GC_bool GC_is_initialized
; /* GC_init() has been run. */
1411 extern GC_bool GC_objects_are_marked
; /* There are marked objects in */
1414 #ifndef SMALL_CONFIG
1415 extern GC_bool GC_incremental
;
1416 /* Using incremental/generational collection. */
1418 # define GC_incremental FALSE
1419 /* Hopefully allow optimizer to remove some code. */
1422 extern GC_bool GC_dirty_maintained
;
1423 /* Dirty bits are being maintained, */
1424 /* either for incremental collection, */
1425 /* or to limit the root set. */
1427 extern word GC_root_size
; /* Total size of registered root sections */
1429 extern GC_bool GC_debugging_started
; /* GC_debug_malloc has been called. */
1431 extern ptr_t GC_least_plausible_heap_addr
;
1432 extern ptr_t GC_greatest_plausible_heap_addr
;
1433 /* Bounds on the heap. Guaranteed valid */
1434 /* Likely to include future heap expansion. */
1438 # define abs(x) ((x) < 0? (-(x)) : (x))
1442 /* Marks are in a reserved area in */
1443 /* each heap block. Each word has one mark bit associated */
1444 /* with it. Only those corresponding to the beginning of an */
1445 /* object are used. */
1448 /* Mark bit operations */
1451 * Retrieve, set, clear the mark bit corresponding
1452 * to the nth word in a given heap block.
1454 * (Recall that bit n corresponds to object beginning at word n
1455 * relative to the beginning of the block, including unused words)
1458 # define mark_bit_from_hdr(hhdr,n) (((hhdr)->hb_marks[divWORDSZ(n)] \
1459 >> (modWORDSZ(n))) & (word)1)
1460 # define set_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1461 |= (word)1 << modWORDSZ(n)
1463 # define clear_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1464 &= ~((word)1 << modWORDSZ(n))
1466 /* Important internal collector routines */
1468 ptr_t
GC_approx_sp();
1470 GC_bool
GC_should_collect();
1472 void GC_apply_to_all_blocks(/*fn, client_data*/);
1473 /* Invoke fn(hbp, client_data) for each */
1474 /* allocated heap block. */
1475 struct hblk
* GC_next_used_block(/* struct hblk * h */);
1476 /* Return first in-use block >= h */
1477 struct hblk
* GC_prev_block(/* struct hblk * h */);
1478 /* Return last block <= h. Returned block */
1479 /* is managed by GC, but may or may not be in */
1481 void GC_mark_init();
1482 void GC_clear_marks(); /* Clear mark bits for all heap objects. */
1483 void GC_invalidate_mark_state(); /* Tell the marker that marked */
1484 /* objects may point to unmarked */
1485 /* ones, and roots may point to */
1486 /* unmarked objects. */
1487 /* Reset mark stack. */
1488 void GC_mark_from_mark_stack(); /* Mark from everything on the mark stack. */
1489 /* Return after about one pages worth of */
1491 GC_bool
GC_mark_stack_empty();
1492 GC_bool
GC_mark_some(/* cold_gc_frame */);
1493 /* Perform about one pages worth of marking */
1494 /* work of whatever kind is needed. Returns */
1495 /* quickly if no collection is in progress. */
1496 /* Return TRUE if mark phase finished. */
1497 void GC_initiate_gc(); /* initiate collection. */
1498 /* If the mark state is invalid, this */
1499 /* becomes full colleection. Otherwise */
1501 void GC_push_all(/*b,t*/); /* Push everything in a range */
1502 /* onto mark stack. */
1503 void GC_push_dirty(/*b,t*/); /* Push all possibly changed */
1504 /* subintervals of [b,t) onto */
1506 #ifndef SMALL_CONFIG
1507 void GC_push_conditional(/* ptr_t b, ptr_t t, GC_bool all*/);
1509 # define GC_push_conditional(b, t, all) GC_push_all(b, t)
1511 /* Do either of the above, depending */
1512 /* on the third arg. */
1513 void GC_push_all_stack(/*b,t*/); /* As above, but consider */
1514 /* interior pointers as valid */
1515 void GC_push_all_eager(/*b,t*/); /* Same as GC_push_all_stack, but */
1516 /* ensures that stack is scanned */
1517 /* immediately, not just scheduled */
1520 void GC_push_all_stack_partially_eager(/* bottom, top, cold_gc_frame */);
1521 /* Similar to GC_push_all_eager, but only the */
1522 /* part hotter than cold_gc_frame is scanned */
1523 /* immediately. Needed to endure that callee- */
1524 /* save registers are not missed. */
1526 /* In the threads case, we push part of the current thread stack */
1527 /* with GC_push_all_eager when we push the registers. This gets the */
1528 /* callee-save registers that may disappear. The remainder of the */
1529 /* stacks are scheduled for scanning in *GC_push_other_roots, which */
1530 /* is thread-package-specific. */
1532 void GC_push_current_stack(/* ptr_t cold_gc_frame */);
1533 /* Push enough of the current stack eagerly to */
1534 /* ensure that callee-save registers saved in */
1535 /* GC frames are scanned. */
1536 /* In the non-threads case, schedule entire */
1537 /* stack for scanning. */
1538 void GC_push_roots(/* GC_bool all, ptr_t cold_gc_frame */);
1539 /* Push all or dirty roots. */
1540 extern void (*GC_push_other_roots
)();
1541 /* Push system or application specific roots */
1542 /* onto the mark stack. In some environments */
1543 /* (e.g. threads environments) this is */
1544 /* predfined to be non-zero. A client supplied */
1545 /* replacement should also call the original */
1547 extern void (*GC_start_call_back
)(/* void */);
1548 /* Called at start of full collections. */
1549 /* Not called if 0. Called with allocation */
1552 void GC_push_regs(); /* Push register contents onto mark stack. */
1553 /* If NURSERY is defined, the default push */
1554 /* action can be overridden with GC_push_proc */
1555 void GC_remark(); /* Mark from all marked objects. Used */
1556 /* only if we had to drop something. */
1559 extern void (*GC_push_proc
)(ptr_t
);
1561 # if defined(MSWIN32)
1562 void __cdecl
GC_push_one();
1564 void GC_push_one(/*p*/); /* If p points to an object, mark it */
1565 /* and push contents on the mark stack */
1567 void GC_push_one_checked(/*p*/); /* Ditto, omits plausibility test */
1568 void GC_push_marked(/* struct hblk h, hdr * hhdr */);
1569 /* Push contents of all marked objects in h onto */
1572 # define GC_push_next_marked_dirty(h) GC_push_next_marked(h)
1574 struct hblk
* GC_push_next_marked_dirty(/* h */);
1575 /* Invoke GC_push_marked on next dirty block above h. */
1576 /* Return a pointer just past the end of this block. */
1577 #endif /* !SMALL_CONFIG */
1578 struct hblk
* GC_push_next_marked(/* h */);
1579 /* Ditto, but also mark from clean pages. */
1580 struct hblk
* GC_push_next_marked_uncollectable(/* h */);
1581 /* Ditto, but mark only from uncollectable pages. */
1582 GC_bool
GC_stopped_mark(); /* Stop world and mark from all roots */
1584 void GC_clear_hdr_marks(/* hhdr */); /* Clear the mark bits in a header */
1585 void GC_set_hdr_marks(/* hhdr */); /* Set the mark bits in a header */
1586 void GC_add_roots_inner();
1587 GC_bool
GC_is_static_root(/* ptr_t p */);
1588 /* Is the address p in one of the registered static */
1589 /* root sections? */
1590 void GC_register_dynamic_libraries();
1591 /* Add dynamic library data sections to the root set. */
1593 /* Machine dependent startup routines */
1594 ptr_t
GC_get_stack_base();
1595 void GC_register_data_segments();
1597 /* Black listing: */
1599 # ifndef ALL_INTERIOR_POINTERS
1600 void GC_add_to_black_list_normal(word
/* bits, maybe source */);
1601 /* Register bits as a possible future false */
1602 /* reference from the heap or static data */
1603 # ifdef PRINT_BLACK_LIST
1604 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1605 GC_add_to_black_list_normal((word) bits, source)
1607 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1608 GC_add_to_black_list_normal((word) bits)
1611 # ifdef PRINT_BLACK_LIST
1612 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1613 GC_add_to_black_list_stack((word) bits, source)
1615 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1616 GC_add_to_black_list_stack((word) bits)
1620 void GC_add_to_black_list_stack(/* bits, maybe source */);
1621 struct hblk
* GC_is_black_listed(/* h, len */);
1622 /* If there are likely to be false references */
1623 /* to a block starting at h of the indicated */
1624 /* length, then return the next plausible */
1625 /* starting location for h that might avoid */
1626 /* these false references. */
1627 void GC_promote_black_lists();
1628 /* Declare an end to a black listing phase. */
1629 void GC_unpromote_black_lists();
1630 /* Approximately undo the effect of the above. */
1631 /* This actually loses some information, but */
1632 /* only in a reasonably safe way. */
1633 word
GC_number_stack_black_listed(/*struct hblk *start, struct hblk *endp1 */);
1634 /* Return the number of (stack) blacklisted */
1635 /* blocks in the range for statistical */
1638 ptr_t
GC_scratch_alloc(/*bytes*/);
1639 /* GC internal memory allocation for */
1640 /* small objects. Deallocation is not */
1643 /* Heap block layout maps: */
1644 void GC_invalidate_map(/* hdr */);
1645 /* Remove the object map associated */
1646 /* with the block. This identifies */
1647 /* the block as invalid to the mark */
1649 GC_bool
GC_add_map_entry(/*sz*/);
1650 /* Add a heap block map for objects of */
1651 /* size sz to obj_map. */
1652 /* Return FALSE on failure. */
1653 void GC_register_displacement_inner(/*offset*/);
1654 /* Version of GC_register_displacement */
1655 /* that assumes lock is already held */
1656 /* and signals are already disabled. */
1658 /* hblk allocation: */
1659 void GC_new_hblk(/*size_in_words, kind*/);
1660 /* Allocate a new heap block, and build */
1661 /* a free list in it. */
1662 struct hblk
* GC_allochblk(/*size_in_words, kind*/);
1663 /* Allocate a heap block, clear it if */
1664 /* for composite objects, inform */
1665 /* the marker that block is valid */
1666 /* for objects of indicated size. */
1667 /* sz < 0 ==> atomic. */
1668 void GC_freehblk(); /* Deallocate a heap block and mark it */
1672 void GC_init_inner();
1673 GC_bool
GC_expand_hp_inner();
1674 void GC_start_reclaim(/*abort_if_found*/);
1675 /* Restore unmarked objects to free */
1676 /* lists, or (if abort_if_found is */
1677 /* TRUE) report them. */
1678 /* Sweeping of small object pages is */
1679 /* largely deferred. */
1680 void GC_continue_reclaim(/*size, kind*/);
1681 /* Sweep pages of the given size and */
1682 /* kind, as long as possible, and */
1683 /* as long as the corr. free list is */
1685 void GC_reclaim_or_delete_all();
1686 /* Arrange for all reclaim lists to be */
1687 /* empty. Judiciously choose between */
1688 /* sweeping and discarding each page. */
1689 GC_bool
GC_reclaim_all(/* GC_stop_func f*/);
1690 /* Reclaim all blocks. Abort (in a */
1691 /* consistent state) if f returns TRUE. */
1692 GC_bool
GC_block_empty(/* hhdr */); /* Block completely unmarked? */
1693 GC_bool
GC_never_stop_func(); /* Returns FALSE. */
1694 GC_bool
GC_try_to_collect_inner(/* GC_stop_func f */);
1695 /* Collect; caller must have acquired */
1696 /* lock and disabled signals. */
1697 /* Collection is aborted if f returns */
1698 /* TRUE. Returns TRUE if it completes */
1700 # define GC_gcollect_inner() \
1701 (void) GC_try_to_collect_inner(GC_never_stop_func)
1702 void GC_finish_collection(); /* Finish collection. Mark bits are */
1703 /* consistent and lock is still held. */
1704 GC_bool
GC_collect_or_expand(/* needed_blocks */);
1705 /* Collect or expand heap in an attempt */
1706 /* make the indicated number of free */
1707 /* blocks available. Should be called */
1708 /* until the blocks are available or */
1709 /* until it fails by returning FALSE. */
1710 GC_API
void GC_init(); /* Initialize collector. */
1711 void GC_collect_a_little_inner(/* int n */);
1712 /* Do n units worth of garbage */
1713 /* collection work, if appropriate. */
1714 /* A unit is an amount appropriate for */
1715 /* HBLKSIZE bytes of allocation. */
1716 ptr_t
GC_generic_malloc(word bytes
, int kind
);
1717 /* Allocate an object of the given */
1718 /* kind. By default, there are only */
1719 /* a few kinds: composite(pointerfree), */
1720 /* atomic, uncollectable, etc. */
1721 /* We claim it's possible for clever */
1722 /* client code that understands GC */
1723 /* internals to add more, e.g. to */
1724 /* communicate object layout info */
1725 /* to the collector. */
1726 ptr_t
GC_generic_malloc_ignore_off_page(/* bytes, kind */);
1727 /* As above, but pointers past the */
1728 /* first page of the resulting object */
1730 ptr_t
GC_generic_malloc_inner(word bytes
, int kind
);
1731 /* Ditto, but I already hold lock, etc. */
1732 ptr_t GC_generic_malloc_words_small
GC_PROTO((size_t words
, int kind
));
1733 /* As above, but size in units of words */
1734 /* Bypasses MERGE_SIZES. Assumes */
1735 /* words <= MAXOBJSZ. */
1736 ptr_t
GC_generic_malloc_inner_ignore_off_page(/* bytes, kind */);
1737 /* Allocate an object, where */
1738 /* the client guarantees that there */
1739 /* will always be a pointer to the */
1740 /* beginning of the object while the */
1741 /* object is live. */
1742 ptr_t
GC_allocobj(/* sz_inn_words, kind */);
1743 /* Make the indicated */
1744 /* free list nonempty, and return its */
1747 void GC_init_headers();
1748 struct hblkhdr
* GC_install_header(/*h*/);
1749 /* Install a header for block h. */
1750 /* Return 0 on failure, or the header */
1752 GC_bool
GC_install_counts(/*h, sz*/);
1753 /* Set up forwarding counts for block */
1755 /* Return FALSE on failure. */
1756 void GC_remove_header(/*h*/);
1757 /* Remove the header for block h. */
1758 void GC_remove_counts(/*h, sz*/);
1759 /* Remove forwarding counts for h. */
1760 hdr
* GC_find_header(ptr_t
/*p*/); /* Debugging only. */
1762 void GC_finalize(); /* Perform all indicated finalization actions */
1763 /* on unmarked objects. */
1764 /* Unreachable finalizable objects are enqueued */
1765 /* for processing by GC_invoke_finalizers. */
1766 /* Invoked with lock. */
1768 void GC_add_to_heap(/*p, bytes*/);
1769 /* Add a HBLKSIZE aligned chunk to the heap. */
1771 void GC_print_obj(/* ptr_t p */);
1772 /* P points to somewhere inside an object with */
1773 /* debugging info. Print a human readable */
1774 /* description of the object to stderr. */
1775 extern void (*GC_check_heap
)();
1776 /* Check that all objects in the heap with */
1777 /* debugging info are intact. Print */
1778 /* descriptions of any that are not. */
1779 extern void (*GC_print_heap_obj
)(/* ptr_t p */);
1780 /* If possible print s followed by a more */
1781 /* detailed description of the object */
1782 /* referred to by p. */
1784 /* Memory unmapping: */
1786 void GC_unmap_old(void);
1787 void GC_merge_unmapped(void);
1788 void GC_unmap(ptr_t start
, word bytes
);
1789 void GC_remap(ptr_t start
, word bytes
);
1790 void GC_unmap_gap(ptr_t start1
, word bytes1
, ptr_t start2
, word bytes2
);
1793 /* Virtual dirty bit implementation: */
1794 /* Each implementation exports the following: */
1795 void GC_read_dirty(); /* Retrieve dirty bits. */
1796 GC_bool
GC_page_was_dirty(/* struct hblk * h */);
1797 /* Read retrieved dirty bits. */
1798 GC_bool
GC_page_was_ever_dirty(/* struct hblk * h */);
1799 /* Could the page contain valid heap pointers? */
1800 void GC_is_fresh(/* struct hblk * h, word number_of_blocks */);
1801 /* Assert the region currently contains no */
1802 /* valid pointers. */
1803 void GC_write_hint(/* struct hblk * h */);
1804 /* h is about to be written. */
1805 void GC_dirty_init();
1807 /* Slow/general mark bit manipulation: */
1808 GC_API GC_bool
GC_is_marked();
1809 void GC_clear_mark_bit();
1810 void GC_set_mark_bit();
1812 /* Stubborn objects: */
1813 void GC_read_changed(); /* Analogous to GC_read_dirty */
1814 GC_bool
GC_page_was_changed(/* h */); /* Analogous to GC_page_was_dirty */
1815 void GC_clean_changing_list(); /* Collect obsolete changing list entries */
1816 void GC_stubborn_init();
1818 /* Debugging print routines: */
1819 void GC_print_block_list();
1820 void GC_print_hblkfreelist();
1821 void GC_print_heap_sects();
1822 void GC_print_static_roots();
1825 #ifdef KEEP_BACK_PTRS
1826 void GC_store_back_pointer(ptr_t source
, ptr_t dest
);
1827 void GC_marked_for_finalization(ptr_t dest
);
1828 # define GC_STORE_BACK_PTR(source, dest) GC_store_back_pointer(source, dest)
1829 # define GC_MARKED_FOR_FINALIZATION(dest) GC_marked_for_finalization(dest)
1831 # define GC_STORE_BACK_PTR(source, dest)
1832 # define GC_MARKED_FOR_FINALIZATION(dest)
1835 /* Make arguments appear live to compiler */
1837 void GC_noop(void*, ...);
1839 GC_API
void GC_noop();
1842 void GC_noop1(/* word arg */);
1844 /* Logging and diagnostic output: */
1845 GC_API
void GC_printf
GC_PROTO((char * format
, long, long, long, long, long, long));
1846 /* A version of printf that doesn't allocate, */
1847 /* is restricted to long arguments, and */
1848 /* (unfortunately) doesn't use varargs for */
1849 /* portability. Restricted to 6 args and */
1850 /* 1K total output length. */
1851 /* (We use sprintf. Hopefully that doesn't */
1852 /* allocate for long arguments.) */
1853 # define GC_printf0(f) GC_printf(f, 0l, 0l, 0l, 0l, 0l, 0l)
1854 # define GC_printf1(f,a) GC_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1855 # define GC_printf2(f,a,b) GC_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1856 # define GC_printf3(f,a,b,c) GC_printf(f, (long)a, (long)b, (long)c, 0l, 0l, 0l)
1857 # define GC_printf4(f,a,b,c,d) GC_printf(f, (long)a, (long)b, (long)c, \
1859 # define GC_printf5(f,a,b,c,d,e) GC_printf(f, (long)a, (long)b, (long)c, \
1860 (long)d, (long)e, 0l)
1861 # define GC_printf6(f,a,b,c,d,e,g) GC_printf(f, (long)a, (long)b, (long)c, \
1862 (long)d, (long)e, (long)g)
1864 void GC_err_printf(/* format, a, b, c, d, e, f */);
1865 # define GC_err_printf0(f) GC_err_puts(f)
1866 # define GC_err_printf1(f,a) GC_err_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1867 # define GC_err_printf2(f,a,b) GC_err_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1868 # define GC_err_printf3(f,a,b,c) GC_err_printf(f, (long)a, (long)b, (long)c, \
1870 # define GC_err_printf4(f,a,b,c,d) GC_err_printf(f, (long)a, (long)b, \
1871 (long)c, (long)d, 0l, 0l)
1872 # define GC_err_printf5(f,a,b,c,d,e) GC_err_printf(f, (long)a, (long)b, \
1875 # define GC_err_printf6(f,a,b,c,d,e,g) GC_err_printf(f, (long)a, (long)b, \
1878 /* Ditto, writes to stderr. */
1880 void GC_err_puts(/* char *s */);
1881 /* Write s to stderr, don't buffer, don't add */
1882 /* newlines, don't ... */
1885 # ifdef GC_ASSERTIONS
1886 # define GC_ASSERT(expr) if(!(expr)) {\
1887 GC_err_printf2("Assertion failure: %s:%ld\n", \
1888 __FILE__, (unsigned long)__LINE__); \
1889 ABORT("assertion failure"); }
1891 # define GC_ASSERT(expr)
1894 # endif /* GC_PRIVATE_H */