PageRenderTime 54ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/libgc/include/private/gc_priv.h

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