Annotation of OpenXM_contrib2/asir2000/gc/gc_priv.h, Revision 1.3
1.1 noro 1: /*
2: * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3: * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
1.2 noro 4: * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
5: * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
6: *
1.1 noro 7: *
8: * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9: * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10: *
11: * Permission is hereby granted to use or copy this program
12: * for any purpose, provided the above notices are retained on all copies.
13: * Permission to modify the code and to distribute modified code is granted,
14: * provided the above notices are retained, and a notice that the code was
15: * modified is included with the above copyright notice.
16: */
17: /* Boehm, February 16, 1996 2:30 pm PST */
18:
19:
20: # ifndef GC_PRIVATE_H
21: # define GC_PRIVATE_H
22:
23: #if defined(mips) && defined(SYSTYPE_BSD) && defined(sony_news)
24: /* sony RISC NEWS, NEWSOS 4 */
25: # define BSD_TIME
26: /* typedef long ptrdiff_t; -- necessary on some really old systems */
27: #endif
28:
29: #if defined(mips) && defined(SYSTYPE_BSD43)
30: /* MIPS RISCOS 4 */
31: # define BSD_TIME
32: #endif
33:
34: #ifdef BSD_TIME
35: # include <sys/types.h>
36: # include <sys/time.h>
37: # include <sys/resource.h>
38: #endif /* BSD_TIME */
39:
40: # ifndef GC_H
41: # include "gc.h"
42: # endif
43:
44: typedef GC_word word;
45: typedef GC_signed_word signed_word;
46:
1.3 ! noro 47: # ifndef GCCONFIG_H
1.1 noro 48: # include "gcconfig.h"
49: # endif
50:
51: # ifndef HEADERS_H
52: # include "gc_hdrs.h"
53: # endif
54:
55: typedef int GC_bool;
56: # define TRUE 1
57: # define FALSE 0
58:
59: typedef char * ptr_t; /* A generic pointer to which we can add */
60: /* byte displacements. */
61: /* Preferably identical to caddr_t, if it */
62: /* exists. */
63:
64: #if defined(__STDC__)
65: # include <stdlib.h>
66: # if !(defined( sony_news ) )
67: # include <stddef.h>
68: # endif
69: # define VOLATILE volatile
70: #else
71: # ifdef MSWIN32
72: # include <stdlib.h>
73: # endif
74: # define VOLATILE
75: #endif
76:
1.2 noro 77: #define CONST GC_CONST
78:
1.1 noro 79: #if 0 /* was once defined for AMIGA */
80: # define GC_FAR __far
81: #else
82: # define GC_FAR
83: #endif
84:
1.3 ! noro 85:
1.1 noro 86: /*********************************/
87: /* */
88: /* Definitions for conservative */
89: /* collector */
90: /* */
91: /*********************************/
92:
93: /*********************************/
94: /* */
95: /* Easily changeable parameters */
96: /* */
97: /*********************************/
98:
99: #define STUBBORN_ALLOC /* Define stubborn allocation primitives */
100: #if defined(SRC_M3) || defined(SMALL_CONFIG)
101: # undef STUBBORN_ALLOC
102: #endif
103:
104:
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 */
127: /* touched. */
128: /* If you can easily avoid using this option, do. */
129: /* If not, try to keep individual objects small. */
130:
131: #define PRINTSTATS /* Print garbage collection statistics */
132: /* For less verbose output, undefine in reclaim.c */
133:
134: #define PRINTTIMES /* Print the amount of time consumed by each garbage */
135: /* collection. */
136:
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. */
142: #undef PRINTBLOCKS
143:
144: #ifdef SILENT
145: # ifdef PRINTSTATS
146: # undef PRINTSTATS
147: # endif
148: # ifdef PRINTTIMES
149: # undef PRINTTIMES
150: # endif
151: # ifdef PRINTNBLOCKS
152: # undef PRINTNBLOCKS
153: # endif
154: #endif
155:
156: #if defined(PRINTSTATS) && !defined(GATHERSTATS)
157: # define GATHERSTATS
158: #endif
159:
160: #ifdef FINALIZE_ON_DEMAND
161: # define GC_INVOKE_FINALIZERS()
162: #else
163: # define GC_INVOKE_FINALIZERS() (void)GC_invoke_finalizers()
164: #endif
165:
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 */
175: /* entries. */
176:
177: /* ALIGN_DOUBLE requires MERGE_SIZES at present. */
178: # if defined(ALIGN_DOUBLE) && !defined(MERGE_SIZES)
179: # define MERGE_SIZES
180: # endif
181:
182: #if defined(ALL_INTERIOR_POINTERS) && !defined(DONT_ADD_BYTE_AT_END)
183: # define ADD_BYTE_AT_END
184: #endif
185:
186:
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 */
191: # else
192: # define MINHINCR 64
193: # define MAXHINCR 4096
194: # endif
195:
196: # define TIME_LIMIT 50 /* We try to keep pause times from exceeding */
197: /* this by much. In milliseconds. */
198:
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 */
209: /* heap growth. */
210:
211: /*********************************/
212: /* */
213: /* Stack saving for debugging */
214: /* */
215: /*********************************/
216:
217: #ifdef SAVE_CALL_CHAIN
218:
219: /*
220: * Number of frames and arguments to save in objects allocated by
221: * debugging allocator.
222: */
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. */
226:
227: # define NEED_CALLINFO
228:
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] */);
232:
233: void GC_print_callers (/* struct callinfo info[NFRAMES] */);
234:
235: #else
236:
237: # ifdef GC_ADD_CALLER
238: # define NFRAMES 1
239: # define NARGS 0
240: # define NEED_CALLINFO
241: # endif
242:
243: #endif
244:
245: #ifdef NEED_CALLINFO
246: struct callinfo {
247: word ci_pc;
248: # if NARGS > 0
249: word ci_arg[NARGS]; /* bit-wise complement to avoid retention */
250: # endif
251: # if defined(ALIGN_DOUBLE) && (NFRAMES * (NARGS + 1)) % 2 == 1
252: /* Likely alignment problem. */
253: word ci_dummy;
254: # endif
255: };
256: #endif
257:
258:
259: /*********************************/
260: /* */
261: /* OS interface routines */
262: /* */
263: /*********************************/
264:
265: #ifdef BSD_TIME
266: # undef CLOCK_TYPE
267: # undef GET_TIME
268: # undef MS_TIME_DIFF
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 */
1.3 ! noro 276: # ifdef MSWIN32
! 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 */
1.1 noro 283: # include <time.h>
284: # if !defined(__STDC__) && defined(SPARC) && defined(SUNOS4)
285: clock_t clock(); /* Not in time.h, where it belongs */
286: # endif
287: # if defined(FREEBSD) && !defined(CLOCKS_PER_SEC)
288: # include <machine/limits.h>
289: # define CLOCKS_PER_SEC CLK_TCK
290: # endif
291: # if !defined(CLOCKS_PER_SEC)
292: # define CLOCKS_PER_SEC 1000000
293: /*
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).
302: */
303: # endif
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))
1.3 ! noro 308: # endif /* !MSWIN32 */
1.1 noro 309: #endif /* !BSD_TIME */
310:
311: /* We use bzero and bcopy internally. They may not be available. */
312: # if defined(SPARC) && defined(SUNOS4)
313: # define BCOPY_EXISTS
314: # endif
315: # if defined(M68K) && defined(AMIGA)
316: # define BCOPY_EXISTS
317: # endif
318: # if defined(M68K) && defined(NEXT)
319: # define BCOPY_EXISTS
320: # endif
321: # if defined(VAX)
322: # define BCOPY_EXISTS
323: # endif
324: # if defined(AMIGA)
325: # include <string.h>
326: # define BCOPY_EXISTS
327: # endif
328:
329: # ifndef BCOPY_EXISTS
330: # include <string.h>
331: # define BCOPY(x,y,n) memcpy(y, x, (size_t)(n))
332: # define BZERO(x,n) memset(x, 0, (size_t)(n))
333: # else
334: # define BCOPY(x,y,n) bcopy((char *)(x),(char *)(y),(int)(n))
335: # define BZERO(x,n) bzero((char *)(x),(int)(n))
336: # endif
337:
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 */
344: /* does. */
345: # ifdef PCR
346: char * real_malloc();
347: # define GET_MEM(bytes) HBLKPTR(real_malloc((size_t)bytes + GC_page_size) \
348: + GC_page_size-1)
349: # else
350: # ifdef OS2
351: void * os2_alloc(size_t bytes);
352: # define GET_MEM(bytes) HBLKPTR((ptr_t)os2_alloc((size_t)bytes \
353: + GC_page_size) \
354: + GC_page_size-1)
355: # else
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) \
359: + GC_page_size-1)
360: # else
361: # ifdef MSWIN32
362: extern ptr_t GC_win32_get_mem();
363: # define GET_MEM(bytes) (struct hblk *)GC_win32_get_mem(bytes)
364: # else
365: # ifdef MACOS
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) \
371: + GC_page_size-1)
372: # else
373: # define GET_MEM(bytes) HBLKPTR( \
374: NewPtrClear(bytes + GC_page_size) + GC_page_size-1)
375: # endif
376: # else
377: extern ptr_t GC_unix_get_mem();
378: # define GET_MEM(bytes) (struct hblk *)GC_unix_get_mem(bytes)
379: # endif
380: # endif
381: # endif
382: # endif
383: # endif
384:
385: /*
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
396: * not restarted.
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.)
400: */
401: # ifdef THREADS
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()
414: # endif
415: # ifdef PCR
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); }
427: # endif
428: # ifdef SRC_M3
429: extern word RT0u__inCritical;
430: # define LOCK() RT0u__inCritical++
431: # define UNLOCK() RT0u__inCritical--
432: # endif
433: # ifdef SOLARIS_THREADS
434: # include <thread.h>
435: # include <signal.h>
436: extern mutex_t GC_allocate_ml;
437: # define LOCK() mutex_lock(&GC_allocate_ml);
438: # define UNLOCK() mutex_unlock(&GC_allocate_ml);
439: # endif
1.3 ! noro 440: # if defined(LINUX_THREADS)
! 441: # if defined(I386)|| defined(POWERPC) || defined(ALPHA) || defined(IA64) \
! 442: || defined(M68K)
1.1 noro 443: # include <pthread.h>
1.3 ! noro 444: # define USE_SPIN_LOCK
1.2 noro 445: # if defined(I386)
1.1 noro 446: inline static int GC_test_and_set(volatile unsigned int *addr) {
447: int oldval;
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)));
452: return oldval;
453: }
1.3 ! noro 454: # endif
! 455: # if defined(IA64)
! 456: inline static int GC_test_and_set(volatile unsigned int *addr) {
! 457: int oldval;
! 458: __asm__ __volatile__("xchg4 %0=%1,%2"
! 459: : "=r"(oldval), "=m"(*addr)
! 460: : "r"(1), "1"(*addr));
! 461: return oldval;
! 462: }
! 463: inline static void GC_clear(volatile unsigned int *addr) {
! 464: __asm__ __volatile__("st4.rel %0=r0" : "=m" (*addr));
! 465: }
! 466: # define GC_CLEAR_DEFINED
! 467: # endif
! 468: # ifdef M68K
! 469: /* Contributed by Tony Mantler. I'm not sure how well it was */
! 470: /* tested. */
1.2 noro 471: inline static int GC_test_and_set(volatile unsigned int *addr) {
1.3 ! noro 472: char oldval; /* this must be no longer than 8 bits */
! 473:
! 474: /* The return value is semi-phony. */
! 475: /* 'tas' sets bit 7 while the return */
! 476: /* value pretends bit 0 was set */
! 477: __asm__ __volatile__(
! 478: "tas %1@; sne %0; negb %0"
! 479: : "=d" (oldval)
! 480: : "a" (addr));
! 481: return oldval;
! 482: }
! 483: # endif
! 484: # if defined(POWERPC)
! 485: inline static int GC_test_and_set(volatile unsigned int *addr) {
1.2 noro 486: int oldval;
487: int temp = 1; // locked value
488:
489: __asm__ __volatile__(
490: "1:\tlwarx %0,0,%3\n" // load and reserve
491: "\tcmpwi %0, 0\n" // if load is
492: "\tbne 2f\n" // non-zero, return already set
493: "\tstwcx. %2,0,%1\n" // else store conditional
494: "\tbne- 1b\n" // retry if lost reservation
495: "2:\t\n" // oldval is zero if we set
496: : "=&r"(oldval), "=p"(addr)
497: : "r"(temp), "1"(addr)
498: : "memory");
499: return (int)oldval;
1.3 ! noro 500: }
! 501: inline static void GC_clear(volatile unsigned int *addr) {
! 502: __asm__ __volatile__("eieio");
! 503: *(addr) = 0;
! 504: }
! 505: # define GC_CLEAR_DEFINED
! 506: # endif
! 507: # ifdef ALPHA
! 508: inline static int GC_test_and_set(volatile unsigned int * addr)
! 509: {
! 510: unsigned long oldvalue;
! 511: unsigned long temp;
1.2 noro 512:
1.3 ! noro 513: __asm__ __volatile__(
! 514: "1: ldl_l %0,%1\n"
! 515: " and %0,%3,%2\n"
! 516: " bne %2,2f\n"
! 517: " xor %0,%3,%0\n"
! 518: " stl_c %0,%1\n"
! 519: " beq %0,3f\n"
! 520: " mb\n"
! 521: "2:\n"
! 522: ".section .text2,\"ax\"\n"
! 523: "3: br 1b\n"
! 524: ".previous"
! 525: :"=&r" (temp), "=m" (*addr), "=&r" (oldvalue)
! 526: :"Ir" (1), "m" (*addr));
! 527:
! 528: return oldvalue;
! 529: }
! 530: /* Should probably also define GC_clear, since it needs */
! 531: /* a memory barrier ?? */
! 532: # endif /* ALPHA */
! 533: # ifdef ARM32
! 534: inline static int GC_test_and_set(volatile unsigned int *addr) {
! 535: int oldval;
! 536: /* SWP on ARM is very similar to XCHG on x86. Doesn't lock the
! 537: * bus because there are no SMP ARM machines. If/when there are,
! 538: * this code will likely need to be updated. */
! 539: /* See linuxthreads/sysdeps/arm/pt-machine.h in glibc-2.1 */
! 540: __asm__ __volatile__("swp %0, %1, [%2]"
! 541: : "=r"(oldval)
! 542: : "r"(1), "r"(addr));
! 543: return oldval;
! 544: }
1.1 noro 545: # endif
1.3 ! noro 546: # ifndef GC_CLEAR_DEFINED
! 547: inline static void GC_clear(volatile unsigned int *addr) {
! 548: /* Try to discourage gcc from moving anything past this. */
! 549: __asm__ __volatile__(" ");
1.2 noro 550: *(addr) = 0;
1.3 ! noro 551: }
! 552: # endif
1.1 noro 553:
554: extern volatile unsigned int GC_allocate_lock;
555: extern pthread_t GC_lock_holder;
556: extern void GC_lock(void);
557: /* Allocation lock holder. Only set if acquired by client through */
558: /* GC_call_with_alloc_lock. */
559: # define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
560: # define NO_THREAD (pthread_t)(-1)
561: # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
562: # define I_HOLD_LOCK() (pthread_equal(GC_lock_holder, pthread_self()))
1.2 noro 563: # define LOCK() \
1.1 noro 564: { if (GC_test_and_set(&GC_allocate_lock)) GC_lock(); }
1.2 noro 565: # define UNLOCK() \
1.1 noro 566: GC_clear(&GC_allocate_lock)
1.3 ! noro 567: extern VOLATILE GC_bool GC_collecting;
1.1 noro 568: # define ENTER_GC() \
569: { \
570: GC_collecting = 1; \
571: }
572: # define EXIT_GC() GC_collecting = 0;
1.3 ! noro 573: # else /* LINUX_THREADS on hardware for which we don't know how */
! 574: /* to do test and set. */
! 575: # include <pthread.h>
! 576: extern pthread_mutex_t GC_allocate_ml;
! 577: # define LOCK() pthread_mutex_lock(&GC_allocate_ml)
! 578: # define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
! 579: # endif
1.1 noro 580: # endif /* LINUX_THREADS */
1.2 noro 581: # if defined(HPUX_THREADS)
1.1 noro 582: # include <pthread.h>
1.2 noro 583: extern pthread_mutex_t GC_allocate_ml;
584: # define LOCK() pthread_mutex_lock(&GC_allocate_ml)
585: # define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
586: # endif
587: # if defined(IRIX_THREADS) || defined(IRIX_JDK_THREADS)
588: /* This may also eventually be appropriate for HPUX_THREADS */
589: # include <pthread.h>
590: # ifndef HPUX_THREADS
591: /* This probably should never be included, but I can't test */
592: /* on Irix anymore. */
593: # include <mutex.h>
594: # endif
1.1 noro 595:
1.2 noro 596: # ifndef HPUX_THREADS
597: # if __mips < 3 || !(defined (_ABIN32) || defined(_ABI64)) \
1.1 noro 598: || !defined(_COMPILER_VERSION) || _COMPILER_VERSION < 700
599: # define GC_test_and_set(addr, v) test_and_set(addr,v)
1.2 noro 600: # else
601: # define GC_test_and_set(addr, v) __test_and_set(addr,v)
602: # endif
1.1 noro 603: # else
1.2 noro 604: /* I couldn't find a way to do this inline on HP/UX */
1.1 noro 605: # endif
606: extern unsigned long GC_allocate_lock;
607: /* This is not a mutex because mutexes that obey the (optional) */
608: /* POSIX scheduling rules are subject to convoys in high contention */
609: /* applications. This is basically a spin lock. */
610: extern pthread_t GC_lock_holder;
611: extern void GC_lock(void);
612: /* Allocation lock holder. Only set if acquired by client through */
613: /* GC_call_with_alloc_lock. */
614: # define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
615: # define NO_THREAD (pthread_t)(-1)
616: # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
617: # define I_HOLD_LOCK() (pthread_equal(GC_lock_holder, pthread_self()))
1.2 noro 618: # ifdef HPUX_THREADS
619: # define LOCK() { if (!GC_test_and_clear(&GC_allocate_lock)) GC_lock(); }
620: /* The following is INCORRECT, since the memory model is too weak. */
621: # define UNLOCK() { GC_noop1(&GC_allocate_lock); \
622: *(volatile unsigned long *)(&GC_allocate_lock) = 1; }
1.1 noro 623: # else
1.2 noro 624: # define LOCK() { if (GC_test_and_set(&GC_allocate_lock, 1)) GC_lock(); }
625: # if __mips >= 3 && (defined (_ABIN32) || defined(_ABI64)) \
1.1 noro 626: && defined(_COMPILER_VERSION) && _COMPILER_VERSION >= 700
627: # define UNLOCK() __lock_release(&GC_allocate_lock)
1.2 noro 628: # else
1.1 noro 629: /* The function call in the following should prevent the */
630: /* compiler from moving assignments to below the UNLOCK. */
631: /* This is probably not necessary for ucode or gcc 2.8. */
632: /* It may be necessary for Ragnarok and future gcc */
633: /* versions. */
634: # define UNLOCK() { GC_noop1(&GC_allocate_lock); \
635: *(volatile unsigned long *)(&GC_allocate_lock) = 0; }
1.2 noro 636: # endif
1.1 noro 637: # endif
1.3 ! noro 638: extern VOLATILE GC_bool GC_collecting;
1.1 noro 639: # define ENTER_GC() \
640: { \
641: GC_collecting = 1; \
642: }
643: # define EXIT_GC() GC_collecting = 0;
644: # endif /* IRIX_THREADS || IRIX_JDK_THREADS */
645: # ifdef WIN32_THREADS
646: # include <windows.h>
647: GC_API CRITICAL_SECTION GC_allocate_ml;
648: # define LOCK() EnterCriticalSection(&GC_allocate_ml);
649: # define UNLOCK() LeaveCriticalSection(&GC_allocate_ml);
650: # endif
651: # ifndef SET_LOCK_HOLDER
652: # define SET_LOCK_HOLDER()
653: # define UNSET_LOCK_HOLDER()
654: # define I_HOLD_LOCK() FALSE
655: /* Used on platforms were locks can be reacquired, */
656: /* so it doesn't matter if we lie. */
657: # endif
658: # else
659: # define LOCK()
660: # define UNLOCK()
661: # endif
662: # ifndef SET_LOCK_HOLDER
663: # define SET_LOCK_HOLDER()
664: # define UNSET_LOCK_HOLDER()
665: # define I_HOLD_LOCK() FALSE
666: /* Used on platforms were locks can be reacquired, */
667: /* so it doesn't matter if we lie. */
668: # endif
669: # ifndef ENTER_GC
670: # define ENTER_GC()
671: # define EXIT_GC()
672: # endif
673:
674: # ifndef DCL_LOCK_STATE
675: # define DCL_LOCK_STATE
676: # endif
677: # ifndef FASTLOCK
678: # define FASTLOCK() LOCK()
679: # define FASTLOCK_SUCCEEDED() TRUE
680: # define FASTUNLOCK() UNLOCK()
681: # endif
682:
683: /* Delay any interrupts or signals that may abort this thread. Data */
684: /* structures are in a consistent state outside this pair of calls. */
685: /* ANSI C allows both to be empty (though the standard isn't very */
686: /* clear on that point). Standard malloc implementations are usually */
687: /* neither interruptable nor thread-safe, and thus correspond to */
688: /* empty definitions. */
689: # ifdef PCR
690: # define DISABLE_SIGNALS() \
691: PCR_Th_SetSigMask(PCR_allSigsBlocked,&GC_old_sig_mask)
692: # define ENABLE_SIGNALS() \
693: PCR_Th_SetSigMask(&GC_old_sig_mask, NIL)
694: # else
695: # if defined(SRC_M3) || defined(AMIGA) || defined(SOLARIS_THREADS) \
696: || defined(MSWIN32) || defined(MACOS) || defined(DJGPP) \
697: || defined(NO_SIGNALS) || defined(IRIX_THREADS) \
698: || defined(IRIX_JDK_THREADS) || defined(LINUX_THREADS)
699: /* Also useful for debugging. */
700: /* Should probably use thr_sigsetmask for SOLARIS_THREADS. */
701: # define DISABLE_SIGNALS()
702: # define ENABLE_SIGNALS()
703: # else
704: # define DISABLE_SIGNALS() GC_disable_signals()
705: void GC_disable_signals();
706: # define ENABLE_SIGNALS() GC_enable_signals()
707: void GC_enable_signals();
708: # endif
709: # endif
710:
711: /*
712: * Stop and restart mutator threads.
713: */
714: # ifdef PCR
715: # include "th/PCR_ThCtl.h"
716: # define STOP_WORLD() \
717: PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_stopNormal, \
718: PCR_allSigsBlocked, \
719: PCR_waitForever)
720: # define START_WORLD() \
721: PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_null, \
722: PCR_allSigsBlocked, \
723: PCR_waitForever);
724: # else
725: # if defined(SOLARIS_THREADS) || defined(WIN32_THREADS) \
726: || defined(IRIX_THREADS) || defined(LINUX_THREADS) \
1.2 noro 727: || defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
1.1 noro 728: void GC_stop_world();
729: void GC_start_world();
730: # define STOP_WORLD() GC_stop_world()
731: # define START_WORLD() GC_start_world()
732: # else
733: # define STOP_WORLD()
734: # define START_WORLD()
735: # endif
736: # endif
737:
738: /* Abandon ship */
739: # ifdef PCR
740: # define ABORT(s) PCR_Base_Panic(s)
741: # else
742: # ifdef SMALL_CONFIG
743: # define ABORT(msg) abort();
744: # else
745: GC_API void GC_abort();
746: # define ABORT(msg) GC_abort(msg);
747: # endif
748: # endif
749:
750: /* Exit abnormally, but without making a mess (e.g. out of memory) */
751: # ifdef PCR
752: # define EXIT() PCR_Base_Exit(1,PCR_waitForever)
753: # else
754: # define EXIT() (void)exit(1)
755: # endif
756:
757: /* Print warning message, e.g. almost out of memory. */
758: # define WARN(msg,arg) (*GC_current_warn_proc)(msg, (GC_word)(arg))
759: extern GC_warn_proc GC_current_warn_proc;
760:
761: /*********************************/
762: /* */
763: /* Word-size-dependent defines */
764: /* */
765: /*********************************/
766:
767: #if CPP_WORDSZ == 32
768: # define WORDS_TO_BYTES(x) ((x)<<2)
769: # define BYTES_TO_WORDS(x) ((x)>>2)
770: # define LOGWL ((word)5) /* log[2] of CPP_WORDSZ */
771: # define modWORDSZ(n) ((n) & 0x1f) /* n mod size of word */
772: # if ALIGNMENT != 4
773: # define UNALIGNED
774: # endif
775: #endif
776:
777: #if CPP_WORDSZ == 64
778: # define WORDS_TO_BYTES(x) ((x)<<3)
779: # define BYTES_TO_WORDS(x) ((x)>>3)
780: # define LOGWL ((word)6) /* log[2] of CPP_WORDSZ */
781: # define modWORDSZ(n) ((n) & 0x3f) /* n mod size of word */
782: # if ALIGNMENT != 8
783: # define UNALIGNED
784: # endif
785: #endif
786:
787: #define WORDSZ ((word)CPP_WORDSZ)
788: #define SIGNB ((word)1 << (WORDSZ-1))
789: #define BYTES_PER_WORD ((word)(sizeof (word)))
790: #define ONES ((word)(-1))
791: #define divWORDSZ(n) ((n) >> LOGWL) /* divide n by size of word */
792:
793: /*********************/
794: /* */
795: /* Size Parameters */
796: /* */
797: /*********************/
798:
799: /* heap block size, bytes. Should be power of 2 */
800:
801: #ifndef HBLKSIZE
802: # ifdef SMALL_CONFIG
803: # define CPP_LOG_HBLKSIZE 10
804: # else
805: # if CPP_WORDSZ == 32
806: # define CPP_LOG_HBLKSIZE 12
807: # else
808: # define CPP_LOG_HBLKSIZE 13
809: # endif
810: # endif
811: #else
812: # if HBLKSIZE == 512
813: # define CPP_LOG_HBLKSIZE 9
814: # endif
815: # if HBLKSIZE == 1024
816: # define CPP_LOG_HBLKSIZE 10
817: # endif
818: # if HBLKSIZE == 2048
819: # define CPP_LOG_HBLKSIZE 11
820: # endif
821: # if HBLKSIZE == 4096
822: # define CPP_LOG_HBLKSIZE 12
823: # endif
824: # if HBLKSIZE == 8192
825: # define CPP_LOG_HBLKSIZE 13
826: # endif
827: # if HBLKSIZE == 16384
828: # define CPP_LOG_HBLKSIZE 14
829: # endif
830: # ifndef CPP_LOG_HBLKSIZE
831: --> fix HBLKSIZE
832: # endif
833: # undef HBLKSIZE
834: #endif
835: # define CPP_HBLKSIZE (1 << CPP_LOG_HBLKSIZE)
836: # define LOG_HBLKSIZE ((word)CPP_LOG_HBLKSIZE)
837: # define HBLKSIZE ((word)CPP_HBLKSIZE)
838:
839:
840: /* max size objects supported by freelist (larger objects may be */
841: /* allocated, but less efficiently) */
842:
843: #define CPP_MAXOBJSZ BYTES_TO_WORDS(CPP_HBLKSIZE/2)
844: #define MAXOBJSZ ((word)CPP_MAXOBJSZ)
845:
846: # define divHBLKSZ(n) ((n) >> LOG_HBLKSIZE)
847:
848: # define HBLK_PTR_DIFF(p,q) divHBLKSZ((ptr_t)p - (ptr_t)q)
849: /* Equivalent to subtracting 2 hblk pointers. */
850: /* We do it this way because a compiler should */
851: /* find it hard to use an integer division */
852: /* instead of a shift. The bundled SunOS 4.1 */
853: /* o.w. sometimes pessimizes the subtraction to */
854: /* involve a call to .div. */
855:
856: # define modHBLKSZ(n) ((n) & (HBLKSIZE-1))
857:
858: # define HBLKPTR(objptr) ((struct hblk *)(((word) (objptr)) & ~(HBLKSIZE-1)))
859:
860: # define HBLKDISPL(objptr) (((word) (objptr)) & (HBLKSIZE-1))
861:
862: /* Round up byte allocation requests to integral number of words, etc. */
863: # ifdef ADD_BYTE_AT_END
864: # define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + WORDS_TO_BYTES(1))
865: # ifdef ALIGN_DOUBLE
866: # define ALIGNED_WORDS(n) (BYTES_TO_WORDS((n) + WORDS_TO_BYTES(2)) & ~1)
867: # else
868: # define ALIGNED_WORDS(n) ROUNDED_UP_WORDS(n)
869: # endif
870: # define SMALL_OBJ(bytes) ((bytes) < WORDS_TO_BYTES(MAXOBJSZ))
871: # define ADD_SLOP(bytes) ((bytes)+1)
872: # else
873: # define ROUNDED_UP_WORDS(n) BYTES_TO_WORDS((n) + (WORDS_TO_BYTES(1) - 1))
874: # ifdef ALIGN_DOUBLE
875: # define ALIGNED_WORDS(n) \
876: (BYTES_TO_WORDS((n) + WORDS_TO_BYTES(2) - 1) & ~1)
877: # else
878: # define ALIGNED_WORDS(n) ROUNDED_UP_WORDS(n)
879: # endif
880: # define SMALL_OBJ(bytes) ((bytes) <= WORDS_TO_BYTES(MAXOBJSZ))
881: # define ADD_SLOP(bytes) (bytes)
882: # endif
883:
884:
885: /*
886: * Hash table representation of sets of pages. This assumes it is
887: * OK to add spurious entries to sets.
888: * Used by black-listing code, and perhaps by dirty bit maintenance code.
889: */
890:
891: # ifdef LARGE_CONFIG
892: # define LOG_PHT_ENTRIES 17
893: # else
894: # define LOG_PHT_ENTRIES 14 /* Collisions are likely if heap grows */
895: /* to more than 16K hblks = 64MB. */
896: /* Each hash table occupies 2K bytes. */
897: # endif
898: # define PHT_ENTRIES ((word)1 << LOG_PHT_ENTRIES)
899: # define PHT_SIZE (PHT_ENTRIES >> LOGWL)
900: typedef word page_hash_table[PHT_SIZE];
901:
902: # define PHT_HASH(addr) ((((word)(addr)) >> LOG_HBLKSIZE) & (PHT_ENTRIES - 1))
903:
904: # define get_pht_entry_from_index(bl, index) \
905: (((bl)[divWORDSZ(index)] >> modWORDSZ(index)) & 1)
906: # define set_pht_entry_from_index(bl, index) \
907: (bl)[divWORDSZ(index)] |= (word)1 << modWORDSZ(index)
908: # define clear_pht_entry_from_index(bl, index) \
909: (bl)[divWORDSZ(index)] &= ~((word)1 << modWORDSZ(index))
910:
911:
912:
913: /********************************************/
914: /* */
915: /* H e a p B l o c k s */
916: /* */
917: /********************************************/
918:
919: /* heap block header */
920: #define HBLKMASK (HBLKSIZE-1)
921:
922: #define BITS_PER_HBLK (HBLKSIZE * 8)
923:
924: #define MARK_BITS_PER_HBLK (BITS_PER_HBLK/CPP_WORDSZ)
925: /* upper bound */
926: /* We allocate 1 bit/word. Only the first word */
927: /* in each object is actually marked. */
928:
929: # ifdef ALIGN_DOUBLE
930: # define MARK_BITS_SZ (((MARK_BITS_PER_HBLK + 2*CPP_WORDSZ - 1) \
931: / (2*CPP_WORDSZ))*2)
932: # else
933: # define MARK_BITS_SZ ((MARK_BITS_PER_HBLK + CPP_WORDSZ - 1)/CPP_WORDSZ)
934: # endif
935: /* Upper bound on number of mark words per heap block */
936:
937: struct hblkhdr {
938: word hb_sz; /* If in use, size in words, of objects in the block. */
939: /* if free, the size in bytes of the whole block */
940: struct hblk * hb_next; /* Link field for hblk free list */
941: /* and for lists of chunks waiting to be */
942: /* reclaimed. */
943: struct hblk * hb_prev; /* Backwards link for free list. */
944: word hb_descr; /* object descriptor for marking. See */
945: /* mark.h. */
946: char* hb_map; /* A pointer to a pointer validity map of the block. */
947: /* See GC_obj_map. */
948: /* Valid for all blocks with headers. */
949: /* Free blocks point to GC_invalid_map. */
950: unsigned char hb_obj_kind;
951: /* Kind of objects in the block. Each kind */
952: /* identifies a mark procedure and a set of */
953: /* list headers. Sometimes called regions. */
954: unsigned char hb_flags;
955: # define IGNORE_OFF_PAGE 1 /* Ignore pointers that do not */
956: /* point to the first page of */
957: /* this object. */
958: # define WAS_UNMAPPED 2 /* This is a free block, which has */
959: /* been unmapped from the address */
960: /* space. */
961: /* GC_remap must be invoked on it */
962: /* before it can be reallocated. */
963: /* Only set with USE_MUNMAP. */
964: unsigned short hb_last_reclaimed;
965: /* Value of GC_gc_no when block was */
966: /* last allocated or swept. May wrap. */
967: /* For a free block, this is maintained */
968: /* unly for USE_MUNMAP, and indicates */
969: /* when the header was allocated, or */
970: /* when the size of the block last */
971: /* changed. */
972: word hb_marks[MARK_BITS_SZ];
973: /* Bit i in the array refers to the */
974: /* object starting at the ith word (header */
975: /* INCLUDED) in the heap block. */
976: /* The lsb of word 0 is numbered 0. */
1.2 noro 977: /* Unused bits are invalid, and are */
978: /* occasionally set, e.g for uncollectable */
979: /* objects. */
1.1 noro 980: };
981:
982: /* heap block body */
983:
984: # define DISCARD_WORDS 0
985: /* Number of words to be dropped at the beginning of each block */
986: /* Must be a multiple of WORDSZ. May reasonably be nonzero */
987: /* on machines that don't guarantee longword alignment of */
988: /* pointers, so that the number of false hits is minimized. */
989: /* 0 and WORDSZ are probably the only reasonable values. */
990:
991: # define BODY_SZ ((HBLKSIZE-WORDS_TO_BYTES(DISCARD_WORDS))/sizeof(word))
992:
993: struct hblk {
994: # if (DISCARD_WORDS != 0)
995: word garbage[DISCARD_WORDS];
996: # endif
997: word hb_body[BODY_SZ];
998: };
999:
1000: # define HDR_WORDS ((word)DISCARD_WORDS)
1001: # define HDR_BYTES ((word)WORDS_TO_BYTES(DISCARD_WORDS))
1002:
1003: # define OBJ_SZ_TO_BLOCKS(sz) \
1004: divHBLKSZ(HDR_BYTES + WORDS_TO_BYTES(sz) + HBLKSIZE-1)
1005: /* Size of block (in units of HBLKSIZE) needed to hold objects of */
1006: /* given sz (in words). */
1007:
1008: /* Object free list link */
1009: # define obj_link(p) (*(ptr_t *)(p))
1010:
1011: /* The type of mark procedures. This really belongs in gc_mark.h. */
1012: /* But we put it here, so that we can avoid scanning the mark proc */
1013: /* table. */
1.3 ! noro 1014: typedef struct ms_entry * (*mark_proc)(/* word * addr,
! 1015: struct ms_entry *mark_stack_ptr,
! 1016: struct ms_entry *mark_stack_limit,
! 1017: word env */);
1.1 noro 1018: # define LOG_MAX_MARK_PROCS 6
1019: # define MAX_MARK_PROCS (1 << LOG_MAX_MARK_PROCS)
1020:
1021: /* Root sets. Logically private to mark_rts.c. But we don't want the */
1022: /* tables scanned, so we put them here. */
1023: /* MAX_ROOT_SETS is the maximum number of ranges that can be */
1024: /* registered as static roots. */
1025: # ifdef LARGE_CONFIG
1026: # define MAX_ROOT_SETS 4096
1027: # else
1028: # ifdef PCR
1029: # define MAX_ROOT_SETS 1024
1030: # else
1031: # ifdef MSWIN32
1032: # define MAX_ROOT_SETS 512
1033: /* Under NT, we add only written pages, which can result */
1034: /* in many small root sets. */
1035: # else
1036: # define MAX_ROOT_SETS 64
1037: # endif
1038: # endif
1039: # endif
1040:
1041: # define MAX_EXCLUSIONS (MAX_ROOT_SETS/4)
1042: /* Maximum number of segments that can be excluded from root sets. */
1043:
1044: /*
1045: * Data structure for excluded static roots.
1046: */
1047: struct exclusion {
1048: ptr_t e_start;
1049: ptr_t e_end;
1050: };
1051:
1052: /* Data structure for list of root sets. */
1053: /* We keep a hash table, so that we can filter out duplicate additions. */
1054: /* Under Win32, we need to do a better job of filtering overlaps, so */
1055: /* we resort to sequential search, and pay the price. */
1056: struct roots {
1057: ptr_t r_start;
1058: ptr_t r_end;
1059: # ifndef MSWIN32
1060: struct roots * r_next;
1061: # endif
1062: GC_bool r_tmp;
1063: /* Delete before registering new dynamic libraries */
1064: };
1065:
1066: #ifndef MSWIN32
1067: /* Size of hash table index to roots. */
1068: # define LOG_RT_SIZE 6
1069: # define RT_SIZE (1 << LOG_RT_SIZE) /* Power of 2, may be != MAX_ROOT_SETS */
1070: #endif
1071:
1072: /* Lists of all heap blocks and free lists */
1073: /* as well as other random data structures */
1074: /* that should not be scanned by the */
1075: /* collector. */
1076: /* These are grouped together in a struct */
1077: /* so that they can be easily skipped by the */
1078: /* GC_mark routine. */
1079: /* The ordering is weird to make GC_malloc */
1080: /* faster by keeping the important fields */
1081: /* sufficiently close together that a */
1082: /* single load of a base register will do. */
1083: /* Scalars that could easily appear to */
1084: /* be pointers are also put here. */
1085: /* The main fields should precede any */
1086: /* conditionally included fields, so that */
1087: /* gc_inl.h will work even if a different set */
1088: /* of macros is defined when the client is */
1089: /* compiled. */
1090:
1091: struct _GC_arrays {
1092: word _heapsize;
1093: word _max_heapsize;
1.3 ! noro 1094: word _requested_heapsize; /* Heap size due to explicit expansion */
1.1 noro 1095: ptr_t _last_heap_addr;
1096: ptr_t _prev_heap_addr;
1097: word _large_free_bytes;
1098: /* Total bytes contained in blocks on large object free */
1099: /* list. */
1100: word _words_allocd_before_gc;
1101: /* Number of words allocated before this */
1102: /* collection cycle. */
1103: word _words_allocd;
1104: /* Number of words allocated during this collection cycle */
1105: word _words_wasted;
1106: /* Number of words wasted due to internal fragmentation */
1107: /* in large objects, or due to dropping blacklisted */
1108: /* blocks, since last gc. Approximate. */
1109: word _words_finalized;
1110: /* Approximate number of words in objects (and headers) */
1111: /* That became ready for finalization in the last */
1112: /* collection. */
1113: word _non_gc_bytes_at_gc;
1114: /* Number of explicitly managed bytes of storage */
1115: /* at last collection. */
1116: word _mem_freed;
1117: /* Number of explicitly deallocated words of memory */
1118: /* since last collection. */
1.3 ! noro 1119: ptr_t _scratch_end_ptr;
! 1120: ptr_t _scratch_last_end_ptr;
! 1121: /* Used by headers.c, and can easily appear to point to */
! 1122: /* heap. */
1.1 noro 1123: mark_proc _mark_procs[MAX_MARK_PROCS];
1124: /* Table of user-defined mark procedures. There is */
1125: /* a small number of these, which can be referenced */
1126: /* by DS_PROC mark descriptors. See gc_mark.h. */
1127: ptr_t _objfreelist[MAXOBJSZ+1];
1128: /* free list for objects */
1129: ptr_t _aobjfreelist[MAXOBJSZ+1];
1130: /* free list for atomic objs */
1131:
1132: ptr_t _uobjfreelist[MAXOBJSZ+1];
1133: /* uncollectable but traced objs */
1134: /* objects on this and auobjfreelist */
1135: /* are always marked, except during */
1136: /* garbage collections. */
1137: # ifdef ATOMIC_UNCOLLECTABLE
1138: ptr_t _auobjfreelist[MAXOBJSZ+1];
1139: # endif
1140: /* uncollectable but traced objs */
1141:
1142: # ifdef GATHERSTATS
1143: word _composite_in_use;
1144: /* Number of words in accessible composite */
1145: /* objects. */
1146: word _atomic_in_use;
1147: /* Number of words in accessible atomic */
1148: /* objects. */
1149: # endif
1150: # ifdef USE_MUNMAP
1151: word _unmapped_bytes;
1152: # endif
1153: # ifdef MERGE_SIZES
1154: unsigned _size_map[WORDS_TO_BYTES(MAXOBJSZ+1)];
1155: /* Number of words to allocate for a given allocation request in */
1156: /* bytes. */
1157: # endif
1158:
1159: # ifdef STUBBORN_ALLOC
1160: ptr_t _sobjfreelist[MAXOBJSZ+1];
1161: # endif
1162: /* free list for immutable objects */
1163: ptr_t _obj_map[MAXOBJSZ+1];
1164: /* If not NIL, then a pointer to a map of valid */
1165: /* object addresses. _obj_map[sz][i] is j if the */
1166: /* address block_start+i is a valid pointer */
1167: /* to an object at */
1168: /* block_start+i&~3 - WORDS_TO_BYTES(j). */
1169: /* (If ALL_INTERIOR_POINTERS is defined, then */
1170: /* instead ((short *)(hb_map[sz])[i] is j if */
1171: /* block_start+WORDS_TO_BYTES(i) is in the */
1172: /* interior of an object starting at */
1173: /* block_start+WORDS_TO_BYTES(i-j)). */
1174: /* It is OBJ_INVALID if */
1175: /* block_start+WORDS_TO_BYTES(i) is not */
1176: /* valid as a pointer to an object. */
1177: /* We assume all values of j <= OBJ_INVALID. */
1178: /* The zeroth entry corresponds to large objects.*/
1179: # ifdef ALL_INTERIOR_POINTERS
1180: # define map_entry_type short
1181: # define OBJ_INVALID 0x7fff
1182: # define MAP_ENTRY(map, bytes) \
1183: (((map_entry_type *)(map))[BYTES_TO_WORDS(bytes)])
1184: # define MAP_ENTRIES BYTES_TO_WORDS(HBLKSIZE)
1185: # define MAP_SIZE (MAP_ENTRIES * sizeof(map_entry_type))
1186: # define OFFSET_VALID(displ) TRUE
1187: # define CPP_MAX_OFFSET (HBLKSIZE - HDR_BYTES - 1)
1188: # define MAX_OFFSET ((word)CPP_MAX_OFFSET)
1189: # else
1190: # define map_entry_type char
1191: # define OBJ_INVALID 0x7f
1192: # define MAP_ENTRY(map, bytes) \
1193: (map)[bytes]
1194: # define MAP_ENTRIES HBLKSIZE
1195: # define MAP_SIZE MAP_ENTRIES
1196: # define CPP_MAX_OFFSET (WORDS_TO_BYTES(OBJ_INVALID) - 1)
1197: # define MAX_OFFSET ((word)CPP_MAX_OFFSET)
1198: # define VALID_OFFSET_SZ \
1199: (CPP_MAX_OFFSET > WORDS_TO_BYTES(CPP_MAXOBJSZ)? \
1200: CPP_MAX_OFFSET+1 \
1201: : WORDS_TO_BYTES(CPP_MAXOBJSZ)+1)
1202: char _valid_offsets[VALID_OFFSET_SZ];
1203: /* GC_valid_offsets[i] == TRUE ==> i */
1204: /* is registered as a displacement. */
1205: # define OFFSET_VALID(displ) GC_valid_offsets[displ]
1206: char _modws_valid_offsets[sizeof(word)];
1207: /* GC_valid_offsets[i] ==> */
1208: /* GC_modws_valid_offsets[i%sizeof(word)] */
1209: # endif
1210: # ifdef STUBBORN_ALLOC
1211: page_hash_table _changed_pages;
1212: /* Stubborn object pages that were changes since last call to */
1213: /* GC_read_changed. */
1214: page_hash_table _prev_changed_pages;
1215: /* Stubborn object pages that were changes before last call to */
1216: /* GC_read_changed. */
1217: # endif
1218: # if defined(PROC_VDB) || defined(MPROTECT_VDB)
1219: page_hash_table _grungy_pages; /* Pages that were dirty at last */
1220: /* GC_read_dirty. */
1221: # endif
1222: # ifdef MPROTECT_VDB
1223: VOLATILE page_hash_table _dirty_pages;
1224: /* Pages dirtied since last GC_read_dirty. */
1225: # endif
1226: # ifdef PROC_VDB
1227: page_hash_table _written_pages; /* Pages ever dirtied */
1228: # endif
1229: # ifdef LARGE_CONFIG
1230: # if CPP_WORDSZ > 32
1231: # define MAX_HEAP_SECTS 4096 /* overflows at roughly 64 GB */
1232: # else
1233: # define MAX_HEAP_SECTS 768 /* Separately added heap sections. */
1234: # endif
1235: # else
1236: # define MAX_HEAP_SECTS 256
1237: # endif
1238: struct HeapSect {
1239: ptr_t hs_start; word hs_bytes;
1240: } _heap_sects[MAX_HEAP_SECTS];
1241: # ifdef MSWIN32
1242: ptr_t _heap_bases[MAX_HEAP_SECTS];
1243: /* Start address of memory regions obtained from kernel. */
1244: # endif
1245: struct roots _static_roots[MAX_ROOT_SETS];
1246: # ifndef MSWIN32
1247: struct roots * _root_index[RT_SIZE];
1248: # endif
1249: struct exclusion _excl_table[MAX_EXCLUSIONS];
1250: /* Block header index; see gc_headers.h */
1251: bottom_index * _all_nils;
1252: bottom_index * _top_index [TOP_SZ];
1253: #ifdef SAVE_CALL_CHAIN
1254: struct callinfo _last_stack[NFRAMES]; /* Stack at last garbage collection.*/
1255: /* Useful for debugging mysterious */
1256: /* object disappearances. */
1257: /* In the multithreaded case, we */
1258: /* currently only save the calling */
1259: /* stack. */
1260: #endif
1261: };
1262:
1263: GC_API GC_FAR struct _GC_arrays GC_arrays;
1264:
1265: # define GC_objfreelist GC_arrays._objfreelist
1266: # define GC_aobjfreelist GC_arrays._aobjfreelist
1267: # define GC_uobjfreelist GC_arrays._uobjfreelist
1268: # ifdef ATOMIC_UNCOLLECTABLE
1269: # define GC_auobjfreelist GC_arrays._auobjfreelist
1270: # endif
1271: # define GC_sobjfreelist GC_arrays._sobjfreelist
1272: # define GC_valid_offsets GC_arrays._valid_offsets
1273: # define GC_modws_valid_offsets GC_arrays._modws_valid_offsets
1274: # ifdef STUBBORN_ALLOC
1275: # define GC_changed_pages GC_arrays._changed_pages
1276: # define GC_prev_changed_pages GC_arrays._prev_changed_pages
1277: # endif
1278: # define GC_obj_map GC_arrays._obj_map
1279: # define GC_last_heap_addr GC_arrays._last_heap_addr
1280: # define GC_prev_heap_addr GC_arrays._prev_heap_addr
1281: # define GC_words_allocd GC_arrays._words_allocd
1282: # define GC_words_wasted GC_arrays._words_wasted
1283: # define GC_large_free_bytes GC_arrays._large_free_bytes
1284: # define GC_words_finalized GC_arrays._words_finalized
1285: # define GC_non_gc_bytes_at_gc GC_arrays._non_gc_bytes_at_gc
1286: # define GC_mem_freed GC_arrays._mem_freed
1.3 ! noro 1287: # define GC_scratch_end_ptr GC_arrays._scratch_end_ptr
! 1288: # define GC_scratch_last_end_ptr GC_arrays._scratch_last_end_ptr
1.1 noro 1289: # define GC_mark_procs GC_arrays._mark_procs
1290: # define GC_heapsize GC_arrays._heapsize
1291: # define GC_max_heapsize GC_arrays._max_heapsize
1.3 ! noro 1292: # define GC_requested_heapsize GC_arrays._requested_heapsize
1.1 noro 1293: # define GC_words_allocd_before_gc GC_arrays._words_allocd_before_gc
1294: # define GC_heap_sects GC_arrays._heap_sects
1295: # define GC_last_stack GC_arrays._last_stack
1296: # ifdef USE_MUNMAP
1297: # define GC_unmapped_bytes GC_arrays._unmapped_bytes
1298: # endif
1299: # ifdef MSWIN32
1300: # define GC_heap_bases GC_arrays._heap_bases
1301: # endif
1302: # define GC_static_roots GC_arrays._static_roots
1303: # define GC_root_index GC_arrays._root_index
1304: # define GC_excl_table GC_arrays._excl_table
1305: # define GC_all_nils GC_arrays._all_nils
1306: # define GC_top_index GC_arrays._top_index
1307: # if defined(PROC_VDB) || defined(MPROTECT_VDB)
1308: # define GC_grungy_pages GC_arrays._grungy_pages
1309: # endif
1310: # ifdef MPROTECT_VDB
1311: # define GC_dirty_pages GC_arrays._dirty_pages
1312: # endif
1313: # ifdef PROC_VDB
1314: # define GC_written_pages GC_arrays._written_pages
1315: # endif
1316: # ifdef GATHERSTATS
1317: # define GC_composite_in_use GC_arrays._composite_in_use
1318: # define GC_atomic_in_use GC_arrays._atomic_in_use
1319: # endif
1320: # ifdef MERGE_SIZES
1321: # define GC_size_map GC_arrays._size_map
1322: # endif
1323:
1324: # define beginGC_arrays ((ptr_t)(&GC_arrays))
1325: # define endGC_arrays (((ptr_t)(&GC_arrays)) + (sizeof GC_arrays))
1326:
1.3 ! noro 1327: #define USED_HEAP_SIZE (GC_heapsize - GC_large_free_bytes)
! 1328:
1.1 noro 1329: /* Object kinds: */
1330: # define MAXOBJKINDS 16
1331:
1332: extern struct obj_kind {
1333: ptr_t *ok_freelist; /* Array of free listheaders for this kind of object */
1334: /* Point either to GC_arrays or to storage allocated */
1335: /* with GC_scratch_alloc. */
1336: struct hblk **ok_reclaim_list;
1337: /* List headers for lists of blocks waiting to be */
1338: /* swept. */
1339: word ok_descriptor; /* Descriptor template for objects in this */
1340: /* block. */
1341: GC_bool ok_relocate_descr;
1342: /* Add object size in bytes to descriptor */
1343: /* template to obtain descriptor. Otherwise */
1344: /* template is used as is. */
1345: GC_bool ok_init; /* Clear objects before putting them on the free list. */
1346: } GC_obj_kinds[MAXOBJKINDS];
1347:
1348: # define endGC_obj_kinds (((ptr_t)(&GC_obj_kinds)) + (sizeof GC_obj_kinds))
1349:
1350: # define end_gc_area ((ptr_t)endGC_arrays == (ptr_t)(&GC_obj_kinds) ? \
1351: endGC_obj_kinds : endGC_arrays)
1352:
1353: /* Predefined kinds: */
1354: # define PTRFREE 0
1355: # define NORMAL 1
1356: # define UNCOLLECTABLE 2
1357: # ifdef ATOMIC_UNCOLLECTABLE
1358: # define AUNCOLLECTABLE 3
1359: # define STUBBORN 4
1360: # define IS_UNCOLLECTABLE(k) (((k) & ~1) == UNCOLLECTABLE)
1361: # else
1362: # define STUBBORN 3
1363: # define IS_UNCOLLECTABLE(k) ((k) == UNCOLLECTABLE)
1364: # endif
1365:
1366: extern int GC_n_kinds;
1367:
1368: GC_API word GC_fo_entries;
1369:
1370: extern word GC_n_heap_sects; /* Number of separately added heap */
1371: /* sections. */
1372:
1373: extern word GC_page_size;
1374:
1375: # ifdef MSWIN32
1376: extern word GC_n_heap_bases; /* See GC_heap_bases. */
1377: # endif
1378:
1379: extern word GC_total_stack_black_listed;
1380: /* Number of bytes on stack blacklist. */
1381:
1382: extern word GC_black_list_spacing;
1383: /* Average number of bytes between blacklisted */
1384: /* blocks. Approximate. */
1385: /* Counts only blocks that are */
1386: /* "stack-blacklisted", i.e. that are */
1387: /* problematic in the interior of an object. */
1388:
1389: extern char * GC_invalid_map;
1390: /* Pointer to the nowhere valid hblk map */
1391: /* Blocks pointing to this map are free. */
1392:
1393: extern struct hblk * GC_hblkfreelist[];
1394: /* List of completely empty heap blocks */
1395: /* Linked through hb_next field of */
1396: /* header structure associated with */
1397: /* block. */
1398:
1399: extern GC_bool GC_is_initialized; /* GC_init() has been run. */
1400:
1401: extern GC_bool GC_objects_are_marked; /* There are marked objects in */
1402: /* the heap. */
1403:
1404: #ifndef SMALL_CONFIG
1405: extern GC_bool GC_incremental;
1406: /* Using incremental/generational collection. */
1407: #else
1.3 ! noro 1408: # define GC_incremental FALSE
1.1 noro 1409: /* Hopefully allow optimizer to remove some code. */
1410: #endif
1411:
1412: extern GC_bool GC_dirty_maintained;
1413: /* Dirty bits are being maintained, */
1414: /* either for incremental collection, */
1415: /* or to limit the root set. */
1416:
1417: extern word GC_root_size; /* Total size of registered root sections */
1418:
1419: extern GC_bool GC_debugging_started; /* GC_debug_malloc has been called. */
1420:
1421: extern ptr_t GC_least_plausible_heap_addr;
1422: extern ptr_t GC_greatest_plausible_heap_addr;
1423: /* Bounds on the heap. Guaranteed valid */
1424: /* Likely to include future heap expansion. */
1425:
1426: /* Operations */
1427: # ifndef abs
1428: # define abs(x) ((x) < 0? (-(x)) : (x))
1429: # endif
1430:
1431:
1432: /* Marks are in a reserved area in */
1433: /* each heap block. Each word has one mark bit associated */
1434: /* with it. Only those corresponding to the beginning of an */
1435: /* object are used. */
1436:
1437:
1438: /* Mark bit operations */
1439:
1440: /*
1441: * Retrieve, set, clear the mark bit corresponding
1442: * to the nth word in a given heap block.
1443: *
1444: * (Recall that bit n corresponds to object beginning at word n
1445: * relative to the beginning of the block, including unused words)
1446: */
1447:
1448: # define mark_bit_from_hdr(hhdr,n) (((hhdr)->hb_marks[divWORDSZ(n)] \
1449: >> (modWORDSZ(n))) & (word)1)
1450: # define set_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1451: |= (word)1 << modWORDSZ(n)
1452:
1453: # define clear_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
1454: &= ~((word)1 << modWORDSZ(n))
1455:
1456: /* Important internal collector routines */
1457:
1458: ptr_t GC_approx_sp();
1459:
1460: GC_bool GC_should_collect();
1.3 ! noro 1461:
1.1 noro 1462: void GC_apply_to_all_blocks(/*fn, client_data*/);
1463: /* Invoke fn(hbp, client_data) for each */
1464: /* allocated heap block. */
1465: struct hblk * GC_next_used_block(/* struct hblk * h */);
1466: /* Return first in-use block >= h */
1467: struct hblk * GC_prev_block(/* struct hblk * h */);
1468: /* Return last block <= h. Returned block */
1469: /* is managed by GC, but may or may not be in */
1470: /* use. */
1471: void GC_mark_init();
1472: void GC_clear_marks(); /* Clear mark bits for all heap objects. */
1473: void GC_invalidate_mark_state(); /* Tell the marker that marked */
1474: /* objects may point to unmarked */
1475: /* ones, and roots may point to */
1476: /* unmarked objects. */
1477: /* Reset mark stack. */
1478: void GC_mark_from_mark_stack(); /* Mark from everything on the mark stack. */
1479: /* Return after about one pages worth of */
1480: /* work. */
1481: GC_bool GC_mark_stack_empty();
1482: GC_bool GC_mark_some(/* cold_gc_frame */);
1483: /* Perform about one pages worth of marking */
1484: /* work of whatever kind is needed. Returns */
1485: /* quickly if no collection is in progress. */
1486: /* Return TRUE if mark phase finished. */
1487: void GC_initiate_gc(); /* initiate collection. */
1488: /* If the mark state is invalid, this */
1489: /* becomes full colleection. Otherwise */
1490: /* it's partial. */
1491: void GC_push_all(/*b,t*/); /* Push everything in a range */
1492: /* onto mark stack. */
1493: void GC_push_dirty(/*b,t*/); /* Push all possibly changed */
1494: /* subintervals of [b,t) onto */
1495: /* mark stack. */
1496: #ifndef SMALL_CONFIG
1497: void GC_push_conditional(/* ptr_t b, ptr_t t, GC_bool all*/);
1498: #else
1499: # define GC_push_conditional(b, t, all) GC_push_all(b, t)
1500: #endif
1501: /* Do either of the above, depending */
1502: /* on the third arg. */
1503: void GC_push_all_stack(/*b,t*/); /* As above, but consider */
1504: /* interior pointers as valid */
1505: void GC_push_all_eager(/*b,t*/); /* Same as GC_push_all_stack, but */
1506: /* ensures that stack is scanned */
1507: /* immediately, not just scheduled */
1508: /* for scanning. */
1509: #ifndef THREADS
1510: void GC_push_all_stack_partially_eager(/* bottom, top, cold_gc_frame */);
1511: /* Similar to GC_push_all_eager, but only the */
1512: /* part hotter than cold_gc_frame is scanned */
1513: /* immediately. Needed to endure that callee- */
1514: /* save registers are not missed. */
1515: #else
1516: /* In the threads case, we push part of the current thread stack */
1517: /* with GC_push_all_eager when we push the registers. This gets the */
1518: /* callee-save registers that may disappear. The remainder of the */
1519: /* stacks are scheduled for scanning in *GC_push_other_roots, which */
1520: /* is thread-package-specific. */
1521: #endif
1522: void GC_push_current_stack(/* ptr_t cold_gc_frame */);
1523: /* Push enough of the current stack eagerly to */
1524: /* ensure that callee-save registers saved in */
1525: /* GC frames are scanned. */
1526: /* In the non-threads case, schedule entire */
1527: /* stack for scanning. */
1528: void GC_push_roots(/* GC_bool all, ptr_t cold_gc_frame */);
1529: /* Push all or dirty roots. */
1530: extern void (*GC_push_other_roots)();
1531: /* Push system or application specific roots */
1532: /* onto the mark stack. In some environments */
1533: /* (e.g. threads environments) this is */
1534: /* predfined to be non-zero. A client supplied */
1535: /* replacement should also call the original */
1536: /* function. */
1537: extern void (*GC_start_call_back)(/* void */);
1538: /* Called at start of full collections. */
1539: /* Not called if 0. Called with allocation */
1540: /* lock held. */
1541: /* 0 by default. */
1542: void GC_push_regs(); /* Push register contents onto mark stack. */
1.2 noro 1543: /* If NURSERY is defined, the default push */
1544: /* action can be overridden with GC_push_proc */
1.1 noro 1545: void GC_remark(); /* Mark from all marked objects. Used */
1546: /* only if we had to drop something. */
1.2 noro 1547:
1548: # ifdef NURSERY
1549: extern void (*GC_push_proc)(ptr_t);
1550: # endif
1.1 noro 1551: # if defined(MSWIN32)
1552: void __cdecl GC_push_one();
1553: # else
1554: void GC_push_one(/*p*/); /* If p points to an object, mark it */
1555: /* and push contents on the mark stack */
1556: # endif
1557: void GC_push_one_checked(/*p*/); /* Ditto, omits plausibility test */
1558: void GC_push_marked(/* struct hblk h, hdr * hhdr */);
1559: /* Push contents of all marked objects in h onto */
1560: /* mark stack. */
1561: #ifdef SMALL_CONFIG
1562: # define GC_push_next_marked_dirty(h) GC_push_next_marked(h)
1563: #else
1564: struct hblk * GC_push_next_marked_dirty(/* h */);
1565: /* Invoke GC_push_marked on next dirty block above h. */
1566: /* Return a pointer just past the end of this block. */
1567: #endif /* !SMALL_CONFIG */
1568: struct hblk * GC_push_next_marked(/* h */);
1569: /* Ditto, but also mark from clean pages. */
1570: struct hblk * GC_push_next_marked_uncollectable(/* h */);
1571: /* Ditto, but mark only from uncollectable pages. */
1572: GC_bool GC_stopped_mark(); /* Stop world and mark from all roots */
1573: /* and rescuers. */
1574: void GC_clear_hdr_marks(/* hhdr */); /* Clear the mark bits in a header */
1575: void GC_set_hdr_marks(/* hhdr */); /* Set the mark bits in a header */
1576: void GC_add_roots_inner();
1577: GC_bool GC_is_static_root(/* ptr_t p */);
1578: /* Is the address p in one of the registered static */
1579: /* root sections? */
1580: void GC_register_dynamic_libraries();
1581: /* Add dynamic library data sections to the root set. */
1582:
1583: /* Machine dependent startup routines */
1584: ptr_t GC_get_stack_base();
1585: void GC_register_data_segments();
1586:
1587: /* Black listing: */
1588: void GC_bl_init();
1589: # ifndef ALL_INTERIOR_POINTERS
1590: void GC_add_to_black_list_normal(/* bits, maybe source */);
1591: /* Register bits as a possible future false */
1592: /* reference from the heap or static data */
1593: # ifdef PRINT_BLACK_LIST
1594: # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1595: GC_add_to_black_list_normal(bits, source)
1596: # else
1597: # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1598: GC_add_to_black_list_normal(bits)
1599: # endif
1600: # else
1601: # ifdef PRINT_BLACK_LIST
1602: # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1603: GC_add_to_black_list_stack(bits, source)
1604: # else
1605: # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1606: GC_add_to_black_list_stack(bits)
1607: # endif
1608: # endif
1609:
1610: void GC_add_to_black_list_stack(/* bits, maybe source */);
1611: struct hblk * GC_is_black_listed(/* h, len */);
1612: /* If there are likely to be false references */
1613: /* to a block starting at h of the indicated */
1614: /* length, then return the next plausible */
1615: /* starting location for h that might avoid */
1616: /* these false references. */
1617: void GC_promote_black_lists();
1618: /* Declare an end to a black listing phase. */
1619: void GC_unpromote_black_lists();
1620: /* Approximately undo the effect of the above. */
1621: /* This actually loses some information, but */
1622: /* only in a reasonably safe way. */
1623: word GC_number_stack_black_listed(/*struct hblk *start, struct hblk *endp1 */);
1624: /* Return the number of (stack) blacklisted */
1625: /* blocks in the range for statistical */
1626: /* purposes. */
1627:
1628: ptr_t GC_scratch_alloc(/*bytes*/);
1629: /* GC internal memory allocation for */
1630: /* small objects. Deallocation is not */
1631: /* possible. */
1632:
1633: /* Heap block layout maps: */
1634: void GC_invalidate_map(/* hdr */);
1635: /* Remove the object map associated */
1636: /* with the block. This identifies */
1637: /* the block as invalid to the mark */
1638: /* routines. */
1639: GC_bool GC_add_map_entry(/*sz*/);
1640: /* Add a heap block map for objects of */
1641: /* size sz to obj_map. */
1642: /* Return FALSE on failure. */
1643: void GC_register_displacement_inner(/*offset*/);
1644: /* Version of GC_register_displacement */
1645: /* that assumes lock is already held */
1646: /* and signals are already disabled. */
1647:
1648: /* hblk allocation: */
1649: void GC_new_hblk(/*size_in_words, kind*/);
1650: /* Allocate a new heap block, and build */
1651: /* a free list in it. */
1652: struct hblk * GC_allochblk(/*size_in_words, kind*/);
1653: /* Allocate a heap block, clear it if */
1654: /* for composite objects, inform */
1655: /* the marker that block is valid */
1656: /* for objects of indicated size. */
1657: /* sz < 0 ==> atomic. */
1658: void GC_freehblk(); /* Deallocate a heap block and mark it */
1659: /* as invalid. */
1660:
1661: /* Misc GC: */
1662: void GC_init_inner();
1663: GC_bool GC_expand_hp_inner();
1664: void GC_start_reclaim(/*abort_if_found*/);
1665: /* Restore unmarked objects to free */
1666: /* lists, or (if abort_if_found is */
1667: /* TRUE) report them. */
1668: /* Sweeping of small object pages is */
1669: /* largely deferred. */
1670: void GC_continue_reclaim(/*size, kind*/);
1671: /* Sweep pages of the given size and */
1672: /* kind, as long as possible, and */
1673: /* as long as the corr. free list is */
1674: /* empty. */
1675: void GC_reclaim_or_delete_all();
1676: /* Arrange for all reclaim lists to be */
1677: /* empty. Judiciously choose between */
1678: /* sweeping and discarding each page. */
1679: GC_bool GC_reclaim_all(/* GC_stop_func f*/);
1680: /* Reclaim all blocks. Abort (in a */
1681: /* consistent state) if f returns TRUE. */
1682: GC_bool GC_block_empty(/* hhdr */); /* Block completely unmarked? */
1683: GC_bool GC_never_stop_func(); /* Returns FALSE. */
1684: GC_bool GC_try_to_collect_inner(/* GC_stop_func f */);
1685: /* Collect; caller must have acquired */
1686: /* lock and disabled signals. */
1687: /* Collection is aborted if f returns */
1688: /* TRUE. Returns TRUE if it completes */
1689: /* successfully. */
1690: # define GC_gcollect_inner() \
1691: (void) GC_try_to_collect_inner(GC_never_stop_func)
1692: void GC_finish_collection(); /* Finish collection. Mark bits are */
1693: /* consistent and lock is still held. */
1694: GC_bool GC_collect_or_expand(/* needed_blocks */);
1695: /* Collect or expand heap in an attempt */
1696: /* make the indicated number of free */
1697: /* blocks available. Should be called */
1698: /* until the blocks are available or */
1699: /* until it fails by returning FALSE. */
1700: GC_API void GC_init(); /* Initialize collector. */
1701: void GC_collect_a_little_inner(/* int n */);
1702: /* Do n units worth of garbage */
1703: /* collection work, if appropriate. */
1704: /* A unit is an amount appropriate for */
1705: /* HBLKSIZE bytes of allocation. */
1706: ptr_t GC_generic_malloc(/* bytes, kind */);
1707: /* Allocate an object of the given */
1708: /* kind. By default, there are only */
1709: /* a few kinds: composite(pointerfree), */
1710: /* atomic, uncollectable, etc. */
1711: /* We claim it's possible for clever */
1712: /* client code that understands GC */
1713: /* internals to add more, e.g. to */
1714: /* communicate object layout info */
1715: /* to the collector. */
1716: ptr_t GC_generic_malloc_ignore_off_page(/* bytes, kind */);
1717: /* As above, but pointers past the */
1718: /* first page of the resulting object */
1719: /* are ignored. */
1720: ptr_t GC_generic_malloc_inner(/* bytes, kind */);
1721: /* Ditto, but I already hold lock, etc. */
1722: ptr_t GC_generic_malloc_words_small GC_PROTO((size_t words, int kind));
1723: /* As above, but size in units of words */
1724: /* Bypasses MERGE_SIZES. Assumes */
1725: /* words <= MAXOBJSZ. */
1726: ptr_t GC_generic_malloc_inner_ignore_off_page(/* bytes, kind */);
1727: /* Allocate an object, where */
1728: /* the client guarantees that there */
1729: /* will always be a pointer to the */
1730: /* beginning of the object while the */
1731: /* object is live. */
1732: ptr_t GC_allocobj(/* sz_inn_words, kind */);
1733: /* Make the indicated */
1734: /* free list nonempty, and return its */
1735: /* head. */
1736:
1737: void GC_init_headers();
1.3 ! noro 1738: struct hblkhdr * GC_install_header(/*h*/);
1.1 noro 1739: /* Install a header for block h. */
1.3 ! noro 1740: /* Return 0 on failure, or the header */
! 1741: /* otherwise. */
1.1 noro 1742: GC_bool GC_install_counts(/*h, sz*/);
1743: /* Set up forwarding counts for block */
1744: /* h of size sz. */
1745: /* Return FALSE on failure. */
1746: void GC_remove_header(/*h*/);
1747: /* Remove the header for block h. */
1748: void GC_remove_counts(/*h, sz*/);
1749: /* Remove forwarding counts for h. */
1750: hdr * GC_find_header(/*p*/); /* Debugging only. */
1751:
1752: void GC_finalize(); /* Perform all indicated finalization actions */
1753: /* on unmarked objects. */
1754: /* Unreachable finalizable objects are enqueued */
1755: /* for processing by GC_invoke_finalizers. */
1756: /* Invoked with lock. */
1757:
1758: void GC_add_to_heap(/*p, bytes*/);
1759: /* Add a HBLKSIZE aligned chunk to the heap. */
1760:
1761: void GC_print_obj(/* ptr_t p */);
1762: /* P points to somewhere inside an object with */
1763: /* debugging info. Print a human readable */
1764: /* description of the object to stderr. */
1765: extern void (*GC_check_heap)();
1766: /* Check that all objects in the heap with */
1767: /* debugging info are intact. Print */
1768: /* descriptions of any that are not. */
1769: extern void (*GC_print_heap_obj)(/* ptr_t p */);
1770: /* If possible print s followed by a more */
1771: /* detailed description of the object */
1772: /* referred to by p. */
1773:
1774: /* Memory unmapping: */
1775: #ifdef USE_MUNMAP
1776: void GC_unmap_old(void);
1777: void GC_merge_unmapped(void);
1778: void GC_unmap(ptr_t start, word bytes);
1779: void GC_remap(ptr_t start, word bytes);
1780: void GC_unmap_gap(ptr_t start1, word bytes1, ptr_t start2, word bytes2);
1781: #endif
1782:
1783: /* Virtual dirty bit implementation: */
1784: /* Each implementation exports the following: */
1785: void GC_read_dirty(); /* Retrieve dirty bits. */
1786: GC_bool GC_page_was_dirty(/* struct hblk * h */);
1787: /* Read retrieved dirty bits. */
1788: GC_bool GC_page_was_ever_dirty(/* struct hblk * h */);
1789: /* Could the page contain valid heap pointers? */
1790: void GC_is_fresh(/* struct hblk * h, word number_of_blocks */);
1791: /* Assert the region currently contains no */
1792: /* valid pointers. */
1793: void GC_write_hint(/* struct hblk * h */);
1794: /* h is about to be written. */
1795: void GC_dirty_init();
1796:
1797: /* Slow/general mark bit manipulation: */
1798: GC_API GC_bool GC_is_marked();
1799: void GC_clear_mark_bit();
1800: void GC_set_mark_bit();
1801:
1802: /* Stubborn objects: */
1803: void GC_read_changed(); /* Analogous to GC_read_dirty */
1804: GC_bool GC_page_was_changed(/* h */); /* Analogous to GC_page_was_dirty */
1805: void GC_clean_changing_list(); /* Collect obsolete changing list entries */
1806: void GC_stubborn_init();
1807:
1808: /* Debugging print routines: */
1809: void GC_print_block_list();
1810: void GC_print_hblkfreelist();
1811: void GC_print_heap_sects();
1812: void GC_print_static_roots();
1813: void GC_dump();
1814:
1815: #ifdef KEEP_BACK_PTRS
1816: void GC_store_back_pointer(ptr_t source, ptr_t dest);
1817: void GC_marked_for_finalization(ptr_t dest);
1818: # define GC_STORE_BACK_PTR(source, dest) GC_store_back_pointer(source, dest)
1819: # define GC_MARKED_FOR_FINALIZATION(dest) GC_marked_for_finalization(dest)
1820: #else
1821: # define GC_STORE_BACK_PTR(source, dest)
1822: # define GC_MARKED_FOR_FINALIZATION(dest)
1823: #endif
1824:
1825: /* Make arguments appear live to compiler */
1826: # ifdef __WATCOMC__
1827: void GC_noop(void*, ...);
1828: # else
1829: GC_API void GC_noop();
1830: # endif
1831:
1832: void GC_noop1(/* word arg */);
1833:
1834: /* Logging and diagnostic output: */
1835: GC_API void GC_printf GC_PROTO((char * format, long, long, long, long, long, long));
1836: /* A version of printf that doesn't allocate, */
1837: /* is restricted to long arguments, and */
1838: /* (unfortunately) doesn't use varargs for */
1839: /* portability. Restricted to 6 args and */
1840: /* 1K total output length. */
1841: /* (We use sprintf. Hopefully that doesn't */
1842: /* allocate for long arguments.) */
1843: # define GC_printf0(f) GC_printf(f, 0l, 0l, 0l, 0l, 0l, 0l)
1844: # define GC_printf1(f,a) GC_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1845: # define GC_printf2(f,a,b) GC_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1846: # define GC_printf3(f,a,b,c) GC_printf(f, (long)a, (long)b, (long)c, 0l, 0l, 0l)
1847: # define GC_printf4(f,a,b,c,d) GC_printf(f, (long)a, (long)b, (long)c, \
1848: (long)d, 0l, 0l)
1849: # define GC_printf5(f,a,b,c,d,e) GC_printf(f, (long)a, (long)b, (long)c, \
1850: (long)d, (long)e, 0l)
1851: # define GC_printf6(f,a,b,c,d,e,g) GC_printf(f, (long)a, (long)b, (long)c, \
1852: (long)d, (long)e, (long)g)
1853:
1854: void GC_err_printf(/* format, a, b, c, d, e, f */);
1855: # define GC_err_printf0(f) GC_err_puts(f)
1856: # define GC_err_printf1(f,a) GC_err_printf(f, (long)a, 0l, 0l, 0l, 0l, 0l)
1857: # define GC_err_printf2(f,a,b) GC_err_printf(f, (long)a, (long)b, 0l, 0l, 0l, 0l)
1858: # define GC_err_printf3(f,a,b,c) GC_err_printf(f, (long)a, (long)b, (long)c, \
1859: 0l, 0l, 0l)
1860: # define GC_err_printf4(f,a,b,c,d) GC_err_printf(f, (long)a, (long)b, \
1861: (long)c, (long)d, 0l, 0l)
1862: # define GC_err_printf5(f,a,b,c,d,e) GC_err_printf(f, (long)a, (long)b, \
1863: (long)c, (long)d, \
1864: (long)e, 0l)
1865: # define GC_err_printf6(f,a,b,c,d,e,g) GC_err_printf(f, (long)a, (long)b, \
1866: (long)c, (long)d, \
1867: (long)e, (long)g)
1868: /* Ditto, writes to stderr. */
1869:
1870: void GC_err_puts(/* char *s */);
1871: /* Write s to stderr, don't buffer, don't add */
1872: /* newlines, don't ... */
1873:
1874:
1875: # ifdef GC_ASSERTIONS
1876: # define GC_ASSERT(expr) if(!(expr)) {\
1877: GC_err_printf2("Assertion failure: %s:%ld\n", \
1878: __FILE__, (unsigned long)__LINE__); \
1879: ABORT("assertion failure"); }
1880: # else
1881: # define GC_ASSERT(expr)
1882: # endif
1883:
1884: # endif /* GC_PRIVATE_H */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>