PageRenderTime 67ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/headers/gc/private/gc_priv.h

http://github.com/nddrylliog/ooc
C++ Header | 1853 lines | 986 code | 179 blank | 688 comment | 128 complexity | 879699c5a094c2b5c5a26f53b2a1ea35 MD5 | raw file
  1. /*
  2. * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3. * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
  4. * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
  5. * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
  6. *
  7. *
  8. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  9. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  10. *
  11. * Permission is hereby granted to use or copy this program
  12. * for any purpose, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. */
  17. # ifndef GC_PRIVATE_H
  18. # define GC_PRIVATE_H
  19. # include <stdlib.h>
  20. # if !(defined( sony_news ) )
  21. # include <stddef.h>
  22. # endif
  23. #ifdef DGUX
  24. # include <sys/types.h>
  25. # include <sys/time.h>
  26. # include <sys/resource.h>
  27. #endif /* DGUX */
  28. #ifdef BSD_TIME
  29. # include <sys/types.h>
  30. # include <sys/time.h>
  31. # include <sys/resource.h>
  32. #endif /* BSD_TIME */
  33. #ifdef PARALLEL_MARK
  34. # define AO_REQUIRE_CAS
  35. #endif
  36. #ifndef _GC_H
  37. # include "../gc.h"
  38. #endif
  39. #ifndef GC_TINY_FL_H
  40. # include "../gc_tiny_fl.h"
  41. #endif
  42. #ifndef GC_MARK_H
  43. # include "../gc_mark.h"
  44. #endif
  45. typedef GC_word word;
  46. typedef GC_signed_word signed_word;
  47. typedef unsigned int unsigned32;
  48. typedef int GC_bool;
  49. # define TRUE 1
  50. # define FALSE 0
  51. typedef char * ptr_t; /* A generic pointer to which we can add */
  52. /* byte displacements and which can be used */
  53. /* for address comparisons. */
  54. # ifndef GCCONFIG_H
  55. # include "gcconfig.h"
  56. # endif
  57. # ifndef HEADERS_H
  58. # include "gc_hdrs.h"
  59. # endif
  60. #if __GNUC__ >= 3
  61. # define EXPECT(expr, outcome) __builtin_expect(expr,outcome)
  62. /* Equivalent to (expr), but predict that usually (expr)==outcome. */
  63. # define INLINE inline
  64. #else
  65. # define EXPECT(expr, outcome) (expr)
  66. # define INLINE
  67. #endif /* __GNUC__ */
  68. # ifndef GC_LOCKS_H
  69. # include "gc_locks.h"
  70. # endif
  71. # ifdef STACK_GROWS_DOWN
  72. # define COOLER_THAN >
  73. # define HOTTER_THAN <
  74. # define MAKE_COOLER(x,y) if ((x)+(y) > (x)) {(x) += (y);} \
  75. else {(x) = (ptr_t)ONES;}
  76. # define MAKE_HOTTER(x,y) (x) -= (y)
  77. # else
  78. # define COOLER_THAN <
  79. # define HOTTER_THAN >
  80. # define MAKE_COOLER(x,y) if ((x)-(y) < (x)) {(x) -= (y);} else {(x) = 0;}
  81. # define MAKE_HOTTER(x,y) (x) += (y)
  82. # endif
  83. #if defined(AMIGA) && defined(__SASC)
  84. # define GC_FAR __far
  85. #else
  86. # define GC_FAR
  87. #endif
  88. /*********************************/
  89. /* */
  90. /* Definitions for conservative */
  91. /* collector */
  92. /* */
  93. /*********************************/
  94. /*********************************/
  95. /* */
  96. /* Easily changeable parameters */
  97. /* */
  98. /*********************************/
  99. /* #define STUBBORN_ALLOC */
  100. /* Enable stubborm allocation, and thus a limited */
  101. /* form of incremental collection w/o dirty bits. */
  102. /* #define ALL_INTERIOR_POINTERS */
  103. /* Forces all pointers into the interior of an */
  104. /* object to be considered valid. Also causes the */
  105. /* sizes of all objects to be inflated by at least */
  106. /* one byte. This should suffice to guarantee */
  107. /* that in the presence of a compiler that does */
  108. /* not perform garbage-collector-unsafe */
  109. /* optimizations, all portable, strictly ANSI */
  110. /* conforming C programs should be safely usable */
  111. /* with malloc replaced by GC_malloc and free */
  112. /* calls removed. There are several disadvantages: */
  113. /* 1. There are probably no interesting, portable, */
  114. /* strictly ANSI conforming C programs. */
  115. /* 2. This option makes it hard for the collector */
  116. /* to allocate space that is not ``pointed to'' */
  117. /* by integers, etc. Under SunOS 4.X with a */
  118. /* statically linked libc, we empiricaly */
  119. /* observed that it would be difficult to */
  120. /* allocate individual objects larger than 100K. */
  121. /* Even if only smaller objects are allocated, */
  122. /* more swap space is likely to be needed. */
  123. /* Fortunately, much of this will never be */
  124. /* touched. */
  125. /* If you can easily avoid using this option, do. */
  126. /* If not, try to keep individual objects small. */
  127. /* This is now really controlled at startup, */
  128. /* through GC_all_interior_pointers. */
  129. #define GC_INVOKE_FINALIZERS() GC_notify_or_invoke_finalizers()
  130. #if !defined(DONT_ADD_BYTE_AT_END)
  131. # define EXTRA_BYTES GC_all_interior_pointers
  132. # define MAX_EXTRA_BYTES 1
  133. #else
  134. # define EXTRA_BYTES 0
  135. # define MAX_EXTRA_BYTES 0
  136. #endif
  137. # ifndef LARGE_CONFIG
  138. # define MINHINCR 16 /* Minimum heap increment, in blocks of HBLKSIZE */
  139. /* Must be multiple of largest page size. */
  140. # define MAXHINCR 2048 /* Maximum heap increment, in blocks */
  141. # else
  142. # define MINHINCR 64
  143. # define MAXHINCR 4096
  144. # endif
  145. # define TIME_LIMIT 50 /* We try to keep pause times from exceeding */
  146. /* this by much. In milliseconds. */
  147. # define BL_LIMIT GC_black_list_spacing
  148. /* If we need a block of N bytes, and we have */
  149. /* a block of N + BL_LIMIT bytes available, */
  150. /* and N > BL_LIMIT, */
  151. /* but all possible positions in it are */
  152. /* blacklisted, we just use it anyway (and */
  153. /* print a warning, if warnings are enabled). */
  154. /* This risks subsequently leaking the block */
  155. /* due to a false reference. But not using */
  156. /* the block risks unreasonable immediate */
  157. /* heap growth. */
  158. /*********************************/
  159. /* */
  160. /* Stack saving for debugging */
  161. /* */
  162. /*********************************/
  163. #ifdef NEED_CALLINFO
  164. struct callinfo {
  165. word ci_pc; /* Caller, not callee, pc */
  166. # if NARGS > 0
  167. word ci_arg[NARGS]; /* bit-wise complement to avoid retention */
  168. # endif
  169. # if (NFRAMES * (NARGS + 1)) % 2 == 1
  170. /* Likely alignment problem. */
  171. word ci_dummy;
  172. # endif
  173. };
  174. #endif
  175. #ifdef SAVE_CALL_CHAIN
  176. /* Fill in the pc and argument information for up to NFRAMES of my */
  177. /* callers. Ignore my frame and my callers frame. */
  178. void GC_save_callers(struct callinfo info[NFRAMES]);
  179. void GC_print_callers(struct callinfo info[NFRAMES]);
  180. #endif
  181. /*********************************/
  182. /* */
  183. /* OS interface routines */
  184. /* */
  185. /*********************************/
  186. #ifdef BSD_TIME
  187. # undef CLOCK_TYPE
  188. # undef GET_TIME
  189. # undef MS_TIME_DIFF
  190. # define CLOCK_TYPE struct timeval
  191. # define GET_TIME(x) { struct rusage rusage; \
  192. getrusage (RUSAGE_SELF, &rusage); \
  193. x = rusage.ru_utime; }
  194. # define MS_TIME_DIFF(a,b) ((double) (a.tv_sec - b.tv_sec) * 1000.0 \
  195. + (double) (a.tv_usec - b.tv_usec) / 1000.0)
  196. #else /* !BSD_TIME */
  197. # if defined(MSWIN32) || defined(MSWINCE)
  198. # include <windows.h>
  199. # include <winbase.h>
  200. # define CLOCK_TYPE DWORD
  201. # define GET_TIME(x) x = GetTickCount()
  202. # define MS_TIME_DIFF(a,b) ((long)((a)-(b)))
  203. # else /* !MSWIN32, !MSWINCE, !BSD_TIME */
  204. # include <time.h>
  205. # if !defined(__STDC__) && defined(SPARC) && defined(SUNOS4)
  206. clock_t clock(); /* Not in time.h, where it belongs */
  207. # endif
  208. # if defined(FREEBSD) && !defined(CLOCKS_PER_SEC)
  209. # include <machine/limits.h>
  210. # define CLOCKS_PER_SEC CLK_TCK
  211. # endif
  212. # if !defined(CLOCKS_PER_SEC)
  213. # define CLOCKS_PER_SEC 1000000
  214. /*
  215. * This is technically a bug in the implementation. ANSI requires that
  216. * CLOCKS_PER_SEC be defined. But at least under SunOS4.1.1, it isn't.
  217. * Also note that the combination of ANSI C and POSIX is incredibly gross
  218. * here. The type clock_t is used by both clock() and times(). But on
  219. * some machines these use different notions of a clock tick, CLOCKS_PER_SEC
  220. * seems to apply only to clock. Hence we use it here. On many machines,
  221. * including SunOS, clock actually uses units of microseconds (which are
  222. * not really clock ticks).
  223. */
  224. # endif
  225. # define CLOCK_TYPE clock_t
  226. # define GET_TIME(x) x = clock()
  227. # define MS_TIME_DIFF(a,b) ((unsigned long) \
  228. (1000.0*(double)((a)-(b))/(double)CLOCKS_PER_SEC))
  229. # endif /* !MSWIN32 */
  230. #endif /* !BSD_TIME */
  231. /* We use bzero and bcopy internally. They may not be available. */
  232. # if defined(SPARC) && defined(SUNOS4)
  233. # define BCOPY_EXISTS
  234. # endif
  235. # if defined(M68K) && defined(AMIGA)
  236. # define BCOPY_EXISTS
  237. # endif
  238. # if defined(M68K) && defined(NEXT)
  239. # define BCOPY_EXISTS
  240. # endif
  241. # if defined(VAX)
  242. # define BCOPY_EXISTS
  243. # endif
  244. # if defined(AMIGA)
  245. # include <string.h>
  246. # define BCOPY_EXISTS
  247. # endif
  248. # if defined(DARWIN)
  249. # include <string.h>
  250. # define BCOPY_EXISTS
  251. # endif
  252. # ifndef BCOPY_EXISTS
  253. # include <string.h>
  254. # define BCOPY(x,y,n) memcpy(y, x, (size_t)(n))
  255. # define BZERO(x,n) memset(x, 0, (size_t)(n))
  256. # else
  257. # define BCOPY(x,y,n) bcopy((void *)(x),(void *)(y),(size_t)(n))
  258. # define BZERO(x,n) bzero((void *)(x),(size_t)(n))
  259. # endif
  260. /*
  261. * Stop and restart mutator threads.
  262. */
  263. # ifdef PCR
  264. # include "th/PCR_ThCtl.h"
  265. # define STOP_WORLD() \
  266. PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_stopNormal, \
  267. PCR_allSigsBlocked, \
  268. PCR_waitForever)
  269. # define START_WORLD() \
  270. PCR_ThCtl_SetExclusiveMode(PCR_ThCtl_ExclusiveMode_null, \
  271. PCR_allSigsBlocked, \
  272. PCR_waitForever);
  273. # else
  274. # if defined(GC_SOLARIS_THREADS) || defined(GC_WIN32_THREADS) \
  275. || defined(GC_PTHREADS)
  276. void GC_stop_world();
  277. void GC_start_world();
  278. # define STOP_WORLD() GC_stop_world()
  279. # define START_WORLD() GC_start_world()
  280. # else
  281. # define STOP_WORLD()
  282. # define START_WORLD()
  283. # endif
  284. # endif
  285. /* Abandon ship */
  286. # ifdef PCR
  287. # define ABORT(s) PCR_Base_Panic(s)
  288. # else
  289. # ifdef SMALL_CONFIG
  290. # define ABORT(msg) abort()
  291. # else
  292. GC_API void GC_abort(const char * msg);
  293. # define ABORT(msg) GC_abort(msg)
  294. # endif
  295. # endif
  296. /* Exit abnormally, but without making a mess (e.g. out of memory) */
  297. # ifdef PCR
  298. # define EXIT() PCR_Base_Exit(1,PCR_waitForever)
  299. # else
  300. # define EXIT() (void)exit(1)
  301. # endif
  302. /* Print warning message, e.g. almost out of memory. */
  303. # define WARN(msg,arg) (*GC_current_warn_proc)("GC Warning: " msg, (GC_word)(arg))
  304. extern GC_warn_proc GC_current_warn_proc;
  305. /* Get environment entry */
  306. #if !defined(NO_GETENV)
  307. # if defined(EMPTY_GETENV_RESULTS)
  308. /* Workaround for a reputed Wine bug. */
  309. static inline char * fixed_getenv(const char *name)
  310. {
  311. char * tmp = getenv(name);
  312. if (tmp == 0 || strlen(tmp) == 0)
  313. return 0;
  314. return tmp;
  315. }
  316. # define GETENV(name) fixed_getenv(name)
  317. # else
  318. # define GETENV(name) getenv(name)
  319. # endif
  320. #else
  321. # define GETENV(name) 0
  322. #endif
  323. #if defined(DARWIN)
  324. # if defined(POWERPC)
  325. # if CPP_WORDSZ == 32
  326. # define GC_THREAD_STATE_T ppc_thread_state_t
  327. # define GC_MACH_THREAD_STATE PPC_THREAD_STATE
  328. # define GC_MACH_THREAD_STATE_COUNT PPC_THREAD_STATE_COUNT
  329. # define GC_MACH_HEADER mach_header
  330. # define GC_MACH_SECTION section
  331. # define GC_GETSECTBYNAME getsectbynamefromheader
  332. # else
  333. # define GC_THREAD_STATE_T ppc_thread_state64_t
  334. # define GC_MACH_THREAD_STATE PPC_THREAD_STATE64
  335. # define GC_MACH_THREAD_STATE_COUNT PPC_THREAD_STATE64_COUNT
  336. # define GC_MACH_HEADER mach_header_64
  337. # define GC_MACH_SECTION section_64
  338. # define GC_GETSECTBYNAME getsectbynamefromheader_64
  339. # endif
  340. # elif defined(I386) || defined(X86_64)
  341. # if CPP_WORDSZ == 32
  342. # define GC_THREAD_STATE_T x86_thread_state32_t
  343. # define GC_MACH_THREAD_STATE x86_THREAD_STATE32
  344. # define GC_MACH_THREAD_STATE_COUNT x86_THREAD_STATE32_COUNT
  345. # define GC_MACH_HEADER mach_header
  346. # define GC_MACH_SECTION section
  347. # define GC_GETSECTBYNAME getsectbynamefromheader
  348. # else
  349. # define GC_THREAD_STATE_T x86_thread_state64_t
  350. # define GC_MACH_THREAD_STATE x86_THREAD_STATE64
  351. # define GC_MACH_THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
  352. # define GC_MACH_HEADER mach_header_64
  353. # define GC_MACH_SECTION section_64
  354. # define GC_GETSECTBYNAME getsectbynamefromheader_64
  355. # endif
  356. # else
  357. # error define GC_THREAD_STATE_T
  358. # define GC_MACH_THREAD_STATE MACHINE_THREAD_STATE
  359. # define GC_MACH_THREAD_STATE_COUNT MACHINE_THREAD_STATE_COUNT
  360. # endif
  361. /* Try to work out the right way to access thread state structure members.
  362. The structure has changed its definition in different Darwin versions.
  363. This now defaults to the (older) names without __, thus hopefully,
  364. not breaking any existing Makefile.direct builds. */
  365. # if defined (HAS_PPC_THREAD_STATE___R0) \
  366. || defined (HAS_PPC_THREAD_STATE64___R0) \
  367. || defined (HAS_X86_THREAD_STATE32___EAX) \
  368. || defined (HAS_X86_THREAD_STATE64___RAX)
  369. # define THREAD_FLD(x) __ ## x
  370. # else
  371. # define THREAD_FLD(x) x
  372. # endif
  373. #endif
  374. /*********************************/
  375. /* */
  376. /* Word-size-dependent defines */
  377. /* */
  378. /*********************************/
  379. #if CPP_WORDSZ == 32
  380. # define WORDS_TO_BYTES(x) ((x)<<2)
  381. # define BYTES_TO_WORDS(x) ((x)>>2)
  382. # define LOGWL ((word)5) /* log[2] of CPP_WORDSZ */
  383. # define modWORDSZ(n) ((n) & 0x1f) /* n mod size of word */
  384. # if ALIGNMENT != 4
  385. # define UNALIGNED
  386. # endif
  387. #endif
  388. #if CPP_WORDSZ == 64
  389. # define WORDS_TO_BYTES(x) ((x)<<3)
  390. # define BYTES_TO_WORDS(x) ((x)>>3)
  391. # define LOGWL ((word)6) /* log[2] of CPP_WORDSZ */
  392. # define modWORDSZ(n) ((n) & 0x3f) /* n mod size of word */
  393. # if ALIGNMENT != 8
  394. # define UNALIGNED
  395. # endif
  396. #endif
  397. /* The first TINY_FREELISTS free lists correspond to the first */
  398. /* TINY_FREELISTS multiples of GRANULE_BYTES, i.e. we keep */
  399. /* separate free lists for each multiple of GRANULE_BYTES */
  400. /* up to (TINY_FREELISTS-1) * GRANULE_BYTES. After that they */
  401. /* may be spread out further. */
  402. #include "../gc_tiny_fl.h"
  403. #define GRANULE_BYTES GC_GRANULE_BYTES
  404. #define TINY_FREELISTS GC_TINY_FREELISTS
  405. #define WORDSZ ((word)CPP_WORDSZ)
  406. #define SIGNB ((word)1 << (WORDSZ-1))
  407. #define BYTES_PER_WORD ((word)(sizeof (word)))
  408. #define ONES ((word)(signed_word)(-1))
  409. #define divWORDSZ(n) ((n) >> LOGWL) /* divide n by size of word */
  410. #if GRANULE_BYTES == 8
  411. # define BYTES_TO_GRANULES(n) ((n)>>3)
  412. # define GRANULES_TO_BYTES(n) ((n)<<3)
  413. # if CPP_WORDSZ == 64
  414. # define GRANULES_TO_WORDS(n) (n)
  415. # elif CPP_WORDSZ == 32
  416. # define GRANULES_TO_WORDS(n) ((n)<<1)
  417. # else
  418. # define GRANULES_TO_WORDS(n) BYTES_TO_WORDS(GRANULES_TO_BYTES(n))
  419. # endif
  420. #elif GRANULE_BYTES == 16
  421. # define BYTES_TO_GRANULES(n) ((n)>>4)
  422. # define GRANULES_TO_BYTES(n) ((n)<<4)
  423. # if CPP_WORDSZ == 64
  424. # define GRANULES_TO_WORDS(n) ((n)<<1)
  425. # elif CPP_WORDSZ == 32
  426. # define GRANULES_TO_WORDS(n) ((n)<<2)
  427. # else
  428. # define GRANULES_TO_WORDS(n) BYTES_TO_WORDS(GRANULES_TO_BYTES(n))
  429. # endif
  430. #else
  431. # error Bad GRANULE_BYTES value
  432. #endif
  433. /*********************/
  434. /* */
  435. /* Size Parameters */
  436. /* */
  437. /*********************/
  438. /* Heap block size, bytes. Should be power of 2. */
  439. /* Incremental GC with MPROTECT_VDB currently requires the */
  440. /* page size to be a multiple of HBLKSIZE. Since most modern */
  441. /* architectures support variable page sizes down to 4K, and */
  442. /* X86 is generally 4K, we now default to 4K, except for */
  443. /* Alpha: Seems to be used with 8K pages. */
  444. /* SMALL_CONFIG: Want less block-level fragmentation. */
  445. #ifndef HBLKSIZE
  446. # ifdef SMALL_CONFIG
  447. # define CPP_LOG_HBLKSIZE 10
  448. # else
  449. # if defined(ALPHA)
  450. # define CPP_LOG_HBLKSIZE 13
  451. # else
  452. # define CPP_LOG_HBLKSIZE 12
  453. # endif
  454. # endif
  455. #else
  456. # if HBLKSIZE == 512
  457. # define CPP_LOG_HBLKSIZE 9
  458. # endif
  459. # if HBLKSIZE == 1024
  460. # define CPP_LOG_HBLKSIZE 10
  461. # endif
  462. # if HBLKSIZE == 2048
  463. # define CPP_LOG_HBLKSIZE 11
  464. # endif
  465. # if HBLKSIZE == 4096
  466. # define CPP_LOG_HBLKSIZE 12
  467. # endif
  468. # if HBLKSIZE == 8192
  469. # define CPP_LOG_HBLKSIZE 13
  470. # endif
  471. # if HBLKSIZE == 16384
  472. # define CPP_LOG_HBLKSIZE 14
  473. # endif
  474. # ifndef CPP_LOG_HBLKSIZE
  475. --> fix HBLKSIZE
  476. # endif
  477. # undef HBLKSIZE
  478. #endif
  479. # define CPP_HBLKSIZE (1 << CPP_LOG_HBLKSIZE)
  480. # define LOG_HBLKSIZE ((size_t)CPP_LOG_HBLKSIZE)
  481. # define HBLKSIZE ((size_t)CPP_HBLKSIZE)
  482. /* max size objects supported by freelist (larger objects are */
  483. /* allocated directly with allchblk(), by rounding to the next */
  484. /* multiple of HBLKSIZE. */
  485. #define CPP_MAXOBJBYTES (CPP_HBLKSIZE/2)
  486. #define MAXOBJBYTES ((size_t)CPP_MAXOBJBYTES)
  487. #define CPP_MAXOBJWORDS BYTES_TO_WORDS(CPP_MAXOBJBYTES)
  488. #define MAXOBJWORDS ((size_t)CPP_MAXOBJWORDS)
  489. #define CPP_MAXOBJGRANULES BYTES_TO_GRANULES(CPP_MAXOBJBYTES)
  490. #define MAXOBJGRANULES ((size_t)CPP_MAXOBJGRANULES)
  491. # define divHBLKSZ(n) ((n) >> LOG_HBLKSIZE)
  492. # define HBLK_PTR_DIFF(p,q) divHBLKSZ((ptr_t)p - (ptr_t)q)
  493. /* Equivalent to subtracting 2 hblk pointers. */
  494. /* We do it this way because a compiler should */
  495. /* find it hard to use an integer division */
  496. /* instead of a shift. The bundled SunOS 4.1 */
  497. /* o.w. sometimes pessimizes the subtraction to */
  498. /* involve a call to .div. */
  499. # define modHBLKSZ(n) ((n) & (HBLKSIZE-1))
  500. # define HBLKPTR(objptr) ((struct hblk *)(((word) (objptr)) & ~(HBLKSIZE-1)))
  501. # define HBLKDISPL(objptr) (((size_t) (objptr)) & (HBLKSIZE-1))
  502. /* Round up byte allocation requests to integral number of words, etc. */
  503. # define ROUNDED_UP_WORDS(n) \
  504. BYTES_TO_WORDS((n) + (WORDS_TO_BYTES(1) - 1 + EXTRA_BYTES))
  505. # define ROUNDED_UP_GRANULES(n) \
  506. BYTES_TO_GRANULES((n) + (GRANULE_BYTES - 1 + EXTRA_BYTES))
  507. # if MAX_EXTRA_BYTES == 0
  508. # define SMALL_OBJ(bytes) EXPECT((bytes) <= (MAXOBJBYTES), 1)
  509. # else
  510. # define SMALL_OBJ(bytes) \
  511. (EXPECT((bytes) <= (MAXOBJBYTES - MAX_EXTRA_BYTES), 1) || \
  512. (bytes) <= (MAXOBJBYTES - EXTRA_BYTES))
  513. /* This really just tests bytes <= MAXOBJBYTES - EXTRA_BYTES. */
  514. /* But we try to avoid looking up EXTRA_BYTES. */
  515. # endif
  516. # define ADD_SLOP(bytes) ((bytes) + EXTRA_BYTES)
  517. # ifndef MIN_WORDS
  518. # define MIN_WORDS 2 /* FIXME: obsolete */
  519. # endif
  520. /*
  521. * Hash table representation of sets of pages.
  522. * Implements a map from aligned HBLKSIZE chunks of the address space to one
  523. * bit each.
  524. * This assumes it is OK to spuriously set bits, e.g. because multiple
  525. * addresses are represented by a single location.
  526. * Used by black-listing code, and perhaps by dirty bit maintenance code.
  527. */
  528. # ifdef LARGE_CONFIG
  529. # if CPP_WORDSZ == 32
  530. # define LOG_PHT_ENTRIES 20 /* Collisions likely at 1M blocks, */
  531. /* which is >= 4GB. Each table takes */
  532. /* 128KB, some of which may never be */
  533. /* touched. */
  534. # else
  535. # define LOG_PHT_ENTRIES 21 /* Collisions likely at 2M blocks, */
  536. /* which is >= 8GB. Each table takes */
  537. /* 256KB, some of which may never be */
  538. /* touched. */
  539. # endif
  540. # else
  541. # ifdef SMALL_CONFIG
  542. # define LOG_PHT_ENTRIES 15 /* Collisions are likely if heap grows */
  543. /* to more than 32K hblks = 128MB. */
  544. /* Each hash table occupies 4K bytes. */
  545. # else /* default "medium" configuration */
  546. # define LOG_PHT_ENTRIES 18 /* Collisions are likely if heap grows */
  547. /* to more than 256K hblks >= 1GB. */
  548. /* Each hash table occupies 32K bytes. */
  549. /* Even for somewhat smaller heaps, */
  550. /* say half that, collisions may be an */
  551. /* issue because we blacklist */
  552. /* addresses outside the heap. */
  553. # endif
  554. # endif
  555. # define PHT_ENTRIES ((word)1 << LOG_PHT_ENTRIES)
  556. # define PHT_SIZE (PHT_ENTRIES >> LOGWL)
  557. typedef word page_hash_table[PHT_SIZE];
  558. # define PHT_HASH(addr) ((((word)(addr)) >> LOG_HBLKSIZE) & (PHT_ENTRIES - 1))
  559. # define get_pht_entry_from_index(bl, index) \
  560. (((bl)[divWORDSZ(index)] >> modWORDSZ(index)) & 1)
  561. # define set_pht_entry_from_index(bl, index) \
  562. (bl)[divWORDSZ(index)] |= (word)1 << modWORDSZ(index)
  563. # define clear_pht_entry_from_index(bl, index) \
  564. (bl)[divWORDSZ(index)] &= ~((word)1 << modWORDSZ(index))
  565. /* And a dumb but thread-safe version of set_pht_entry_from_index. */
  566. /* This sets (many) extra bits. */
  567. # define set_pht_entry_from_index_safe(bl, index) \
  568. (bl)[divWORDSZ(index)] = ONES
  569. /********************************************/
  570. /* */
  571. /* H e a p B l o c k s */
  572. /* */
  573. /********************************************/
  574. /* heap block header */
  575. #define HBLKMASK (HBLKSIZE-1)
  576. #define MARK_BITS_PER_HBLK (HBLKSIZE/GRANULE_BYTES)
  577. /* upper bound */
  578. /* We allocate 1 bit per allocation granule. */
  579. /* If MARK_BIT_PER_GRANULE is defined, we use */
  580. /* every nth bit, where n is the number of */
  581. /* allocation granules per object. If */
  582. /* MARK_BIT_PER_OBJ is defined, we only use the */
  583. /* initial group of mark bits, and it is safe */
  584. /* to allocate smaller header for large objects. */
  585. # ifdef USE_MARK_BYTES
  586. # define MARK_BITS_SZ (MARK_BITS_PER_HBLK + 1)
  587. /* Unlike the other case, this is in units of bytes. */
  588. /* Since we force doubleword alignment, we need at most one */
  589. /* mark bit per 2 words. But we do allocate and set one */
  590. /* extra mark bit to avoid an explicit check for the */
  591. /* partial object at the end of each block. */
  592. # else
  593. # define MARK_BITS_SZ (MARK_BITS_PER_HBLK/CPP_WORDSZ + 1)
  594. # endif
  595. #ifdef PARALLEL_MARK
  596. # include <atomic_ops.h>
  597. typedef AO_t counter_t;
  598. #else
  599. typedef size_t counter_t;
  600. #endif
  601. /* We maintain layout maps for heap blocks containing objects of a given */
  602. /* size. Each entry in this map describes a byte offset and has the */
  603. /* following type. */
  604. struct hblkhdr {
  605. struct hblk * hb_next; /* Link field for hblk free list */
  606. /* and for lists of chunks waiting to be */
  607. /* reclaimed. */
  608. struct hblk * hb_prev; /* Backwards link for free list. */
  609. struct hblk * hb_block; /* The corresponding block. */
  610. unsigned char hb_obj_kind;
  611. /* Kind of objects in the block. Each kind */
  612. /* identifies a mark procedure and a set of */
  613. /* list headers. Sometimes called regions. */
  614. unsigned char hb_flags;
  615. # define IGNORE_OFF_PAGE 1 /* Ignore pointers that do not */
  616. /* point to the first page of */
  617. /* this object. */
  618. # define WAS_UNMAPPED 2 /* This is a free block, which has */
  619. /* been unmapped from the address */
  620. /* space. */
  621. /* GC_remap must be invoked on it */
  622. /* before it can be reallocated. */
  623. /* Only set with USE_MUNMAP. */
  624. # define FREE_BLK 4 /* Block is free, i.e. not in use. */
  625. unsigned short hb_last_reclaimed;
  626. /* Value of GC_gc_no when block was */
  627. /* last allocated or swept. May wrap. */
  628. /* For a free block, this is maintained */
  629. /* only for USE_MUNMAP, and indicates */
  630. /* when the header was allocated, or */
  631. /* when the size of the block last */
  632. /* changed. */
  633. size_t hb_sz; /* If in use, size in bytes, of objects in the block. */
  634. /* if free, the size in bytes of the whole block */
  635. /* We assume that this is convertible to signed_word */
  636. /* without generating a negative result. We avoid */
  637. /* generating free blocks larger than that. */
  638. word hb_descr; /* object descriptor for marking. See */
  639. /* mark.h. */
  640. # ifdef MARK_BIT_PER_OBJ
  641. unsigned32 hb_inv_sz; /* A good upper bound for 2**32/hb_sz. */
  642. /* For large objects, we use */
  643. /* LARGE_INV_SZ. */
  644. # define LARGE_INV_SZ (1 << 16)
  645. # else
  646. unsigned char hb_large_block;
  647. short * hb_map; /* Essentially a table of remainders */
  648. /* mod BYTES_TO_GRANULES(hb_sz), except */
  649. /* for large blocks. See GC_obj_map. */
  650. # endif
  651. counter_t hb_n_marks; /* Number of set mark bits, excluding */
  652. /* the one always set at the end. */
  653. /* Currently it is concurrently */
  654. /* updated and hence only approximate. */
  655. /* But a zero value does guarantee that */
  656. /* the block contains no marked */
  657. /* objects. */
  658. /* Ensuring this property means that we */
  659. /* never decrement it to zero during a */
  660. /* collection, and hence the count may */
  661. /* be one too high. Due to concurrent */
  662. /* updates, an arbitrary number of */
  663. /* increments, but not all of them (!) */
  664. /* may be lost, hence it may in theory */
  665. /* be much too low. */
  666. /* The count may also be too high if */
  667. /* multiple mark threads mark the */
  668. /* same object due to a race. */
  669. /* Without parallel marking, the count */
  670. /* is accurate. */
  671. # ifdef USE_MARK_BYTES
  672. union {
  673. char _hb_marks[MARK_BITS_SZ];
  674. /* The i'th byte is 1 if the object */
  675. /* starting at granule i or object i is */
  676. /* marked, 0 o.w. */
  677. /* The mark bit for the "one past the */
  678. /* end" object is always set to avoid a */
  679. /* special case test in the marker. */
  680. word dummy; /* Force word alignment of mark bytes. */
  681. } _mark_byte_union;
  682. # define hb_marks _mark_byte_union._hb_marks
  683. # else
  684. word hb_marks[MARK_BITS_SZ];
  685. # endif /* !USE_MARK_BYTES */
  686. };
  687. # define ANY_INDEX 23 /* "Random" mark bit index for assertions */
  688. /* heap block body */
  689. # define HBLK_WORDS (HBLKSIZE/sizeof(word))
  690. # define HBLK_GRANULES (HBLKSIZE/GRANULE_BYTES)
  691. /* The number of objects in a block dedicated to a certain size. */
  692. /* may erroneously yield zero (instead of one) for large objects. */
  693. # define HBLK_OBJS(sz_in_bytes) (HBLKSIZE/(sz_in_bytes))
  694. struct hblk {
  695. char hb_body[HBLKSIZE];
  696. };
  697. # define HBLK_IS_FREE(hdr) (((hdr) -> hb_flags & FREE_BLK) != 0)
  698. # define OBJ_SZ_TO_BLOCKS(sz) divHBLKSZ(sz + HBLKSIZE-1)
  699. /* Size of block (in units of HBLKSIZE) needed to hold objects of */
  700. /* given sz (in bytes). */
  701. /* Object free list link */
  702. # define obj_link(p) (*(void **)(p))
  703. # define LOG_MAX_MARK_PROCS 6
  704. # define MAX_MARK_PROCS (1 << LOG_MAX_MARK_PROCS)
  705. /* Root sets. Logically private to mark_rts.c. But we don't want the */
  706. /* tables scanned, so we put them here. */
  707. /* MAX_ROOT_SETS is the maximum number of ranges that can be */
  708. /* registered as static roots. */
  709. # ifdef LARGE_CONFIG
  710. # define MAX_ROOT_SETS 8192
  711. # else
  712. # ifdef SMALL_CONFIG
  713. # define MAX_ROOT_SETS 512
  714. # else
  715. # define MAX_ROOT_SETS 2048
  716. # endif
  717. # endif
  718. # define MAX_EXCLUSIONS (MAX_ROOT_SETS/4)
  719. /* Maximum number of segments that can be excluded from root sets. */
  720. /*
  721. * Data structure for excluded static roots.
  722. */
  723. struct exclusion {
  724. ptr_t e_start;
  725. ptr_t e_end;
  726. };
  727. /* Data structure for list of root sets. */
  728. /* We keep a hash table, so that we can filter out duplicate additions. */
  729. /* Under Win32, we need to do a better job of filtering overlaps, so */
  730. /* we resort to sequential search, and pay the price. */
  731. struct roots {
  732. ptr_t r_start;
  733. ptr_t r_end;
  734. # if !defined(MSWIN32) && !defined(MSWINCE)
  735. struct roots * r_next;
  736. # endif
  737. GC_bool r_tmp;
  738. /* Delete before registering new dynamic libraries */
  739. };
  740. #if !defined(MSWIN32) && !defined(MSWINCE)
  741. /* Size of hash table index to roots. */
  742. # define LOG_RT_SIZE 6
  743. # define RT_SIZE (1 << LOG_RT_SIZE) /* Power of 2, may be != MAX_ROOT_SETS */
  744. #endif
  745. /* Lists of all heap blocks and free lists */
  746. /* as well as other random data structures */
  747. /* that should not be scanned by the */
  748. /* collector. */
  749. /* These are grouped together in a struct */
  750. /* so that they can be easily skipped by the */
  751. /* GC_mark routine. */
  752. /* The ordering is weird to make GC_malloc */
  753. /* faster by keeping the important fields */
  754. /* sufficiently close together that a */
  755. /* single load of a base register will do. */
  756. /* Scalars that could easily appear to */
  757. /* be pointers are also put here. */
  758. /* The main fields should precede any */
  759. /* conditionally included fields, so that */
  760. /* gc_inl.h will work even if a different set */
  761. /* of macros is defined when the client is */
  762. /* compiled. */
  763. struct _GC_arrays {
  764. word _heapsize; /* Heap size in bytes. */
  765. word _max_heapsize;
  766. word _requested_heapsize; /* Heap size due to explicit expansion */
  767. ptr_t _last_heap_addr;
  768. ptr_t _prev_heap_addr;
  769. word _large_free_bytes;
  770. /* Total bytes contained in blocks on large object free */
  771. /* list. */
  772. word _large_allocd_bytes;
  773. /* Total number of bytes in allocated large objects blocks. */
  774. /* For the purposes of this counter and the next one only, a */
  775. /* large object is one that occupies a block of at least */
  776. /* 2*HBLKSIZE. */
  777. word _max_large_allocd_bytes;
  778. /* Maximum number of bytes that were ever allocated in */
  779. /* large object blocks. This is used to help decide when it */
  780. /* is safe to split up a large block. */
  781. word _bytes_allocd_before_gc;
  782. /* Number of words allocated before this */
  783. /* collection cycle. */
  784. # ifndef SEPARATE_GLOBALS
  785. word _bytes_allocd;
  786. /* Number of words allocated during this collection cycle */
  787. # endif
  788. word _bytes_dropped;
  789. /* Number of black-listed bytes dropped during GC cycle */
  790. /* as a result of repeated scanning during allocation */
  791. /* attempts. These are treated largely as allocated, */
  792. /* even though they are not useful to the client. */
  793. word _bytes_finalized;
  794. /* Approximate number of bytes in objects (and headers) */
  795. /* That became ready for finalization in the last */
  796. /* collection. */
  797. word _non_gc_bytes_at_gc;
  798. /* Number of explicitly managed bytes of storage */
  799. /* at last collection. */
  800. word _bytes_freed;
  801. /* Number of explicitly deallocated bytes of memory */
  802. /* since last collection. */
  803. word _finalizer_bytes_freed;
  804. /* Bytes of memory explicitly deallocated while */
  805. /* finalizers were running. Used to approximate mem. */
  806. /* explicitly deallocated by finalizers. */
  807. ptr_t _scratch_end_ptr;
  808. ptr_t _scratch_last_end_ptr;
  809. /* Used by headers.c, and can easily appear to point to */
  810. /* heap. */
  811. GC_mark_proc _mark_procs[MAX_MARK_PROCS];
  812. /* Table of user-defined mark procedures. There is */
  813. /* a small number of these, which can be referenced */
  814. /* by DS_PROC mark descriptors. See gc_mark.h. */
  815. # ifndef SEPARATE_GLOBALS
  816. void *_objfreelist[MAXOBJGRANULES+1];
  817. /* free list for objects */
  818. void *_aobjfreelist[MAXOBJGRANULES+1];
  819. /* free list for atomic objs */
  820. # endif
  821. void *_uobjfreelist[MAXOBJGRANULES+1];
  822. /* uncollectable but traced objs */
  823. /* objects on this and auobjfreelist */
  824. /* are always marked, except during */
  825. /* garbage collections. */
  826. # ifdef ATOMIC_UNCOLLECTABLE
  827. void *_auobjfreelist[MAXOBJGRANULES+1];
  828. # endif
  829. /* uncollectable but traced objs */
  830. word _composite_in_use;
  831. /* Number of words in accessible composite */
  832. /* objects. */
  833. word _atomic_in_use;
  834. /* Number of words in accessible atomic */
  835. /* objects. */
  836. # ifdef USE_MUNMAP
  837. word _unmapped_bytes;
  838. # endif
  839. size_t _size_map[MAXOBJBYTES+1];
  840. /* Number of words to allocate for a given allocation request in */
  841. /* bytes. */
  842. # ifdef STUBBORN_ALLOC
  843. ptr_t _sobjfreelist[MAXOBJGRANULES+1];
  844. # endif
  845. /* free list for immutable objects */
  846. # ifdef MARK_BIT_PER_GRANULE
  847. short * _obj_map[MAXOBJGRANULES+1];
  848. /* If not NIL, then a pointer to a map of valid */
  849. /* object addresses. */
  850. /* _obj_map[sz_in_granules][i] is */
  851. /* i % sz_in_granules. */
  852. /* This is now used purely to replace a */
  853. /* division in the marker by a table lookup. */
  854. /* _obj_map[0] is used for large objects and */
  855. /* contains all nonzero entries. This gets us */
  856. /* out of the marker fast path without an extra */
  857. /* test. */
  858. # define MAP_LEN BYTES_TO_GRANULES(HBLKSIZE)
  859. # endif
  860. # define VALID_OFFSET_SZ HBLKSIZE
  861. char _valid_offsets[VALID_OFFSET_SZ];
  862. /* GC_valid_offsets[i] == TRUE ==> i */
  863. /* is registered as a displacement. */
  864. char _modws_valid_offsets[sizeof(word)];
  865. /* GC_valid_offsets[i] ==> */
  866. /* GC_modws_valid_offsets[i%sizeof(word)] */
  867. # ifdef STUBBORN_ALLOC
  868. page_hash_table _changed_pages;
  869. /* Stubborn object pages that were changes since last call to */
  870. /* GC_read_changed. */
  871. page_hash_table _prev_changed_pages;
  872. /* Stubborn object pages that were changes before last call to */
  873. /* GC_read_changed. */
  874. # endif
  875. # if defined(PROC_VDB) || defined(MPROTECT_VDB) || \
  876. defined(GWW_VDB) || defined(MANUAL_VDB)
  877. page_hash_table _grungy_pages; /* Pages that were dirty at last */
  878. /* GC_read_dirty. */
  879. # endif
  880. # if defined(MPROTECT_VDB) || defined(MANUAL_VDB)
  881. volatile page_hash_table _dirty_pages;
  882. /* Pages dirtied since last GC_read_dirty. */
  883. # endif
  884. # if defined(PROC_VDB) || defined(GWW_VDB)
  885. page_hash_table _written_pages; /* Pages ever dirtied */
  886. # endif
  887. # ifdef LARGE_CONFIG
  888. # if CPP_WORDSZ > 32
  889. # define MAX_HEAP_SECTS 4096 /* overflows at roughly 64 GB */
  890. # else
  891. # define MAX_HEAP_SECTS 768 /* Separately added heap sections. */
  892. # endif
  893. # else
  894. # ifdef SMALL_CONFIG
  895. # define MAX_HEAP_SECTS 128 /* Roughly 256MB (128*2048*1K) */
  896. # else
  897. # define MAX_HEAP_SECTS 384 /* Roughly 3GB */
  898. # endif
  899. # endif
  900. struct HeapSect {
  901. ptr_t hs_start; size_t hs_bytes;
  902. } _heap_sects[MAX_HEAP_SECTS]; /* Heap segments potentially */
  903. /* client objects. */
  904. # if defined(USE_PROC_FOR_LIBRARIES)
  905. struct HeapSect _our_memory[MAX_HEAP_SECTS];
  906. /* All GET_MEM allocated */
  907. /* memory. Includes block */
  908. /* headers and the like. */
  909. # endif
  910. # if defined(MSWIN32) || defined(MSWINCE)
  911. ptr_t _heap_bases[MAX_HEAP_SECTS];
  912. /* Start address of memory regions obtained from kernel. */
  913. # endif
  914. # ifdef MSWINCE
  915. word _heap_lengths[MAX_HEAP_SECTS];
  916. /* Commited lengths of memory regions obtained from kernel. */
  917. # endif
  918. struct roots _static_roots[MAX_ROOT_SETS];
  919. # if !defined(MSWIN32) && !defined(MSWINCE)
  920. struct roots * _root_index[RT_SIZE];
  921. # endif
  922. struct exclusion _excl_table[MAX_EXCLUSIONS];
  923. /* Block header index; see gc_headers.h */
  924. bottom_index * _all_nils;
  925. bottom_index * _top_index [TOP_SZ];
  926. #ifdef ENABLE_TRACE
  927. ptr_t _trace_addr;
  928. #endif
  929. #ifdef SAVE_CALL_CHAIN
  930. struct callinfo _last_stack[NFRAMES]; /* Stack at last garbage collection.*/
  931. /* Useful for debugging mysterious */
  932. /* object disappearances. */
  933. /* In the multithreaded case, we */
  934. /* currently only save the calling */
  935. /* stack. */
  936. #endif
  937. };
  938. GC_API GC_FAR struct _GC_arrays GC_arrays;
  939. # ifndef SEPARATE_GLOBALS
  940. # define GC_objfreelist GC_arrays._objfreelist
  941. # define GC_aobjfreelist GC_arrays._aobjfreelist
  942. # define GC_bytes_allocd GC_arrays._bytes_allocd
  943. # endif
  944. # define GC_uobjfreelist GC_arrays._uobjfreelist
  945. # ifdef ATOMIC_UNCOLLECTABLE
  946. # define GC_auobjfreelist GC_arrays._auobjfreelist
  947. # endif
  948. # define GC_sobjfreelist GC_arrays._sobjfreelist
  949. # define GC_valid_offsets GC_arrays._valid_offsets
  950. # define GC_modws_valid_offsets GC_arrays._modws_valid_offsets
  951. # ifdef STUBBORN_ALLOC
  952. # define GC_changed_pages GC_arrays._changed_pages
  953. # define GC_prev_changed_pages GC_arrays._prev_changed_pages
  954. # endif
  955. # ifdef MARK_BIT_PER_GRANULE
  956. # define GC_obj_map GC_arrays._obj_map
  957. # endif
  958. # define GC_last_heap_addr GC_arrays._last_heap_addr
  959. # define GC_prev_heap_addr GC_arrays._prev_heap_addr
  960. # define GC_large_free_bytes GC_arrays._large_free_bytes
  961. # define GC_large_allocd_bytes GC_arrays._large_allocd_bytes
  962. # define GC_max_large_allocd_bytes GC_arrays._max_large_allocd_bytes
  963. # define GC_bytes_dropped GC_arrays._bytes_dropped
  964. # define GC_bytes_finalized GC_arrays._bytes_finalized
  965. # define GC_non_gc_bytes_at_gc GC_arrays._non_gc_bytes_at_gc
  966. # define GC_bytes_freed GC_arrays._bytes_freed
  967. # define GC_finalizer_bytes_freed GC_arrays._finalizer_bytes_freed
  968. # define GC_scratch_end_ptr GC_arrays._scratch_end_ptr
  969. # define GC_scratch_last_end_ptr GC_arrays._scratch_last_end_ptr
  970. # define GC_mark_procs GC_arrays._mark_procs
  971. # define GC_heapsize GC_arrays._heapsize
  972. # define GC_max_heapsize GC_arrays._max_heapsize
  973. # define GC_requested_heapsize GC_arrays._requested_heapsize
  974. # define GC_bytes_allocd_before_gc GC_arrays._bytes_allocd_before_gc
  975. # define GC_heap_sects GC_arrays._heap_sects
  976. # ifdef USE_PROC_FOR_LIBRARIES
  977. # define GC_our_memory GC_arrays._our_memory
  978. # endif
  979. # define GC_last_stack GC_arrays._last_stack
  980. #ifdef ENABLE_TRACE
  981. #define GC_trace_addr GC_arrays._trace_addr
  982. #endif
  983. # ifdef USE_MUNMAP
  984. # define GC_unmapped_bytes GC_arrays._unmapped_bytes
  985. # endif
  986. # if defined(MSWIN32) || defined(MSWINCE)
  987. # define GC_heap_bases GC_arrays._heap_bases
  988. # endif
  989. # ifdef MSWINCE
  990. # define GC_heap_lengths GC_arrays._heap_lengths
  991. # endif
  992. # define GC_static_roots GC_arrays._static_roots
  993. # define GC_root_index GC_arrays._root_index
  994. # define GC_excl_table GC_arrays._excl_table
  995. # define GC_all_nils GC_arrays._all_nils
  996. # define GC_top_index GC_arrays._top_index
  997. # if defined(PROC_VDB) || defined(MPROTECT_VDB) || \
  998. defined(GWW_VDB) || defined(MANUAL_VDB)
  999. # define GC_grungy_pages GC_arrays._grungy_pages
  1000. # endif
  1001. # if defined(MPROTECT_VDB) || defined(MANUAL_VDB)
  1002. # define GC_dirty_pages GC_arrays._dirty_pages
  1003. # endif
  1004. # if defined(PROC_VDB) || defined(GWW_VDB)
  1005. # define GC_written_pages GC_arrays._written_pages
  1006. # endif
  1007. # define GC_composite_in_use GC_arrays._composite_in_use
  1008. # define GC_atomic_in_use GC_arrays._atomic_in_use
  1009. # define GC_size_map GC_arrays._size_map
  1010. # define beginGC_arrays ((ptr_t)(&GC_arrays))
  1011. # define endGC_arrays (((ptr_t)(&GC_arrays)) + (sizeof GC_arrays))
  1012. #define USED_HEAP_SIZE (GC_heapsize - GC_large_free_bytes)
  1013. /* Object kinds: */
  1014. # define MAXOBJKINDS 16
  1015. extern struct obj_kind {
  1016. void **ok_freelist; /* Array of free listheaders for this kind of object */
  1017. /* Point either to GC_arrays or to storage allocated */
  1018. /* with GC_scratch_alloc. */
  1019. struct hblk **ok_reclaim_list;
  1020. /* List headers for lists of blocks waiting to be */
  1021. /* swept. */
  1022. /* Indexed by object size in granules. */
  1023. word ok_descriptor; /* Descriptor template for objects in this */
  1024. /* block. */
  1025. GC_bool ok_relocate_descr;
  1026. /* Add object size in bytes to descriptor */
  1027. /* template to obtain descriptor. Otherwise */
  1028. /* template is used as is. */
  1029. GC_bool ok_init; /* Clear objects before putting them on the free list. */
  1030. } GC_obj_kinds[MAXOBJKINDS];
  1031. # define beginGC_obj_kinds ((ptr_t)(&GC_obj_kinds))
  1032. # define endGC_obj_kinds (beginGC_obj_kinds + (sizeof GC_obj_kinds))
  1033. /* Variables that used to be in GC_arrays, but need to be accessed by */
  1034. /* inline allocation code. If they were in GC_arrays, the inlined */
  1035. /* allocation code would include GC_arrays offsets (as it did), which */
  1036. /* introduce maintenance problems. */
  1037. #ifdef SEPARATE_GLOBALS
  1038. word GC_bytes_allocd;
  1039. /* Number of words allocated during this collection cycle */
  1040. ptr_t GC_objfreelist[MAXOBJGRANULES+1];
  1041. /* free list for NORMAL objects */
  1042. # define beginGC_objfreelist ((ptr_t)(&GC_objfreelist))
  1043. # define endGC_objfreelist (beginGC_objfreelist + sizeof(GC_objfreelist))
  1044. ptr_t GC_aobjfreelist[MAXOBJGRANULES+1];
  1045. /* free list for atomic (PTRFREE) objs */
  1046. # define beginGC_aobjfreelist ((ptr_t)(&GC_aobjfreelist))
  1047. # define endGC_aobjfreelist (beginGC_aobjfreelist + sizeof(GC_aobjfreelist))
  1048. #endif
  1049. /* Predefined kinds: */
  1050. # define PTRFREE 0
  1051. # define NORMAL 1
  1052. # define UNCOLLECTABLE 2
  1053. # ifdef ATOMIC_UNCOLLECTABLE
  1054. # define AUNCOLLECTABLE 3
  1055. # define STUBBORN 4
  1056. # define IS_UNCOLLECTABLE(k) (((k) & ~1) == UNCOLLECTABLE)
  1057. # else
  1058. # define STUBBORN 3
  1059. # define IS_UNCOLLECTABLE(k) ((k) == UNCOLLECTABLE)
  1060. # endif
  1061. extern unsigned GC_n_kinds;
  1062. GC_API word GC_fo_entries;
  1063. extern word GC_n_heap_sects; /* Number of separately added heap */
  1064. /* sections. */
  1065. #ifdef USE_PROC_FOR_LIBRARIES
  1066. extern word GC_n_memory; /* Number of GET_MEM allocated memory */
  1067. /* sections. */
  1068. #endif
  1069. extern word GC_page_size;
  1070. # if defined(MSWIN32) || defined(MSWINCE)
  1071. struct _SYSTEM_INFO;
  1072. extern struct _SYSTEM_INFO GC_sysinfo;
  1073. extern word GC_n_heap_bases; /* See GC_heap_bases. */
  1074. # endif
  1075. extern word GC_total_stack_black_listed;
  1076. /* Number of bytes on stack blacklist. */
  1077. extern word GC_black_list_spacing;
  1078. /* Average number of bytes between blacklisted */
  1079. /* blocks. Approximate. */
  1080. /* Counts only blocks that are */
  1081. /* "stack-blacklisted", i.e. that are */
  1082. /* problematic in the interior of an object. */
  1083. extern struct hblk * GC_hblkfreelist[];
  1084. /* List of completely empty heap blocks */
  1085. /* Linked through hb_next field of */
  1086. /* header structure associated with */
  1087. /* block. */
  1088. extern GC_bool GC_objects_are_marked; /* There are marked objects in */
  1089. /* the heap. */
  1090. #ifndef SMALL_CONFIG
  1091. extern GC_bool GC_incremental;
  1092. /* Using incremental/generational collection. */
  1093. # define TRUE_INCREMENTAL \
  1094. (GC_incremental && GC_time_limit != GC_TIME_UNLIMITED)
  1095. /* True incremental, not just generational, mode */
  1096. #else
  1097. # define GC_incremental FALSE
  1098. /* Hopefully allow optimizer to remove some code. */
  1099. # define TRUE_INCREMENTAL FALSE
  1100. #endif
  1101. extern GC_bool GC_dirty_maintained;
  1102. /* Dirty bits are being maintained, */
  1103. /* either for incremental collection, */
  1104. /* or to limit the root set. */
  1105. extern word GC_root_size; /* Total size of registered root sections */
  1106. extern GC_bool GC_debugging_started; /* GC_debug_malloc has been called. */
  1107. extern long GC_large_alloc_warn_interval;
  1108. /* Interval between unsuppressed warnings. */
  1109. extern long GC_large_alloc_warn_suppressed;
  1110. /* Number of warnings suppressed so far. */
  1111. #ifdef THREADS
  1112. extern GC_bool GC_world_stopped;
  1113. #endif
  1114. /* Operations */
  1115. # ifndef abs
  1116. # define abs(x) ((x) < 0? (-(x)) : (x))
  1117. # endif
  1118. /* Marks are in a reserved area in */
  1119. /* each heap block. Each word has one mark bit associated */
  1120. /* with it. Only those corresponding to the beginning of an */
  1121. /* object are used. */
  1122. /* Set mark bit correctly, even if mark bits may be concurrently */
  1123. /* accessed. */
  1124. #ifdef PARALLEL_MARK
  1125. # define OR_WORD(addr, bits) \
  1126. { AO_or((volatile AO_t *)(addr), (AO_t)bits); }
  1127. #else
  1128. # define OR_WORD(addr, bits) *(addr) |= (bits)
  1129. #endif
  1130. /* Mark bit operations */
  1131. /*
  1132. * Retrieve, set, clear the nth mark bit in a given heap block.
  1133. *
  1134. * (Recall that bit n corresponds to nth object or allocation granule
  1135. * relative to the beginning of the block, including unused words)
  1136. */
  1137. #ifdef USE_MARK_BYTES
  1138. # define mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n])
  1139. # define set_mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n]) = 1
  1140. # define clear_mark_bit_from_hdr(hhdr,n) ((hhdr)->hb_marks[n]) = 0
  1141. #else /* !USE_MARK_BYTES */
  1142. # define mark_bit_from_hdr(hhdr,n) (((hhdr)->hb_marks[divWORDSZ(n)] \
  1143. >> (modWORDSZ(n))) & (word)1)
  1144. # define set_mark_bit_from_hdr(hhdr,n) \
  1145. OR_WORD((hhdr)->hb_marks+divWORDSZ(n), \
  1146. (word)1 << modWORDSZ(n))
  1147. # define clear_mark_bit_from_hdr(hhdr,n) (hhdr)->hb_marks[divWORDSZ(n)] \
  1148. &= ~((word)1 << modWORDSZ(n))
  1149. #endif /* !USE_MARK_BYTES */
  1150. #ifdef MARK_BIT_PER_OBJ
  1151. # define MARK_BIT_NO(offset, sz) (((unsigned)(offset))/(sz))
  1152. /* Get the mark bit index corresponding to the given byte */
  1153. /* offset and size (in bytes). */
  1154. # define MARK_BIT_OFFSET(sz) 1
  1155. /* Spacing between useful mark bits. */
  1156. # define IF_PER_OBJ(x) x
  1157. # define FINAL_MARK_BIT(sz) ((sz) > MAXOBJBYTES? 1 : HBLK_OBJS(sz))
  1158. /* Position of final, always set, mark bit. */
  1159. #else /* MARK_BIT_PER_GRANULE */
  1160. # define MARK_BIT_NO(offset, sz) BYTES_TO_GRANULES((unsigned)(offset))
  1161. # define MARK_BIT_OFFSET(sz) BYTES_TO_GRANULES(sz)
  1162. # define IF_PER_OBJ(x)
  1163. # define FINAL_MARK_BIT(sz) \
  1164. ((sz) > MAXOBJBYTES? MARK_BITS_PER_HBLK \
  1165. : BYTES_TO_GRANULES(sz * HBLK_OBJS(sz)))
  1166. #endif
  1167. /* Important internal collector routines */
  1168. ptr_t GC_approx_sp(void);
  1169. GC_bool GC_should_collect(void);
  1170. void GC_apply_to_all_blocks(void (*fn) (struct hblk *h, word client_data),
  1171. word client_data);
  1172. /* Invoke fn(hbp, client_data) for each */
  1173. /* allocated heap block. */
  1174. struct hblk * GC_next_used_block(struct hblk * h);
  1175. /* Return first in-use block >= h */
  1176. struct hblk * GC_prev_block(struct hblk * h);
  1177. /* Return last block <= h. Returned block */
  1178. /* is managed by GC, but may or may not be in */
  1179. /* use. */
  1180. void GC_mark_init(void);
  1181. void GC_clear_marks(void); /* Clear mark bits for all heap objects. */
  1182. void GC_invalidate_mark_state(void);
  1183. /* Tell the marker that marked */
  1184. /* objects may point to unmarked */
  1185. /* ones, and roots may point to */
  1186. /* unmarked objects. */
  1187. /* Reset mark stack. */
  1188. GC_bool GC_mark_stack_empty(void);
  1189. GC_bool GC_mark_some(ptr_t cold_gc_frame);
  1190. /* Perform about one pages worth of marking */
  1191. /* work of whatever kind is needed. Returns */
  1192. /* quickly if no collection is in progress. */
  1193. /* Return TRUE if mark phase finished. */
  1194. void GC_initiate_gc(void);
  1195. /* initiate collection. */
  1196. /* If the mark state is invalid, this */
  1197. /* becomes full colleection. Otherwise */
  1198. /* it's partial. */
  1199. void GC_push_all(ptr_t bottom, ptr_t top);
  1200. /* Push everything in a range */
  1201. /* onto mark stack. */
  1202. void GC_push_selected(ptr_t bottom, ptr_t top,
  1203. int (*dirty_fn) (struct hblk *h),
  1204. void (*push_fn) (ptr_t bottom, ptr_t top) );
  1205. /* Push all pages h in [b,t) s.t. */
  1206. /* select_fn(h) != 0 onto mark stack. */
  1207. #ifndef SMALL_CONFIG
  1208. void GC_push_conditional (ptr_t b, ptr_t t, GC_bool all);
  1209. #else
  1210. # define GC_push_conditional(b, t, all) GC_push_all(b, t)
  1211. #endif
  1212. /* Do either of the above, depending */
  1213. /* on the third arg. */
  1214. void GC_push_all_stack (ptr_t b, ptr_t t);
  1215. /* As above, but consider */
  1216. /* interior pointers as valid */
  1217. void GC_push_all_eager (ptr_t b, ptr_t t);
  1218. /* Same as GC_push_all_stack, but */
  1219. /* ensures that stack is scanned */
  1220. /* immediately, not just scheduled */
  1221. /* for scanning. */
  1222. #ifndef THREADS
  1223. void GC_push_all_stack_partially_eager(ptr_t bottom, ptr_t top,
  1224. ptr_t cold_gc_frame);
  1225. /* Similar to GC_push_all_eager, but only the */
  1226. /* part hotter than cold_gc_frame is scanned */
  1227. /* immediately. Needed to ensure that callee- */
  1228. /* save registers are not missed. */
  1229. #else
  1230. /* In the threads case, we push part of the current thread stack */
  1231. /* with GC_push_all_eager when we push the registers. This gets the */
  1232. /* callee-save registers that may disappear. The remainder of the */
  1233. /* stacks are scheduled for scanning in *GC_push_other_roots, which */
  1234. /* is thread-package-specific. */
  1235. #endif
  1236. void GC_push_current_stack(ptr_t cold_gc_frame, void *context);
  1237. /* Push enough of the current stack eagerly to */
  1238. /* ensure that callee-save registers saved in */
  1239. /* GC frames are scanned. */
  1240. /* In the non-threads case, schedule entire */
  1241. /* stack for scanning. */
  1242. /* The second argument is a pointer to the */
  1243. /* (possibly null) thread context, for */
  1244. /* (currently hypothetical) more precise */
  1245. /* stack scanning. */
  1246. void GC_push_roots(GC_bool all, ptr_t cold_gc_frame);
  1247. /* Push all or dirty roots. */
  1248. extern void (*GC_push_other_roots)(void);
  1249. /* Push system or application specific roots */
  1250. /* onto the mark stack. In some environments */
  1251. /* (e.g. threads environments) this is */
  1252. /* predfined to be non-zero. A client supplied */
  1253. /* replacement should also call the original */
  1254. /* function. */
  1255. extern void GC_push_gc_structures(void);
  1256. /* Push GC internal roots. These are normally */
  1257. /* included in the static data segment, and */
  1258. /* Thus implicitly pushed. But we must do this */
  1259. /* explicitly if normal root processing is */
  1260. /* disabled. Calls the following: */
  1261. extern void GC_push_finalizer_structures(void);
  1262. extern void GC_push_stubborn_structures (void);
  1263. # ifdef THREADS
  1264. extern void GC_push_thread_structures (void);
  1265. # endif
  1266. extern void (*GC_push_typed_structures) (void);
  1267. /* A pointer such that we can avoid linking in */
  1268. /* the typed allocation support if unused. */
  1269. extern void (*GC_start_call_back) (void);
  1270. /* Called at start of full collections. */
  1271. /* Not called if 0. Called with allocation */
  1272. /* lock held. */
  1273. /* 0 by default. */
  1274. void GC_push_regs_and_stack(ptr_t cold_gc_frame);
  1275. void GC_push_regs(void);
  1276. void GC_with_callee_saves_pushed(void (*fn)(ptr_t, void *),
  1277. ptr_t arg);
  1278. # if defined(SPARC) || defined(IA64)
  1279. /* Cause all stacked registers to be saved in memory. Return a */
  1280. /* pointer to the top of the corresponding memory stack. */
  1281. ptr_t GC_save_regs_in_stack(void);
  1282. # endif
  1283. /* Push register contents onto mark stack. */
  1284. /* If NURSERY is defined, the default push */
  1285. /* action can be overridden with GC_push_proc */
  1286. # ifdef NURSERY
  1287. extern void (*GC_push_proc)(ptr_t);
  1288. # endif
  1289. # if defined(MSWIN32) || defined(MSWINCE)
  1290. void __cdecl GC_push_one(word p);
  1291. # else
  1292. void GC_push_one(word p);
  1293. /* If p points to an object, mark it */
  1294. /* and push contents on the mark stack */
  1295. /* Pointer recognition test always */
  1296. /* accepts interior pointers, i.e. this */
  1297. /* is appropriate for pointers found on */
  1298. /* stack. */
  1299. # endif
  1300. # if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
  1301. void GC_mark_and_push_stack(ptr_t p, ptr_t source);
  1302. /* Ditto, omits plausibility test */
  1303. # else
  1304. void GC_mark_and_push_stack(ptr_t p);
  1305. # endif
  1306. void GC_push_marked(struct hblk * h, hdr * hhdr);
  1307. /* Push contents of all marked objects in h onto */
  1308. /* mark stack. */
  1309. #ifdef SMALL_CONFIG
  1310. # define GC_push_next_marked_dirty(h) GC_push_next_marked(h)
  1311. #else
  1312. struct hblk * GC_push_next_marked_dirty(struct hblk * h);
  1313. /* Invoke GC_push_marked on next dirty block above h. */
  1314. /* Return a pointer just past the end of this block. */
  1315. #endif /* !SMALL_CONFIG */
  1316. struct hblk * GC_push_next_marked(struct hblk * h);
  1317. /* Ditto, but also mark from clean pages. */
  1318. struct hblk * GC_push_next_marked_uncollectable(struct hblk * h);
  1319. /* Ditto, but mark only from uncollectable pages. */
  1320. GC_bool GC_stopped_mark(GC_stop_func stop_func);
  1321. /* Stop world and mark from all roots */
  1322. /* and rescuers. */
  1323. void GC_clear_hdr_marks(hdr * hhdr);
  1324. /* Clear the mark bits in a header */
  1325. void GC_set_hdr_marks(hdr * hhdr);
  1326. /* Set the mark bits in a header */
  1327. void GC_set_fl_marks(ptr_t p);
  1328. /* Set all mark bits associated with */
  1329. /* a free list. */
  1330. #ifdef GC_ASSERTIONS
  1331. void GC_check_fl_marks(ptr_t p);
  1332. /* Check that all mark bits */
  1333. /* associated with a free list are */
  1334. /* set. Abort if not. */
  1335. #endif
  1336. void GC_add_roots_inner(ptr_t b, ptr_t e, GC_bool tmp);
  1337. void GC_remove_roots_inner(ptr_t b, ptr_t e);
  1338. GC_bool GC_is_static_root(ptr_t p);
  1339. /* Is the address p in one of the registered static */
  1340. /* root sections? */
  1341. # if defined(MSWIN32) || defined(_WIN32_WCE_EMULATION)
  1342. GC_bool GC_is_tmp_root(ptr_t p);
  1343. /* Is the address p in one of the temporary static */
  1344. /* root sections? */
  1345. # endif
  1346. void GC_register_dynamic_libraries(void);
  1347. /* Add dynamic library data sections to the root set. */
  1348. void GC_cond_register_dynamic_libraries(void);
  1349. /* Remove and reregister dynamic libraries if we're */
  1350. /* configured to do that at each GC. */
  1351. GC_bool GC_register_main_static_data(void);
  1352. /* We need to register the main data segment. Returns */
  1353. /* TRUE unless this is done implicitly as part of */
  1354. /* dynamic library registration. */
  1355. /* Machine dependent startup routines */
  1356. ptr_t GC_get_main_stack_base(void); /* Cold end of stack */
  1357. #ifdef IA64
  1358. ptr_t GC_get_register_stack_base(void);
  1359. /* Cold end of register stack. */
  1360. #endif
  1361. void GC_register_data_segments(void);
  1362. /* Black listing: */
  1363. void GC_bl_init(void);
  1364. # ifdef PRINT_BLACK_LIST
  1365. void GC_add_to_black_list_normal(word p, ptr_t source);
  1366. /* Register bits as a possible future false */
  1367. /* reference from the heap or static data */
  1368. # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
  1369. if (GC_all_interior_pointers) { \
  1370. GC_add_to_black_list_stack((word)(bits), (source)); \
  1371. } else { \
  1372. GC_add_to_black_list_normal((word)(bits), (source)); \
  1373. }
  1374. # else
  1375. void GC_add_to_black_list_normal(word p);
  1376. # define GC_ADD_TO_BLACK_LIST_NORMAL(bits, source) \
  1377. if (GC_all_interior_pointers) { \
  1378. GC_add_to_black_list_stack((word)(bits)); \
  1379. } else { \
  1380. GC_add_to_black_list_normal((word)(bits)); \
  1381. }
  1382. # endif
  1383. # ifdef PRINT_BLACK_LIST
  1384. void GC_add_to_black_list_stack(word p, ptr_t source);
  1385. # define GC_ADD_TO_BLACK_LIST_STACK(bits, source) \
  1386. GC_add_to_black_list_stack((word)(bits), (source))
  1387. # else
  1388. void GC_add_to_black_list_stack(word p);
  1389. # define GC_ADD_TO_BLACK_LIST_STACK(bits, source) \
  1390. GC_add_to_black_list_stack((word)(bits))
  1391. # endif
  1392. struct hblk * GC_is_black_listed(struct hblk * h, word len);
  1393. /* If there are likely to be false references */
  1394. /* to a block starting at h of the indicated */
  1395. /* length, then return the next plausible */
  1396. /* starting location for h that might avoid */
  1397. /* these false references. */
  1398. void GC_promote_black_lists(void);
  1399. /* Declare an end to a black listing phase. */
  1400. void GC_unpromote_black_lists(void);
  1401. /* Approximately undo the effect of the above. */
  1402. /* This actually loses some information, but */
  1403. /* only in a reasonably safe way. */
  1404. word GC_number_stack_black_listed(struct hblk *start, struct hblk *endp1);
  1405. /* Return the number of (stack) blacklisted */
  1406. /* blocks in the range for statistical */
  1407. /* purposes. */
  1408. ptr_t GC_scratch_alloc(size_t bytes);
  1409. /* GC internal memory allocation for */
  1410. /* small objects. Deallocation is not */
  1411. /* possible. */
  1412. /* Heap block layout maps: */
  1413. GC_bool GC_add_map_entry(size_t sz);
  1414. /* Add a heap block map for objects of */
  1415. /* size sz to obj_map. */
  1416. /* Return FALSE on failure. */
  1417. void GC_register_displacement_inner(size_t offset);
  1418. /* Version of GC_register_displacement */
  1419. /* that assumes lock is already held */
  1420. /* and signals are already disabled. */
  1421. void GC_initialize_offsets(void);
  1422. /* Initialize GC_valid_offsets, */
  1423. /* depending on current */
  1424. /* GC_all_interior_pointers settings. */
  1425. /* hblk allocation: */
  1426. void GC_new_hblk(size_t size_in_granules, int kind);
  1427. /* Allocate a new heap block, and build */
  1428. /* a free list in it. */
  1429. ptr_t GC_build_fl(struct hblk *h, size_t words, GC_bool clear, ptr_t list);
  1430. /* Build a free list for objects of */
  1431. /* size sz in block h. Append list to */
  1432. /* end of the free lists. Possibly */
  1433. /* clear objects on the list. Normally */
  1434. /* called by GC_new_hblk, but also */
  1435. /* called explicitly without GC lock. */
  1436. struct hblk * GC_allochblk (size_t size_in_bytes, int kind,
  1437. unsigned flags);
  1438. /* Allocate a heap block, inform */
  1439. /* the marker that block is valid */
  1440. /* for objects of indicated size. */
  1441. ptr_t GC_alloc_large (size_t lb, int k, unsigned flags);
  1442. /* Allocate a large block of size lb bytes. */
  1443. /* The block is not cleared. */
  1444. /* Flags is 0 or IGNORE_OFF_PAGE. */
  1445. /* Calls GC_allchblk to do the actual */
  1446. /* allocation, but also triggers GC and/or */
  1447. /* heap expansion as appropriate. */
  1448. /* Does not update GC_bytes_allocd, but does */
  1449. /* other accounting. */
  1450. ptr_t GC_alloc_large_and_clear(size_t lb, int k, unsigned flags);
  1451. /* As above, but clear block if appropriate */
  1452. /* for kind k. */
  1453. void GC_freehblk(struct hblk * p);
  1454. /* Deallocate a heap block and mark it */
  1455. /* as invalid. */
  1456. /* Misc GC: */
  1457. void GC_init_inner(void);
  1458. GC_bool GC_expand_hp_inner(word n);
  1459. void GC_start_reclaim(int abort_if_found);
  1460. /* Restore unmarked objects to free */
  1461. /* lists, or (if abort_if_found is */
  1462. /* TRUE) report them. */
  1463. /* Sweeping of small object pages is */
  1464. /* largely deferred. */
  1465. void GC_continue_reclaim(size_t sz, int kind);
  1466. /* Sweep pages of the given size and */
  1467. /* kind, as long as possible, and */
  1468. /* as long as the corr. free list is */
  1469. /* empty. Sz is in granules. */
  1470. void GC_reclaim_or_delete_all(void);
  1471. /* Arrange for all reclaim lists to be */
  1472. /* empty. Judiciously choose between */
  1473. /* sweeping and discarding each page. */
  1474. GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old);
  1475. /* Reclaim all blocks. Abort (in a */
  1476. /* consistent state) if f returns TRUE. */
  1477. ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
  1478. GC_bool init, ptr_t list, signed_word *count);
  1479. /* Rebuild free list in hbp with */
  1480. /* header hhdr, with objects of size sz */
  1481. /* bytes. Add list to the end of the */
  1482. /* free list. Add the number of */
  1483. /* reclaimed bytes to *count. */
  1484. GC_bool GC_block_empty(hdr * hhdr);
  1485. /* Block completely unmarked? */
  1486. GC_bool GC_never_stop_func(void);
  1487. /* Returns FALSE. */
  1488. GC_bool GC_try_to_collect_inner(GC_stop_func f);
  1489. /* Collect; caller must have acquired */
  1490. /* lock and disabled signals. */
  1491. /* Collection is aborted if f returns */
  1492. /* TRUE. Returns TRUE if it completes */
  1493. /* successfully. */
  1494. # define GC_gcollect_inner() \
  1495. (void) GC_try_to_collect_inner(GC_never_stop_func)
  1496. void GC_finish_collection(void);
  1497. /* Finish collection. Mark bits are */
  1498. /* consistent and lock is still held. */
  1499. GC_bool GC_collect_or_expand(word needed_blocks, GC_bool ignore_off_page);
  1500. /* Collect or expand heap in an attempt */
  1501. /* make the indicated number of free */
  1502. /* blocks available. Should be called */
  1503. /* until the blocks are available or */
  1504. /* until it fails by returning FALSE. */
  1505. extern GC_bool GC_is_initialized; /* GC_init() has been run. */
  1506. #if defined(MSWIN32) || defined(MSWINCE)
  1507. void GC_deinit(void);
  1508. /* Free any resources allocated by */
  1509. /* GC_init */
  1510. #endif
  1511. void GC_collect_a_little_inner(int n);
  1512. /* Do n units worth of garbage */
  1513. /* collection work, if appropriate. */
  1514. /* A unit is an amount appropriate for */
  1515. /* HBLKSIZE bytes of allocation. */
  1516. /* void * GC_generic_malloc(size_t lb, int k); */
  1517. /* Allocate an object of the given */
  1518. /* kind. By default, there are only */
  1519. /* a few kinds: composite(pointerfree), */
  1520. /* atomic, uncollectable, etc. */
  1521. /* We claim it's possible for clever */
  1522. /* client code that understands GC */
  1523. /* internals to add more, e.g. to */
  1524. /* communicate object layout info */
  1525. /* to the collector. */
  1526. /* The actual decl is in gc_mark.h. */
  1527. void * GC_generic_malloc_ignore_off_page(size_t b, int k);
  1528. /* As above, but pointers past the */
  1529. /* first page of the resulting object */
  1530. /* are ignored. */
  1531. void * GC_generic_malloc_inner(size_t lb, int k);
  1532. /* Ditto, but I already hold lock, etc. */
  1533. void * GC_generic_malloc_inner_ignore_off_page(size_t lb, int k);
  1534. /* Allocate an object, where */
  1535. /* the client guarantees that there */
  1536. /* will always be a pointer to the */
  1537. /* beginning of the object while the */
  1538. /* object is live. */
  1539. void GC_generic_malloc_many(size_t lb, int k, void **result);
  1540. /* Store a pointer to a list of newly */
  1541. /* allocated objects of kind k and size */
  1542. /* lb in *result. */
  1543. /* Caler must make sure that *result is */
  1544. /* traced even if objects are ptrfree. */
  1545. ptr_t GC_allocobj(size_t sz, int kind);
  1546. /* Make the indicated */
  1547. /* free list nonempty, and return its */
  1548. /* head. Sz is in granules. */
  1549. /* Allocation routines that bypass the thread local cache. */
  1550. /* Used internally. */
  1551. #ifdef THREAD_LOCAL_ALLOC
  1552. void * GC_core_malloc(size_t);
  1553. void * GC_core_malloc_atomic(size_t);
  1554. # ifdef GC_GCJ_SUPPORT
  1555. void *GC_core_gcj_malloc(size_t, void *);
  1556. # endif
  1557. #endif /* THREAD_LOCAL_ALLOC */
  1558. void GC_free_inner(void * p);
  1559. void GC_debug_free_inner(void * p);
  1560. void GC_init_headers(void);
  1561. struct hblkhdr * GC_install_header(struct hblk *h);
  1562. /* Install a header for block h. */
  1563. /* Return 0 on failure, or the header */
  1564. /* otherwise. */
  1565. GC_bool GC_install_counts(struct hblk * h, size_t sz);
  1566. /* Set up forwarding counts for block */
  1567. /* h of size sz. */
  1568. /* Return FALSE on failure. */
  1569. void GC_remove_header(struct hblk * h);
  1570. /* Remove the header for block h. */
  1571. void GC_remove_counts(struct hblk * h, size_t sz);
  1572. /* Remove forwarding counts for h. */
  1573. hdr * GC_find_header(ptr_t h); /* Debugging only. */
  1574. void GC_finalize(void);
  1575. /* Perform all indicated finalization actions */
  1576. /* on unmarked objects. */
  1577. /* Unreachable finalizable objects are enqueued */
  1578. /* for processing by GC_invoke_finalizers. */
  1579. /* Invoked with lock. */
  1580. void GC_notify_or_invoke_finalizers(void);
  1581. /* If GC_finalize_on_demand is not set, invoke */
  1582. /* eligible finalizers. Otherwise: */
  1583. /* Call *GC_finalizer_notifier if there are */
  1584. /* finalizers to be run, and we haven't called */
  1585. /* this procedure yet this GC cycle. */
  1586. GC_API void * GC_make_closure(GC_finalization_proc fn, void * data);
  1587. GC_API void GC_debug_invoke_finalizer(void * obj, void * data);
  1588. /* Auxiliary fns to make finalization work */
  1589. /* correctly with displaced pointers introduced */
  1590. /* by the debugging allocators. */
  1591. void GC_add_to_heap(struct hblk *p, size_t bytes);
  1592. /* Add a HBLKSIZE aligned chunk to the heap. */
  1593. #ifdef USE_PROC_FOR_LIBRARIES
  1594. void GC_add_to_our_memory(ptr_t p, size_t bytes);
  1595. /* Add a chunk to GC_our_memory. */
  1596. /* If p == 0, do nothing. */
  1597. #else
  1598. # define GC_add_to_our_memory(p, bytes)
  1599. #endif
  1600. void GC_print_obj(ptr_t p);
  1601. /* P points to somewhere inside an object with */
  1602. /* debugging info. Print a human readable */
  1603. /* description of the object to stderr. */
  1604. extern void (*GC_check_heap)(void);
  1605. /* Check that all objects in the heap with */
  1606. /* debugging info are intact. */
  1607. /* Add any that are not to GC_smashed list. */
  1608. extern void (*GC_print_all_smashed) (void);
  1609. /* Print GC_smashed if it's not empty. */
  1610. /* Clear GC_smashed list. */
  1611. extern void GC_print_all_errors (void);
  1612. /* Print smashed and leaked objects, if any. */
  1613. /* Clear the lists of such objects. */
  1614. extern void (*GC_print_heap_obj) (ptr_t p);
  1615. /* If possible print s followed by a more */
  1616. /* detailed description of the object */
  1617. /* referred to by p. */
  1618. #if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
  1619. void GC_print_address_map (void);
  1620. /* Print an address map of the process. */
  1621. #endif
  1622. extern GC_bool GC_have_errors; /* We saw a smashed or leaked object. */
  1623. /* Call error printing routine */
  1624. /* occasionally. */
  1625. #ifndef SMALL_CONFIG
  1626. extern int GC_print_stats; /* Nonzero generates basic GC log. */
  1627. /* VERBOSE generates add'l messages. */
  1628. #else
  1629. # define GC_print_stats 0
  1630. /* Will this keep the message character strings from the executable? */
  1631. /* It should ... */
  1632. #endif
  1633. #define VERBOSE 2
  1634. #ifndef NO_DEBUGGING
  1635. extern GC_bool GC_dump_regularly; /* Generate regular debugging dumps. */
  1636. # define COND_DUMP if (GC_dump_regularly) GC_dump();
  1637. #else
  1638. # define COND_DUMP
  1639. #endif
  1640. #ifdef KEEP_BACK_PTRS
  1641. extern long GC_backtraces;
  1642. void GC_generate_random_backtrace_no_gc(void);
  1643. #endif
  1644. extern GC_bool GC_print_back_height;
  1645. #ifdef MAKE_BACK_GRAPH
  1646. void GC_print_back_graph_stats(void);
  1647. #endif
  1648. /* Macros used for collector internal allocation. */
  1649. /* These assume the collector lock is held. */
  1650. #ifdef DBG_HDRS_ALL
  1651. extern void * GC_debug_generic_malloc_inner(size_t lb, int k);
  1652. extern void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
  1653. int k);
  1654. # define GC_INTERNAL_MALLOC GC_debug_generic_malloc_inner
  1655. # define GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE \
  1656. GC_debug_generic_malloc_inner_ignore_off_page
  1657. # ifdef THREADS
  1658. # define GC_INTERNAL_FREE GC_debug_free_inner
  1659. # else
  1660. # define GC_INTERNAL_FREE GC_debug_free
  1661. # endif
  1662. #else
  1663. # define GC_INTERNAL_MALLOC GC_generic_malloc_inner
  1664. # define GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE \
  1665. GC_generic_malloc_inner_ignore_off_page
  1666. # ifdef THREADS
  1667. # define GC_INTERNAL_FREE GC_free_inner
  1668. # else
  1669. # define GC_INTERNAL_FREE GC_free
  1670. # endif
  1671. #endif
  1672. /* Memory unmapping: */
  1673. #ifdef USE_MUNMAP
  1674. v