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