Debian Codegraph @

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