Debian Codegraph @

external/bdwgc/include/private/gc_priv.h

raw
1 /*
2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
5 * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
6 *
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
18 #ifndef GC_PRIVATE_H
19 #define GC_PRIVATE_H
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #ifndef GC_BUILD
26 # define GC_BUILD
27 #endif
28
29 #if (defined(__linux__) || defined(__GLIBC__) || defined(__GNU__)) \
30 && !defined(_GNU_SOURCE)
31 /* Can't test LINUX, since this must be defined before other includes. */
32 # define _GNU_SOURCE 1
33 #endif
34
35 #if (defined(DGUX) && defined(GC_THREADS) || defined(DGUX386_THREADS) \
36 || defined(GC_DGUX386_THREADS)) && !defined(_USING_POSIX4A_DRAFT10)
37 # define _USING_POSIX4A_DRAFT10 1
38 #endif
39
40 #if defined(__MINGW32__) && !defined(__MINGW_EXCPT_DEFINE_PSDK) \
41 && defined(__i386__) && defined(GC_EXTERN) /* defined in gc.c */
42 /* See the description in mark.c. */
43 # define __MINGW_EXCPT_DEFINE_PSDK 1
44 #endif
45
46 # if defined(NO_DEBUGGING) && !defined(GC_ASSERTIONS) && !defined(NDEBUG)
47 /* To turn off assertion checking (in atomic_ops.h). */
48 # define NDEBUG 1
49 # endif
50
51 #ifndef GC_H
52 # include "../gc.h"
53 #endif
54
55 #include <stdlib.h>
56 #if !defined(sony_news)
57 # include <stddef.h>
58 #endif
59
60 #ifdef DGUX
61 # include <sys/types.h>
62 # include <sys/time.h>
63 # include <sys/resource.h>
64 #endif /* DGUX */
65
66 #ifdef BSD_TIME
67 # include <sys/types.h>
68 # include <sys/time.h>
69 # include <sys/resource.h>
70 #endif /* BSD_TIME */
71
72 #ifdef PARALLEL_MARK
73 # define AO_REQUIRE_CAS
74 # if !defined(__GNUC__) && !defined(AO_ASSUME_WINDOWS98)
75 # define AO_ASSUME_WINDOWS98
76 # endif
77 #endif
78
79 #ifndef GC_TINY_FL_H
80 # include "../gc_tiny_fl.h"
81 #endif
82
83 #ifndef GC_MARK_H
84 # include "../gc_mark.h"
85 #endif
86
87 typedef GC_word word;
88 typedef GC_signed_word signed_word;
89 typedef unsigned int unsigned32;
90
91 typedef int GC_bool;
92 #define TRUE 1
93 #define FALSE 0
94
95 typedef char * ptr_t; /* A generic pointer to which we can add */
96 /* byte displacements and which can be used */
97 /* for address comparisons. */
98
99 #ifndef SIZE_MAX
100 # include <limits.h>
101 #endif
102 #if defined(SIZE_MAX) && !defined(CPPCHECK)
103 # define GC_SIZE_MAX ((size_t)SIZE_MAX)
104 /* Extra cast to workaround some buggy SIZE_MAX definitions. */
105 #else
106 # define GC_SIZE_MAX (~(size_t)0)
107 #endif
108
109 #if GC_GNUC_PREREQ(3, 0) && !defined(LINT2)
110 # define EXPECT(expr, outcome) __builtin_expect(expr,outcome)
111 /* Equivalent to (expr), but predict that usually (expr)==outcome. */
112 #else
113 # define EXPECT(expr, outcome) (expr)
114 #endif /* __GNUC__ */
115
116 /* Saturated addition of size_t values. Used to avoid value wrap */
117 /* around on overflow. The arguments should have no side effects. */
118 #define SIZET_SAT_ADD(a, b) \
119 (EXPECT((a) < GC_SIZE_MAX - (b), TRUE) ? (a) + (b) : GC_SIZE_MAX)
120
121 #ifndef GCCONFIG_H
122 # include "gcconfig.h"
123 #endif
124
125 #if !defined(GC_ATOMIC_UNCOLLECTABLE) && defined(ATOMIC_UNCOLLECTABLE)
126 /* For compatibility with old-style naming. */
127 # define GC_ATOMIC_UNCOLLECTABLE
128 #endif
129
130 #ifndef GC_INNER
131 /* This tagging macro must be used at the start of every variable */
132 /* definition which is declared with GC_EXTERN. Should be also used */
133 /* for the GC-scope function definitions and prototypes. Must not be */
134 /* used in gcconfig.h. Shouldn't be used for the debugging-only */
135 /* functions. Currently, not used for the functions declared in or */
136 /* called from the "dated" source files (located in "extra" folder). */
137 # if defined(GC_DLL) && defined(__GNUC__) && !defined(MSWIN32) \
138 && !defined(MSWINCE) && !defined(CYGWIN32)
139 # if GC_GNUC_PREREQ(4, 0) && !defined(GC_NO_VISIBILITY)
140 /* See the corresponding GC_API definition. */
141 # define GC_INNER __attribute__((__visibility__("hidden")))
142 # else
143 /* The attribute is unsupported. */
144 # define GC_INNER /* empty */
145 # endif
146 # else
147 # define GC_INNER /* empty */
148 # endif
149
150 # define GC_EXTERN extern GC_INNER
151 /* Used only for the GC-scope variables (prefixed with "GC_") */
152 /* declared in the header files. Must not be used for thread-local */
153 /* variables. Must not be used in gcconfig.h. Shouldn't be used for */
154 /* the debugging-only or profiling-only variables. Currently, not */
155 /* used for the variables accessed from the "dated" source files */
156 /* (specific.c/h, and in the "extra" folder). */
157 /* The corresponding variable definition must start with GC_INNER. */
158 #endif /* !GC_INNER */
159
160 #ifdef __cplusplus
161 /* Register storage specifier is deprecated in C++11. */
162 # define REGISTER /* empty */
163 #else
164 /* Used only for several local variables in the performance-critical */
165 /* functions. Should not be used for new code. */
166 # define REGISTER register
167 #endif
168
169 #ifndef HEADERS_H
170 # include "gc_hdrs.h"
171 #endif
172
173 #ifndef GC_ATTR_NO_SANITIZE_ADDR
174 # ifndef ADDRESS_SANITIZER
175 # define GC_ATTR_NO_SANITIZE_ADDR /* empty */
176 # elif GC_CLANG_PREREQ(3, 8)
177 # define GC_ATTR_NO_SANITIZE_ADDR __attribute__((no_sanitize("address")))
178 # else
179 # define GC_ATTR_NO_SANITIZE_ADDR __attribute__((no_sanitize_address))
180 # endif
181 #endif /* !GC_ATTR_NO_SANITIZE_ADDR */
182
183 #ifndef GC_ATTR_NO_SANITIZE_MEMORY
184 # ifndef MEMORY_SANITIZER
185 # define GC_ATTR_NO_SANITIZE_MEMORY /* empty */
186 # elif GC_CLANG_PREREQ(3, 8)
187 # define GC_ATTR_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
188 # else
189 # define GC_ATTR_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
190 # endif
191 #endif /* !GC_ATTR_NO_SANITIZE_MEMORY */
192
193 #ifndef GC_ATTR_NO_SANITIZE_THREAD
194 # ifndef THREAD_SANITIZER
195 # define GC_ATTR_NO_SANITIZE_THREAD /* empty */
196 # elif GC_CLANG_PREREQ(3, 8)
197 # define GC_ATTR_NO_SANITIZE_THREAD __attribute__((no_sanitize("thread")))
198 # else
199 # define GC_ATTR_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
200 # endif
201 #endif /* !GC_ATTR_NO_SANITIZE_THREAD */
202
203 #ifndef GC_ATTR_UNUSED
204 # if GC_GNUC_PREREQ(3, 4)
205 # define GC_ATTR_UNUSED __attribute__((__unused__))
206 # else
207 # define GC_ATTR_UNUSED /* empty */
208 # endif
209 #endif /* !GC_ATTR_UNUSED */
210
211 #ifdef HAVE_CONFIG_H
212 /* The "inline" keyword is determined by Autoconf AC_C_INLINE. */
213 # define GC_INLINE static inline
214 #elif defined(_MSC_VER) || defined(__INTEL_COMPILER) || defined(__DMC__) \
215 || (GC_GNUC_PREREQ(3, 0) && defined(__STRICT_ANSI__)) \
216 || defined(__WATCOMC__)
217 # define GC_INLINE static __inline
218 #elif GC_GNUC_PREREQ(3, 0) || defined(__sun)
219 # define GC_INLINE static inline
220 #else
221 # define GC_INLINE static
222 #endif
223
224 #ifndef GC_ATTR_NOINLINE
225 # if GC_GNUC_PREREQ(4, 0)
226 # define GC_ATTR_NOINLINE __attribute__((__noinline__))
227 # elif _MSC_VER >= 1400
228 # define GC_ATTR_NOINLINE __declspec(noinline)
229 # else
230 # define GC_ATTR_NOINLINE /* empty */
231 # endif
232 #endif
233
234 #ifndef GC_API_OSCALL
235 /* This is used to identify GC routines called by name from OS. */
236 # if defined(__GNUC__)
237 # if GC_GNUC_PREREQ(4, 0) && !defined(GC_NO_VISIBILITY)
238 /* Same as GC_API if GC_DLL. */
239 # define GC_API_OSCALL extern __attribute__((__visibility__("default")))
240 # else
241 /* The attribute is unsupported. */
242 # define GC_API_OSCALL extern
243 # endif
244 # else
245 # define GC_API_OSCALL GC_API
246 # endif
247 #endif
248
249 #ifndef GC_API_PRIV
250 # define GC_API_PRIV GC_API
251 #endif
252
253 #if defined(THREADS)
254 # include "gc_atomic_ops.h"
255 #endif
256
257 #ifndef GC_LOCKS_H
258 # include "gc_locks.h"
259 #endif
260
261 #define ONES ((word)(signed_word)(-1))
262
263 # ifdef STACK_GROWS_DOWN
264 # define COOLER_THAN >
265 # define HOTTER_THAN <
266 # define MAKE_COOLER(x,y) if ((word)((x) + (y)) > (word)(x)) {(x) += (y);} \
267 else (x) = (ptr_t)ONES
268 # define MAKE_HOTTER(x,y) (x) -= (y)
269 # else
270 # define COOLER_THAN <
271 # define HOTTER_THAN >
272 # define MAKE_COOLER(x,y) if ((word)((x) - (y)) < (word)(x)) {(x) -= (y);} \
273 else (x) = 0
274 # define MAKE_HOTTER(x,y) (x) += (y)
275 # endif
276
277 #if defined(AMIGA) && defined(__SASC)
278 # define GC_FAR __far
279 #else
280 # define GC_FAR
281 #endif
282
283
284 /*********************************/
285 /* */
286 /* Definitions for conservative */
287 /* collector */
288 /* */
289 /*********************************/
290
291 /*********************************/
292 /* */
293 /* Easily changeable parameters */
294 /* */
295 /*********************************/
296
297 /* #define ALL_INTERIOR_POINTERS */
298 /* Forces all pointers into the interior of an */
299 /* object to be considered valid. Also causes the */
300 /* sizes of all objects to be inflated by at least */
301 /* one byte. This should suffice to guarantee */
302 /* that in the presence of a compiler that does */
303 /* not perform garbage-collector-unsafe */
304 /* optimizations, all portable, strictly ANSI */
305 /* conforming C programs should be safely usable */
306 /* with malloc replaced by GC_malloc and free */
307 /* calls removed. There are several disadvantages: */
308 /* 1. There are probably no interesting, portable, */
309 /* strictly ANSI conforming C programs. */
310 /* 2. This option makes it hard for the collector */
311 /* to allocate space that is not "pointed to" */
312 /* by integers, etc. Under SunOS 4.X with a */
313 /* statically linked libc, we empirically */
314 /* observed that it would be difficult to */
315 /* allocate individual objects larger than 100K. */
316 /* Even if only smaller objects are allocated, */
317 /* more swap space is likely to be needed. */
318 /* Fortunately, much of this will never be */
319 /* touched. */
320 /* If you can easily avoid using this option, do. */
321 /* If not, try to keep individual objects small. */
322 /* This is now really controlled at startup, */
323 /* through GC_all_interior_pointers. */
324
325 EXTERN_C_BEGIN
326
327 #ifndef GC_NO_FINALIZATION
328 # define GC_INVOKE_FINALIZERS() GC_notify_or_invoke_finalizers()
329 GC_INNER void GC_notify_or_invoke_finalizers(void);
330 /* If GC_finalize_on_demand is not set, invoke */
331 /* eligible finalizers. Otherwise: */
332 /* Call *GC_finalizer_notifier if there are */
333 /* finalizers to be run, and we haven't called */
334 /* this procedure yet this GC cycle. */
335
336 GC_INNER void GC_finalize(void);
337 /* Perform all indicated finalization actions */
338 /* on unmarked objects. */
339 /* Unreachable finalizable objects are enqueued */
340 /* for processing by GC_invoke_finalizers. */
341 /* Invoked with lock. */
342
343 # ifndef GC_TOGGLE_REFS_NOT_NEEDED
344 GC_INNER void GC_process_togglerefs(void);
345 /* Process the toggle-refs before GC starts. */
346 # endif
347 # ifndef SMALL_CONFIG
348 GC_INNER void GC_print_finalization_stats(void);
349 # endif
350 #else
351 # define GC_INVOKE_FINALIZERS() (void)0
352 #endif /* GC_NO_FINALIZATION */
353
354 #if !defined(DONT_ADD_BYTE_AT_END)
355 # ifdef LINT2
356 /* Explicitly instruct the code analysis tool that */
357 /* GC_all_interior_pointers is assumed to have only 0 or 1 value. */
358 # define EXTRA_BYTES ((size_t)(GC_all_interior_pointers? 1 : 0))
359 # else
360 # define EXTRA_BYTES (size_t)GC_all_interior_pointers
361 # endif
362 # define MAX_EXTRA_BYTES 1
363 #else
364 # define EXTRA_BYTES 0
365 # define MAX_EXTRA_BYTES 0
366 #endif
367
368
369 # ifndef LARGE_CONFIG
370 # define MINHINCR 16 /* Minimum heap increment, in blocks of HBLKSIZE */
371 /* Must be multiple of largest page size. */
372 # define MAXHINCR 2048 /* Maximum heap increment, in blocks */
373 # else
374 # define MINHINCR 64
375 # define MAXHINCR 4096
376 # endif
377
378 # define BL_LIMIT GC_black_list_spacing
379 /* If we need a block of N bytes, and we have */
380 /* a block of N + BL_LIMIT bytes available, */
381 /* and N > BL_LIMIT, */
382 /* but all possible positions in it are */
383 /* blacklisted, we just use it anyway (and */
384 /* print a warning, if warnings are enabled). */
385 /* This risks subsequently leaking the block */
386 /* due to a false reference. But not using */
387 /* the block risks unreasonable immediate */
388 /* heap growth. */
389
390 /*********************************/
391 /* */
392 /* Stack saving for debugging */
393 /* */
394 /*********************************/
395
396 #ifdef NEED_CALLINFO
397 struct callinfo {
398 word ci_pc; /* Caller, not callee, pc */
399 # if NARGS > 0
400 word ci_arg[NARGS]; /* bit-wise complement to avoid retention */
401 # endif
402 # if (NFRAMES * (NARGS + 1)) % 2 == 1
403 /* Likely alignment problem. */
404 word ci_dummy;
405 # endif
406 };
407 #endif
408
409 #ifdef SAVE_CALL_CHAIN
410 /* Fill in the pc and argument information for up to NFRAMES of my */
411 /* callers. Ignore my frame and my callers frame. */
412 GC_INNER void GC_save_callers(struct callinfo info[NFRAMES]);
413 GC_INNER void GC_print_callers(struct callinfo info[NFRAMES]);
414 #endif
415
416 EXTERN_C_END
417
418 /*********************************/
419 /* */
420 /* OS interface routines */
421 /* */
422 /*********************************/
423
424 #ifndef NO_CLOCK
425 #ifdef BSD_TIME
426 # undef CLOCK_TYPE
427 # undef GET_TIME
428 # undef MS_TIME_DIFF
429 # define CLOCK_TYPE struct timeval
430 # define GET_TIME(x) \
431 do { \
432 struct rusage rusage; \
433 getrusage(RUSAGE_SELF, &rusage); \
434 x = rusage.ru_utime; \
435 } while (0)
436 # define MS_TIME_DIFF(a,b) ((unsigned long)(a.tv_sec - b.tv_sec) * 1000 \
437 + (unsigned long)(a.tv_usec - b.tv_usec) / 1000)
438 # define NS_TIME_DIFF(a,b) ((unsigned long long)(a.tv_sec - b.tv_sec) * 1000000000 \
439 + (unsigned long long)(a.tv_usec - b.tv_usec) * 1000)
440 #elif defined(MSWIN32) || defined(MSWINCE)
441 # ifndef WIN32_LEAN_AND_MEAN
442 # define WIN32_LEAN_AND_MEAN 1
443 # endif
444 # define NOSERVICE
445 # include <windows.h>
446 # include <winbase.h>
447 # define CLOCK_TYPE LONGLONG
448 # define GET_TIME(x) \
449 do { \
450 LARGE_INTEGER freq, t; \
451 QueryPerformanceFrequency(&freq); \
452 QueryPerformanceCounter(&t); \
453 x = (LONGLONG)(t.QuadPart * (1000000000.0 / (double)freq.QuadPart)); \
454 } while (0)
455 # define MS_TIME_DIFF(a,b) ((unsigned long)(((a) - (b)) / 1000000))
456 # define NS_TIME_DIFF(a,b) ((a) - (b))
457 #elif defined(NN_PLATFORM_CTR)
458 # define CLOCK_TYPE long long
459 EXTERN_C_BEGIN
460 CLOCK_TYPE n3ds_get_system_tick(void);
461 CLOCK_TYPE n3ds_convert_tick_to_ms(CLOCK_TYPE tick);
462 EXTERN_C_END
463 # define GET_TIME(x) (void)(x = n3ds_get_system_tick())
464 # define MS_TIME_DIFF(a,b) ((long)n3ds_convert_tick_to_ms((a)-(b)))
465 # define NS_TIME_DIFF(a,b) ((long long)n3ds_convert_tick_to_ms((a)-(b)) * 1000000)
466 #elif defined(NINTENDO_SWITCH)
467 #include <time.h>
468 # define CLOCK_TYPE long long
469 # define GET_TIME(x) \
470 do { \
471 struct timespec t;\
472 int r = clock_gettime(CLOCK_REALTIME, &t);\
473 x=-1;\
474 if (r != -1) {\
475 x = (t.tv_sec * 1000000000) + t.tv_nsec;\
476 }\
477 } while (0)
478 # define MS_TIME_DIFF(a,b) ((unsigned long)(((a) - (b)) / 1000000))
479 # define NS_TIME_DIFF(a,b) ((a)-(b))
480 #else /* !BSD_TIME && !NN_PLATFORM_CTR && !MSWIN32 && !MSWINCE && !NINTENDO_SWITCH */
481 # include <time.h>
482 # if defined(FREEBSD) && !defined(CLOCKS_PER_SEC)
483 # include <machine/limits.h>
484 # define CLOCKS_PER_SEC CLK_TCK
485 # endif
486 # if !defined(CLOCKS_PER_SEC)
487 # define CLOCKS_PER_SEC 1000000
488 /* This is technically a bug in the implementation. */
489 /* ANSI requires that CLOCKS_PER_SEC be defined. But at least */
490 /* under SunOS 4.1.1, it isn't. Also note that the combination of */
491 /* ANSI C and POSIX is incredibly gross here. The type clock_t */
492 /* is used by both clock() and times(). But on some machines */
493 /* these use different notions of a clock tick, CLOCKS_PER_SEC */
494 /* seems to apply only to clock. Hence we use it here. On many */
495 /* machines, including SunOS, clock actually uses units of */
496 /* microseconds (which are not really clock ticks). */
497 # endif
498 # define CLOCK_TYPE clock_t
499 # define GET_TIME(x) (void)(x = clock())
500 # define MS_TIME_DIFF(a,b) (CLOCKS_PER_SEC % 1000 == 0 ? \
501 (unsigned long)((a) - (b)) / (unsigned long)(CLOCKS_PER_SEC / 1000) \
502 : ((unsigned long)((a) - (b)) * 1000) / (unsigned long)CLOCKS_PER_SEC)
503 # define NS_TIME_DIFF(a,b) (((unsigned long long)((a) - (b)) * 1000000000) / (unsigned long long)CLOCKS_PER_SEC)
504 /* Avoid using double type since some targets (like ARM) might */
505 /* require -lm option for double-to-long conversion. */
506 #endif /* !BSD_TIME && !MSWIN32 */
507 #endif /* !NO_CLOCK */
508
509 /* We use bzero and bcopy internally. They may not be available. */
510 # if defined(SPARC) && defined(SUNOS4) \
511 || (defined(M68K) && defined(NEXT)) || defined(VAX)
512 # define BCOPY_EXISTS
513 # elif defined(AMIGA) || defined(DARWIN)
514 # include <string.h>
515 # define BCOPY_EXISTS
516 # elif defined(MACOS) && defined(POWERPC)
517 # include <MacMemory.h>
518 # define bcopy(x,y,n) BlockMoveData(x, y, n)
519 # define bzero(x,n) BlockZero(x, n)
520 # define BCOPY_EXISTS
521 # endif
522
523 # if !defined(BCOPY_EXISTS) || defined(CPPCHECK)
524 # include <string.h>
525 # define BCOPY(x,y,n) memcpy(y, x, (size_t)(n))
526 # define BZERO(x,n) memset(x, 0, (size_t)(n))
527 # else
528 # define BCOPY(x,y,n) bcopy((void *)(x),(void *)(y),(size_t)(n))
529 # define BZERO(x,n) bzero((void *)(x),(size_t)(n))
530 # endif
531
532 #ifdef PCR
533 # include "th/PCR_ThCtl.h"
534 #endif
535
536 EXTERN_C_BEGIN
537
538 /*
539 * Stop and restart mutator threads.
540 */
541 # ifdef PCR
542 # define STOP_WORLD() \
543 PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_stopNormal, \
544 PCR_allSigsBlocked, \
545 PCR_waitForever)
546 # define START_WORLD() \
547 PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_null, \
548 PCR_allSigsBlocked, \
549 PCR_waitForever)
550 # else
551 # if defined(NN_PLATFORM_CTR) || defined(NINTENDO_SWITCH) \
552 || defined(GC_WIN32_THREADS) || defined(GC_PTHREADS)
553 GC_INNER void GC_stop_world(void);
554 GC_INNER void GC_start_world(void);
555 # define STOP_WORLD() GC_stop_world()
556 # define START_WORLD() GC_start_world()
557 # else
558 /* Just do a sanity check: we are not inside GC_do_blocking(). */
559 # define STOP_WORLD() GC_ASSERT(GC_blocked_sp == NULL)
560 # define START_WORLD()
561 # endif
562 # endif
563
564 #ifdef THREADS
565 GC_EXTERN GC_on_thread_event_proc GC_on_thread_event;
566 #endif
567
568 /* Abandon ship */
569 # if defined(SMALL_CONFIG) || defined(PCR)
570 # define GC_on_abort(msg) (void)0 /* be silent on abort */
571 # else
572 GC_API_PRIV GC_abort_func GC_on_abort;
573 # endif
574 # if defined(CPPCHECK)
575 # define ABORT(msg) { GC_on_abort(msg); abort(); }
576 # elif defined(PCR)
577 # define ABORT(s) PCR_Base_Panic(s)
578 # else
579 # if defined(MSWINCE) && !defined(DebugBreak) \
580 && (!defined(UNDER_CE) || (defined(__MINGW32CE__) && !defined(ARM32)))
581 /* This simplifies linking for WinCE (and, probably, doesn't */
582 /* hurt debugging much); use -DDebugBreak=DebugBreak to override */
583 /* this behavior if really needed. This is also a workaround for */
584 /* x86mingw32ce toolchain (if it is still declaring DebugBreak() */
585 /* instead of defining it as a macro). */
586 # define DebugBreak() _exit(-1) /* there is no abort() in WinCE */
587 # endif
588 # if defined(MSWIN32) && (defined(NO_DEBUGGING) || defined(LINT2))
589 /* A more user-friendly abort after showing fatal message. */
590 # define ABORT(msg) (GC_on_abort(msg), _exit(-1))
591 /* Exit on error without running "at-exit" callbacks. */
592 # elif defined(MSWINCE) && defined(NO_DEBUGGING)
593 # define ABORT(msg) (GC_on_abort(msg), ExitProcess(-1))
594 # elif defined(MSWIN32) || defined(MSWINCE)
595 # if defined(_CrtDbgBreak) && defined(_DEBUG) && defined(_MSC_VER)
596 # define ABORT(msg) { GC_on_abort(msg); \
597 _CrtDbgBreak() /* __debugbreak() */; }
598 # else
599 # define ABORT(msg) { GC_on_abort(msg); __debugbreak(); }
600 /* Note that: on a WinCE box, this could be silently */
601 /* ignored (i.e., the program is not aborted); */
602 /* DebugBreak is a statement in some toolchains. */
603 # endif
604 # else
605 # define ABORT(msg) (GC_on_abort(msg), abort())
606 # endif /* !MSWIN32 */
607 # endif /* !PCR */
608
609 /* For abort message with 1-3 arguments. C_msg and C_fmt should be */
610 /* literals. C_msg should not contain format specifiers. Arguments */
611 /* should match their format specifiers. */
612 #define ABORT_ARG1(C_msg, C_fmt, arg1) \
613 do { \
614 GC_INFOLOG_PRINTF(C_msg /* + */ C_fmt "\n", arg1); \
615 ABORT(C_msg); \
616 } while (0)
617 #define ABORT_ARG2(C_msg, C_fmt, arg1, arg2) \
618 do { \
619 GC_INFOLOG_PRINTF(C_msg /* + */ C_fmt "\n", arg1, arg2); \
620 ABORT(C_msg); \
621 } while (0)
622 #define ABORT_ARG3(C_msg, C_fmt, arg1, arg2, arg3) \
623 do { \
624 GC_INFOLOG_PRINTF(C_msg /* + */ C_fmt "\n", \
625 arg1, arg2, arg3); \
626 ABORT(C_msg); \
627 } while (0)
628
629 /* Same as ABORT but does not have 'no-return' attribute. */
630 /* ABORT on a dummy condition (which is always true). */
631 #define ABORT_RET(msg) \
632 if ((signed_word)GC_current_warn_proc == -1) {} else ABORT(msg)
633
634 /* Exit abnormally, but without making a mess (e.g. out of memory) */
635 # ifdef PCR
636 # define EXIT() PCR_Base_Exit(1,PCR_waitForever)
637 # else
638 # define EXIT() (GC_on_abort(NULL), exit(1 /* EXIT_FAILURE */))
639 # endif
640
641 /* Print warning message, e.g. almost out of memory. */
642 /* The argument (if any) format specifier should be: */
643 /* "%s", "%p" or "%"WARN_PRIdPTR. */
644 #define WARN(msg, arg) \
645 (*GC_current_warn_proc)((/* no const */ char *)("GC Warning: " msg), \
646 (word)(arg))
647 GC_EXTERN GC_warn_proc GC_current_warn_proc;
648
649 /* Print format type macro for decimal signed_word value passed WARN(). */
650 /* This could be redefined for Win64 or LLP64, but typically should */
651 /* not be done as the WARN format string is, possibly, processed on the */
652 /* client side, so non-standard print type modifiers (like MS "I64d") */
653 /* should be avoided here if possible. */
654 #ifndef WARN_PRIdPTR
655 /* Assume sizeof(void *) == sizeof(long) (or a little-endian machine) */
656 # define WARN_PRIdPTR "ld"
657 #endif
658
659 /* A tagging macro (for a code static analyzer) to indicate that the */
660 /* string obtained from an untrusted source (e.g., argv[], getenv) is */
661 /* safe to use in a vulnerable operation (e.g., open, exec). */
662 #define TRUSTED_STRING(s) (char*)COVERT_DATAFLOW(s)
663
664 /* Get environment entry */
665 #ifdef GC_READ_ENV_FILE
666 GC_INNER char * GC_envfile_getenv(const char *name);
667 # define GETENV(name) GC_envfile_getenv(name)
668 #elif defined(NO_GETENV) && !defined(CPPCHECK)
669 # define GETENV(name) NULL
670 #elif defined(EMPTY_GETENV_RESULTS)
671 /* Workaround for a reputed Wine bug. */
672 GC_INLINE char * fixed_getenv(const char *name)
673 {
674 char *value = getenv(name);
675 return value != NULL && *value != '\0' ? value : NULL;
676 }
677 # define GETENV(name) fixed_getenv(name)
678 #else
679 # define GETENV(name) getenv(name)
680 #endif
681
682 EXTERN_C_END
683
684 #if defined(DARWIN)
685 # include <mach/thread_status.h>
686 # ifndef MAC_OS_X_VERSION_MAX_ALLOWED
687 # include <AvailabilityMacros.h>
688 /* Include this header just to import the above macro. */
689 # endif
690 # if defined(POWERPC)
691 # if CPP_WORDSZ == 32
692 # define GC_THREAD_STATE_T ppc_thread_state_t
693 # else
694 # define GC_THREAD_STATE_T ppc_thread_state64_t
695 # define GC_MACH_THREAD_STATE PPC_THREAD_STATE64
696 # define GC_MACH_THREAD_STATE_COUNT PPC_THREAD_STATE64_COUNT
697 # endif
698 # elif defined(I386) || defined(X86_64)
699 # if CPP_WORDSZ == 32
700 # if defined(i386_THREAD_STATE_COUNT) && !defined(x86_THREAD_STATE32_COUNT)
701 /* Use old naming convention for 32-bit x86. */
702 # define GC_THREAD_STATE_T i386_thread_state_t
703 # define GC_MACH_THREAD_STATE i386_THREAD_STATE
704 # define GC_MACH_THREAD_STATE_COUNT i386_THREAD_STATE_COUNT
705 # else
706 # define GC_THREAD_STATE_T x86_thread_state32_t
707 # define GC_MACH_THREAD_STATE x86_THREAD_STATE32
708 # define GC_MACH_THREAD_STATE_COUNT x86_THREAD_STATE32_COUNT
709 # endif
710 # else
711 # define GC_THREAD_STATE_T x86_thread_state64_t
712 # define GC_MACH_THREAD_STATE x86_THREAD_STATE64
713 # define GC_MACH_THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
714 # endif
715 # elif defined(ARM32) && defined(ARM_UNIFIED_THREAD_STATE) \
716 && !defined(CPPCHECK)
717 # define GC_THREAD_STATE_T arm_unified_thread_state_t
718 # define GC_MACH_THREAD_STATE ARM_UNIFIED_THREAD_STATE
719 # define GC_MACH_THREAD_STATE_COUNT ARM_UNIFIED_THREAD_STATE_COUNT
720 # elif defined(ARM32)
721 # define GC_THREAD_STATE_T arm_thread_state_t
722 # ifdef ARM_MACHINE_THREAD_STATE_COUNT
723 # define GC_MACH_THREAD_STATE ARM_MACHINE_THREAD_STATE
724 # define GC_MACH_THREAD_STATE_COUNT ARM_MACHINE_THREAD_STATE_COUNT
725 # endif
726 # elif defined(AARCH64)
727 # define GC_THREAD_STATE_T arm_thread_state64_t
728 # define GC_MACH_THREAD_STATE ARM_THREAD_STATE64
729 # define GC_MACH_THREAD_STATE_COUNT ARM_THREAD_STATE64_COUNT
730 # elif !defined(CPPCHECK)
731 # error define GC_THREAD_STATE_T
732 # endif
733 # ifndef GC_MACH_THREAD_STATE
734 # define GC_MACH_THREAD_STATE MACHINE_THREAD_STATE
735 # define GC_MACH_THREAD_STATE_COUNT MACHINE_THREAD_STATE_COUNT
736 # endif
737
738 # if CPP_WORDSZ == 32
739 # define GC_MACH_HEADER mach_header
740 # define GC_MACH_SECTION section
741 # define GC_GETSECTBYNAME getsectbynamefromheader
742 # else
743 # define GC_MACH_HEADER mach_header_64
744 # define GC_MACH_SECTION section_64
745 # define GC_GETSECTBYNAME getsectbynamefromheader_64
746 # endif
747
748 /* Try to work out the right way to access thread state structure */
749 /* members. The structure has changed its definition in different */
750 /* Darwin versions. This now defaults to the (older) names */
751 /* without __, thus hopefully, not breaking any existing */
752 /* Makefile.direct builds. */
753 # if __DARWIN_UNIX03
754 # define THREAD_FLD_NAME(x) __ ## x
755 # else
756 # define THREAD_FLD_NAME(x) x
757 # endif
758 # if defined(ARM32) && defined(ARM_UNIFIED_THREAD_STATE)
759 # define THREAD_FLD(x) ts_32.THREAD_FLD_NAME(x)
760 # else
761 # define THREAD_FLD(x) THREAD_FLD_NAME(x)
762 # endif
763 #endif /* DARWIN */
764
765 #include "../gc_tiny_fl.h"
766
767 #include <setjmp.h>
768
769 EXTERN_C_BEGIN
770
771 /*********************************/
772 /* */
773 /* Word-size-dependent defines */
774 /* */
775 /*********************************/
776
777 #if CPP_WORDSZ == 32
778 # define WORDS_TO_BYTES(x) ((x)<<2)
779 # define BYTES_TO_WORDS(x) ((x)>>2)
780 # define LOGWL ((word)5) /* log[2] of CPP_WORDSZ */
781 # define modWORDSZ(n) ((n) & 0x1f) /* n mod size of word */
782 # if ALIGNMENT != 4
783 # define UNALIGNED_PTRS
784 # endif
785 #endif
786
787 #if CPP_WORDSZ == 64
788 # define WORDS_TO_BYTES(x) ((x)<<3)
789 # define BYTES_TO_WORDS(x) ((x)>>3)
790 # define LOGWL ((word)6) /* log[2] of CPP_WORDSZ */
791 # define modWORDSZ(n) ((n) & 0x3f) /* n mod size of word */
792 # if ALIGNMENT != 8
793 # define UNALIGNED_PTRS
794 # endif
795 #endif
796
797 /* The first TINY_FREELISTS free lists correspond to the first */
798 /* TINY_FREELISTS multiples of GRANULE_BYTES, i.e. we keep */
799 /* separate free lists for each multiple of GRANULE_BYTES */
800 /* up to (TINY_FREELISTS-1) * GRANULE_BYTES. After that they */
801 /* may be spread out further. */
802
803 #define GRANULE_BYTES GC_GRANULE_BYTES
804 #define TINY_FREELISTS GC_TINY_FREELISTS
805
806 #define WORDSZ ((word)CPP_WORDSZ)
807 #define SIGNB ((word)1 << (WORDSZ-1))
808 #define BYTES_PER_WORD ((word)(sizeof (word)))
809 #define divWORDSZ(n) ((n) >> LOGWL) /* divide n by size of word */
810
811 #if GRANULE_BYTES == 8
812 # define BYTES_TO_GRANULES(n) ((n)>>3)
813 # define GRANULES_TO_BYTES(n) ((n)<<3)
814 # if CPP_WORDSZ == 64
815 # define GRANULES_TO_WORDS(n) (n)
816 # elif CPP_WORDSZ == 32
817 # define GRANULES_TO_WORDS(n) ((n)<<1)
818 # else
819 # define GRANULES_TO_WORDS(n) BYTES_TO_WORDS(GRANULES_TO_BYTES(n))
820 # endif
821 #elif GRANULE_BYTES == 16
822 # define BYTES_TO_GRANULES(n) ((n)>>4)
823 # define GRANULES_TO_BYTES(n) ((n)<<4)
824 # if CPP_WORDSZ == 64
825 # define GRANULES_TO_WORDS(n) ((n)<<1)
826 # elif CPP_WORDSZ == 32
827 # define GRANULES_TO_WORDS(n) ((n)<<2)
828 # else
829 # define GRANULES_TO_WORDS(n) BYTES_TO_WORDS(GRANULES_TO_BYTES(n))
830 # endif
831 #else
832 # error Bad GRANULE_BYTES value
833 #endif
834
835 /*********************/
836 /* */
837 /* Size Parameters */
838 /* */
839 /*********************/
840
841 /* Heap block size, bytes. Should be power of 2. */
842 /* Incremental GC with MPROTECT_VDB currently requires the */
843 /* page size to be a multiple of HBLKSIZE. Since most modern */
844 /* architectures support variable page sizes down to 4K, and */
845 /* X86 is generally 4K, we now default to 4K, except for */
846 /* Alpha: Seems to be used with 8K pages. */
847 /* SMALL_CONFIG: Want less block-level fragmentation. */
848 #ifndef HBLKSIZE
849 # if defined(LARGE_CONFIG) || !defined(SMALL_CONFIG)
850 # ifdef ALPHA
851 # define CPP_LOG_HBLKSIZE 13
852 # elif defined(SN_TARGET_PSP2)
853 # define CPP_LOG_HBLKSIZE 16 /* page size is set to 64K */
854 # else
855 # define CPP_LOG_HBLKSIZE 12
856 # endif
857 # else
858 # define CPP_LOG_HBLKSIZE 10
859 # endif
860 #else
861 # if HBLKSIZE == 512
862 # define CPP_LOG_HBLKSIZE 9
863 # elif HBLKSIZE == 1024
864 # define CPP_LOG_HBLKSIZE 10
865 # elif HBLKSIZE == 2048
866 # define CPP_LOG_HBLKSIZE 11
867 # elif HBLKSIZE == 4096
868 # define CPP_LOG_HBLKSIZE 12
869 # elif HBLKSIZE == 8192
870 # define CPP_LOG_HBLKSIZE 13
871 # elif HBLKSIZE == 16384
872 # define CPP_LOG_HBLKSIZE 14
873 # else
874 # error fix HBLKSIZE
875 # endif
876 # undef HBLKSIZE
877 #endif
878
879 # define CPP_HBLKSIZE (1 << CPP_LOG_HBLKSIZE)
880 # define LOG_HBLKSIZE ((size_t)CPP_LOG_HBLKSIZE)
881 # define HBLKSIZE ((size_t)CPP_HBLKSIZE)
882
883 #define GC_SQRT_SIZE_MAX ((((size_t)1) << (WORDSZ / 2)) - 1)
884
885 /* Max size objects supported by freelist (larger objects are */
886 /* allocated directly with allchblk(), by rounding to the next */
887 /* multiple of HBLKSIZE). */
888 #define CPP_MAXOBJBYTES (CPP_HBLKSIZE/2)
889 #define MAXOBJBYTES ((size_t)CPP_MAXOBJBYTES)
890 #define CPP_MAXOBJWORDS BYTES_TO_WORDS(CPP_MAXOBJBYTES)
891 #define MAXOBJWORDS ((size_t)CPP_MAXOBJWORDS)
892 #define CPP_MAXOBJGRANULES BYTES_TO_GRANULES(CPP_MAXOBJBYTES)
893 #define MAXOBJGRANULES ((size_t)CPP_MAXOBJGRANULES)
894
895 # define divHBLKSZ(n) ((n) >> LOG_HBLKSIZE)
896
897 # define HBLK_PTR_DIFF(p,q) divHBLKSZ((ptr_t)p - (ptr_t)q)
898 /* Equivalent to subtracting 2 hblk pointers. */
899 /* We do it this way because a compiler should */
900 /* find it hard to use an integer division */
901 /* instead of a shift. The bundled SunOS 4.1 */
902 /* o.w. sometimes pessimizes the subtraction to */
903 /* involve a call to .div. */
904
905 # define modHBLKSZ(n) ((n) & (HBLKSIZE-1))
906
907 # define HBLKPTR(objptr) ((struct hblk *)(((word)(objptr)) \
908 & ~(word)(HBLKSIZE-1)))
909 # define HBLKDISPL(objptr) (((size_t) (objptr)) & (HBLKSIZE-1))
910
911 /* Round up allocation size (in bytes) to a multiple of a granule. */
912 #define ROUNDUP_GRANULE_SIZE(lb) /* lb should have no side-effect */ \
913 (SIZET_SAT_ADD(lb, GRANULE_BYTES - 1) & ~(GRANULE_BYTES - 1))
914
915 /* Round up byte allocation requests to integral number of words, etc. */
916 # define ROUNDED_UP_GRANULES(lb) /* lb should have no side-effect */ \
917 BYTES_TO_GRANULES(SIZET_SAT_ADD(lb, GRANULE_BYTES - 1 + EXTRA_BYTES))
918 # if MAX_EXTRA_BYTES == 0
919 # define SMALL_OBJ(bytes) EXPECT((bytes) <= (MAXOBJBYTES), TRUE)
920 # else
921 # define SMALL_OBJ(bytes) \
922 (EXPECT((bytes) <= (MAXOBJBYTES - MAX_EXTRA_BYTES), TRUE) \
923 || (bytes) <= MAXOBJBYTES - EXTRA_BYTES)
924 /* This really just tests bytes <= MAXOBJBYTES - EXTRA_BYTES. */
925 /* But we try to avoid looking up EXTRA_BYTES. */
926 # endif
927 # define ADD_SLOP(lb) /* lb should have no side-effect */ \
928 SIZET_SAT_ADD(lb, EXTRA_BYTES)
929 # ifndef MIN_WORDS
930 # define MIN_WORDS 2 /* FIXME: obsolete */
931 # endif
932
933 /*
934 * Hash table representation of sets of pages.
935 * Implements a map from aligned HBLKSIZE chunks of the address space to one
936 * bit each.
937 * This assumes it is OK to spuriously set bits, e.g. because multiple
938 * addresses are represented by a single location.
939 * Used by black-listing code, and perhaps by dirty bit maintenance code.
940 */
941
942 # ifdef LARGE_CONFIG
943 # if CPP_WORDSZ == 32
944 # define LOG_PHT_ENTRIES 20 /* Collisions likely at 1M blocks, */
945 /* which is >= 4GB. Each table takes */
946 /* 128KB, some of which may never be */
947 /* touched. */
948 # else
949 # define LOG_PHT_ENTRIES 21 /* Collisions likely at 2M blocks, */
950 /* which is >= 8GB. Each table takes */
951 /* 256KB, some of which may never be */
952 /* touched. */
953 # endif
954 # elif !defined(SMALL_CONFIG)
955 # define LOG_PHT_ENTRIES 18 /* Collisions are likely if heap grows */
956 /* to more than 256K hblks >= 1GB. */
957 /* Each hash table occupies 32K bytes. */
958 /* Even for somewhat smaller heaps, */
959 /* say half that, collisions may be an */
960 /* issue because we blacklist */
961 /* addresses outside the heap. */
962 # else
963 # define LOG_PHT_ENTRIES 15 /* Collisions are likely if heap grows */
964 /* to more than 32K hblks = 128MB. */
965 /* Each hash table occupies 4K bytes. */
966 # endif
967 # define PHT_ENTRIES ((word)1 << LOG_PHT_ENTRIES)
968 # define PHT_SIZE (PHT_ENTRIES >> LOGWL)
969 typedef word page_hash_table[PHT_SIZE];
970
971 # define PHT_HASH(addr) ((((word)(addr)) >> LOG_HBLKSIZE) & (PHT_ENTRIES - 1))
972
973 # define get_pht_entry_from_index(bl, index) \
974 (((bl)[divWORDSZ(index)] >> modWORDSZ(index)) & 1)
975 # define set_pht_entry_from_index(bl, index) \
976 (void)((bl)[divWORDSZ(index)] |= (word)1 << modWORDSZ(index))
977
978 #if defined(THREADS) && defined(AO_HAVE_or)
979 /* And, one more version for GC_add_to_black_list_normal/stack */
980 /* (invoked indirectly by GC_do_local_mark) and */
981 /* async_set_pht_entry_from_index (invoked by GC_dirty or the write */
982 /* fault handler). */
983 # define set_pht_entry_from_index_concurrent(bl, index) \
984 AO_or((volatile AO_t *)&(bl)[divWORDSZ(index)], \
985 (AO_t)((word)1 << modWORDSZ(index)))
986 #endif
987
988
989 /********************************************/
990 /* */
991 /* H e a p B l o c k s */
992 /* */
993 /********************************************/
994
995 /* heap block header */
996 #define HBLKMASK (HBLKSIZE-1)
997
998 #define MARK_BITS_PER_HBLK (HBLKSIZE/GRANULE_BYTES)
999 /* upper bound */
1000 /* We allocate 1 bit per allocation granule. */
1001 /* If MARK_BIT_PER_GRANULE is defined, we use */
1002 /* every nth bit, where n is the number of */
1003 /* allocation granules per object. If */
1004 /* MARK_BIT_PER_OBJ is defined, we only use the */
1005 /* initial group of mark bits, and it is safe */
1006 /* to allocate smaller header for large objects. */
1007
1008 union word_ptr_ao_u {
1009 word w;
1010 signed_word sw;
1011 void *vp;
1012 # ifdef PARALLEL_MARK
1013 volatile AO_t ao;
1014 # endif
1015 };
1016
1017 /* We maintain layout maps for heap blocks containing objects of a given */
1018 /* size. Each entry in this map describes a byte offset and has the */
1019 /* following type. */
1020 struct hblkhdr {
1021 struct hblk * hb_next; /* Link field for hblk free list */
1022 /* and for lists of chunks waiting to be */
1023 /* reclaimed. */
1024 struct hblk * hb_prev; /* Backwards link for free list. */
1025 struct hblk * hb_block; /* The corresponding block. */
1026 unsigned char hb_obj_kind;
1027 /* Kind of objects in the block. Each kind */
1028 /* identifies a mark procedure and a set of */
1029 /* list headers. Sometimes called regions. */
1030 unsigned char hb_flags;
1031 # define IGNORE_OFF_PAGE 1 /* Ignore pointers that do not */
1032 /* point to the first page of */
1033 /* this object. */
1034 # define WAS_UNMAPPED 2 /* This is a free block, which has */
1035 /* been unmapped from the address */
1036 /* space. */
1037 /* GC_remap must be invoked on it */
1038 /* before it can be reallocated. */
1039 /* Only set with USE_MUNMAP. */
1040 # define FREE_BLK 4 /* Block is free, i.e. not in use. */
1041 # ifdef ENABLE_DISCLAIM
1042 # define HAS_DISCLAIM 8
1043 /* This kind has a callback on reclaim. */
1044 # define MARK_UNCONDITIONALLY 0x10
1045 /* Mark from all objects, marked or */
1046 /* not. Used to mark objects needed by */
1047 /* reclaim notifier. */
1048 # endif
1049 # ifdef MARK_BIT_PER_GRANULE
1050 # define LARGE_BLOCK 0x20
1051 # endif
1052 unsigned short hb_last_reclaimed;
1053 /* Value of GC_gc_no when block was */
1054 /* last allocated or swept. May wrap. */
1055 /* For a free block, this is maintained */
1056 /* only for USE_MUNMAP, and indicates */
1057 /* when the header was allocated, or */
1058 /* when the size of the block last */
1059 /* changed. */
1060 # ifdef MARK_BIT_PER_OBJ
1061 unsigned32 hb_inv_sz; /* A good upper bound for 2**32/hb_sz. */
1062 /* For large objects, we use */
1063 /* LARGE_INV_SZ. */
1064 # define LARGE_INV_SZ (1 << 16)
1065 # endif
1066 word hb_sz; /* If in use, size in bytes, of objects in the block. */
1067 /* if free, the size in bytes of the whole block. */
1068 /* We assume that this is convertible to signed_word */
1069 /* without generating a negative result. We avoid */
1070 /* generating free blocks larger than that. */
1071 word hb_descr; /* object descriptor for marking. See */
1072 /* gc_mark.h. */
1073 # ifdef MARK_BIT_PER_GRANULE
1074 unsigned short * hb_map; /* Essentially a table of remainders */
1075 /* mod BYTES_TO_GRANULES(hb_sz), except */
1076 /* for large blocks. See GC_obj_map. */
1077 # endif
1078 # ifdef PARALLEL_MARK
1079 volatile AO_t hb_n_marks; /* Number of set mark bits, excluding */
1080 /* the one always set at the end. */
1081 /* Currently it is concurrently */
1082 /* updated and hence only approximate. */
1083 /* But a zero value does guarantee that */
1084 /* the block contains no marked */
1085 /* objects. */
1086 /* Ensuring this property means that we */
1087 /* never decrement it to zero during a */
1088 /* collection, and hence the count may */
1089 /* be one too high. Due to concurrent */
1090 /* updates, an arbitrary number of */
1091 /* increments, but not all of them (!) */
1092 /* may be lost, hence it may in theory */
1093 /* be much too low. */
1094 /* The count may also be too high if */
1095 /* multiple mark threads mark the */
1096 /* same object due to a race. */
1097 # else
1098 size_t hb_n_marks; /* Without parallel marking, the count */
1099 /* is accurate. */
1100 # endif
1101 # ifdef USE_MARK_BYTES
1102 # define MARK_BITS_SZ (MARK_BITS_PER_HBLK + 1)
1103 /* Unlike the other case, this is in units of bytes. */
1104 /* Since we force double-word alignment, we need at most one */
1105 /* mark bit per 2 words. But we do allocate and set one */
1106 /* extra mark bit to avoid an explicit check for the */
1107 /* partial object at the end of each block. */
1108 union {
1109 char _hb_marks[MARK_BITS_SZ];
1110 /* The i'th byte is 1 if the object */
1111 /* starting at granule i or object i is */
1112 /* marked, 0 o.w. */
1113 /* The mark bit for the "one past the */
1114 /* end" object is always set to avoid a */
1115 /* special case test in the marker. */
1116 word dummy; /* Force word alignment of mark bytes. */
1117 } _mark_byte_union;
1118 # define hb_marks _mark_byte_union._hb_marks
1119 # else
1120 # define MARK_BITS_SZ (MARK_BITS_PER_HBLK/CPP_WORDSZ + 1)
1121 word hb_marks[MARK_BITS_SZ];
1122 # endif /* !USE_MARK_BYTES */
1123 };
1124
1125 # define ANY_INDEX 23 /* "Random" mark bit index for assertions */
1126
1127 /* heap block body */
1128
1129 # define HBLK_WORDS (HBLKSIZE/sizeof(word))
1130 # define HBLK_GRANULES (HBLKSIZE/GRANULE_BYTES)
1131
1132 /* The number of objects in a block dedicated to a certain size. */
1133 /* may erroneously yield zero (instead of one) for large objects. */
1134 # define HBLK_OBJS(sz_in_bytes) (HBLKSIZE/(sz_in_bytes))
1135
1136 struct hblk {
1137 char hb_body[HBLKSIZE];
1138 };
1139
1140 # define HBLK_IS_FREE(hdr) (((hdr) -> hb_flags & FREE_BLK) != 0)
1141
1142 # define OBJ_SZ_TO_BLOCKS(lb) divHBLKSZ((lb) + HBLKSIZE-1)
1143 # define OBJ_SZ_TO_BLOCKS_CHECKED(lb) /* lb should have no side-effect */ \
1144 divHBLKSZ(SIZET_SAT_ADD(lb, HBLKSIZE - 1))
1145 /* Size of block (in units of HBLKSIZE) needed to hold objects of */
1146 /* given lb (in bytes). The checked variant prevents wrap around. */
1147
1148 /* Object free list link */
1149 # define obj_link(p) (*(void **)(p))
1150
1151 # define LOG_MAX_MARK_PROCS 6
1152 # define MAX_MARK_PROCS (1 << LOG_MAX_MARK_PROCS)
1153
1154 /* Root sets. Logically private to mark_rts.c. But we don't want the */
1155 /* tables scanned, so we put them here. */
1156 /* MAX_ROOT_SETS is the maximum number of ranges that can be */
1157 /* registered as static roots. */
1158 # ifdef LARGE_CONFIG
1159 # define MAX_ROOT_SETS 8192
1160 # elif !defined(SMALL_CONFIG)
1161 # define MAX_ROOT_SETS 2048
1162 # else
1163 # define MAX_ROOT_SETS 512
1164 # endif
1165
1166 # define MAX_EXCLUSIONS (MAX_ROOT_SETS/4)
1167 /* Maximum number of segments that can be excluded from root sets. */
1168
1169 /*
1170 * Data structure for excluded static roots.
1171 */
1172 struct exclusion {
1173 ptr_t e_start;
1174 ptr_t e_end;
1175 };
1176
1177 /* Data structure for list of root sets. */
1178 /* We keep a hash table, so that we can filter out duplicate additions. */
1179 /* Under Win32, we need to do a better job of filtering overlaps, so */
1180 /* we resort to sequential search, and pay the price. */
1181 struct roots {
1182 ptr_t r_start;/* multiple of word size */
1183 ptr_t r_end; /* multiple of word size and greater than r_start */
1184 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
1185 struct roots * r_next;
1186 # endif
1187 GC_bool r_tmp;
1188 /* Delete before registering new dynamic libraries */
1189 };
1190
1191 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
1192 /* Size of hash table index to roots. */
1193 # define LOG_RT_SIZE 6
1194 # define RT_SIZE (1 << LOG_RT_SIZE) /* Power of 2, may be != MAX_ROOT_SETS */
1195 #endif
1196
1197 #ifndef MAX_HEAP_SECTS
1198 # ifdef LARGE_CONFIG
1199 # if CPP_WORDSZ > 32
1200 # define MAX_HEAP_SECTS 81920
1201 # else
1202 # define MAX_HEAP_SECTS 7680
1203 # endif
1204 # elif defined(SMALL_CONFIG) && !defined(USE_PROC_FOR_LIBRARIES)
1205 # if defined(PARALLEL_MARK) && (defined(MSWIN32) || defined(CYGWIN32))
1206 # define MAX_HEAP_SECTS 384
1207 # else
1208 # define MAX_HEAP_SECTS 128 /* Roughly 256MB (128*2048*1K) */
1209 # endif
1210 # elif CPP_WORDSZ > 32
1211 # define MAX_HEAP_SECTS 1024 /* Roughly 8GB */
1212 # else
1213 # define MAX_HEAP_SECTS 512 /* Roughly 4GB */
1214 # endif
1215 #endif /* !MAX_HEAP_SECTS */
1216
1217 typedef struct GC_ms_entry {
1218 ptr_t mse_start; /* First word of object, word aligned. */
1219 union word_ptr_ao_u mse_descr;
1220 /* Descriptor; low order two bits are tags, */
1221 /* as described in gc_mark.h. */
1222 } mse;
1223
1224 /* Lists of all heap blocks and free lists */
1225 /* as well as other random data structures */
1226 /* that should not be scanned by the */
1227 /* collector. */
1228 /* These are grouped together in a struct */
1229 /* so that they can be easily skipped by the */
1230 /* GC_mark routine. */
1231 /* The ordering is weird to make GC_malloc */
1232 /* faster by keeping the important fields */
1233 /* sufficiently close together that a */
1234 /* single load of a base register will do. */
1235 /* Scalars that could easily appear to */
1236 /* be pointers are also put here. */
1237 /* The main fields should precede any */
1238 /* conditionally included fields, so that */
1239 /* gc_inl.h will work even if a different set */
1240 /* of macros is defined when the client is */
1241 /* compiled. */
1242
1243 struct _GC_arrays {
1244 word _heapsize; /* Heap size in bytes (value never goes down). */
1245 word _requested_heapsize; /* Heap size due to explicit expansion. */
1246 ptr_t _last_heap_addr;
1247 ptr_t _prev_heap_addr;
1248 word _large_free_bytes;
1249 /* Total bytes contained in blocks on large object free */
1250 /* list. */
1251 word _large_allocd_bytes;
1252 /* Total number of bytes in allocated large objects blocks. */
1253 /* For the purposes of this counter and the next one only, a */
1254 /* large object is one that occupies a block of at least */
1255 /* 2*HBLKSIZE. */
1256 word _max_large_allocd_bytes;
1257 /* Maximum number of bytes that were ever allocated in */
1258 /* large object blocks. This is used to help decide when it */
1259 /* is safe to split up a large block. */
1260 word _bytes_allocd_before_gc;
1261 /* Number of bytes allocated before this */
1262 /* collection cycle. */
1263 # ifndef SEPARATE_GLOBALS
1264 # define GC_bytes_allocd GC_arrays._bytes_allocd
1265 word _bytes_allocd;
1266 /* Number of bytes allocated during this collection cycle. */
1267 # endif
1268 word _bytes_dropped;
1269 /* Number of black-listed bytes dropped during GC cycle */
1270 /* as a result of repeated scanning during allocation */
1271 /* attempts. These are treated largely as allocated, */
1272 /* even though they are not useful to the client. */
1273 word _bytes_finalized;
1274 /* Approximate number of bytes in objects (and headers) */
1275 /* that became ready for finalization in the last */
1276 /* collection. */
1277 word _bytes_freed;
1278 /* Number of explicitly deallocated bytes of memory */
1279 /* since last collection. */
1280 word _finalizer_bytes_freed;
1281 /* Bytes of memory explicitly deallocated while */
1282 /* finalizers were running. Used to approximate memory */
1283 /* explicitly deallocated by finalizers. */
1284 ptr_t _scratch_end_ptr;
1285 ptr_t _scratch_last_end_ptr;
1286 /* Used by headers.c, and can easily appear to point to */
1287 /* heap. Also used by GC_register_dynamic_libraries(). */
1288 mse *_mark_stack;
1289 /* Limits of stack for GC_mark routine. All ranges */
1290 /* between GC_mark_stack (incl.) and GC_mark_stack_top */
1291 /* (incl.) still need to be marked from. */
1292 mse *_mark_stack_limit;
1293 # ifdef PARALLEL_MARK
1294 mse *volatile _mark_stack_top;
1295 /* Updated only with mark lock held, but read asynchronously. */
1296 /* TODO: Use union to avoid casts to AO_t */
1297 # else
1298 mse *_mark_stack_top;
1299 # endif
1300 word _composite_in_use; /* Number of bytes in the accessible */
1301 /* composite objects. */
1302 word _atomic_in_use; /* Number of bytes in the accessible */
1303 /* atomic objects. */
1304 # ifdef USE_MUNMAP
1305 # define GC_unmapped_bytes GC_arrays._unmapped_bytes
1306 word _unmapped_bytes;
1307 # else
1308 # define GC_unmapped_bytes 0
1309 # endif
1310 bottom_index * _all_nils;
1311 # ifdef ENABLE_TRACE
1312 # define GC_trace_addr GC_arrays._trace_addr
1313 ptr_t _trace_addr;
1314 # endif
1315 GC_mark_proc _mark_procs[MAX_MARK_PROCS];
1316 /* Table of user-defined mark procedures. There is */
1317 /* a small number of these, which can be referenced */
1318 /* by DS_PROC mark descriptors. See gc_mark.h. */
1319 char _modws_valid_offsets[sizeof(word)];
1320 /* GC_valid_offsets[i] ==> */
1321 /* GC_modws_valid_offsets[i%sizeof(word)] */
1322 # if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
1323 # define GC_root_index GC_arrays._root_index
1324 struct roots * _root_index[RT_SIZE];
1325 # endif
1326 # ifdef SAVE_CALL_CHAIN
1327 # define GC_last_stack GC_arrays._last_stack
1328 struct callinfo _last_stack[NFRAMES];
1329 /* Stack at last garbage collection. Useful for */
1330 /* debugging mysterious object disappearances. In the */
1331 /* multi-threaded case, we currently only save the */
1332 /* calling stack. */
1333 # endif
1334 # ifndef SEPARATE_GLOBALS
1335 # define GC_objfreelist GC_arrays._objfreelist
1336 void *_objfreelist[MAXOBJGRANULES+1];
1337 /* free list for objects */
1338 # define GC_aobjfreelist GC_arrays._aobjfreelist
1339 void *_aobjfreelist[MAXOBJGRANULES+1];
1340 /* free list for atomic objects */
1341 # endif
1342 void *_uobjfreelist[MAXOBJGRANULES+1];
1343 /* Uncollectible but traced objects. */
1344 /* Objects on this and _auobjfreelist */
1345 /* are always marked, except during */
1346 /* garbage collections. */
1347 # ifdef GC_ATOMIC_UNCOLLECTABLE
1348 # define GC_auobjfreelist GC_arrays._auobjfreelist
1349 void *_auobjfreelist[MAXOBJGRANULES+1];
1350 /* Atomic uncollectible but traced objects. */
1351 # endif
1352 size_t _size_map[MAXOBJBYTES+1];
1353 /* Number of granules to allocate when asked for a certain */
1354 /* number of bytes. Should be accessed with the allocation */
1355 /* lock held. */
1356 # ifdef MARK_BIT_PER_GRANULE
1357 # define GC_obj_map GC_arrays._obj_map
1358 unsigned short * _obj_map[MAXOBJGRANULES + 1];
1359 /* If not NULL, then a pointer to a map of valid */
1360 /* object addresses. */
1361 /* _obj_map[sz_in_granules][i] is */
1362 /* i % sz_in_granules. */
1363 /* This is now used purely to replace a */
1364 /* division in the marker by a table lookup. */
1365 /* _obj_map[0] is used for large objects and */
1366 /* contains all nonzero entries. This gets us */
1367 /* out of the marker fast path without an extra */
1368 /* test. */
1369 # define MAP_LEN BYTES_TO_GRANULES(HBLKSIZE)
1370 # endif
1371 # define VALID_OFFSET_SZ HBLKSIZE
1372 char _valid_offsets[VALID_OFFSET_SZ];
1373 /* GC_valid_offsets[i] == TRUE ==> i */
1374 /* is registered as a displacement. */
1375 # if defined(PROC_VDB) || defined(MPROTECT_VDB) \
1376 || defined(GWW_VDB) || defined(MANUAL_VDB)
1377 # define GC_grungy_pages GC_arrays._grungy_pages
1378 page_hash_table _grungy_pages; /* Pages that were dirty at last */
1379 /* GC_read_dirty. */
1380 # endif
1381 # if defined(MPROTECT_VDB) || defined(MANUAL_VDB)
1382 # define GC_dirty_pages GC_arrays._dirty_pages
1383 volatile page_hash_table _dirty_pages;
1384 /* Pages dirtied since last GC_read_dirty. */
1385 # endif
1386 # if (defined(CHECKSUMS) && defined(GWW_VDB)) || defined(PROC_VDB)
1387 # define GC_written_pages GC_arrays._written_pages
1388 page_hash_table _written_pages; /* Pages ever dirtied */
1389 # endif
1390 # define GC_heap_sects GC_arrays._heap_sects
1391 struct HeapSect {
1392 ptr_t hs_start;
1393 size_t hs_bytes;
1394 } _heap_sects[MAX_HEAP_SECTS]; /* Heap segments potentially */
1395 /* client objects. */
1396 # if defined(USE_PROC_FOR_LIBRARIES)
1397 # define GC_our_memory GC_arrays._our_memory
1398 struct HeapSect _our_memory[MAX_HEAP_SECTS];
1399 /* All GET_MEM allocated */
1400 /* memory. Includes block */
1401 /* headers and the like. */
1402 # endif
1403 # if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
1404 # define GC_heap_bases GC_arrays._heap_bases
1405 ptr_t _heap_bases[MAX_HEAP_SECTS];
1406 /* Start address of memory regions obtained from kernel. */
1407 # endif
1408 # ifdef MSWINCE
1409 # define GC_heap_lengths GC_arrays._heap_lengths
1410 word _heap_lengths[MAX_HEAP_SECTS];
1411 /* Committed lengths of memory regions obtained from kernel. */
1412 # endif
1413 struct roots _static_roots[MAX_ROOT_SETS];
1414 struct exclusion _excl_table[MAX_EXCLUSIONS];
1415 /* Block header index; see gc_headers.h */
1416 bottom_index * _top_index[TOP_SZ];
1417 };
1418
1419 GC_API_PRIV GC_FAR struct _GC_arrays GC_arrays;
1420
1421 #define GC_all_nils GC_arrays._all_nils
1422 #define GC_atomic_in_use GC_arrays._atomic_in_use
1423 #define GC_bytes_allocd_before_gc GC_arrays._bytes_allocd_before_gc
1424 #define GC_bytes_dropped GC_arrays._bytes_dropped
1425 #define GC_bytes_finalized GC_arrays._bytes_finalized
1426 #define GC_bytes_freed GC_arrays._bytes_freed
1427 #define GC_composite_in_use GC_arrays._composite_in_use
1428 #define GC_excl_table GC_arrays._excl_table
1429 #define GC_finalizer_bytes_freed GC_arrays._finalizer_bytes_freed
1430 #define GC_heapsize GC_arrays._heapsize
1431 #define GC_large_allocd_bytes GC_arrays._large_allocd_bytes
1432 #define GC_large_free_bytes GC_arrays._large_free_bytes
1433 #define GC_last_heap_addr GC_arrays._last_heap_addr
1434 #define GC_mark_stack GC_arrays._mark_stack
1435 #define GC_mark_stack_limit GC_arrays._mark_stack_limit
1436 #define GC_mark_stack_top GC_arrays._mark_stack_top
1437 #define GC_mark_procs GC_arrays._mark_procs
1438 #define GC_max_large_allocd_bytes GC_arrays._max_large_allocd_bytes
1439 #define GC_modws_valid_offsets GC_arrays._modws_valid_offsets
1440 #define GC_prev_heap_addr GC_arrays._prev_heap_addr
1441 #define GC_requested_heapsize GC_arrays._requested_heapsize
1442 #define GC_scratch_end_ptr GC_arrays._scratch_end_ptr
1443 #define GC_scratch_last_end_ptr GC_arrays._scratch_last_end_ptr
1444 #define GC_size_map GC_arrays._size_map
1445 #define GC_static_roots GC_arrays._static_roots
1446 #define GC_top_index GC_arrays._top_index
1447 #define GC_uobjfreelist GC_arrays._uobjfreelist
1448 #define GC_valid_offsets GC_arrays._valid_offsets
1449
1450 #define beginGC_arrays ((ptr_t)(&GC_arrays))
1451 #define endGC_arrays (((ptr_t)(&GC_arrays)) + (sizeof GC_arrays))
1452 #define USED_HEAP_SIZE (GC_heapsize - GC_large_free_bytes)
1453
1454 /* Object kinds: */
1455 #ifndef MAXOBJKINDS
1456 # define MAXOBJKINDS 16
1457 #endif
1458 GC_EXTERN struct obj_kind {
1459 void **ok_freelist; /* Array of free list headers for this kind of */
1460 /* object. Point either to GC_arrays or to */
1461 /* storage allocated with GC_scratch_alloc. */
1462 struct hblk **ok_reclaim_list;
1463 /* List headers for lists of blocks waiting to */
1464 /* be swept. Indexed by object size in */
1465 /* granules. */
1466 word ok_descriptor; /* Descriptor template for objects in this */
1467 /* block. */
1468 GC_bool ok_relocate_descr;
1469 /* Add object size in bytes to descriptor */
1470 /* template to obtain descriptor. Otherwise */
1471 /* template is used as is. */
1472 GC_bool ok_init; /* Clear objects before putting them on the free list. */
1473 # ifdef ENABLE_DISCLAIM
1474 GC_bool ok_mark_unconditionally;
1475 /* Mark from all, including unmarked, objects */
1476 /* in block. Used to protect objects reachable */
1477 /* from reclaim notifiers. */
1478 int (GC_CALLBACK *ok_disclaim_proc)(void * /*obj*/);
1479 /* The disclaim procedure is called before obj */
1480 /* is reclaimed, but must also tolerate being */
1481 /* called with object from freelist. Non-zero */
1482 /* exit prevents object from being reclaimed. */
1483 # define OK_DISCLAIM_INITZ /* comma */, FALSE, 0
1484 # else
1485 # define OK_DISCLAIM_INITZ /* empty */
1486 # endif /* !ENABLE_DISCLAIM */
1487 } GC_obj_kinds[MAXOBJKINDS];
1488
1489 #define beginGC_obj_kinds ((ptr_t)(&GC_obj_kinds))
1490 #define endGC_obj_kinds (beginGC_obj_kinds + (sizeof GC_obj_kinds))
1491
1492 /* Variables that used to be in GC_arrays, but need to be accessed by */
1493 /* inline allocation code. If they were in GC_arrays, the inlined */
1494 /* allocation code would include GC_arrays offsets (as it did), which */
1495 /* introduce maintenance problems. */
1496
1497 #ifdef SEPARATE_GLOBALS
1498 extern word GC_bytes_allocd;
1499 /* Number of bytes allocated during this collection cycle. */
1500 extern ptr_t GC_objfreelist[MAXOBJGRANULES+1];
1501 /* free list for NORMAL objects */
1502 # define beginGC_objfreelist ((ptr_t)(&GC_objfreelist))
1503 # define endGC_objfreelist (beginGC_objfreelist + sizeof(GC_objfreelist))
1504
1505 extern ptr_t GC_aobjfreelist[MAXOBJGRANULES+1];
1506 /* free list for atomic (PTRFREE) objects */
1507 # define beginGC_aobjfreelist ((ptr_t)(&GC_aobjfreelist))
1508 # define endGC_aobjfreelist (beginGC_aobjfreelist + sizeof(GC_aobjfreelist))
1509 #endif /* SEPARATE_GLOBALS */
1510
1511 /* Predefined kinds: */
1512 #define PTRFREE 0
1513 #define NORMAL 1
1514 #define UNCOLLECTABLE 2
1515 #ifdef GC_ATOMIC_UNCOLLECTABLE
1516 # define AUNCOLLECTABLE 3
1517 # define IS_UNCOLLECTABLE(k) (((k) & ~1) == UNCOLLECTABLE)
1518 # define GC_N_KINDS_INITIAL_VALUE 4
1519 #else
1520 # define IS_UNCOLLECTABLE(k) ((k) == UNCOLLECTABLE)
1521 # define GC_N_KINDS_INITIAL_VALUE 3
1522 #endif
1523
1524 GC_EXTERN unsigned GC_n_kinds;
1525
1526 GC_EXTERN word GC_n_heap_sects; /* Number of separately added heap */
1527 /* sections. */
1528
1529 #ifdef USE_PROC_FOR_LIBRARIES
1530 GC_EXTERN word GC_n_memory; /* Number of GET_MEM allocated memory */
1531 /* sections. */
1532 #endif
1533
1534 GC_EXTERN size_t GC_page_size;
1535
1536 /* Round up allocation size to a multiple of a page size. */
1537 /* GC_setpagesize() is assumed to be already invoked. */
1538 #define ROUNDUP_PAGESIZE(lb) /* lb should have no side-effect */ \
1539 (SIZET_SAT_ADD(lb, GC_page_size - 1) & ~(GC_page_size - 1))
1540
1541 /* Same as above but used to make GET_MEM() argument safe. */
1542 #ifdef MMAP_SUPPORTED
1543 # define ROUNDUP_PAGESIZE_IF_MMAP(lb) ROUNDUP_PAGESIZE(lb)
1544 #else
1545 # define ROUNDUP_PAGESIZE_IF_MMAP(lb) (lb)
1546 #endif
1547
1548 #if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
1549 struct _SYSTEM_INFO;
1550 GC_EXTERN struct _SYSTEM_INFO GC_sysinfo;
1551 GC_INNER GC_bool GC_is_heap_base(void *p);
1552 #endif
1553
1554 GC_EXTERN word GC_black_list_spacing;
1555 /* Average number of bytes between blacklisted */
1556 /* blocks. Approximate. */
1557 /* Counts only blocks that are */
1558 /* "stack-blacklisted", i.e. that are */
1559 /* problematic in the interior of an object. */
1560
1561 #ifdef GC_GCJ_SUPPORT
1562 extern struct hblk * GC_hblkfreelist[];
1563 extern word GC_free_bytes[]; /* Both remain visible to GNU GCJ. */
1564 #endif
1565
1566 #ifdef GC_DISABLE_INCREMENTAL
1567 # define GC_incremental FALSE
1568 /* Hopefully allow optimizer to remove some code. */
1569 # define TRUE_INCREMENTAL FALSE
1570 #else
1571 GC_EXTERN GC_bool GC_incremental;
1572 /* Using incremental/generational collection. */
1573 /* Assumes dirty bits are being maintained. */
1574 # define TRUE_INCREMENTAL \
1575 (GC_incremental && GC_time_limit != GC_TIME_UNLIMITED)
1576 /* True incremental, not just generational, mode */
1577 #endif /* !GC_DISABLE_INCREMENTAL */
1578
1579 GC_EXTERN word GC_root_size; /* Total size of registered root sections. */
1580
1581 GC_EXTERN GC_bool GC_debugging_started;
1582 /* GC_debug_malloc has been called. */
1583
1584 /* This is used by GC_do_blocking[_inner](). */
1585 struct blocking_data {
1586 GC_fn_type fn;
1587 void * client_data; /* and result */
1588 };
1589
1590 /* This is used by GC_call_with_gc_active(), GC_push_all_stack_sections(). */
1591 struct GC_traced_stack_sect_s {
1592 ptr_t saved_stack_ptr;
1593 # ifdef IA64
1594 ptr_t saved_backing_store_ptr;
1595 ptr_t backing_store_end;
1596 # endif
1597 struct GC_traced_stack_sect_s *prev;
1598 };
1599
1600 #ifdef THREADS
1601 /* Process all "traced stack sections" - scan entire stack except for */
1602 /* frames belonging to the user functions invoked by GC_do_blocking. */
1603 GC_INNER void GC_push_all_stack_sections(ptr_t lo, ptr_t hi,
1604 struct GC_traced_stack_sect_s *traced_stack_sect);
1605 GC_EXTERN word GC_total_stacksize; /* updated on every push_all_stacks */
1606 #else
1607 GC_EXTERN ptr_t GC_blocked_sp;
1608 GC_EXTERN struct GC_traced_stack_sect_s *GC_traced_stack_sect;
1609 /* Points to the "frame" data held in stack by */
1610 /* the innermost GC_call_with_gc_active(). */
1611 /* NULL if no such "frame" active. */
1612 #endif /* !THREADS */
1613
1614 #ifdef IA64
1615 /* Similar to GC_push_all_stack_sections() but for IA-64 registers store. */
1616 GC_INNER void GC_push_all_register_sections(ptr_t bs_lo, ptr_t bs_hi,
1617 int eager, struct GC_traced_stack_sect_s *traced_stack_sect);
1618 #endif
1619
1620 /* Marks are in a reserved area in */
1621 /* each heap block. Each word has one mark bit associated */
1622 /* with it. Only those corresponding to the beginning of an */
1623 /* object are used. */
1624
1625 /* Mark bit operations */
1626
1627 /*
1628 * Retrieve, set, clear the nth mark bit in a given heap block.
1629 *
1630 * (Recall that bit n corresponds to nth object or allocation granule
1631 * relative to the beginning of the block, including unused words)
1632 */
1633
1634 #ifdef USE_MARK_BYTES
1635 # define mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n])
1636 # define set_mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n] = 1)
1637 # define clear_mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n] = 0)
1638 #else
1639 /* Set mark bit correctly, even if mark bits may be concurrently */
1640 /* accessed. */
1641 # if defined(PARALLEL_MARK) || (defined(THREAD_SANITIZER) && defined(THREADS))
1642 /* Workaround TSan false positive: there is no race between */
1643 /* mark_bit_from_hdr and set_mark_bit_from_hdr when n is different */
1644 /* (alternatively, USE_MARK_BYTES could be used). If TSan is off, */
1645 /* AO_or() is used only if we set USE_MARK_BITS explicitly. */
1646 # define OR_WORD(addr, bits) AO_or((volatile AO_t *)(addr), (AO_t)(bits))
1647 # else
1648 # define OR_WORD(addr, bits) (void)(*(addr) |= (bits))
1649 # endif
1650 # define mark_bit_from_hdr(hhdr,n) \
1651 (((hhdr)->hb_marks[divWORDSZ(n)] >> modWORDSZ(n)) & (word)1)
1652 # define set_mark_bit_from_hdr(hhdr,n) \
1653 OR_WORD((hhdr)->hb_marks+divWORDSZ(n), (word)1 << modWORDSZ(n))
1654 # define clear_mark_bit_from_hdr(hhdr,n) \
1655 ((hhdr)->hb_marks[divWORDSZ(n)] &= ~((word)1 << modWORDSZ(n)))
1656 #endif /* !USE_MARK_BYTES */
1657
1658 #ifdef MARK_BIT_PER_OBJ
1659 # define MARK_BIT_NO(offset, sz) (((word)(offset))/(sz))
1660 /* Get the mark bit index corresponding to the given byte */
1661 /* offset and size (in bytes). */
1662 # define MARK_BIT_OFFSET(sz) 1
1663 /* Spacing between useful mark bits. */
1664 # define IF_PER_OBJ(x) x
1665 # define FINAL_MARK_BIT(sz) ((sz) > MAXOBJBYTES? 1 : HBLK_OBJS(sz))
1666 /* Position of final, always set, mark bit. */
1667 #else /* MARK_BIT_PER_GRANULE */
1668 # define MARK_BIT_NO(offset, sz) BYTES_TO_GRANULES((word)(offset))
1669 # define MARK_BIT_OFFSET(sz) BYTES_TO_GRANULES(sz)
1670 # define IF_PER_OBJ(x)
1671 # define FINAL_MARK_BIT(sz) \
1672 ((sz) > MAXOBJBYTES ? MARK_BITS_PER_HBLK \
1673 : BYTES_TO_GRANULES((sz) * HBLK_OBJS(sz)))
1674 #endif
1675
1676 /* Important internal collector routines */
1677
1678 GC_INNER ptr_t GC_approx_sp(void);
1679
1680 GC_INNER GC_bool GC_should_collect(void);
1681
1682 void GC_apply_to_all_blocks(void (*fn)(struct hblk *h, word client_data),
1683 word client_data);
1684 /* Invoke fn(hbp, client_data) for each */
1685 /* allocated heap block. */
1686 GC_INNER struct hblk * GC_next_used_block(struct hblk * h);
1687 /* Return first in-use block >= h */
1688 GC_INNER struct hblk * GC_prev_block(struct hblk * h);
1689 /* Return last block <= h. Returned block */
1690 /* is managed by GC, but may or may not be in */
1691 /* use. */
1692 GC_INNER void GC_mark_init(void);
1693 GC_INNER void GC_clear_marks(void);
1694 /* Clear mark bits for all heap objects. */
1695 GC_INNER void GC_invalidate_mark_state(void);
1696 /* Tell the marker that marked */
1697 /* objects may point to unmarked */
1698 /* ones, and roots may point to */
1699 /* unmarked objects. Reset mark stack. */
1700 GC_INNER GC_bool GC_mark_some(ptr_t cold_gc_frame);
1701 /* Perform about one pages worth of marking */
1702 /* work of whatever kind is needed. Returns */
1703 /* quickly if no collection is in progress. */
1704 /* Return TRUE if mark phase finished. */
1705 GC_INNER void GC_initiate_gc(void);
1706 /* initiate collection. */
1707 /* If the mark state is invalid, this */
1708 /* becomes full collection. Otherwise */
1709 /* it's partial. */
1710
1711 GC_INNER GC_bool GC_collection_in_progress(void);
1712 /* Collection is in progress, or was abandoned. */
1713
1714 #define GC_PUSH_ALL_SYM(sym) \
1715 GC_push_all((/* no volatile */ void *)&(sym), \
1716 (/* no volatile */ void *)(&(sym) + 1))
1717
1718 GC_INNER void GC_push_all_stack(ptr_t b, ptr_t t);
1719 /* As GC_push_all but consider */
1720 /* interior pointers as valid. */
1721
1722 #if defined(WRAP_MARK_SOME) && defined(PARALLEL_MARK)
1723 /* GC_mark_local does not handle memory protection faults yet. So, */
1724 /* the static data regions are scanned immediately by GC_push_roots. */
1725 GC_INNER void GC_push_conditional_eager(void *bottom, void *top,
1726 GC_bool all);
1727 #endif
1728
1729 /* In the threads case, we push part of the current thread stack */
1730 /* with GC_push_all_eager when we push the registers. This gets the */
1731 /* callee-save registers that may disappear. The remainder of the */
1732 /* stacks are scheduled for scanning in *GC_push_other_roots, which */
1733 /* is thread-package-specific. */
1734
1735 GC_INNER void GC_push_roots(GC_bool all, ptr_t cold_gc_frame);
1736 /* Push all or dirty roots. */
1737
1738 GC_API_PRIV GC_push_other_roots_proc GC_push_other_roots;
1739 /* Push system or application specific roots */
1740 /* onto the mark stack. In some environments */
1741 /* (e.g. threads environments) this is */
1742 /* predefined to be non-zero. A client */
1743 /* supplied replacement should also call the */
1744 /* original function. Remains externally */
1745 /* visible as used by some well-known 3rd-party */
1746 /* software (e.g., ECL) currently. */
1747
1748 #ifdef THREADS
1749 void GC_push_thread_structures(void);
1750 #endif
1751 GC_EXTERN void (*GC_push_typed_structures)(void);
1752 /* A pointer such that we can avoid linking in */
1753 /* the typed allocation support if unused. */
1754
1755 GC_INNER void GC_with_callee_saves_pushed(void (*fn)(ptr_t, void *),
1756 volatile ptr_t arg);
1757
1758 #if defined(SPARC) || defined(IA64)
1759 /* Cause all stacked registers to be saved in memory. Return a */
1760 /* pointer to the top of the corresponding memory stack. */
1761 ptr_t GC_save_regs_in_stack(void);
1762 #endif
1763 /* Push register contents onto mark stack. */
1764
1765 #if defined(MSWIN32) || defined(MSWINCE)
1766 void __cdecl GC_push_one(word p);
1767 #else
1768 void GC_push_one(word p);
1769 /* If p points to an object, mark it */
1770 /* and push contents on the mark stack */
1771 /* Pointer recognition test always */
1772 /* accepts interior pointers, i.e. this */
1773 /* is appropriate for pointers found on */
1774 /* stack. */
1775 #endif
1776
1777 #if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
1778 GC_INNER void GC_mark_and_push_stack(ptr_t p, ptr_t source);
1779 /* Ditto, omits plausibility test */
1780 #else
1781 GC_INNER void GC_mark_and_push_stack(ptr_t p);
1782 #endif
1783
1784 GC_INNER void GC_clear_hdr_marks(hdr * hhdr);
1785 /* Clear the mark bits in a header */
1786 GC_INNER void GC_set_hdr_marks(hdr * hhdr);
1787 /* Set the mark bits in a header */
1788 GC_INNER void GC_set_fl_marks(ptr_t p);
1789 /* Set all mark bits associated with */
1790 /* a free list. */
1791 #if defined(GC_ASSERTIONS) && defined(THREAD_LOCAL_ALLOC)
1792 void GC_check_fl_marks(void **);
1793 /* Check that all mark bits */
1794 /* associated with a free list are */
1795 /* set. Abort if not. */
1796 #endif
1797 void GC_add_roots_inner(ptr_t b, ptr_t e, GC_bool tmp);
1798 #ifdef USE_PROC_FOR_LIBRARIES
1799 GC_INNER void GC_remove_roots_subregion(ptr_t b, ptr_t e);
1800 #endif
1801 GC_INNER void GC_exclude_static_roots_inner(void *start, void *finish);
1802 #if defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
1803 || defined(CYGWIN32) || defined(PCR)
1804 GC_INNER void GC_register_dynamic_libraries(void);
1805 /* Add dynamic library data sections to the root set. */
1806 #endif
1807 GC_INNER void GC_cond_register_dynamic_libraries(void);
1808 /* Remove and reregister dynamic libraries if we're */
1809 /* configured to do that at each GC. */
1810
1811 /* Machine dependent startup routines */
1812 ptr_t GC_get_main_stack_base(void); /* Cold end of stack. */
1813 #ifdef IA64
1814 GC_INNER ptr_t GC_get_register_stack_base(void);
1815 /* Cold end of register stack. */
1816 #endif
1817 void GC_register_data_segments(void);
1818
1819 #ifdef THREADS
1820 GC_INNER void GC_thr_init(void);
1821 GC_INNER void GC_init_parallel(void);
1822 #else
1823 GC_INNER GC_bool GC_is_static_root(void *p);
1824 /* Is the address p in one of the registered static */
1825 /* root sections? */
1826 # ifdef TRACE_BUF
1827 void GC_add_trace_entry(char *kind, word arg1, word arg2);
1828 # endif
1829 #endif /* !THREADS */
1830
1831 /* Black listing: */
1832 #ifdef PRINT_BLACK_LIST
1833 GC_INNER void GC_add_to_black_list_normal(word p, ptr_t source);
1834 /* Register bits as a possible future false */
1835 /* reference from the heap or static data */
1836 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1837 if (GC_all_interior_pointers) { \
1838 GC_add_to_black_list_stack((word)(bits), (source)); \
1839 } else \
1840 GC_add_to_black_list_normal((word)(bits), (source))
1841 GC_INNER void GC_add_to_black_list_stack(word p, ptr_t source);
1842 # define GC_ADD_TO_BLACK_LIST_STACK(bits, source) \
1843 GC_add_to_black_list_stack((word)(bits), (source))
1844 #else
1845 GC_INNER void GC_add_to_black_list_normal(word p);
1846 # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
1847 if (GC_all_interior_pointers) { \
1848 GC_add_to_black_list_stack((word)(bits)); \
1849 } else \
1850 GC_add_to_black_list_normal((word)(bits))
1851 GC_INNER void GC_add_to_black_list_stack(word p);
1852 # define GC_ADD_TO_BLACK_LIST_STACK(bits, source) \
1853 GC_add_to_black_list_stack((word)(bits))
1854 #endif /* PRINT_BLACK_LIST */
1855
1856 struct hblk * GC_is_black_listed(struct hblk * h, word len);
1857 /* If there are likely to be false references */
1858 /* to a block starting at h of the indicated */
1859 /* length, then return the next plausible */
1860 /* starting location for h that might avoid */
1861 /* these false references. Remains externally */
1862 /* visible as used by GNU GCJ currently. */
1863
1864 GC_INNER void GC_promote_black_lists(void);
1865 /* Declare an end to a black listing phase. */
1866 GC_INNER void GC_unpromote_black_lists(void);
1867 /* Approximately undo the effect of the above. */
1868 /* This actually loses some information, but */
1869 /* only in a reasonably safe way. */
1870
1871 GC_INNER ptr_t GC_scratch_alloc(size_t bytes);
1872 /* GC internal memory allocation for */
1873 /* small objects. Deallocation is not */
1874 /* possible. May return NULL. */
1875
1876 #ifdef GWW_VDB
1877 /* GC_scratch_recycle_no_gww() not used. */
1878 #else
1879 # define GC_scratch_recycle_no_gww GC_scratch_recycle_inner
1880 #endif
1881 GC_INNER void GC_scratch_recycle_inner(void *ptr, size_t bytes);
1882 /* Reuse the memory region by the heap. */
1883
1884 /* Heap block layout maps: */
1885 GC_INNER GC_bool GC_add_map_entry(size_t sz);
1886 /* Add a heap block map for objects of */
1887 /* size sz to obj_map. */
1888 /* Return FALSE on failure. */
1889 GC_INNER void GC_register_displacement_inner(size_t offset);
1890 /* Version of GC_register_displacement */
1891 /* that assumes lock is already held. */
1892
1893 /* hblk allocation: */
1894 GC_INNER void GC_new_hblk(size_t size_in_granules, int kind);
1895 /* Allocate a new heap block, and build */
1896 /* a free list in it. */
1897
1898 GC_INNER ptr_t GC_build_fl(struct hblk *h, size_t words, GC_bool clear,
1899 ptr_t list);
1900 /* Build a free list for objects of */
1901 /* size sz in block h. Append list to */
1902 /* end of the free lists. Possibly */
1903 /* clear objects on the list. Normally */
1904 /* called by GC_new_hblk, but also */
1905 /* called explicitly without GC lock. */
1906
1907 GC_INNER struct hblk * GC_allochblk(size_t size_in_bytes, int kind,
1908 unsigned flags);
1909 /* Allocate a heap block, inform */
1910 /* the marker that block is valid */
1911 /* for objects of indicated size. */
1912
1913 GC_INNER ptr_t GC_alloc_large(size_t lb, int k, unsigned flags);
1914 /* Allocate a large block of size lb bytes. */
1915 /* The block is not cleared. */
1916 /* Flags is 0 or IGNORE_OFF_PAGE. */
1917 /* Calls GC_allchblk to do the actual */
1918 /* allocation, but also triggers GC and/or */
1919 /* heap expansion as appropriate. */
1920 /* Does not update GC_bytes_allocd, but does */
1921 /* other accounting. */
1922
1923 GC_INNER void GC_freehblk(struct hblk * p);
1924 /* Deallocate a heap block and mark it */
1925 /* as invalid. */
1926
1927 /* Misc GC: */
1928 GC_INNER GC_bool GC_expand_hp_inner(word n);
1929 GC_INNER void GC_start_reclaim(GC_bool abort_if_found);
1930 /* Restore unmarked objects to free */
1931 /* lists, or (if abort_if_found is */
1932 /* TRUE) report them. */
1933 /* Sweeping of small object pages is */
1934 /* largely deferred. */
1935 GC_INNER void GC_continue_reclaim(word sz, int kind);
1936 /* Sweep pages of the given size and */
1937 /* kind, as long as possible, and */
1938 /* as long as the corr. free list is */
1939 /* empty. Sz is in granules. */
1940
1941 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old);
1942 /* Reclaim all blocks. Abort (in a */
1943 /* consistent state) if f returns TRUE. */
1944 GC_INNER ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
1945 GC_bool init, ptr_t list,
1946 signed_word *count);
1947 /* Rebuild free list in hbp with */
1948 /* header hhdr, with objects of size sz */
1949 /* bytes. Add list to the end of the */
1950 /* free list. Add the number of */
1951 /* reclaimed bytes to *count. */
1952 GC_INNER GC_bool GC_block_empty(hdr * hhdr);
1953 /* Block completely unmarked? */
1954 GC_INNER int GC_CALLBACK GC_never_stop_func(void);
1955 /* Always returns 0 (FALSE). */
1956 GC_INNER GC_bool GC_try_to_collect_inner(GC_stop_func f);
1957
1958 /* Collect; caller must have acquired */
1959 /* lock. Collection is aborted if f */
1960 /* returns TRUE. Returns TRUE if it */
1961 /* completes successfully. */
1962 #define GC_gcollect_inner() \
1963 (void)GC_try_to_collect_inner(GC_never_stop_func)
1964
1965 GC_EXTERN GC_bool GC_is_initialized; /* GC_init() has been run. */
1966
1967 GC_INNER void GC_collect_a_little_inner(int n);
1968 /* Do n units worth of garbage */
1969 /* collection work, if appropriate. */
1970 /* A unit is an amount appropriate for */
1971 /* HBLKSIZE bytes of allocation. */
1972
1973 GC_INNER void * GC_generic_malloc_inner(size_t lb, int k);
1974 /* Allocate an object of the given */
1975 /* kind but assuming lock already held. */
1976 #if defined(DBG_HDRS_ALL) || defined(GC_GCJ_SUPPORT) \
1977 || !defined(GC_NO_FINALIZATION)
1978 GC_INNER void * GC_generic_malloc_inner_ignore_off_page(size_t lb, int k);
1979 /* Allocate an object, where */
1980 /* the client guarantees that there */
1981 /* will always be a pointer to the */
1982 /* beginning of the object while the */
1983 /* object is live. */
1984 #endif
1985
1986 GC_INNER GC_bool GC_collect_or_expand(word needed_blocks,
1987 GC_bool ignore_off_page, GC_bool retry);
1988
1989 GC_INNER ptr_t GC_allocobj(size_t sz, int kind);
1990 /* Make the indicated */
1991 /* free list nonempty, and return its */
1992 /* head. Sz is in granules. */
1993
1994 #ifdef GC_ADD_CALLER
1995 /* GC_DBG_EXTRAS is used by GC debug API functions (unlike GC_EXTRAS */
1996 /* used by GC debug API macros) thus GC_RETURN_ADDR_PARENT (pointing */
1997 /* to client caller) should be used if possible. */
1998 # ifdef GC_HAVE_RETURN_ADDR_PARENT
1999 # define GC_DBG_EXTRAS GC_RETURN_ADDR_PARENT, NULL, 0
2000 # else
2001 # define GC_DBG_EXTRAS GC_RETURN_ADDR, NULL, 0
2002 # endif
2003 #else
2004 # define GC_DBG_EXTRAS "unknown", 0
2005 #endif
2006
2007 #ifdef GC_COLLECT_AT_MALLOC
2008 extern size_t GC_dbg_collect_at_malloc_min_lb;
2009 /* variable visible outside for debugging */
2010 # define GC_DBG_COLLECT_AT_MALLOC(lb) \
2011 (void)((lb) >= GC_dbg_collect_at_malloc_min_lb ? \
2012 (GC_gcollect(), 0) : 0)
2013 #else
2014 # define GC_DBG_COLLECT_AT_MALLOC(lb) (void)0
2015 #endif /* !GC_COLLECT_AT_MALLOC */
2016
2017 /* Allocation routines that bypass the thread local cache. */
2018 #if defined(THREAD_LOCAL_ALLOC) && defined(GC_GCJ_SUPPORT)
2019 GC_INNER void * GC_core_gcj_malloc(size_t, void *);
2020 #endif
2021
2022 GC_INNER void GC_init_headers(void);
2023 GC_INNER struct hblkhdr * GC_install_header(struct hblk *h);
2024 /* Install a header for block h. */
2025 /* Return 0 on failure, or the header */
2026 /* otherwise. */
2027 GC_INNER GC_bool GC_install_counts(struct hblk * h, size_t sz);
2028 /* Set up forwarding counts for block */
2029 /* h of size sz. */
2030 /* Return FALSE on failure. */
2031 GC_INNER void GC_remove_header(struct hblk * h);
2032 /* Remove the header for block h. */
2033 GC_INNER void GC_remove_counts(struct hblk * h, size_t sz);
2034 /* Remove forwarding counts for h. */
2035 GC_INNER hdr * GC_find_header(ptr_t h);
2036
2037 GC_INNER void GC_add_to_heap(struct hblk *p, size_t bytes);
2038 /* Add a HBLKSIZE aligned chunk to the heap. */
2039
2040 #ifdef USE_PROC_FOR_LIBRARIES
2041 GC_INNER void GC_add_to_our_memory(ptr_t p, size_t bytes);
2042 /* Add a chunk to GC_our_memory. */
2043 /* If p == 0, do nothing. */
2044 #else
2045 # define GC_add_to_our_memory(p, bytes)
2046 #endif
2047
2048 GC_INNER void GC_print_all_errors(void);
2049 /* Print smashed and leaked objects, if any. */
2050 /* Clear the lists of such objects. */
2051
2052 GC_EXTERN void (*GC_check_heap)(void);
2053 /* Check that all objects in the heap with */
2054 /* debugging info are intact. */
2055 /* Add any that are not to GC_smashed list. */
2056 GC_EXTERN void (*GC_print_all_smashed)(void);
2057 /* Print GC_smashed if it's not empty. */
2058 /* Clear GC_smashed list. */
2059 GC_EXTERN void (*GC_print_heap_obj)(ptr_t p);
2060 /* If possible print (using GC_err_printf) */
2061 /* a more detailed description (terminated with */
2062 /* "\n") of the object referred to by p. */
2063
2064 #if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
2065 void GC_print_address_map(void);
2066 /* Print an address map of the process. */
2067 #endif
2068
2069 #ifndef SHORT_DBG_HDRS
2070 GC_EXTERN GC_bool GC_findleak_delay_free;
2071 /* Do not immediately deallocate object on */
2072 /* free() in the leak-finding mode, just mark */
2073 /* it as freed (and deallocate it after GC). */
2074 GC_INNER GC_bool GC_check_leaked(ptr_t base); /* from dbg_mlc.c */
2075 #endif
2076
2077 GC_EXTERN GC_bool GC_have_errors; /* We saw a smashed or leaked object. */
2078 /* Call error printing routine */
2079 /* occasionally. It is OK to read it */
2080 /* without acquiring the lock. */
2081
2082 #define VERBOSE 2
2083 #if !defined(NO_CLOCK) || !defined(SMALL_CONFIG)
2084 /* GC_print_stats should be visible to extra/MacOS.c. */
2085 extern int GC_print_stats; /* Nonzero generates basic GC log. */
2086 /* VERBOSE generates add'l messages. */
2087 #else /* SMALL_CONFIG */
2088 # define GC_print_stats 0
2089 /* Will this remove the message character strings from the executable? */
2090 /* With a particular level of optimizations, it should... */
2091 #endif
2092
2093 #ifdef KEEP_BACK_PTRS
2094 GC_EXTERN long GC_backtraces;
2095 GC_INNER void GC_generate_random_backtrace_no_gc(void);
2096 #endif
2097
2098 #ifdef LINT2
2099 # define GC_RAND_MAX (~0U >> 1)
2100 GC_API_PRIV long GC_random(void);
2101 #endif
2102
2103 GC_EXTERN GC_bool GC_print_back_height;
2104
2105 #ifdef MAKE_BACK_GRAPH
2106 void GC_print_back_graph_stats(void);
2107 #endif
2108
2109 #ifdef THREADS
2110 GC_INNER void GC_free_inner(void * p);
2111 #endif
2112
2113 /* Macros used for collector internal allocation. */
2114 /* These assume the collector lock is held. */
2115 #ifdef DBG_HDRS_ALL
2116 GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k);
2117 GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
2118 int k);
2119 # define GC_INTERNAL_MALLOC GC_debug_generic_malloc_inner
2120 # define GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE \
2121 GC_debug_generic_malloc_inner_ignore_off_page
2122 # ifdef THREADS
2123 GC_INNER void GC_debug_free_inner(void * p);
2124 # define GC_INTERNAL_FREE GC_debug_free_inner
2125 # else
2126 # define GC_INTERNAL_FREE GC_debug_free
2127 # endif
2128 #else
2129 # define GC_INTERNAL_MALLOC GC_generic_malloc_inner
2130 # define GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE \
2131 GC_generic_malloc_inner_ignore_off_page
2132 # ifdef THREADS
2133 # define GC_INTERNAL_FREE GC_free_inner
2134 # else
2135 # define GC_INTERNAL_FREE GC_free
2136 # endif
2137 #endif /* !DBG_HDRS_ALL */
2138
2139 #ifdef USE_MUNMAP
2140 /* Memory unmapping: */
2141 GC_INNER void GC_unmap_old(void);
2142 GC_INNER void GC_merge_unmapped(void);
2143 GC_INNER void GC_unmap(ptr_t start, size_t bytes);
2144 GC_INNER void GC_remap(ptr_t start, size_t bytes);
2145 GC_INNER void GC_unmap_gap(ptr_t start1, size_t bytes1, ptr_t start2,
2146 size_t bytes2);
2147 #endif
2148
2149 #ifdef CAN_HANDLE_FORK
2150 GC_EXTERN int GC_handle_fork;
2151 /* Fork-handling mode: */
2152 /* 0 means no fork handling requested (but client could */
2153 /* anyway call fork() provided it is surrounded with */
2154 /* GC_atfork_prepare/parent/child calls); */
2155 /* -1 means GC tries to use pthread_at_fork if it is */
2156 /* available (if it succeeds then GC_handle_fork value */
2157 /* is changed to 1), client should nonetheless surround */
2158 /* fork() with GC_atfork_prepare/parent/child (for the */
2159 /* case of pthread_at_fork failure or absence); */
2160 /* 1 (or other values) means client fully relies on */
2161 /* pthread_at_fork (so if it is missing or failed then */
2162 /* abort occurs in GC_init), GC_atfork_prepare and the */
2163 /* accompanying routines are no-op in such a case. */
2164 #endif
2165
2166 #ifndef GC_DISABLE_INCREMENTAL
2167 /* Virtual dirty bit implementation: */
2168 /* Each implementation exports the following: */
2169 GC_INNER void GC_read_dirty(GC_bool output_unneeded);
2170 /* Retrieve dirty bits. Set output_unneeded to */
2171 /* indicate that reading of the retrieved dirty */
2172 /* bits is not planned till the next retrieval. */
2173 GC_INNER GC_bool GC_page_was_dirty(struct hblk *h);
2174 /* Read retrieved dirty bits. */
2175
2176 GC_INNER void GC_remove_protection(struct hblk *h, word nblocks,
2177 GC_bool pointerfree);
2178 /* h is about to be written or allocated. Ensure that */
2179 /* it is not write protected by the virtual dirty bit */
2180 /* implementation. I.e., this is a call that: */
2181 /* - hints that [h, h+nblocks) is about to be written; */
2182 /* - guarantees that protection is removed; */
2183 /* - may speed up some dirty bit implementations; */
2184 /* - may be essential if we need to ensure that */
2185 /* pointer-free system call buffers in the heap are */
2186 /* not protected. */
2187
2188 GC_INNER GC_bool GC_dirty_init(void);
2189 /* Returns true if dirty bits are maintained (otherwise */
2190 /* it is OK to be called again if the client invokes */
2191 /* GC_enable_incremental once more). */
2192 #endif /* !GC_DISABLE_INCREMENTAL */
2193
2194 #ifdef MANUAL_VDB
2195 GC_API void GC_dirty_inner(const void *p); /* does not require locking */
2196 # define GC_dirty(p) (GC_incremental ? GC_dirty_inner(p) : (void)0)
2197 #else
2198 # define GC_dirty(p) (void)(p)
2199 #endif
2200
2201 /* Same as GC_base but excepts and returns a pointer to const object. */
2202 #define GC_base_C(p) ((const void *)GC_base((/* no const */ void *)(p)))
2203
2204 /* Debugging print routines: */
2205 void GC_print_block_list(void);
2206 void GC_print_hblkfreelist(void);
2207 void GC_print_heap_sects(void);
2208 void GC_print_static_roots(void);
2209
2210 extern word GC_fo_entries; /* should be visible in extra/MacOS.c */
2211
2212 #ifdef KEEP_BACK_PTRS
2213 GC_INNER void GC_store_back_pointer(ptr_t source, ptr_t dest);
2214 GC_INNER void GC_marked_for_finalization(ptr_t dest);
2215 # define GC_STORE_BACK_PTR(source, dest) GC_store_back_pointer(source, dest)
2216 # define GC_MARKED_FOR_FINALIZATION(dest) GC_marked_for_finalization(dest)
2217 #else
2218 # define GC_STORE_BACK_PTR(source, dest)
2219 # define GC_MARKED_FOR_FINALIZATION(dest)
2220 #endif
2221
2222 /* Make arguments appear live to compiler */
2223 void GC_noop6(word, word, word, word, word, word);
2224
2225 GC_API void GC_CALL GC_noop1(word);
2226
2227 #ifndef GC_ATTR_FORMAT_PRINTF
2228 # if GC_GNUC_PREREQ(3, 0)
2229 # define GC_ATTR_FORMAT_PRINTF(spec_argnum, first_checked) \
2230 __attribute__((__format__(__printf__, spec_argnum, first_checked)))
2231 # else
2232 # define GC_ATTR_FORMAT_PRINTF(spec_argnum, first_checked)
2233 # endif
2234 #endif
2235
2236 /* Logging and diagnostic output: */
2237 /* GC_printf is used typically on client explicit print requests. */
2238 /* For all GC_X_printf routines, it is recommended to put "\n" at */
2239 /* 'format' string end (for output atomicity). */
2240 GC_API_PRIV void GC_printf(const char * format, ...)
2241 GC_ATTR_FORMAT_PRINTF(1, 2);
2242 /* A version of printf that doesn't allocate, */
2243 /* 1K total output length. */
2244 /* (We use sprintf. Hopefully that doesn't */
2245 /* allocate for long arguments.) */
2246 GC_API_PRIV void GC_err_printf(const char * format, ...)
2247 GC_ATTR_FORMAT_PRINTF(1, 2);
2248
2249 /* Basic logging routine. Typically, GC_log_printf is called directly */
2250 /* only inside various DEBUG_x blocks. */
2251 GC_API_PRIV void GC_log_printf(const char * format, ...)
2252 GC_ATTR_FORMAT_PRINTF(1, 2);
2253
2254 #ifndef GC_ANDROID_LOG
2255 # define GC_PRINT_STATS_FLAG (GC_print_stats != 0)
2256 # define GC_INFOLOG_PRINTF GC_COND_LOG_PRINTF
2257 /* GC_verbose_log_printf is called only if GC_print_stats is VERBOSE. */
2258 # define GC_verbose_log_printf GC_log_printf
2259 #else
2260 extern GC_bool GC_quiet;
2261 # define GC_PRINT_STATS_FLAG (!GC_quiet)
2262 /* INFO/DBG loggers are enabled even if GC_print_stats is off. */
2263 # ifndef GC_INFOLOG_PRINTF
2264 # define GC_INFOLOG_PRINTF if (GC_quiet) {} else GC_info_log_printf
2265 # endif
2266 GC_INNER void GC_info_log_printf(const char *format, ...)
2267 GC_ATTR_FORMAT_PRINTF(1, 2);
2268 GC_INNER void GC_verbose_log_printf(const char *format, ...)
2269 GC_ATTR_FORMAT_PRINTF(1, 2);
2270 #endif /* GC_ANDROID_LOG */
2271
2272 /* Convenient macros for GC_[verbose_]log_printf invocation. */
2273 #define GC_COND_LOG_PRINTF \
2274 if (EXPECT(!GC_print_stats, TRUE)) {} else GC_log_printf
2275 #define GC_VERBOSE_LOG_PRINTF \
2276 if (EXPECT(GC_print_stats != VERBOSE, TRUE)) {} else GC_verbose_log_printf
2277 #ifndef GC_DBGLOG_PRINTF
2278 # define GC_DBGLOG_PRINTF if (!GC_PRINT_STATS_FLAG) {} else GC_log_printf
2279 #endif
2280
2281 void GC_err_puts(const char *s);
2282 /* Write s to stderr, don't buffer, don't add */
2283 /* newlines, don't ... */
2284
2285 /* Handy macro for logging size values (of word type) in KiB (rounding */
2286 /* to nearest value). */
2287 #define TO_KiB_UL(v) ((unsigned long)(((v) + ((1 << 9) - 1)) >> 10))
2288
2289 GC_EXTERN unsigned GC_fail_count;
2290 /* How many consecutive GC/expansion failures? */
2291 /* Reset by GC_allochblk(); defined in alloc.c. */
2292
2293 GC_EXTERN long GC_large_alloc_warn_interval; /* defined in misc.c */
2294
2295 GC_EXTERN signed_word GC_bytes_found;
2296 /* Number of reclaimed bytes after garbage collection; */
2297 /* protected by GC lock; defined in reclaim.c. */
2298
2299 #ifndef GC_GET_HEAP_USAGE_NOT_NEEDED
2300 GC_EXTERN word GC_reclaimed_bytes_before_gc;
2301 /* Number of bytes reclaimed before this */
2302 /* collection cycle; used for statistics only. */
2303 #endif
2304
2305 #ifdef USE_MUNMAP
2306 GC_EXTERN int GC_unmap_threshold; /* defined in allchblk.c */
2307 GC_EXTERN GC_bool GC_force_unmap_on_gcollect; /* defined in misc.c */
2308 #endif
2309
2310 #ifdef MSWIN32
2311 GC_EXTERN GC_bool GC_no_win32_dlls; /* defined in os_dep.c */
2312 GC_EXTERN GC_bool GC_wnt; /* Is Windows NT derivative; */
2313 /* defined and set in os_dep.c. */
2314 #endif
2315
2316 #ifdef THREADS
2317 # if defined(MSWIN32) || defined(MSWINCE) || defined(MSWIN_XBOX1)
2318 GC_EXTERN CRITICAL_SECTION GC_write_cs; /* defined in misc.c */
2319 # endif
2320 # if defined(GC_ASSERTIONS) && (defined(MSWIN32) || defined(MSWINCE))
2321 GC_EXTERN GC_bool GC_write_disabled;
2322 /* defined in win32_threads.c; */
2323 /* protected by GC_write_cs. */
2324
2325 # endif
2326 # if defined(GC_DISABLE_INCREMENTAL) \
2327 || defined(set_pht_entry_from_index_concurrent)
2328 # define GC_acquire_dirty_lock() (void)0
2329 # define GC_release_dirty_lock() (void)0
2330 # else
2331 /* Acquire the spin lock we use to update dirty bits. */
2332 /* Threads should not get stopped holding it. But we may */
2333 /* acquire and release it during GC_remove_protection call. */
2334 # define GC_acquire_dirty_lock() \
2335 do { /* empty */ \
2336 } while (AO_test_and_set_acquire(&GC_fault_handler_lock) == AO_TS_SET)
2337 # define GC_release_dirty_lock() AO_CLEAR(&GC_fault_handler_lock)
2338 GC_EXTERN volatile AO_TS_t GC_fault_handler_lock;
2339 /* defined in os_dep.c */
2340 # endif
2341 # ifdef MSWINCE
2342 GC_EXTERN GC_bool GC_dont_query_stack_min;
2343 /* Defined and set in os_dep.c. */
2344 # endif
2345 #elif defined(IA64)
2346 GC_EXTERN ptr_t GC_save_regs_ret_val; /* defined in mach_dep.c. */
2347 /* Previously set to backing store pointer. */
2348 #endif /* !THREADS */
2349
2350 #ifdef THREAD_LOCAL_ALLOC
2351 GC_EXTERN GC_bool GC_world_stopped; /* defined in alloc.c */
2352 GC_INNER void GC_mark_thread_local_free_lists(void);
2353 #endif
2354
2355 #ifdef GC_GCJ_SUPPORT
2356 # ifdef GC_ASSERTIONS
2357 GC_EXTERN GC_bool GC_gcj_malloc_initialized; /* defined in gcj_mlc.c */
2358 # endif
2359 GC_EXTERN ptr_t * GC_gcjobjfreelist;
2360 #endif
2361
2362 #ifdef MPROTECT_VDB
2363 # ifdef GWW_VDB
2364 GC_INNER GC_bool GC_gww_dirty_init(void);
2365 /* Returns TRUE if GetWriteWatch is available. */
2366 /* May be called repeatedly. */
2367 # endif
2368 # ifdef USE_MUNMAP
2369 GC_INNER GC_bool GC_mprotect_dirty_init(void);
2370 GC_INNER GC_bool GC_has_unmapped_memory(void);
2371 # endif
2372 #endif /* MPROTECT_VDB */
2373
2374 #if defined(CHECKSUMS) || defined(PROC_VDB)
2375 GC_INNER GC_bool GC_page_was_ever_dirty(struct hblk * h);
2376 /* Could the page contain valid heap pointers? */
2377 #endif
2378
2379 #ifdef CHECKSUMS
2380 # if defined(MPROTECT_VDB) && !defined(DARWIN)
2381 void GC_record_fault(struct hblk * h);
2382 # endif
2383 void GC_check_dirty(void);
2384 #endif
2385
2386 GC_INNER void GC_default_print_heap_obj_proc(ptr_t p);
2387
2388 GC_INNER void GC_setpagesize(void);
2389
2390 GC_INNER void GC_initialize_offsets(void); /* defined in obj_map.c */
2391
2392 GC_INNER void GC_bl_init(void);
2393 GC_INNER void GC_bl_init_no_interiors(void); /* defined in blacklst.c */
2394
2395 GC_INNER void GC_start_debugging_inner(void); /* defined in dbg_mlc.c. */
2396 /* Should not be called if GC_debugging_started. */
2397
2398 /* Store debugging info into p. Return displaced pointer. */
2399 /* Assumes we hold the allocation lock. */
2400 GC_INNER void *GC_store_debug_info_inner(void *p, word sz, const char *str,
2401 int linenum);
2402
2403 #ifdef REDIRECT_MALLOC
2404 # ifdef GC_LINUX_THREADS
2405 GC_INNER GC_bool GC_text_mapping(char *nm, ptr_t *startp, ptr_t *endp);
2406 /* from os_dep.c */
2407 # endif
2408 #elif defined(USE_WINALLOC)
2409 GC_INNER void GC_add_current_malloc_heap(void);
2410 #endif /* !REDIRECT_MALLOC */
2411
2412 #ifdef MAKE_BACK_GRAPH
2413 GC_INNER void GC_build_back_graph(void);
2414 GC_INNER void GC_traverse_back_graph(void);
2415 #endif
2416
2417 #ifdef MSWIN32
2418 GC_INNER void GC_init_win32(void);
2419 #endif
2420
2421 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
2422 GC_INNER void * GC_roots_present(ptr_t);
2423 /* The type is a lie, since the real type doesn't make sense here, */
2424 /* and we only test for NULL. */
2425 #endif
2426
2427 #ifdef GC_WIN32_THREADS
2428 GC_INNER void GC_get_next_stack(char *start, char * limit, char **lo,
2429 char **hi);
2430 # ifdef MPROTECT_VDB
2431 GC_INNER void GC_set_write_fault_handler(void);
2432 # endif
2433 # if defined(WRAP_MARK_SOME) && !defined(GC_PTHREADS)
2434 GC_INNER GC_bool GC_started_thread_while_stopped(void);
2435 /* Did we invalidate mark phase with an unexpected thread start? */
2436 # endif
2437 #endif /* GC_WIN32_THREADS */
2438
2439 #ifdef THREADS
2440 GC_INNER void GC_reset_finalizer_nested(void);
2441 GC_INNER unsigned char *GC_check_finalizer_nested(void);
2442 GC_INNER void GC_do_blocking_inner(ptr_t data, void * context);
2443 GC_INNER void GC_push_all_stacks(void);
2444 # ifdef USE_PROC_FOR_LIBRARIES
2445 GC_INNER GC_bool GC_segment_is_thread_stack(ptr_t lo, ptr_t hi);
2446 # endif
2447 # ifdef IA64
2448 GC_INNER ptr_t GC_greatest_stack_base_below(ptr_t bound);
2449 # endif
2450 #endif /* THREADS */
2451
2452 #ifdef DYNAMIC_LOADING
2453 GC_INNER GC_bool GC_register_main_static_data(void);
2454 # ifdef DARWIN
2455 GC_INNER void GC_init_dyld(void);
2456 # endif
2457 #endif /* DYNAMIC_LOADING */
2458
2459 #ifdef SEARCH_FOR_DATA_START
2460 GC_INNER void GC_init_linux_data_start(void);
2461 ptr_t GC_find_limit(ptr_t, GC_bool);
2462 #endif
2463
2464 #if defined(NETBSD) && defined(__ELF__)
2465 GC_INNER void GC_init_netbsd_elf(void);
2466 ptr_t GC_find_limit(ptr_t, GC_bool);
2467 #endif
2468
2469 #ifdef UNIX_LIKE
2470 GC_INNER void GC_set_and_save_fault_handler(void (*handler)(int));
2471 #endif
2472
2473 #ifdef NEED_PROC_MAPS
2474 # if defined(DYNAMIC_LOADING) && defined(USE_PROC_FOR_LIBRARIES)
2475 GC_INNER char *GC_parse_map_entry(char *buf_ptr, ptr_t *start, ptr_t *end,
2476 char **prot, unsigned int *maj_dev,
2477 char **mapping_name);
2478 # endif
2479 # if defined(IA64) || defined(INCLUDE_LINUX_THREAD_DESCR)
2480 GC_INNER GC_bool GC_enclosing_mapping(ptr_t addr,
2481 ptr_t *startp, ptr_t *endp);
2482 # endif
2483 GC_INNER char *GC_get_maps(void); /* from os_dep.c */
2484 #endif /* NEED_PROC_MAPS */
2485
2486 #ifdef GC_ASSERTIONS
2487 # define GC_ASSERT(expr) \
2488 do { \
2489 if (!(expr)) { \
2490 GC_err_printf("Assertion failure: %s:%d\n", \
2491 __FILE__, __LINE__); \
2492 ABORT("assertion failure"); \
2493 } \
2494 } while (0)
2495 GC_INNER word GC_compute_large_free_bytes(void);
2496 GC_INNER word GC_compute_root_size(void);
2497 #else
2498 # define GC_ASSERT(expr)
2499 #endif
2500
2501 /* Check a compile time assertion at compile time. */
2502 #if defined(static_assert) && (__STDC_VERSION__ >= 201112L)
2503 # define GC_STATIC_ASSERT(expr) static_assert(expr, "")
2504 #elif defined(mips) && !defined(__GNUC__)
2505 /* DOB: MIPSPro C gets an internal error taking the sizeof an array type.
2506 This code works correctly (ugliness is to avoid "unused var" warnings) */
2507 # define GC_STATIC_ASSERT(expr) \
2508 do { if (0) { char j[(expr)? 1 : -1]; j[0]='\0'; j[0]=j[0]; } } while(0)
2509 #else
2510 /* The error message for failure is a bit baroque, but ... */
2511 # define GC_STATIC_ASSERT(expr) (void)sizeof(char[(expr)? 1 : -1])
2512 #endif
2513
2514 /* Runtime check for an argument declared as non-null is actually not null. */
2515 #if GC_GNUC_PREREQ(4, 0)
2516 /* Workaround tautological-pointer-compare Clang warning. */
2517 # define NONNULL_ARG_NOT_NULL(arg) (*(volatile void **)&(arg) != NULL)
2518 #else
2519 # define NONNULL_ARG_NOT_NULL(arg) (NULL != (arg))
2520 #endif
2521
2522 #define COND_DUMP_CHECKS \
2523 do { \
2524 GC_ASSERT(GC_compute_large_free_bytes() == GC_large_free_bytes); \
2525 GC_ASSERT(GC_compute_root_size() == GC_root_size); \
2526 } while (0)
2527
2528 #ifndef NO_DEBUGGING
2529 GC_EXTERN GC_bool GC_dump_regularly;
2530 /* Generate regular debugging dumps. */
2531 # define COND_DUMP if (EXPECT(GC_dump_regularly, FALSE)) { \
2532 GC_dump_named(NULL); \
2533 } else COND_DUMP_CHECKS
2534 #else
2535 # define COND_DUMP COND_DUMP_CHECKS
2536 #endif
2537
2538 #if defined(PARALLEL_MARK)
2539 /* We need additional synchronization facilities from the thread */
2540 /* support. We believe these are less performance critical */
2541 /* than the main garbage collector lock; standard pthreads-based */
2542 /* implementations should be sufficient. */
2543
2544 # define GC_markers_m1 GC_parallel
2545 /* Number of mark threads we would like to have */
2546 /* excluding the initiating thread. */
2547
2548 /* The mark lock and condition variable. If the GC lock is also */
2549 /* acquired, the GC lock must be acquired first. The mark lock is */
2550 /* used to both protect some variables used by the parallel */
2551 /* marker, and to protect GC_fl_builder_count, below. */
2552 /* GC_notify_all_marker() is called when */
2553 /* the state of the parallel marker changes */
2554 /* in some significant way (see gc_mark.h for details). The */
2555 /* latter set of events includes incrementing GC_mark_no. */
2556 /* GC_notify_all_builder() is called when GC_fl_builder_count */
2557 /* reaches 0. */
2558
2559 GC_INNER void GC_wait_for_markers_init(void);
2560 GC_INNER void GC_acquire_mark_lock(void);
2561 GC_INNER void GC_release_mark_lock(void);
2562 GC_INNER void GC_notify_all_builder(void);
2563 GC_INNER void GC_wait_for_reclaim(void);
2564
2565 GC_EXTERN signed_word GC_fl_builder_count; /* Protected by mark lock. */
2566
2567 GC_INNER void GC_notify_all_marker(void);
2568 GC_INNER void GC_wait_marker(void);
2569 GC_EXTERN word GC_mark_no; /* Protected by mark lock. */
2570
2571 GC_INNER void GC_help_marker(word my_mark_no);
2572 /* Try to help out parallel marker for mark cycle */
2573 /* my_mark_no. Returns if the mark cycle finishes or */
2574 /* was already done, or there was nothing to do for */
2575 /* some other reason. */
2576
2577 GC_INNER void GC_start_mark_threads_inner(void);
2578 #endif /* PARALLEL_MARK */
2579
2580 #if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS) && !defined(NACL) \
2581 && !defined(GC_DARWIN_THREADS) && !defined(SIG_SUSPEND)
2582 /* We define the thread suspension signal here, so that we can refer */
2583 /* to it in the dirty bit implementation, if necessary. Ideally we */
2584 /* would allocate a (real-time?) signal using the standard mechanism. */
2585 /* unfortunately, there is no standard mechanism. (There is one */
2586 /* in Linux glibc, but it's not exported.) Thus we continue to use */
2587 /* the same hard-coded signals we've always used. */
2588 # if (defined(GC_LINUX_THREADS) || defined(GC_DGUX386_THREADS)) \
2589 && !defined(GC_USESIGRT_SIGNALS)
2590 # if defined(SPARC) && !defined(SIGPWR)
2591 /* SPARC/Linux doesn't properly define SIGPWR in <signal.h>. */
2592 /* It is aliased to SIGLOST in asm/signal.h, though. */
2593 # define SIG_SUSPEND SIGLOST
2594 # else
2595 /* Linuxthreads itself uses SIGUSR1 and SIGUSR2. */
2596 # define SIG_SUSPEND SIGPWR
2597 # endif
2598 # elif defined(GC_OPENBSD_THREADS)
2599 # ifndef GC_OPENBSD_UTHREADS
2600 # define SIG_SUSPEND SIGXFSZ
2601 # endif
2602 # elif defined(_SIGRTMIN) && !defined(CPPCHECK)
2603 # define SIG_SUSPEND _SIGRTMIN + 6
2604 # else
2605 # define SIG_SUSPEND SIGRTMIN + 6
2606 # endif
2607 #endif /* GC_PTHREADS && !SIG_SUSPEND */
2608
2609 #if defined(GC_PTHREADS) && !defined(GC_SEM_INIT_PSHARED)
2610 # define GC_SEM_INIT_PSHARED 0
2611 #endif
2612
2613 /* Some macros for setjmp that works across signal handlers */
2614 /* were possible, and a couple of routines to facilitate */
2615 /* catching accesses to bad addresses when that's */
2616 /* possible/needed. */
2617 #if (defined(UNIX_LIKE) || (defined(NEED_FIND_LIMIT) && defined(CYGWIN32))) \
2618 && !defined(GC_NO_SIGSETJMP)
2619 # if defined(SUNOS5SIGS) && !defined(FREEBSD) && !defined(LINUX)
2620 EXTERN_C_END
2621 # include <sys/siginfo.h>
2622 EXTERN_C_BEGIN
2623 # endif
2624 /* Define SETJMP and friends to be the version that restores */
2625 /* the signal mask. */
2626 # define SETJMP(env) sigsetjmp(env, 1)
2627 # define LONGJMP(env, val) siglongjmp(env, val)
2628 # define JMP_BUF sigjmp_buf
2629 #else
2630 # ifdef ECOS
2631 # define SETJMP(env) hal_setjmp(env)
2632 # else
2633 # define SETJMP(env) setjmp(env)
2634 # endif
2635 # define LONGJMP(env, val) longjmp(env, val)
2636 # define JMP_BUF jmp_buf
2637 #endif /* !UNIX_LIKE || GC_NO_SIGSETJMP */
2638
2639 /* Do we need the GC_find_limit machinery to find the end of a */
2640 /* data segment. */
2641 #if defined(HEURISTIC2) || defined(SEARCH_FOR_DATA_START) \
2642 || (!defined(STACKBOTTOM) && defined(HEURISTIC2)) \
2643 || ((defined(SVR4) || defined(AIX) || defined(DGUX) \
2644 || (defined(LINUX) && defined(SPARC))) && !defined(PCR))
2645 # define NEED_FIND_LIMIT
2646 #endif
2647
2648 #if defined(DATASTART_USES_BSDGETDATASTART)
2649 EXTERN_C_END
2650 # include <machine/trap.h>
2651 EXTERN_C_BEGIN
2652 # if !defined(PCR)
2653 # define NEED_FIND_LIMIT
2654 # endif
2655 GC_INNER ptr_t GC_FreeBSDGetDataStart(size_t, ptr_t);
2656 # define DATASTART_IS_FUNC
2657 #endif /* DATASTART_USES_BSDGETDATASTART */
2658
2659 #if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__) \
2660 && !defined(NEED_FIND_LIMIT)
2661 /* Used by GC_init_netbsd_elf() in os_dep.c. */
2662 # define NEED_FIND_LIMIT
2663 #endif
2664
2665 #if defined(IA64) && !defined(NEED_FIND_LIMIT)
2666 # define NEED_FIND_LIMIT
2667 /* May be needed for register backing store base. */
2668 #endif
2669
2670 #if defined(NEED_FIND_LIMIT) \
2671 || (defined(USE_PROC_FOR_LIBRARIES) && defined(THREADS))
2672 GC_EXTERN JMP_BUF GC_jmp_buf;
2673
2674 /* Set up a handler for address faults which will longjmp to */
2675 /* GC_jmp_buf. */
2676 GC_INNER void GC_setup_temporary_fault_handler(void);
2677 /* Undo the effect of GC_setup_temporary_fault_handler. */
2678 GC_INNER void GC_reset_fault_handler(void);
2679 #endif /* NEED_FIND_LIMIT || USE_PROC_FOR_LIBRARIES */
2680
2681 /* Some convenience macros for cancellation support. */
2682 #if defined(CANCEL_SAFE)
2683 # if defined(GC_ASSERTIONS) \
2684 && (defined(USE_COMPILER_TLS) \
2685 || (defined(LINUX) && !defined(ARM32) && GC_GNUC_PREREQ(3, 3) \
2686 || defined(HPUX) /* and probably others ... */))
2687 extern __thread unsigned char GC_cancel_disable_count;
2688 # define NEED_CANCEL_DISABLE_COUNT
2689 # define INCR_CANCEL_DISABLE() ++GC_cancel_disable_count
2690 # define DECR_CANCEL_DISABLE() --GC_cancel_disable_count
2691 # define ASSERT_CANCEL_DISABLED() GC_ASSERT(GC_cancel_disable_count > 0)
2692 # else
2693 # define INCR_CANCEL_DISABLE()
2694 # define DECR_CANCEL_DISABLE()
2695 # define ASSERT_CANCEL_DISABLED() (void)0
2696 # endif /* GC_ASSERTIONS & ... */
2697 # define DISABLE_CANCEL(state) \
2698 do { pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &state); \
2699 INCR_CANCEL_DISABLE(); } while (0)
2700 # define RESTORE_CANCEL(state) \
2701 do { ASSERT_CANCEL_DISABLED(); \
2702 pthread_setcancelstate(state, NULL); \
2703 DECR_CANCEL_DISABLE(); } while (0)
2704 #else /* !CANCEL_SAFE */
2705 # define DISABLE_CANCEL(state) (void)0
2706 # define RESTORE_CANCEL(state) (void)0
2707 # define ASSERT_CANCEL_DISABLED() (void)0
2708 #endif /* !CANCEL_SAFE */
2709
2710 EXTERN_C_END
2711
2712 #endif /* GC_PRIVATE_H */