PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/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

Large files files are truncated, but you can click here to view the full 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-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 */

Large files files are truncated, but you can click here to view the full file