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 */
20 #if defined(mips) && defined(SYSTYPE_BSD) && defined(sony_news)
21 /* sony RISC NEWS, NEWSOS 4 */
23 /* typedef long ptrdiff_t; -- necessary on some really old systems */
26 #if defined(mips) && defined(SYSTYPE_BSD43)
32 # include <sys/types.h>
33 # include <sys/time.h>
34 # include <sys/resource.h>
42 typedef GC_signed_word signed_word;
56 typedef char * ptr_t; /* A generic pointer to which we can add */
57 /* byte displacements. */
58 /* Preferably identical to caddr_t, if it */
63 # if !(defined( sony_news ) )
66 # define VOLATILE volatile
82 /*********************************/
84 /* Definitions for conservative */
87 /*********************************/
89 /*********************************/
91 /* Easily changeable parameters */
93 /*********************************/
95 #define STUBBORN_ALLOC /* Define stubborn allocation primitives */
96 #if defined(SRC_M3) || defined(SMALL_CONFIG)
97 # undef STUBBORN_ALLOC
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 */
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 */
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. */
152 #if defined(PRINTSTATS) && !defined(GATHERSTATS)
156 #ifdef FINALIZE_ON_DEMAND
157 # define GC_INVOKE_FINALIZERS()
159 # define GC_INVOKE_FINALIZERS() (void)GC_invoke_finalizers()
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 */
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 */
182 /* ALIGN_DOUBLE requires MERGE_SIZES at present. */
183 # if defined(ALIGN_DOUBLE) && !defined(MERGE_SIZES)
187 #if defined(ALL_INTERIOR_POINTERS) && !defined(DONT_ADD_BYTE_AT_END)
188 # define ADD_BYTE_AT_END
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 */
198 # define MAXHINCR 4096
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 */
216 /*********************************/
218 /* Stack saving for debugging */
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] */);
242 # ifdef GC_ADD_CALLER
245 # define NEED_CALLINFO
254 word ci_arg[NARGS]; /* bit-wise complement to avoid retention */
256 # if defined(ALIGN_DOUBLE) && (NFRAMES * (NARGS + 1)) % 2 == 1
257 /* Likely alignment problem. */
264 /*********************************/
266 /* OS interface routines */
268 /*********************************/
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 */
282 # if !defined(__STDC__) && defined(SPARC) && defined(SUNOS4)
283 clock_t clock(); /* Not in time.h, where it belongs */
285 # if defined(FREEBSD) && !defined(CLOCKS_PER_SEC)
286 # include <machine/limits.h>
287 # define CLOCKS_PER_SEC CLK_TCK
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).
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
312 # if defined(M68K) && defined(AMIGA)
313 # define BCOPY_EXISTS
315 # if defined(M68K) && defined(NEXT)
316 # define BCOPY_EXISTS
319 # define BCOPY_EXISTS
323 # define BCOPY_EXISTS
326 # ifndef BCOPY_EXISTS
328 # define BCOPY(x,y,n) memcpy(y, x, (size_t)(n))
329 # define BZERO(x,n) memset(x, 0, (size_t)(n))
331 # define BCOPY(x,y,n) bcopy((char *)(x),(char *)(y),(int)(n))
332 # define BZERO(x,n) bzero((char *)(x),(int)(n))
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. */
340 char * real_malloc();
341 # define GET_MEM(bytes) HBLKPTR(real_malloc((size_t)bytes + GC_page_size) \
345 void * os2_alloc(size_t bytes);
346 # define GET_MEM(bytes) HBLKPTR((ptr_t)os2_alloc((size_t)bytes \
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) \
356 extern ptr_t GC_win32_get_mem();
357 # define GET_MEM(bytes) (struct hblk *)GC_win32_get_mem(bytes)
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) \
367 # define GET_MEM(bytes) HBLKPTR( \
368 NewPtrClear(bytes + GC_page_size) + GC_page_size-1)
371 extern ptr_t GC_unix_get_mem();
372 # define GET_MEM(bytes) (struct hblk *)GC_unix_get_mem(bytes)
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
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.)
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()
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); }
423 extern word RT0u__inCritical;
424 # define LOCK() RT0u__inCritical++
425 # define UNLOCK() RT0u__inCritical--
427 # ifdef SOLARIS_THREADS
430 extern mutex_t GC_allocate_ml;
431 # define LOCK() mutex_lock(&GC_allocate_ml);
432 # define UNLOCK() mutex_unlock(&GC_allocate_ml);
434 # ifdef LINUX_THREADS
435 # include <pthread.h>
437 inline static int GC_test_and_set(volatile unsigned int *addr) {
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)));
445 inline static void GC_clear(volatile unsigned int *addr) {
448 # elif defined(__alpha__)
449 inline static int GC_test_and_set(volatile unsigned int *addr) {
452 __asm__ __volatile__(
459 : "=&r"(oldval), "=&r"(temp), "=m"(*(addr))
464 inline static void GC_clear(volatile unsigned int *addr) {
465 __asm__ __volatile__("mb": : :"memory");
468 # elif defined(__powerpc__)
469 inline static int GC_test_and_set(volatile unsigned int *addr) {
470 int ret, oldval=0, newval=1;
472 __asm__ __volatile__("sync" : : : "memory");
473 __asm__ __volatile__(
481 : "r"(addr), "r"(newval), "r"(oldval)
483 __asm__ __volatile__("sync" : : : "memory");
486 inline static void GC_clear(volatile unsigned int *addr) {
487 __asm__ __volatile__("sync": : :"memory");
492 -- > Need implementation of GC_test_and_set()
495 extern volatile unsigned int GC_allocate_lock;
496 /* This is not a mutex because mutexes that obey the (optional) */
497 /* POSIX scheduling rules are subject to convoys in high contention */
498 /* applications. This is basically a spin lock. */
499 extern pthread_t GC_lock_holder;
500 extern void GC_lock(void);
501 /* Allocation lock holder. Only set if acquired by client through */
502 /* GC_call_with_alloc_lock. */
503 # define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
504 # define NO_THREAD (pthread_t)(-1)
505 # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
506 # define I_HOLD_LOCK() (pthread_equal(GC_lock_holder, pthread_self()))
508 # define LOCK() pthread_mutex_lock(&GC_allocate_ml)
509 # define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
512 { if (GC_test_and_set(&GC_allocate_lock)) GC_lock(); }
514 GC_clear(&GC_allocate_lock)
516 extern GC_bool GC_collecting;
517 # define ENTER_GC() \
521 # define EXIT_GC() GC_collecting = 0;
522 # endif /* LINUX_THREADS */
524 # include <pthread.h>
527 # if __mips < 3 || !(defined (_ABIN32) || defined(_ABI64))
528 # define GC_test_and_set(addr, v) test_and_set(addr,v)
530 # define GC_test_and_set(addr, v) __test_and_set(addr,v)
532 extern unsigned long GC_allocate_lock;
533 /* This is not a mutex because mutexes that obey the (optional) */
534 /* POSIX scheduling rules are subject to convoys in high contention */
535 /* applications. This is basically a spin lock. */
536 extern pthread_t GC_lock_holder;
537 extern void GC_lock(void);
538 /* Allocation lock holder. Only set if acquired by client through */
539 /* GC_call_with_alloc_lock. */
540 # define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
541 # define NO_THREAD (pthread_t)(-1)
542 # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
543 # define I_HOLD_LOCK() (pthread_equal(GC_lock_holder, pthread_self()))
545 # define LOCK() pthread_mutex_lock(&GC_allocate_ml)
546 # define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
548 # define LOCK() { if (GC_test_and_set(&GC_allocate_lock, 1)) GC_lock(); }
549 # if __mips >= 3 && (defined (_ABIN32) || defined(_ABI64))
550 # define UNLOCK() __lock_release(&GC_allocate_lock)
552 # define UNLOCK() GC_allocate_lock = 0
555 extern GC_bool GC_collecting;
556 # define ENTER_GC() \
560 # define EXIT_GC() GC_collecting = 0;
561 # endif /* IRIX_THREADS */
562 # ifdef WIN32_THREADS
563 # include <windows.h>
564 GC_API CRITICAL_SECTION GC_allocate_ml;
565 # define LOCK() EnterCriticalSection(&GC_allocate_ml);
566 # define UNLOCK() LeaveCriticalSection(&GC_allocate_ml);
568 # ifdef QUICK_THREADS
572 # ifndef SET_LOCK_HOLDER
573 # define SET_LOCK_HOLDER()
574 # define UNSET_LOCK_HOLDER()
575 # define I_HOLD_LOCK() FALSE
576 /* Used on platforms were locks can be reacquired, */
577 /* so it doesn't matter if we lie. */
583 # ifndef SET_LOCK_HOLDER
584 # define SET_LOCK_HOLDER()
585 # define UNSET_LOCK_HOLDER()
586 # define I_HOLD_LOCK() FALSE
587 /* Used on platforms were locks can be reacquired, */
588 /* so it doesn't matter if we lie. */
595 # ifndef DCL_LOCK_STATE
596 # define DCL_LOCK_STATE
599 # define FASTLOCK() LOCK()
600 # define FASTLOCK_SUCCEEDED() TRUE
601 # define FASTUNLOCK() UNLOCK()
604 /* Delay any interrupts or signals that may abort this thread. Data */
605 /* structures are in a consistent state outside this pair of calls. */
606 /* ANSI C allows both to be empty (though the standard isn't very */
607 /* clear on that point). Standard malloc implementations are usually */
608 /* neither interruptable nor thread-safe, and thus correspond to */
609 /* empty definitions. */
611 # define DISABLE_SIGNALS() \
612 PCR_Th_SetSigMask(PCR_allSigsBlocked,&GC_old_sig_mask)
613 # define ENABLE_SIGNALS() \
614 PCR_Th_SetSigMask(&GC_old_sig_mask, NIL)
616 # if defined(SRC_M3) || defined(AMIGA) || defined(SOLARIS_THREADS) \
617 || defined(MSWIN32) || defined(MACOS) || defined(DJGPP) \
618 || defined(NO_SIGNALS) || defined(IRIX_THREADS) \
619 || defined(LINUX_THREADS)
620 /* Also useful for debugging. */
621 /* Should probably use thr_sigsetmask for SOLARIS_THREADS. */
622 # define DISABLE_SIGNALS()
623 # define ENABLE_SIGNALS()
625 # define DISABLE_SIGNALS() GC_disable_signals()
626 void GC_disable_signals();
627 # define ENABLE_SIGNALS() GC_enable_signals()
628 void GC_enable_signals();
633 * Stop and restart mutator threads.
636 # include "th/PCR_ThCtl.h"
637 # define STOP_WORLD() \
638 PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_stopNormal, \
639 PCR_allSigsBlocked, \
641 # define START_WORLD() \
642 PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_null, \
643 PCR_allSigsBlocked, \
646 # if defined(SOLARIS_THREADS) || defined(WIN32_THREADS) \
647 || defined(IRIX_THREADS) || defined(LINUX_THREADS)
648 void GC_stop_world();
649 void GC_start_world();
650 # define STOP_WORLD() GC_stop_world()
651 # define START_WORLD() GC_start_world()
653 # define STOP_WORLD()
654 # define START_WORLD()
660 # define ABORT(s) PCR_Base_Panic(s)
663 # define ABORT(msg) abort();
665 GC_API void GC_abort();
666 # define ABORT(msg) GC_abort(msg);
670 /* Exit abnormally, but without making a mess (e.g. out of memory) */
672 # define EXIT() PCR_Base_Exit(1,PCR_waitForever)
674 # define EXIT() (void)exit(1)
677 /* Print warning message, e.g. almost out of memory. */
678 # define WARN(msg,arg) (*GC_current_warn_proc)(msg, (GC_word)(arg))
679 extern GC_warn_proc GC_current_warn_proc;
681 /*********************************/
683 /* Word-size-dependent defines */
685 /*********************************/
688 # define WORDS_TO_BYTES(x) ((x)<<2)
689 # define BYTES_TO_WORDS(x) ((x)>>2)
690 # define LOGWL ((word)5) /* log[2] of CPP_WORDSZ */
691 # define modWORDSZ(n) ((n) & 0x1f) /* n mod size of word */
698 # define WORDS_TO_BYTES(x) ((x)<<3)
699 # define BYTES_TO_WORDS(x) ((x)>>3)
700 # define LOGWL ((word)6) /* log[2] of CPP_WORDSZ */
701 # define modWORDSZ(n) ((n) & 0x3f) /* n mod size of word */
707 #define WORDSZ ((word)CPP_WORDSZ)
708 #define SIGNB ((word)1 << (WORDSZ-1))
709 #define BYTES_PER_WORD ((word)(sizeof (word)))
710 #define ONES ((word)(-1))
711 #define divWORDSZ(n) ((n) >> LOGWL) /* divide n by size of word */
713 /*********************/
715 /* Size Parameters */
717 /*********************/
719 /* heap block size, bytes. Should be power of 2 */
723 # define CPP_LOG_HBLKSIZE 10
725 # if CPP_WORDSZ == 32
726 # define CPP_LOG_HBLKSIZE 12
728 # define CPP_LOG_HBLKSIZE 13
733 # define CPP_LOG_HBLKSIZE 9
735 # if HBLKSIZE == 1024
736 # define CPP_LOG_HBLKSIZE 10
738 # if HBLKSIZE == 2048
739 # define CPP_LOG_HBLKSIZE 11
741 # if HBLKSIZE == 4096
742 # define CPP_LOG_HBLKSIZE 12
744 # if HBLKSIZE == 8192
745 # define CPP_LOG_HBLKSIZE 13
747 # if HBLKSIZE == 16384
748 # define CPP_LOG_HBLKSIZE 14
750 # ifndef CPP_LOG_HBLKSIZE
755 # define CPP_HBLKSIZE (1 << CPP_LOG_HBLKSIZE)
756 # define LOG_HBLKSIZE ((word)CPP_LOG_HBLKSIZE)
757 # define HBLKSIZE ((word)CPP_HBLKSIZE)
760 /* max size objects supported by freelist (larger objects may be */
761 /* allocated, but less efficiently) */
763 #define CPP_MAXOBJSZ BYTES_TO_WORDS(CPP_HBLKSIZE/2)
764 #define MAXOBJSZ ((word)CPP_MAXOBJSZ)
766 # define divHBLKSZ(n) ((n) >> LOG_HBLKSIZE)
768 # define HBLK_PTR_DIFF(p,q) divHBLKSZ((ptr_t)p - (ptr_t)q)
769 /* Equivalent to subtracting 2 hblk pointers. */
770 /* We do it this way because a compiler should */
771 /* find it hard to use an integer division */
772 /* instead of a shift. The bundled SunOS 4.1 */
773 /* o.w. sometimes pessimizes the subtraction to */
774 /* involve a call to .div. */
776 # define modHBLKSZ(n) ((n) & (HBLKSIZE-1))
778 # define HBLKPTR(objptr) ((struct hblk *)(((word) (objptr)) & ~(HBLKSIZE-1)))
780 # define HBLKDISPL(objptr) (((word) (objptr)) & (HBLKSIZE-1))
782 /* Round up byte allocation requests to integral number of words, etc. */
783 # ifdef ADD_BYTE_AT_END
784 # define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + WORDS_TO_BYTES(1))
786 # define ALIGNED_WORDS(n) (BYTES_TO_WORDS((n) + WORDS_TO_BYTES(2)) & ~1)
788 # define ALIGNED_WORDS(n) ROUNDED_UP_WORDS(n)
790 # define SMALL_OBJ(bytes) ((bytes) < WORDS_TO_BYTES(MAXOBJSZ))
791 # define ADD_SLOP(bytes) ((bytes)+1)
793 # define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + (WORDS_TO_BYTES(1) - 1))
795 # define ALIGNED_WORDS(n) \
796 (BYTES_TO_WORDS((n) + WORDS_TO_BYTES(2) - 1) & ~1)
798 # define ALIGNED_WORDS(n) ROUNDED_UP_WORDS(n)
800 # define SMALL_OBJ(bytes) ((bytes) <= WORDS_TO_BYTES(MAXOBJSZ))
801 # define ADD_SLOP(bytes) (bytes)
806 * Hash table representation of sets of pages. This assumes it is
807 * OK to add spurious entries to sets.
808 * Used by black-listing code, and perhaps by dirty bit maintenance code.
812 # define LOG_PHT_ENTRIES 17
814 # define LOG_PHT_ENTRIES 14 /* Collisions are likely if heap grows */
815 /* to more than 16K hblks = 64MB. */
816 /* Each hash table occupies 2K bytes. */
818 # define PHT_ENTRIES ((word)1 << LOG_PHT_ENTRIES)
819 # define PHT_SIZE (PHT_ENTRIES >> LOGWL)
820 typedef word page_hash_table[PHT_SIZE];
822 # define PHT_HASH(addr) ((((word)(addr)) >> LOG_HBLKSIZE) & (PHT_ENTRIES - 1))
824 # define get_pht_entry_from_index(bl, index) \
825 (((bl)[divWORDSZ(index)] >> modWORDSZ(index)) & 1)
826 # define set_pht_entry_from_index(bl, index) \
827 (bl)[divWORDSZ(index)] |= (word)1 << modWORDSZ(index)
828 # define clear_pht_entry_from_index(bl, index) \
829 (bl)[divWORDSZ(index)] &= ~((word)1 << modWORDSZ(index))
833 /********************************************/
835 /* H e a p B l o c k s */
837 /********************************************/
839 /* heap block header */
840 #define HBLKMASK (HBLKSIZE-1)
842 #define BITS_PER_HBLK (HBLKSIZE * 8)
844 #define MARK_BITS_PER_HBLK (BITS_PER_HBLK/CPP_WORDSZ)
846 /* We allocate 1 bit/word. Only the first word */
847 /* in each object is actually marked. */
850 # define MARK_BITS_SZ (((MARK_BITS_PER_HBLK + 2*CPP_WORDSZ - 1) \
853 # define MARK_BITS_SZ ((MARK_BITS_PER_HBLK + CPP_WORDSZ - 1)/CPP_WORDSZ)
855 /* Upper bound on number of mark words per heap block */
858 word hb_sz; /* If in use, size in words, of objects in the block. */
859 /* if free, the size in bytes of the whole block */
860 struct hblk * hb_next; /* Link field for hblk free list */
861 /* and for lists of chunks waiting to be */
863 word hb_descr; /* object descriptor for marking. See */
865 char* hb_map; /* A pointer to a pointer validity map of the block. */
866 /* See GC_obj_map. */
867 /* Valid for all blocks with headers. */
868 /* Free blocks point to GC_invalid_map. */
869 unsigned char hb_obj_kind;
870 /* Kind of objects in the block. Each kind */
871 /* identifies a mark procedure and a set of */
872 /* list headers. Sometimes called regions. */
873 unsigned char hb_flags;
874 # define IGNORE_OFF_PAGE 1 /* Ignore pointers that do not */
875 /* point to the first page of */
877 unsigned short hb_last_reclaimed;
878 /* Value of GC_gc_no when block was */
879 /* last allocated or swept. May wrap. */
880 word hb_marks[MARK_BITS_SZ];
881 /* Bit i in the array refers to the */
882 /* object starting at the ith word (header */
883 /* INCLUDED) in the heap block. */
884 /* The lsb of word 0 is numbered 0. */
887 /* heap block body */
889 # define DISCARD_WORDS 0
890 /* Number of words to be dropped at the beginning of each block */
891 /* Must be a multiple of WORDSZ. May reasonably be nonzero */
892 /* on machines that don't guarantee longword alignment of */
893 /* pointers, so that the number of false hits is minimized. */
894 /* 0 and WORDSZ are probably the only reasonable values. */
896 # define BODY_SZ ((HBLKSIZE-WORDS_TO_BYTES(DISCARD_WORDS))/sizeof(word))
899 # if (DISCARD_WORDS != 0)
900 word garbage[DISCARD_WORDS];
902 word hb_body[BODY_SZ];
905 # define HDR_WORDS ((word)DISCARD_WORDS)
906 # define HDR_BYTES ((word)WORDS_TO_BYTES(DISCARD_WORDS))
908 # define OBJ_SZ_TO_BLOCKS(sz) \
909 divHBLKSZ(HDR_BYTES + WORDS_TO_BYTES(sz) + HBLKSIZE-1)
910 /* Size of block (in units of HBLKSIZE) needed to hold objects of */
911 /* given sz (in words). */
913 /* Object free list link */
914 # define obj_link(p) (*(ptr_t *)(p))
916 /* lists of all heap blocks and free lists */
917 /* These are grouped together in a struct */
918 /* so that they can be easily skipped by the */
919 /* GC_mark routine. */
920 /* The ordering is weird to make GC_malloc */
921 /* faster by keeping the important fields */
922 /* sufficiently close together that a */
923 /* single load of a base register will do. */
924 /* Scalars that could easily appear to */
925 /* be pointers are also put here. */
926 /* The main fields should precede any */
927 /* conditionally included fields, so that */
928 /* gc_inl.h will work even if a different set */
929 /* of macros is defined when the client is */
935 ptr_t _last_heap_addr;
936 ptr_t _prev_heap_addr;
937 word _words_allocd_before_gc;
938 /* Number of words allocated before this */
939 /* collection cycle. */
941 /* Number of words allocated during this collection cycle */
943 /* Number of words wasted due to internal fragmentation */
944 /* in large objects, or due to dropping blacklisted */
945 /* blocks, since last gc. Approximate. */
946 word _words_finalized;
947 /* Approximate number of words in objects (and headers) */
948 /* That became ready for finalization in the last */
950 word _non_gc_bytes_at_gc;
951 /* Number of explicitly managed bytes of storage */
952 /* at last collection. */
954 /* Number of explicitly deallocated words of memory */
955 /* since last collection. */
957 ptr_t _objfreelist[MAXOBJSZ+1];
958 /* free list for objects */
959 ptr_t _aobjfreelist[MAXOBJSZ+1];
960 /* free list for atomic objs */
962 ptr_t _uobjfreelist[MAXOBJSZ+1];
963 /* uncollectable but traced objs */
964 /* objects on this and auobjfreelist */
965 /* are always marked, except during */
966 /* garbage collections. */
967 # ifdef ATOMIC_UNCOLLECTABLE
968 ptr_t _auobjfreelist[MAXOBJSZ+1];
970 /* uncollectable but traced objs */
973 word _composite_in_use;
974 /* Number of words in accessible composite */
977 /* Number of words in accessible atomic */
981 unsigned _size_map[WORDS_TO_BYTES(MAXOBJSZ+1)];
982 /* Number of words to allocate for a given allocation request in */
986 # ifdef STUBBORN_ALLOC
987 ptr_t _sobjfreelist[MAXOBJSZ+1];
989 /* free list for immutable objects */
990 ptr_t _obj_map[MAXOBJSZ+1];
991 /* If not NIL, then a pointer to a map of valid */
992 /* object addresses. _obj_map[sz][i] is j if the */
993 /* address block_start+i is a valid pointer */
994 /* to an object at */
995 /* block_start+i&~3 - WORDS_TO_BYTES(j). */
996 /* (If ALL_INTERIOR_POINTERS is defined, then */
997 /* instead ((short *)(hbh_map[sz])[i] is j if */
998 /* block_start+WORDS_TO_BYTES(i) is in the */
999 /* interior of an object starting at */
1000 /* block_start+WORDS_TO_BYTES(i-j)). */
1001 /* It is OBJ_INVALID if */
1002 /* block_start+WORDS_TO_BYTES(i) is not */
1003 /* valid as a pointer to an object. */
1004 /* We assume all values of j <= OBJ_INVALID. */
1005 /* The zeroth entry corresponds to large objects.*/
1006 # ifdef ALL_INTERIOR_POINTERS
1007 # define map_entry_type short
1008 # define OBJ_INVALID 0x7fff
1009 # define MAP_ENTRY(map, bytes) \
1010 (((map_entry_type *)(map))[BYTES_TO_WORDS(bytes)])
1011 # define MAP_ENTRIES BYTES_TO_WORDS(HBLKSIZE)
1012 # define MAP_SIZE (MAP_ENTRIES * sizeof(map_entry_type))
1013 # define OFFSET_VALID(displ) TRUE
1014 # define CPP_MAX_OFFSET (HBLKSIZE - HDR_BYTES - 1)
1015 # define MAX_OFFSET ((word)CPP_MAX_OFFSET)
1017 # define map_entry_type char
1018 # define OBJ_INVALID 0x7f
1019 # define MAP_ENTRY(map, bytes) \
1021 # define MAP_ENTRIES HBLKSIZE
1022 # define MAP_SIZE MAP_ENTRIES
1023 # define CPP_MAX_OFFSET (WORDS_TO_BYTES(OBJ_INVALID) - 1)
1024 # define MAX_OFFSET ((word)CPP_MAX_OFFSET)
1025 # define VALID_OFFSET_SZ \
1026 (CPP_MAX_OFFSET > WORDS_TO_BYTES(CPP_MAXOBJSZ)? \
1028 : WORDS_TO_BYTES(CPP_MAXOBJSZ)+1)
1029 char _valid_offsets[VALID_OFFSET_SZ];
1030 /* GC_valid_offsets[i] == TRUE ==> i */
1031 /* is registered as a displacement. */
1032 # define OFFSET_VALID(displ) GC_valid_offsets[displ]
1033 char _modws_valid_offsets[sizeof(word)];
1034 /* GC_valid_offsets[i] ==> */
1035 /* GC_modws_valid_offsets[i%sizeof(word)] */
1037 # ifdef STUBBORN_ALLOC
1038 page_hash_table _changed_pages;
1039 /* Stubborn object pages that were changes since last call to */
1040 /* GC_read_changed. */
1041 page_hash_table _prev_changed_pages;
1042 /* Stubborn object pages that were changes before last call to */
1043 /* GC_read_changed. */
1045 # if defined(PROC_VDB) || defined(MPROTECT_VDB)
1046 page_hash_table _grungy_pages; /* Pages that were dirty at last */
1047 /* GC_read_dirty. */
1049 # ifdef LARGE_CONFIG
1050 # if CPP_WORDSZ > 32
1051 # define MAX_HEAP_SECTS 4096 /* overflows at roughly 64 GB */
1053 # define MAX_HEAP_SECTS 768 /* Separately added heap sections. */
1056 # define MAX_HEAP_SECTS 256
1059 ptr_t hs_start; word hs_bytes;
1060 } _heap_sects[MAX_HEAP_SECTS];
1062 ptr_t _heap_bases[MAX_HEAP_SECTS];
1063 /* Start address of memory regions obtained from kernel. */
1065 /* Block header index; see gc_headers.h */
1066 bottom_index * _all_nils;
1067 bottom_index * _top_index [TOP_SZ];
1068 #ifdef SAVE_CALL_CHAIN
1069 struct callinfo _last_stack[NFRAMES]; /* Stack at last garbage collection.*/
1070 /* Useful for debugging mysterious */
1071 /* object disappearances. */
1072 /* In the multithreaded case, we */
1073 /* currently only save the calling */
1078 GC_API GC_FAR struct _GC_arrays GC_arrays;
1080 # define GC_objfreelist GC_arrays._objfreelist
1081 # define GC_aobjfreelist GC_arrays._aobjfreelist
1082 # define GC_uobjfreelist GC_arrays._uobjfreelist
1083 # ifdef ATOMIC_UNCOLLECTABLE
1084 # define GC_auobjfreelist GC_arrays._auobjfreelist
1086 # define GC_sobjfreelist GC_arrays._sobjfreelist
1087 # define GC_valid_offsets GC_arrays._valid_offsets
1088 # define GC_modws_valid_offsets GC_arrays._modws_valid_offsets
1089 # ifdef STUBBORN_ALLOC
1090 # define GC_changed_pages GC_arrays._changed_pages
1091 # define GC_prev_changed_pages GC_arrays._prev_changed_pages
1093 # define GC_obj_map GC_arrays._obj_map
1094 # define GC_last_heap_addr GC_arrays._last_heap_addr
1095 # define GC_prev_heap_addr GC_arrays._prev_heap_addr
1096 # define GC_words_allocd GC_arrays._words_allocd
1097 # define GC_words_wasted GC_arrays._words_wasted
1098 # define GC_words_finalized GC_arrays._words_finalized
1099 # define GC_non_gc_bytes_at_gc GC_arrays._non_gc_bytes_at_gc
1100 # define GC_mem_freed GC_arrays._mem_freed
1101 # define GC_heapsize GC_arrays._heapsize
1102 # define GC_max_heapsize GC_arrays._max_heapsize
1103 # define GC_words_allocd_before_gc GC_arrays._words_allocd_before_gc
1104 # define GC_heap_sects GC_arrays._heap_sects
1105 # define GC_last_stack GC_arrays._last_stack
1107 # define GC_heap_bases GC_arrays._heap_bases
1109 # define GC_all_nils GC_arrays._all_nils
1110 # define GC_top_index GC_arrays._top_index
1111 # if defined(PROC_VDB) || defined(MPROTECT_VDB)
1112 # define GC_grungy_pages GC_arrays._grungy_pages
1115 # define GC_composite_in_use GC_arrays._composite_in_use
1116 # define GC_atomic_in_use GC_arrays._atomic_in_use
1119 # define GC_size_map GC_arrays._size_map
1122 # define beginGC_arrays ((ptr_t)(&GC_arrays))
1123 # define endGC_arrays (((ptr_t)(&GC_arrays)) + (sizeof GC_arrays))
1125 GC_API word GC_fo_entries;
1127 # define MAXOBJKINDS 16
1130 extern struct obj_kind {
1131 ptr_t *ok_freelist; /* Array of free listheaders for this kind of object */
1132 /* Point either to GC_arrays or to storage allocated */
1133 /* with GC_scratch_alloc. */
1134 struct hblk **ok_reclaim_list;
1135 /* List headers for lists of blocks waiting to be */
1137 word ok_descriptor; /* Descriptor template for objects in this */
1139 GC_bool ok_relocate_descr;
1140 /* Add object size in bytes to descriptor */
1141 /* template to obtain descriptor. Otherwise */
1142 /* template is used as is. */
1143 GC_bool ok_init; /* Clear objects before putting them on the free list. */
1144 } GC_obj_kinds[MAXOBJKINDS];
1145 /* Predefined kinds: */
1148 # define UNCOLLECTABLE 2
1149 # ifdef ATOMIC_UNCOLLECTABLE
1150 # define AUNCOLLECTABLE 3
1152 # define IS_UNCOLLECTABLE(k) (((k) & ~1) == UNCOLLECTABLE)
1155 # define IS_UNCOLLECTABLE(k) ((k) == UNCOLLECTABLE)
1158 extern int GC_n_kinds;
1160 extern word GC_n_heap_sects; /* Number of separately added heap */
1163 extern word GC_page_size;
1166 extern word GC_n_heap_bases; /* See GC_heap_bases. */
1169 extern word GC_total_stack_black_listed;
1170 /* Number of bytes on stack blacklist. */
1172 extern word GC_black_list_spacing;
1173 /* Average number of bytes between blacklisted */
1174 /* blocks. Approximate. */
1175 /* Counts only blocks that are */
1176 /* "stack-blacklisted", i.e. that are */
1177 /* problematic in the interior of an object. */
1179 extern char * GC_invalid_map;
1180 /* Pointer to the nowhere valid hblk map */
1181 /* Blocks pointing to this map are free. */
1183 extern struct hblk * GC_hblkfreelist;
1184 /* List of completely empty heap blocks */
1185 /* Linked through hb_next field of */
1186 /* header structure associated with */
1189 extern GC_bool GC_is_initialized; /* GC_init() has been run. */
1191 extern GC_bool GC_objects_are_marked; /* There are marked objects in */
1194 extern GC_bool GC_incremental; /* Using incremental/generational collection. */
1196 extern GC_bool GC_dirty_maintained;
1197 /* Dirty bits are being maintained, */
1198 /* either for incremental collection, */
1199 /* or to limit the root set. */
1202 extern ptr_t GC_stackbottom; /* Cool end of user stack */
1205 extern word GC_root_size; /* Total size of registered root sections */
1207 extern GC_bool GC_debugging_started; /* GC_debug_malloc has been called. */
1209 extern ptr_t GC_least_plausible_heap_addr;
1210 extern ptr_t GC_greatest_plausible_heap_addr;
1211 /* Bounds on the heap. Guaranteed valid */
1212 /* Likely to include future heap expansion. */
1216 # define abs(x) ((x) < 0? (-(x)) : (x))
1220 /* Marks are in a reserved area in */
1221 /* each heap block. Each word has one mark bit associated */
1222 /* with it. Only those corresponding to the beginning of an */
1223 /* object are used. */
1226 /* Mark bit operations */
1229 * Retrieve, set, clear the mark bit corresponding
1230 * to the nth word in a given heap block.
1232 * (Recall that bit n corresponds to object beginning at word n
1233 * relative to the beginning of the block, including unused words)
1236 # define mark_bit_from_hdr(hhdr,n) (((hhdr)->hb_marks[divWORDSZ(n)] \
1237 >> (modWORDSZ(n))) & (word)1)
1238 # define set_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1239 |= (word)1 << modWORDSZ(n)
1241 # define clear_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1242 &= ~((word)1 << modWORDSZ(n))
1244 /* Important internal collector routines */
1246 ptr_t GC_approx_sp();
1248 GC_bool GC_should_collect();
1249 #ifdef PRESERVE_LAST
1250 GC_bool GC_in_last_heap_sect(/* ptr_t */);
1251 /* In last added heap section? If so, avoid breaking up. */
1253 void GC_apply_to_all_blocks(/*fn, client_data*/);
1254 /* Invoke fn(hbp, client_data) for each */
1255 /* allocated heap block. */
1256 struct hblk * GC_next_block(/* struct hblk * h */);
1257 void GC_mark_init();
1258 void GC_clear_marks(); /* Clear mark bits for all heap objects. */
1259 void GC_invalidate_mark_state(); /* Tell the marker that marked */
1260 /* objects may point to unmarked */
1261 /* ones, and roots may point to */
1262 /* unmarked objects. */
1263 /* Reset mark stack. */
1264 void GC_mark_from_mark_stack(); /* Mark from everything on the mark stack. */
1265 /* Return after about one pages worth of */
1267 GC_bool GC_mark_stack_empty();
1268 GC_bool GC_mark_some(); /* Perform about one pages worth of marking */
1269 /* work of whatever kind is needed. Returns */
1270 /* quickly if no collection is in progress. */
1271 /* Return TRUE if mark phase finished. */
1272 void GC_initiate_gc(); /* initiate collection. */
1273 /* If the mark state is invalid, this */
1274 /* becomes full colleection. Otherwise */
1276 void GC_push_all(/*b,t*/); /* Push everything in a range */
1277 /* onto mark stack. */
1278 void GC_push_dirty(/*b,t*/); /* Push all possibly changed */
1279 /* subintervals of [b,t) onto */
1281 #ifndef SMALL_CONFIG
1282 void GC_push_conditional(/* ptr_t b, ptr_t t, GC_bool all*/);
1284 # define GC_push_conditional(b, t, all) GC_push_all(b, t)
1286 /* Do either of the above, depending */
1287 /* on the third arg. */
1288 void GC_push_all_stack(/*b,t*/); /* As above, but consider */
1289 /* interior pointers as valid */
1290 void GC_push_roots(/* GC_bool all */); /* Push all or dirty roots. */
1291 extern void (*GC_push_other_roots)();
1292 /* Push system or application specific roots */
1293 /* onto the mark stack. In some environments */
1294 /* (e.g. threads environments) this is */
1295 /* predfined to be non-zero. A client supplied */
1296 /* replacement should also call the original */
1298 extern void (*GC_start_call_back)(/* void */);
1299 /* Called at start of full collections. */
1300 /* Not called if 0. Called with allocation */
1303 void GC_push_regs(); /* Push register contents onto mark stack. */
1304 void GC_remark(); /* Mark from all marked objects. Used */
1305 /* only if we had to drop something. */
1306 # if defined(MSWIN32)
1307 void __cdecl GC_push_one();
1309 void GC_push_one(/*p*/); /* If p points to an object, mark it */
1310 /* and push contents on the mark stack */
1312 void GC_push_one_checked(/*p*/); /* Ditto, omits plausibility test */
1313 void GC_push_marked(/* struct hblk h, hdr * hhdr */);
1314 /* Push contents of all marked objects in h onto */
1317 # define GC_push_next_marked_dirty(h) GC_push_next_marked(h)
1319 struct hblk * GC_push_next_marked_dirty(/* h */);
1320 /* Invoke GC_push_marked on next dirty block above h. */
1321 /* Return a pointer just past the end of this block. */
1322 #endif /* !SMALL_CONFIG */
1323 struct hblk * GC_push_next_marked(/* h */);
1324 /* Ditto, but also mark from clean pages. */
1325 struct hblk * GC_push_next_marked_uncollectable(/* h */);
1326 /* Ditto, but mark only from uncollectable pages. */
1327 GC_bool GC_stopped_mark(); /* Stop world and mark from all roots */
1329 void GC_clear_hdr_marks(/* hhdr */); /* Clear the mark bits in a header */
1330 void GC_set_hdr_marks(/* hhdr */); /* Set the mark bits in a header */
1331 void GC_add_roots_inner();
1332 GC_bool GC_is_static_root(/* ptr_t p */);
1333 /* Is the address p in one of the registered static */
1334 /* root sections? */
1335 void GC_register_dynamic_libraries();
1336 /* Add dynamic library data sections to the root set. */
1338 /* Machine dependent startup routines */
1339 ptr_t GC_get_stack_base();
1340 void GC_register_data_segments();
1342 /* Black listing: */
1344 # ifndef ALL_INTERIOR_POINTERS
1345 void GC_add_to_black_list_normal(/* bits, maybe source */);
1346 /* Register bits as a possible future false */
1347 /* reference from the heap or static data */
1348 # ifdef PRINT_BLACK_LIST
1349 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1350 GC_add_to_black_list_normal(bits, source)
1352 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1353 GC_add_to_black_list_normal(bits)
1356 # ifdef PRINT_BLACK_LIST
1357 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1358 GC_add_to_black_list_stack(bits, source)
1360 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1361 GC_add_to_black_list_stack(bits)
1365 void GC_add_to_black_list_stack(/* bits, maybe source */);
1366 struct hblk * GC_is_black_listed(/* h, len */);
1367 /* If there are likely to be false references */
1368 /* to a block starting at h of the indicated */
1369 /* length, then return the next plausible */
1370 /* starting location for h that might avoid */
1371 /* these false references. */
1372 void GC_promote_black_lists();
1373 /* Declare an end to a black listing phase. */
1374 void GC_unpromote_black_lists();
1375 /* Approximately undo the effect of the above. */
1376 /* This actually loses some information, but */
1377 /* only in a reasonably safe way. */
1378 word GC_number_stack_black_listed(/*struct hblk *start, struct hblk *endp1 */);
1379 /* Return the number of (stack) blacklisted */
1380 /* blocks in the range for statistical */
1383 ptr_t GC_scratch_alloc(/*bytes*/);
1384 /* GC internal memory allocation for */
1385 /* small objects. Deallocation is not */
1388 /* Heap block layout maps: */
1389 void GC_invalidate_map(/* hdr */);
1390 /* Remove the object map associated */
1391 /* with the block. This identifies */
1392 /* the block as invalid to the mark */
1394 GC_bool GC_add_map_entry(/*sz*/);
1395 /* Add a heap block map for objects of */
1396 /* size sz to obj_map. */
1397 /* Return FALSE on failure. */
1398 void GC_register_displacement_inner(/*offset*/);
1399 /* Version of GC_register_displacement */
1400 /* that assumes lock is already held */
1401 /* and signals are already disabled. */
1403 /* hblk allocation: */
1404 void GC_new_hblk(/*size_in_words, kind*/);
1405 /* Allocate a new heap block, and build */
1406 /* a free list in it. */
1407 struct hblk * GC_allochblk(/*size_in_words, kind*/);
1408 /* Allocate a heap block, clear it if */
1409 /* for composite objects, inform */
1410 /* the marker that block is valid */
1411 /* for objects of indicated size. */
1412 /* sz < 0 ==> atomic. */
1413 void GC_freehblk(); /* Deallocate a heap block and mark it */
1417 void GC_init_inner();
1418 GC_bool GC_expand_hp_inner();
1419 void GC_start_reclaim(/*abort_if_found*/);
1420 /* Restore unmarked objects to free */
1421 /* lists, or (if abort_if_found is */
1422 /* TRUE) report them. */
1423 /* Sweeping of small object pages is */
1424 /* largely deferred. */
1425 void GC_continue_reclaim(/*size, kind*/);
1426 /* Sweep pages of the given size and */
1427 /* kind, as long as possible, and */
1428 /* as long as the corr. free list is */
1430 void GC_reclaim_or_delete_all();
1431 /* Arrange for all reclaim lists to be */
1432 /* empty. Judiciously choose between */
1433 /* sweeping and discarding each page. */
1434 GC_bool GC_reclaim_all(/* GC_stop_func f*/);
1435 /* Reclaim all blocks. Abort (in a */
1436 /* consistent state) if f returns TRUE. */
1437 GC_bool GC_block_empty(/* hhdr */); /* Block completely unmarked? */
1438 GC_bool GC_never_stop_func(); /* Returns FALSE. */
1439 GC_bool GC_try_to_collect_inner(/* GC_stop_func f */);
1440 /* Collect; caller must have acquired */
1441 /* lock and disabled signals. */
1442 /* Collection is aborted if f returns */
1443 /* TRUE. Returns TRUE if it completes */
1445 # define GC_gcollect_inner() \
1446 (void) GC_try_to_collect_inner(GC_never_stop_func)
1447 void GC_finish_collection(); /* Finish collection. Mark bits are */
1448 /* consistent and lock is still held. */
1449 GC_bool GC_collect_or_expand(/* needed_blocks */);
1450 /* Collect or expand heap in an attempt */
1451 /* make the indicated number of free */
1452 /* blocks available. Should be called */
1453 /* until the blocks are available or */
1454 /* until it fails by returning FALSE. */
1455 void GC_init(); /* Initialize collector. */
1456 void GC_collect_a_little_inner(/* int n */);
1457 /* Do n units worth of garbage */
1458 /* collection work, if appropriate. */
1459 /* A unit is an amount appropriate for */
1460 /* HBLKSIZE bytes of allocation. */
1461 ptr_t GC_generic_malloc(/* bytes, kind */);
1462 /* Allocate an object of the given */
1463 /* kind. By default, there are only */
1464 /* a few kinds: composite(pointerfree), */
1465 /* atomic, uncollectable, etc. */
1466 /* We claim it's possible for clever */
1467 /* client code that understands GC */
1468 /* internals to add more, e.g. to */
1469 /* communicate object layout info */
1470 /* to the collector. */
1471 ptr_t GC_generic_malloc_ignore_off_page(/* bytes, kind */);
1472 /* As above, but pointers past the */
1473 /* first page of the resulting object */
1475 ptr_t GC_generic_malloc_inner(/* bytes, kind */);
1476 /* Ditto, but I already hold lock, etc. */
1477 ptr_t GC_generic_malloc_words_small GC_PROTO((size_t words, int kind));
1478 /* As above, but size in units of words */
1479 /* Bypasses MERGE_SIZES. Assumes */
1480 /* words <= MAXOBJSZ. */
1481 ptr_t GC_generic_malloc_inner_ignore_off_page(/* bytes, kind */);
1482 /* Allocate an object, where */
1483 /* the client guarantees that there */
1484 /* will always be a pointer to the */
1485 /* beginning of the object while the */
1486 /* object is live. */
1487 ptr_t GC_allocobj(/* sz_inn_words, kind */);
1488 /* Make the indicated */
1489 /* free list nonempty, and return its */
1492 void GC_init_headers();
1493 GC_bool GC_install_header(/*h*/);
1494 /* Install a header for block h. */
1495 /* Return FALSE on failure. */
1496 GC_bool GC_install_counts(/*h, sz*/);
1497 /* Set up forwarding counts for block */
1499 /* Return FALSE on failure. */
1500 void GC_remove_header(/*h*/);
1501 /* Remove the header for block h. */
1502 void GC_remove_counts(/*h, sz*/);
1503 /* Remove forwarding counts for h. */
1504 hdr * GC_find_header(/*p*/); /* Debugging only. */
1506 void GC_finalize(); /* Perform all indicated finalization actions */
1507 /* on unmarked objects. */
1508 /* Unreachable finalizable objects are enqueued */
1509 /* for processing by GC_invoke_finalizers. */
1510 /* Invoked with lock. */
1512 void GC_add_to_heap(/*p, bytes*/);
1513 /* Add a HBLKSIZE aligned chunk to the heap. */
1515 void GC_print_obj(/* ptr_t p */);
1516 /* P points to somewhere inside an object with */
1517 /* debugging info. Print a human readable */
1518 /* description of the object to stderr. */
1519 ptr_t GC_debug_object_start(/* ptr_t p */);
1520 /* P points to the start of an object that may */
1521 /* have debug info at its head. Return the */
1522 /* start of the real data. */
1523 extern void (*GC_check_heap)();
1524 /* Check that all objects in the heap with */
1525 /* debugging info are intact. Print */
1526 /* descriptions of any that are not. */
1527 extern void (*GC_print_heap_obj)(/* ptr_t p */);
1528 /* If possible print s followed by a more */
1529 /* detailed description of the object */
1530 /* referred to by p. */
1532 /* Virtual dirty bit implementation: */
1533 /* Each implementation exports the following: */
1534 void GC_read_dirty(); /* Retrieve dirty bits. */
1535 GC_bool GC_page_was_dirty(/* struct hblk * h */);
1536 /* Read retrieved dirty bits. */
1537 GC_bool GC_page_was_ever_dirty(/* struct hblk * h */);
1538 /* Could the page contain valid heap pointers? */
1539 void GC_is_fresh(/* struct hblk * h, word number_of_blocks */);
1540 /* Assert the region currently contains no */
1541 /* valid pointers. */
1542 void GC_write_hint(/* struct hblk * h */);
1543 /* h is about to be written. */
1544 void GC_dirty_init();
1546 /* Slow/general mark bit manipulation: */
1547 GC_bool GC_is_marked();
1548 void GC_clear_mark_bit();
1549 void GC_set_mark_bit();
1551 /* Stubborn objects: */
1552 void GC_read_changed(); /* Analogous to GC_read_dirty */
1553 GC_bool GC_page_was_changed(/* h */); /* Analogous to GC_page_was_dirty */
1554 void GC_clean_changing_list(); /* Collect obsolete changing list entries */
1555 void GC_stubborn_init();
1557 /* Debugging print routines: */
1558 void GC_print_block_list();
1559 void GC_print_hblkfreelist();
1560 void GC_print_heap_sects();
1561 void GC_print_static_roots();
1564 /* Make arguments appear live to compiler */
1566 void GC_noop(void*, ...);
1568 GC_API void GC_noop();
1571 void GC_noop1(/* word arg */);
1573 /* Logging and diagnostic output: */
1574 GC_API void GC_printf GC_PROTO((char * format, long, long, long, long, long, long));
1575 /* A version of printf that doesn't allocate, */
1576 /* is restricted to long arguments, and */
1577 /* (unfortunately) doesn't use varargs for */
1578 /* portability. Restricted to 6 args and */
1579 /* 1K total output length. */
1580 /* (We use sprintf. Hopefully that doesn't */
1581 /* allocate for long arguments.) */
1582 # define GC_printf0(f) GC_printf(f, 0l, 0l, 0l, 0l, 0l, 0l)
1583 # define GC_printf1(f,a) GC_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1584 # define GC_printf2(f,a,b) GC_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1585 # define GC_printf3(f,a,b,c) GC_printf(f, (long)a, (long)b, (long)c, 0l, 0l, 0l)
1586 # define GC_printf4(f,a,b,c,d) GC_printf(f, (long)a, (long)b, (long)c, \
1588 # define GC_printf5(f,a,b,c,d,e) GC_printf(f, (long)a, (long)b, (long)c, \
1589 (long)d, (long)e, 0l)
1590 # define GC_printf6(f,a,b,c,d,e,g) GC_printf(f, (long)a, (long)b, (long)c, \
1591 (long)d, (long)e, (long)g)
1593 void GC_err_printf(/* format, a, b, c, d, e, f */);
1594 # define GC_err_printf0(f) GC_err_puts(f)
1595 # define GC_err_printf1(f,a) GC_err_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1596 # define GC_err_printf2(f,a,b) GC_err_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1597 # define GC_err_printf3(f,a,b,c) GC_err_printf(f, (long)a, (long)b, (long)c, \
1599 # define GC_err_printf4(f,a,b,c,d) GC_err_printf(f, (long)a, (long)b, \
1600 (long)c, (long)d, 0l, 0l)
1601 # define GC_err_printf5(f,a,b,c,d,e) GC_err_printf(f, (long)a, (long)b, \
1604 # define GC_err_printf6(f,a,b,c,d,e,g) GC_err_printf(f, (long)a, (long)b, \
1607 /* Ditto, writes to stderr. */
1609 void GC_err_puts(/* char *s */);
1610 /* Write s to stderr, don't buffer, don't add */
1611 /* newlines, don't ... */
1614 # endif /* GC_PRIVATE_H */