mark.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  1. /*
  2. * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3. * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
  4. * Copyright (c) 2000 by Hewlett-Packard Company. All rights reserved.
  5. *
  6. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  7. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  8. *
  9. * Permission is hereby granted to use or copy this program
  10. * for any purpose, provided the above notices are retained on all copies.
  11. * Permission to modify the code and to distribute modified code is granted,
  12. * provided the above notices are retained, and a notice that the code was
  13. * modified is included with the above copyright notice.
  14. *
  15. */
  16. #if defined(__MINGW32__) && !defined(__MINGW_EXCPT_DEFINE_PSDK) \
  17. && defined(__i386__) /* cannot use macros from gcconfig.h */
  18. /* Otherwise EXCEPTION_REGISTRATION type declaration from winnt.h */
  19. /* might be used. That declaration has "handler" callback with NTAPI */
  20. /* attribute. The proper type (with "handler" field compatible with */
  21. /* GC mark_ex_handler) is declared in excpt.h. The given macro is */
  22. /* defined before any system header include. */
  23. # define __MINGW_EXCPT_DEFINE_PSDK 1
  24. #endif
  25. #include "private/gc_pmark.h"
  26. #include <stdio.h>
  27. #if defined(MSWIN32) && defined(__GNUC__)
  28. # include <excpt.h>
  29. #endif
  30. /* Make arguments appear live to compiler. Put here to minimize the */
  31. /* risk of inlining. Used to minimize junk left in registers. */
  32. GC_ATTR_NOINLINE
  33. void GC_noop6(word arg1 GC_ATTR_UNUSED, word arg2 GC_ATTR_UNUSED,
  34. word arg3 GC_ATTR_UNUSED, word arg4 GC_ATTR_UNUSED,
  35. word arg5 GC_ATTR_UNUSED, word arg6 GC_ATTR_UNUSED)
  36. {
  37. /* Avoid GC_noop6 calls to be optimized away. */
  38. # ifdef AO_CLEAR
  39. AO_compiler_barrier(); /* to serve as a special side-effect */
  40. # else
  41. GC_noop1(0);
  42. # endif
  43. }
  44. volatile word GC_noop_sink;
  45. /* Single argument version, robust against whole program analysis. */
  46. GC_ATTR_NO_SANITIZE_THREAD
  47. GC_API void GC_CALL GC_noop1(word x)
  48. {
  49. GC_noop_sink = x;
  50. }
  51. /* mark_proc GC_mark_procs[MAX_MARK_PROCS] = {0} -- declared in gc_priv.h */
  52. GC_INNER unsigned GC_n_mark_procs = GC_RESERVED_MARK_PROCS;
  53. /* Initialize GC_obj_kinds properly and standard free lists properly. */
  54. /* This must be done statically since they may be accessed before */
  55. /* GC_init is called. */
  56. /* It's done here, since we need to deal with mark descriptors. */
  57. GC_INNER struct obj_kind GC_obj_kinds[MAXOBJKINDS] = {
  58. /* PTRFREE */ { &GC_aobjfreelist[0], 0 /* filled in dynamically */,
  59. /* 0 | */ GC_DS_LENGTH, FALSE, FALSE
  60. /*, */ OK_DISCLAIM_INITZ },
  61. /* NORMAL */ { &GC_objfreelist[0], 0,
  62. /* 0 | */ GC_DS_LENGTH,
  63. /* adjusted in GC_init for EXTRA_BYTES */
  64. TRUE /* add length to descr */, TRUE
  65. /*, */ OK_DISCLAIM_INITZ },
  66. /* UNCOLLECTABLE */
  67. { &GC_uobjfreelist[0], 0,
  68. /* 0 | */ GC_DS_LENGTH, TRUE /* add length to descr */, TRUE
  69. /*, */ OK_DISCLAIM_INITZ },
  70. # ifdef GC_ATOMIC_UNCOLLECTABLE
  71. { &GC_auobjfreelist[0], 0,
  72. /* 0 | */ GC_DS_LENGTH, FALSE /* add length to descr */, FALSE
  73. /*, */ OK_DISCLAIM_INITZ },
  74. # endif
  75. };
  76. GC_INNER unsigned GC_n_kinds = GC_N_KINDS_INITIAL_VALUE;
  77. # ifndef INITIAL_MARK_STACK_SIZE
  78. # define INITIAL_MARK_STACK_SIZE (1*HBLKSIZE)
  79. /* INITIAL_MARK_STACK_SIZE * sizeof(mse) should be a */
  80. /* multiple of HBLKSIZE. */
  81. /* The incremental collector actually likes a larger */
  82. /* size, since it wants to push all marked dirty */
  83. /* objects before marking anything new. Currently we */
  84. /* let it grow dynamically. */
  85. # endif
  86. #if !defined(GC_DISABLE_INCREMENTAL)
  87. STATIC word GC_n_rescuing_pages = 0;
  88. /* Number of dirty pages we marked from */
  89. /* excludes ptrfree pages, etc. */
  90. /* Used for logging only. */
  91. #endif
  92. GC_INNER size_t GC_mark_stack_size = 0;
  93. #ifdef PARALLEL_MARK
  94. STATIC volatile AO_t GC_first_nonempty = 0;
  95. /* Lowest entry on mark stack */
  96. /* that may be nonempty. */
  97. /* Updated only by initiating */
  98. /* thread. */
  99. #endif
  100. GC_INNER mark_state_t GC_mark_state = MS_NONE;
  101. GC_INNER GC_bool GC_mark_stack_too_small = FALSE;
  102. static struct hblk * scan_ptr;
  103. STATIC GC_bool GC_objects_are_marked = FALSE;
  104. /* Are there collectible marked objects in the heap? */
  105. void GC_reset_mark_statics()
  106. {
  107. GC_n_mark_procs = GC_RESERVED_MARK_PROCS;
  108. GC_n_kinds = GC_N_KINDS_INITIAL_VALUE;
  109. GC_mark_stack_size = 0;
  110. GC_mark_state = MS_NONE;
  111. GC_mark_stack_too_small = FALSE;
  112. scan_ptr = NULL;
  113. GC_objects_are_marked = FALSE;
  114. }
  115. /* Is a collection in progress? Note that this can return true in the */
  116. /* nonincremental case, if a collection has been abandoned and the */
  117. /* mark state is now MS_INVALID. */
  118. GC_INNER GC_bool GC_collection_in_progress(void)
  119. {
  120. return(GC_mark_state != MS_NONE);
  121. }
  122. /* clear all mark bits in the header */
  123. GC_INNER void GC_clear_hdr_marks(hdr *hhdr)
  124. {
  125. size_t last_bit;
  126. # ifdef AO_HAVE_load
  127. /* Atomic access is used to avoid racing with GC_realloc. */
  128. last_bit = FINAL_MARK_BIT((size_t)AO_load((volatile AO_t *)&hhdr->hb_sz));
  129. # else
  130. /* No race as GC_realloc holds the lock while updating hb_sz. */
  131. last_bit = FINAL_MARK_BIT((size_t)hhdr->hb_sz);
  132. # endif
  133. BZERO(hhdr -> hb_marks, sizeof(hhdr->hb_marks));
  134. set_mark_bit_from_hdr(hhdr, last_bit);
  135. hhdr -> hb_n_marks = 0;
  136. }
  137. /* Set all mark bits in the header. Used for uncollectible blocks. */
  138. GC_INNER void GC_set_hdr_marks(hdr *hhdr)
  139. {
  140. unsigned i;
  141. size_t sz = (size_t)hhdr->hb_sz;
  142. unsigned n_marks = (unsigned)FINAL_MARK_BIT(sz);
  143. # ifdef USE_MARK_BYTES
  144. for (i = 0; i <= n_marks; i += (unsigned)MARK_BIT_OFFSET(sz)) {
  145. hhdr -> hb_marks[i] = 1;
  146. }
  147. # else
  148. for (i = 0; i < divWORDSZ(n_marks + WORDSZ); ++i) {
  149. hhdr -> hb_marks[i] = ONES;
  150. }
  151. # endif
  152. # ifdef MARK_BIT_PER_OBJ
  153. hhdr -> hb_n_marks = n_marks;
  154. # else
  155. hhdr -> hb_n_marks = HBLK_OBJS(sz);
  156. # endif
  157. }
  158. /*
  159. * Clear all mark bits associated with block h.
  160. */
  161. static void clear_marks_for_block(struct hblk *h, word dummy GC_ATTR_UNUSED)
  162. {
  163. hdr * hhdr = HDR(h);
  164. if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) return;
  165. /* Mark bit for these is cleared only once the object is */
  166. /* explicitly deallocated. This either frees the block, or */
  167. /* the bit is cleared once the object is on the free list. */
  168. GC_clear_hdr_marks(hhdr);
  169. }
  170. /* Slow but general routines for setting/clearing/asking about mark bits */
  171. GC_API void GC_CALL GC_set_mark_bit(const void *p)
  172. {
  173. struct hblk *h = HBLKPTR(p);
  174. hdr * hhdr = HDR(h);
  175. word bit_no = MARK_BIT_NO((ptr_t)p - (ptr_t)h, hhdr -> hb_sz);
  176. if (!mark_bit_from_hdr(hhdr, bit_no)) {
  177. set_mark_bit_from_hdr(hhdr, bit_no);
  178. ++hhdr -> hb_n_marks;
  179. }
  180. }
  181. GC_API void GC_CALL GC_clear_mark_bit(const void *p)
  182. {
  183. struct hblk *h = HBLKPTR(p);
  184. hdr * hhdr = HDR(h);
  185. word bit_no = MARK_BIT_NO((ptr_t)p - (ptr_t)h, hhdr -> hb_sz);
  186. if (mark_bit_from_hdr(hhdr, bit_no)) {
  187. size_t n_marks = hhdr -> hb_n_marks;
  188. GC_ASSERT(n_marks != 0);
  189. clear_mark_bit_from_hdr(hhdr, bit_no);
  190. n_marks--;
  191. # ifdef PARALLEL_MARK
  192. if (n_marks != 0 || !GC_parallel)
  193. hhdr -> hb_n_marks = n_marks;
  194. /* Don't decrement to zero. The counts are approximate due to */
  195. /* concurrency issues, but we need to ensure that a count of */
  196. /* zero implies an empty block. */
  197. # else
  198. hhdr -> hb_n_marks = n_marks;
  199. # endif
  200. }
  201. }
  202. GC_API int GC_CALL GC_is_marked(const void *p)
  203. {
  204. struct hblk *h = HBLKPTR(p);
  205. hdr * hhdr = HDR(h);
  206. word bit_no = MARK_BIT_NO((ptr_t)p - (ptr_t)h, hhdr -> hb_sz);
  207. return (int)mark_bit_from_hdr(hhdr, bit_no); /* 0 or 1 */
  208. }
  209. /*
  210. * Clear mark bits in all allocated heap blocks. This invalidates
  211. * the marker invariant, and sets GC_mark_state to reflect this.
  212. * (This implicitly starts marking to reestablish the invariant.)
  213. */
  214. GC_INNER void GC_clear_marks(void)
  215. {
  216. GC_apply_to_all_blocks(clear_marks_for_block, (word)0);
  217. GC_objects_are_marked = FALSE;
  218. GC_mark_state = MS_INVALID;
  219. scan_ptr = 0;
  220. }
  221. /* Initiate a garbage collection. Initiates a full collection if the */
  222. /* mark state is invalid. */
  223. GC_INNER void GC_initiate_gc(void)
  224. {
  225. GC_ASSERT(I_HOLD_LOCK());
  226. # ifndef GC_DISABLE_INCREMENTAL
  227. if (GC_incremental) {
  228. # ifdef CHECKSUMS
  229. GC_read_dirty(FALSE);
  230. # else
  231. GC_read_dirty(GC_mark_state == MS_INVALID);
  232. # endif
  233. }
  234. # endif
  235. # ifdef CHECKSUMS
  236. if (GC_incremental) GC_check_dirty();
  237. # endif
  238. # if !defined(GC_DISABLE_INCREMENTAL)
  239. GC_n_rescuing_pages = 0;
  240. # endif
  241. if (GC_mark_state == MS_NONE) {
  242. GC_mark_state = MS_PUSH_RESCUERS;
  243. } else if (GC_mark_state != MS_INVALID) {
  244. ABORT("Unexpected state");
  245. } /* else this is really a full collection, and mark */
  246. /* bits are invalid. */
  247. scan_ptr = 0;
  248. }
  249. #ifdef PARALLEL_MARK
  250. STATIC void GC_do_parallel_mark(void); /* initiate parallel marking. */
  251. #endif /* PARALLEL_MARK */
  252. #ifdef GC_DISABLE_INCREMENTAL
  253. # define GC_push_next_marked_dirty(h) GC_push_next_marked(h)
  254. #else
  255. STATIC struct hblk * GC_push_next_marked_dirty(struct hblk *h);
  256. /* Invoke GC_push_marked on next dirty block above h. */
  257. /* Return a pointer just past the end of this block. */
  258. #endif /* !GC_DISABLE_INCREMENTAL */
  259. STATIC struct hblk * GC_push_next_marked(struct hblk *h);
  260. /* Ditto, but also mark from clean pages. */
  261. STATIC struct hblk * GC_push_next_marked_uncollectable(struct hblk *h);
  262. /* Ditto, but mark only from uncollectible pages. */
  263. static void alloc_mark_stack(size_t);
  264. /* Perform a small amount of marking. */
  265. /* We try to touch roughly a page of memory. */
  266. /* Return TRUE if we just finished a mark phase. */
  267. /* Cold_gc_frame is an address inside a GC frame that */
  268. /* remains valid until all marking is complete. */
  269. /* A zero value indicates that it's OK to miss some */
  270. /* register values. */
  271. /* We hold the allocation lock. In the case of */
  272. /* incremental collection, the world may not be stopped.*/
  273. #ifdef WRAP_MARK_SOME
  274. /* For win32, this is called after we establish a structured */
  275. /* exception handler, in case Windows unmaps one of our root */
  276. /* segments. See below. In either case, we acquire the */
  277. /* allocator lock long before we get here. */
  278. STATIC GC_bool GC_mark_some_inner(ptr_t cold_gc_frame)
  279. #else
  280. GC_INNER GC_bool GC_mark_some(ptr_t cold_gc_frame)
  281. #endif
  282. {
  283. switch(GC_mark_state) {
  284. case MS_NONE:
  285. break;
  286. case MS_PUSH_RESCUERS:
  287. if ((word)GC_mark_stack_top
  288. >= (word)(GC_mark_stack_limit - INITIAL_MARK_STACK_SIZE/2)) {
  289. /* Go ahead and mark, even though that might cause us to */
  290. /* see more marked dirty objects later on. Avoid this */
  291. /* in the future. */
  292. GC_mark_stack_too_small = TRUE;
  293. MARK_FROM_MARK_STACK();
  294. break;
  295. } else {
  296. scan_ptr = GC_push_next_marked_dirty(scan_ptr);
  297. if (scan_ptr == 0) {
  298. # if !defined(GC_DISABLE_INCREMENTAL)
  299. GC_COND_LOG_PRINTF("Marked from %lu dirty pages\n",
  300. (unsigned long)GC_n_rescuing_pages);
  301. # endif
  302. GC_push_roots(FALSE, cold_gc_frame);
  303. GC_objects_are_marked = TRUE;
  304. if (GC_mark_state != MS_INVALID) {
  305. GC_mark_state = MS_ROOTS_PUSHED;
  306. }
  307. }
  308. }
  309. break;
  310. case MS_PUSH_UNCOLLECTABLE:
  311. if ((word)GC_mark_stack_top
  312. >= (word)(GC_mark_stack + GC_mark_stack_size/4)) {
  313. # ifdef PARALLEL_MARK
  314. /* Avoid this, since we don't parallelize the marker */
  315. /* here. */
  316. if (GC_parallel) GC_mark_stack_too_small = TRUE;
  317. # endif
  318. MARK_FROM_MARK_STACK();
  319. break;
  320. } else {
  321. scan_ptr = GC_push_next_marked_uncollectable(scan_ptr);
  322. if (scan_ptr == 0) {
  323. GC_push_roots(TRUE, cold_gc_frame);
  324. GC_objects_are_marked = TRUE;
  325. if (GC_mark_state != MS_INVALID) {
  326. GC_mark_state = MS_ROOTS_PUSHED;
  327. }
  328. }
  329. }
  330. break;
  331. case MS_ROOTS_PUSHED:
  332. # ifdef PARALLEL_MARK
  333. /* In the incremental GC case, this currently doesn't */
  334. /* quite do the right thing, since it runs to */
  335. /* completion. On the other hand, starting a */
  336. /* parallel marker is expensive, so perhaps it is */
  337. /* the right thing? */
  338. /* Eventually, incremental marking should run */
  339. /* asynchronously in multiple threads, without grabbing */
  340. /* the allocation lock. */
  341. if (GC_parallel) {
  342. GC_do_parallel_mark();
  343. GC_ASSERT((word)GC_mark_stack_top < (word)GC_first_nonempty);
  344. GC_mark_stack_top = GC_mark_stack - 1;
  345. if (GC_mark_stack_too_small) {
  346. alloc_mark_stack(2*GC_mark_stack_size);
  347. }
  348. if (GC_mark_state == MS_ROOTS_PUSHED) {
  349. GC_mark_state = MS_NONE;
  350. return(TRUE);
  351. }
  352. break;
  353. }
  354. # endif
  355. if ((word)GC_mark_stack_top >= (word)GC_mark_stack) {
  356. MARK_FROM_MARK_STACK();
  357. break;
  358. } else {
  359. GC_mark_stack_empty_proc mark_stack_empty_proc = GC_get_mark_stack_empty();
  360. if (mark_stack_empty_proc) {
  361. GC_mark_stack_top = mark_stack_empty_proc(GC_mark_stack_top, GC_mark_stack_limit);
  362. /* if we pushed new items we need to continue processing */
  363. if ((word)GC_mark_stack_top >= (word)GC_mark_stack) {
  364. break;
  365. }
  366. }
  367. if (GC_mark_stack_too_small) {
  368. alloc_mark_stack(2 * GC_mark_stack_size);
  369. }
  370. GC_mark_state = MS_NONE;
  371. return(TRUE);
  372. break;
  373. }
  374. case MS_INVALID:
  375. case MS_PARTIALLY_INVALID:
  376. if (!GC_objects_are_marked) {
  377. GC_mark_state = MS_PUSH_UNCOLLECTABLE;
  378. break;
  379. }
  380. if ((word)GC_mark_stack_top >= (word)GC_mark_stack) {
  381. MARK_FROM_MARK_STACK();
  382. break;
  383. }
  384. if (scan_ptr == 0 && GC_mark_state == MS_INVALID) {
  385. /* About to start a heap scan for marked objects. */
  386. /* Mark stack is empty. OK to reallocate. */
  387. if (GC_mark_stack_too_small) {
  388. alloc_mark_stack(2*GC_mark_stack_size);
  389. }
  390. GC_mark_state = MS_PARTIALLY_INVALID;
  391. }
  392. scan_ptr = GC_push_next_marked(scan_ptr);
  393. if (scan_ptr == 0 && GC_mark_state == MS_PARTIALLY_INVALID) {
  394. GC_push_roots(TRUE, cold_gc_frame);
  395. GC_objects_are_marked = TRUE;
  396. if (GC_mark_state != MS_INVALID) {
  397. GC_mark_state = MS_ROOTS_PUSHED;
  398. }
  399. }
  400. break;
  401. default:
  402. ABORT("GC_mark_some: bad state");
  403. }
  404. return(FALSE);
  405. }
  406. #ifdef WRAP_MARK_SOME
  407. # if (defined(MSWIN32) || defined(MSWINCE)) && defined(__GNUC__)
  408. typedef struct {
  409. EXCEPTION_REGISTRATION ex_reg;
  410. void *alt_path;
  411. } ext_ex_regn;
  412. static EXCEPTION_DISPOSITION mark_ex_handler(
  413. struct _EXCEPTION_RECORD *ex_rec,
  414. void *est_frame,
  415. struct _CONTEXT *context,
  416. void *disp_ctxt GC_ATTR_UNUSED)
  417. {
  418. if (ex_rec->ExceptionCode == STATUS_ACCESS_VIOLATION) {
  419. ext_ex_regn *xer = (ext_ex_regn *)est_frame;
  420. /* Unwind from the inner function assuming the standard */
  421. /* function prologue. */
  422. /* Assumes code has not been compiled with */
  423. /* -fomit-frame-pointer. */
  424. context->Esp = context->Ebp;
  425. context->Ebp = *((DWORD *)context->Esp);
  426. context->Esp = context->Esp - 8;
  427. /* Resume execution at the "real" handler within the */
  428. /* wrapper function. */
  429. context->Eip = (DWORD )(xer->alt_path);
  430. return ExceptionContinueExecution;
  431. } else {
  432. return ExceptionContinueSearch;
  433. }
  434. }
  435. # endif /* __GNUC__ && MSWIN32 */
  436. GC_INNER GC_bool GC_mark_some(ptr_t cold_gc_frame)
  437. {
  438. GC_bool ret_val;
  439. # if defined(MSWIN32) || defined(MSWINCE)
  440. # ifndef __GNUC__
  441. /* Windows 98 appears to asynchronously create and remove */
  442. /* writable memory mappings, for reasons we haven't yet */
  443. /* understood. Since we look for writable regions to */
  444. /* determine the root set, we may try to mark from an */
  445. /* address range that disappeared since we started the */
  446. /* collection. Thus we have to recover from faults here. */
  447. /* This code does not appear to be necessary for Windows */
  448. /* 95/NT/2000+. Note that this code should never generate */
  449. /* an incremental GC write fault. */
  450. /* This code seems to be necessary for WinCE (at least in */
  451. /* the case we'd decide to add MEM_PRIVATE sections to */
  452. /* data roots in GC_register_dynamic_libraries()). */
  453. /* It's conceivable that this is the same issue with */
  454. /* terminating threads that we see with Linux and */
  455. /* USE_PROC_FOR_LIBRARIES. */
  456. #ifndef NO_CRT
  457. __try {
  458. #endif
  459. ret_val = GC_mark_some_inner(cold_gc_frame);
  460. #ifndef NO_CRT
  461. } __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
  462. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
  463. goto handle_ex;
  464. }
  465. #endif
  466. # if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
  467. /* With DllMain-based thread tracking, a thread may have */
  468. /* started while we were marking. This is logically equivalent */
  469. /* to the exception case; our results are invalid and we have */
  470. /* to start over. This cannot be prevented since we can't */
  471. /* block in DllMain. */
  472. if (GC_started_thread_while_stopped()) goto handle_ex;
  473. # endif
  474. rm_handler:
  475. return ret_val;
  476. # else /* __GNUC__ */
  477. /* Manually install an exception handler since GCC does */
  478. /* not yet support Structured Exception Handling (SEH) on */
  479. /* Win32. */
  480. ext_ex_regn er;
  481. # if GC_GNUC_PREREQ(4, 7) || GC_CLANG_PREREQ(3, 3)
  482. # pragma GCC diagnostic push
  483. /* Suppress "taking the address of label is non-standard" warning. */
  484. # if defined(__clang__) || GC_GNUC_PREREQ(6, 4)
  485. # pragma GCC diagnostic ignored "-Wpedantic"
  486. # else
  487. /* GCC before ~4.8 does not accept "-Wpedantic" quietly. */
  488. # pragma GCC diagnostic ignored "-pedantic"
  489. # endif
  490. er.alt_path = &&handle_ex;
  491. # pragma GCC diagnostic pop
  492. # else /* pragma diagnostic is not supported */
  493. er.alt_path = &&handle_ex;
  494. # endif
  495. er.ex_reg.handler = mark_ex_handler;
  496. __asm__ __volatile__ ("movl %%fs:0, %0" : "=r" (er.ex_reg.prev));
  497. __asm__ __volatile__ ("movl %0, %%fs:0" : : "r" (&er));
  498. ret_val = GC_mark_some_inner(cold_gc_frame);
  499. /* Prevent GCC from considering the following code unreachable */
  500. /* and thus eliminating it. */
  501. if (er.alt_path == 0)
  502. goto handle_ex;
  503. # if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
  504. if (GC_started_thread_while_stopped())
  505. goto handle_ex;
  506. # endif
  507. rm_handler:
  508. /* Uninstall the exception handler */
  509. __asm__ __volatile__ ("mov %0, %%fs:0" : : "r" (er.ex_reg.prev));
  510. return ret_val;
  511. # endif /* __GNUC__ */
  512. # else /* !MSWIN32 */
  513. /* Here we are handling the case in which /proc is used for root */
  514. /* finding, and we have threads. We may find a stack for a */
  515. /* thread that is in the process of exiting, and disappears */
  516. /* while we are marking it. This seems extremely difficult to */
  517. /* avoid otherwise. */
  518. if (GC_incremental) {
  519. WARN("Incremental GC incompatible with /proc roots\n", 0);
  520. /* I'm not sure if this could still work ... */
  521. }
  522. GC_setup_temporary_fault_handler();
  523. if(SETJMP(GC_jmp_buf) != 0) goto handle_ex;
  524. ret_val = GC_mark_some_inner(cold_gc_frame);
  525. rm_handler:
  526. GC_reset_fault_handler();
  527. return ret_val;
  528. # endif /* !MSWIN32 */
  529. handle_ex:
  530. /* Exception handler starts here for all cases. */
  531. {
  532. static word warned_gc_no;
  533. /* Warn about it at most once per collection. */
  534. if (warned_gc_no != GC_gc_no) {
  535. warned_gc_no = GC_gc_no;
  536. WARN("Caught ACCESS_VIOLATION in marker;"
  537. " memory mapping disappeared\n", 0);
  538. }
  539. }
  540. /* We have bad roots on the stack. Discard mark stack. */
  541. /* Rescan from marked objects. Redetermine roots. */
  542. # ifdef REGISTER_LIBRARIES_EARLY
  543. START_WORLD();
  544. GC_cond_register_dynamic_libraries();
  545. STOP_WORLD();
  546. # endif
  547. GC_invalidate_mark_state();
  548. scan_ptr = 0;
  549. ret_val = FALSE;
  550. goto rm_handler; /* Back to platform-specific code. */
  551. }
  552. #endif /* WRAP_MARK_SOME */
  553. GC_INNER void GC_invalidate_mark_state(void)
  554. {
  555. GC_mark_state = MS_INVALID;
  556. GC_mark_stack_top = GC_mark_stack-1;
  557. }
  558. GC_INNER mse * GC_signal_mark_stack_overflow(mse *msp)
  559. {
  560. GC_mark_state = MS_INVALID;
  561. # ifdef PARALLEL_MARK
  562. /* We are using a local_mark_stack in parallel mode, so */
  563. /* do not signal the global mark stack to be resized. */
  564. /* That will be done if required in GC_return_mark_stack. */
  565. if (!GC_parallel)
  566. GC_mark_stack_too_small = TRUE;
  567. # else
  568. GC_mark_stack_too_small = TRUE;
  569. # endif
  570. GC_COND_LOG_PRINTF("Mark stack overflow; current size = %lu entries\n",
  571. (unsigned long)GC_mark_stack_size);
  572. return(msp - GC_MARK_STACK_DISCARDS);
  573. }
  574. /*
  575. * Mark objects pointed to by the regions described by
  576. * mark stack entries between mark_stack and mark_stack_top,
  577. * inclusive. Assumes the upper limit of a mark stack entry
  578. * is never 0. A mark stack entry never has size 0.
  579. * We try to traverse on the order of a hblk of memory before we return.
  580. * Caller is responsible for calling this until the mark stack is empty.
  581. * Note that this is the most performance critical routine in the
  582. * collector. Hence it contains all sorts of ugly hacks to speed
  583. * things up. In particular, we avoid procedure calls on the common
  584. * path, we take advantage of peculiarities of the mark descriptor
  585. * encoding, we optionally maintain a cache for the block address to
  586. * header mapping, we prefetch when an object is "grayed", etc.
  587. */
  588. GC_ATTR_NO_SANITIZE_ADDR GC_ATTR_NO_SANITIZE_MEMORY GC_ATTR_NO_SANITIZE_THREAD
  589. GC_INNER mse * GC_mark_from(mse *mark_stack_top, mse *mark_stack,
  590. mse *mark_stack_limit)
  591. {
  592. signed_word credit = HBLKSIZE; /* Remaining credit for marking work */
  593. ptr_t current_p; /* Pointer to current candidate ptr. */
  594. word current; /* Candidate pointer. */
  595. ptr_t limit = 0; /* (Incl) limit of current candidate range. */
  596. word descr;
  597. ptr_t greatest_ha = (ptr_t)GC_greatest_plausible_heap_addr;
  598. ptr_t least_ha = (ptr_t)GC_least_plausible_heap_addr;
  599. DECLARE_HDR_CACHE;
  600. # define SPLIT_RANGE_WORDS 128 /* Must be power of 2. */
  601. GC_objects_are_marked = TRUE;
  602. INIT_HDR_CACHE;
  603. # ifdef OS2 /* Use untweaked version to circumvent compiler problem */
  604. while ((word)mark_stack_top >= (word)mark_stack && credit >= 0)
  605. # else
  606. while (((((word)mark_stack_top - (word)mark_stack) | (word)credit)
  607. & SIGNB) == 0)
  608. # endif
  609. {
  610. current_p = mark_stack_top -> mse_start;
  611. descr = mark_stack_top -> mse_descr.w;
  612. retry:
  613. /* current_p and descr describe the current object. */
  614. /* *mark_stack_top is vacant. */
  615. /* The following is 0 only for small objects described by a simple */
  616. /* length descriptor. For many applications this is the common */
  617. /* case, so we try to detect it quickly. */
  618. if (descr & ((~(WORDS_TO_BYTES(SPLIT_RANGE_WORDS) - 1)) | GC_DS_TAGS)) {
  619. word tag = descr & GC_DS_TAGS;
  620. GC_STATIC_ASSERT(GC_DS_TAGS == 0x3);
  621. switch(tag) {
  622. case GC_DS_LENGTH:
  623. /* Large length. */
  624. /* Process part of the range to avoid pushing too much on the */
  625. /* stack. */
  626. GC_ASSERT(descr < (word)GC_greatest_plausible_heap_addr
  627. - (word)GC_least_plausible_heap_addr
  628. || (word)(current_p + descr)
  629. <= (word)GC_least_plausible_heap_addr
  630. || (word)current_p >= (word)GC_greatest_plausible_heap_addr);
  631. # ifdef PARALLEL_MARK
  632. # define SHARE_BYTES 2048
  633. if (descr > SHARE_BYTES && GC_parallel
  634. && (word)mark_stack_top < (word)(mark_stack_limit - 1)) {
  635. word new_size = (descr/2) & ~(word)(sizeof(word)-1);
  636. mark_stack_top -> mse_start = current_p;
  637. mark_stack_top -> mse_descr.w = new_size + sizeof(word);
  638. /* makes sure we handle */
  639. /* misaligned pointers. */
  640. mark_stack_top++;
  641. # ifdef ENABLE_TRACE
  642. if ((word)GC_trace_addr >= (word)current_p
  643. && (word)GC_trace_addr < (word)(current_p + descr)) {
  644. GC_log_printf("GC #%u: large section; start %p, len %lu,"
  645. " splitting (parallel) at %p\n",
  646. (unsigned)GC_gc_no, (void *)current_p,
  647. (unsigned long)descr,
  648. (void *)(current_p + new_size));
  649. }
  650. # endif
  651. current_p += new_size;
  652. descr -= new_size;
  653. goto retry;
  654. }
  655. # endif /* PARALLEL_MARK */
  656. mark_stack_top -> mse_start =
  657. limit = current_p + WORDS_TO_BYTES(SPLIT_RANGE_WORDS-1);
  658. mark_stack_top -> mse_descr.w =
  659. descr - WORDS_TO_BYTES(SPLIT_RANGE_WORDS-1);
  660. # ifdef ENABLE_TRACE
  661. if ((word)GC_trace_addr >= (word)current_p
  662. && (word)GC_trace_addr < (word)(current_p + descr)) {
  663. GC_log_printf("GC #%u: large section; start %p, len %lu,"
  664. " splitting at %p\n",
  665. (unsigned)GC_gc_no, (void *)current_p,
  666. (unsigned long)descr, (void *)limit);
  667. }
  668. # endif
  669. /* Make sure that pointers overlapping the two ranges are */
  670. /* considered. */
  671. limit += sizeof(word) - ALIGNMENT;
  672. break;
  673. case GC_DS_BITMAP:
  674. mark_stack_top--;
  675. # ifdef ENABLE_TRACE
  676. if ((word)GC_trace_addr >= (word)current_p
  677. && (word)GC_trace_addr < (word)(current_p
  678. + WORDS_TO_BYTES(WORDSZ-2))) {
  679. GC_log_printf("GC #%u: tracing from %p bitmap descr %lu\n",
  680. (unsigned)GC_gc_no, (void *)current_p,
  681. (unsigned long)descr);
  682. }
  683. # endif /* ENABLE_TRACE */
  684. descr &= ~GC_DS_TAGS;
  685. credit -= WORDS_TO_BYTES(WORDSZ/2); /* guess */
  686. while (descr != 0) {
  687. if ((descr & SIGNB) != 0) {
  688. current = *(word *)current_p;
  689. FIXUP_POINTER(current);
  690. if (current >= (word)least_ha && current < (word)greatest_ha) {
  691. PREFETCH((ptr_t)current);
  692. # ifdef ENABLE_TRACE
  693. if (GC_trace_addr == current_p) {
  694. GC_log_printf("GC #%u: considering(3) %p -> %p\n",
  695. (unsigned)GC_gc_no, (void *)current_p,
  696. (void *)current);
  697. }
  698. # endif /* ENABLE_TRACE */
  699. PUSH_CONTENTS((ptr_t)current, mark_stack_top,
  700. mark_stack_limit, current_p);
  701. }
  702. }
  703. descr <<= 1;
  704. current_p += sizeof(word);
  705. }
  706. continue;
  707. case GC_DS_PROC:
  708. mark_stack_top--;
  709. # ifdef ENABLE_TRACE
  710. if ((word)GC_trace_addr >= (word)current_p
  711. && GC_base(current_p) != 0
  712. && GC_base(current_p) == GC_base(GC_trace_addr)) {
  713. GC_log_printf("GC #%u: tracing from %p, proc descr %lu\n",
  714. (unsigned)GC_gc_no, (void *)current_p,
  715. (unsigned long)descr);
  716. }
  717. # endif /* ENABLE_TRACE */
  718. credit -= GC_PROC_BYTES;
  719. mark_stack_top = (*PROC(descr))((word *)current_p, mark_stack_top,
  720. mark_stack_limit, ENV(descr));
  721. continue;
  722. case GC_DS_PER_OBJECT:
  723. if ((signed_word)descr >= 0) {
  724. /* Descriptor is in the object. */
  725. descr = *(word *)(current_p + descr - GC_DS_PER_OBJECT);
  726. } else {
  727. /* Descriptor is in type descriptor pointed to by first */
  728. /* word in object. */
  729. ptr_t type_descr = *(ptr_t *)current_p;
  730. /* type_descr is either a valid pointer to the descriptor */
  731. /* structure, or this object was on a free list. */
  732. /* If it was anything but the last object on the free list, */
  733. /* we will misinterpret the next object on the free list as */
  734. /* the type descriptor, and get a 0 GC descriptor, which */
  735. /* is ideal. Unfortunately, we need to check for the last */
  736. /* object case explicitly. */
  737. if (EXPECT(0 == type_descr, FALSE)) {
  738. mark_stack_top--;
  739. continue;
  740. }
  741. descr = *(word *)(type_descr
  742. - ((signed_word)descr + (GC_INDIR_PER_OBJ_BIAS
  743. - GC_DS_PER_OBJECT)));
  744. }
  745. if (0 == descr) {
  746. /* Can happen either because we generated a 0 descriptor */
  747. /* or we saw a pointer to a free object. */
  748. mark_stack_top--;
  749. continue;
  750. }
  751. goto retry;
  752. }
  753. } else /* Small object with length descriptor */ {
  754. mark_stack_top--;
  755. # ifndef SMALL_CONFIG
  756. if (descr < sizeof(word))
  757. continue;
  758. # endif
  759. # ifdef ENABLE_TRACE
  760. if ((word)GC_trace_addr >= (word)current_p
  761. && (word)GC_trace_addr < (word)(current_p + descr)) {
  762. GC_log_printf("GC #%u: small object; start %p, len %lu\n",
  763. (unsigned)GC_gc_no, (void *)current_p,
  764. (unsigned long)descr);
  765. }
  766. # endif
  767. limit = current_p + (word)descr;
  768. }
  769. /* The simple case in which we're scanning a range. */
  770. GC_ASSERT(!((word)current_p & (ALIGNMENT-1)));
  771. credit -= limit - current_p;
  772. limit -= sizeof(word);
  773. {
  774. # define PREF_DIST 4
  775. # ifndef SMALL_CONFIG
  776. word deferred;
  777. /* Try to prefetch the next pointer to be examined ASAP. */
  778. /* Empirically, this also seems to help slightly without */
  779. /* prefetches, at least on linux/X86. Presumably this loop */
  780. /* ends up with less register pressure, and gcc thus ends up */
  781. /* generating slightly better code. Overall gcc code quality */
  782. /* for this loop is still not great. */
  783. for(;;) {
  784. PREFETCH(limit - PREF_DIST*CACHE_LINE_SIZE);
  785. GC_ASSERT((word)limit >= (word)current_p);
  786. deferred = *(word *)limit;
  787. FIXUP_POINTER(deferred);
  788. limit -= ALIGNMENT;
  789. if (deferred >= (word)least_ha && deferred < (word)greatest_ha) {
  790. PREFETCH((ptr_t)deferred);
  791. break;
  792. }
  793. if ((word)current_p > (word)limit) goto next_object;
  794. /* Unroll once, so we don't do too many of the prefetches */
  795. /* based on limit. */
  796. deferred = *(word *)limit;
  797. FIXUP_POINTER(deferred);
  798. limit -= ALIGNMENT;
  799. if (deferred >= (word)least_ha && deferred < (word)greatest_ha) {
  800. PREFETCH((ptr_t)deferred);
  801. break;
  802. }
  803. if ((word)current_p > (word)limit) goto next_object;
  804. }
  805. # endif
  806. while ((word)current_p <= (word)limit) {
  807. /* Empirically, unrolling this loop doesn't help a lot. */
  808. /* Since PUSH_CONTENTS expands to a lot of code, */
  809. /* we don't. */
  810. current = *(word *)current_p;
  811. FIXUP_POINTER(current);
  812. PREFETCH(current_p + PREF_DIST*CACHE_LINE_SIZE);
  813. if (current >= (word)least_ha && current < (word)greatest_ha) {
  814. /* Prefetch the contents of the object we just pushed. It's */
  815. /* likely we will need them soon. */
  816. PREFETCH((ptr_t)current);
  817. # ifdef ENABLE_TRACE
  818. if (GC_trace_addr == current_p) {
  819. GC_log_printf("GC #%u: considering(1) %p -> %p\n",
  820. (unsigned)GC_gc_no, (void *)current_p,
  821. (void *)current);
  822. }
  823. # endif /* ENABLE_TRACE */
  824. PUSH_CONTENTS((ptr_t)current, mark_stack_top,
  825. mark_stack_limit, current_p);
  826. }
  827. current_p += ALIGNMENT;
  828. }
  829. # ifndef SMALL_CONFIG
  830. /* We still need to mark the entry we previously prefetched. */
  831. /* We already know that it passes the preliminary pointer */
  832. /* validity test. */
  833. # ifdef ENABLE_TRACE
  834. if (GC_trace_addr == current_p) {
  835. GC_log_printf("GC #%u: considering(2) %p -> %p\n",
  836. (unsigned)GC_gc_no, (void *)current_p,
  837. (void *)deferred);
  838. }
  839. # endif /* ENABLE_TRACE */
  840. PUSH_CONTENTS((ptr_t)deferred, mark_stack_top,
  841. mark_stack_limit, current_p);
  842. next_object:;
  843. # endif
  844. }
  845. }
  846. return mark_stack_top;
  847. }
  848. #ifdef PARALLEL_MARK
  849. STATIC GC_bool GC_help_wanted = FALSE; /* Protected by mark lock */
  850. STATIC unsigned GC_helper_count = 0; /* Number of running helpers. */
  851. /* Protected by mark lock */
  852. STATIC unsigned GC_active_count = 0; /* Number of active helpers. */
  853. /* Protected by mark lock */
  854. /* May increase and decrease */
  855. /* within each mark cycle. But */
  856. /* once it returns to 0, it */
  857. /* stays zero for the cycle. */
  858. GC_INNER word GC_mark_no = 0;
  859. static mse *main_local_mark_stack;
  860. #ifdef LINT2
  861. # define LOCAL_MARK_STACK_SIZE (HBLKSIZE / 8)
  862. #else
  863. # define LOCAL_MARK_STACK_SIZE HBLKSIZE
  864. /* Under normal circumstances, this is big enough to guarantee */
  865. /* we don't overflow half of it in a single call to */
  866. /* GC_mark_from. */
  867. #endif
  868. /* Wait all markers to finish initialization (i.e. store */
  869. /* marker_[b]sp, marker_mach_threads, GC_marker_Id). */
  870. GC_INNER void GC_wait_for_markers_init(void)
  871. {
  872. signed_word count;
  873. if (GC_markers_m1 == 0)
  874. return;
  875. /* Allocate the local mark stack for the thread that holds GC lock. */
  876. # ifndef CAN_HANDLE_FORK
  877. GC_ASSERT(NULL == main_local_mark_stack);
  878. # else
  879. if (NULL == main_local_mark_stack)
  880. # endif
  881. {
  882. size_t bytes_to_get =
  883. ROUNDUP_PAGESIZE_IF_MMAP(LOCAL_MARK_STACK_SIZE * sizeof(mse));
  884. main_local_mark_stack = (mse *)GET_MEM(bytes_to_get);
  885. if (NULL == main_local_mark_stack)
  886. ABORT("Insufficient memory for main local_mark_stack");
  887. GC_add_to_our_memory((ptr_t)main_local_mark_stack, bytes_to_get);
  888. }
  889. /* Reuse marker lock and builders count to synchronize */
  890. /* marker threads startup. */
  891. GC_acquire_mark_lock();
  892. GC_fl_builder_count += GC_markers_m1;
  893. count = GC_fl_builder_count;
  894. GC_release_mark_lock();
  895. if (count != 0) {
  896. GC_ASSERT(count > 0);
  897. GC_wait_for_reclaim();
  898. }
  899. }
  900. /* Steal mark stack entries starting at mse low into mark stack local */
  901. /* until we either steal mse high, or we have max entries. */
  902. /* Return a pointer to the top of the local mark stack. */
  903. /* *next is replaced by a pointer to the next unscanned mark stack */
  904. /* entry. */
  905. STATIC mse * GC_steal_mark_stack(mse * low, mse * high, mse * local,
  906. unsigned max, mse **next)
  907. {
  908. mse *p;
  909. mse *top = local - 1;
  910. unsigned i = 0;
  911. GC_ASSERT((word)high >= (word)(low - 1)
  912. && (word)(high - low + 1) <= GC_mark_stack_size);
  913. for (p = low; (word)p <= (word)high && i <= max; ++p) {
  914. word descr = (word)AO_load(&p->mse_descr.ao);
  915. if (descr != 0) {
  916. /* Must be ordered after read of descr: */
  917. AO_store_release_write(&p->mse_descr.ao, 0);
  918. /* More than one thread may get this entry, but that's only */
  919. /* a minor performance problem. */
  920. ++top;
  921. top -> mse_descr.w = descr;
  922. top -> mse_start = p -> mse_start;
  923. GC_ASSERT((descr & GC_DS_TAGS) != GC_DS_LENGTH
  924. || descr < (word)GC_greatest_plausible_heap_addr
  925. - (word)GC_least_plausible_heap_addr
  926. || (word)(p->mse_start + descr)
  927. <= (word)GC_least_plausible_heap_addr
  928. || (word)p->mse_start
  929. >= (word)GC_greatest_plausible_heap_addr);
  930. /* If this is a big object, count it as */
  931. /* size/256 + 1 objects. */
  932. ++i;
  933. if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) i += (int)(descr >> 8);
  934. }
  935. }
  936. *next = p;
  937. return top;
  938. }
  939. /* Copy back a local mark stack. */
  940. /* low and high are inclusive bounds. */
  941. STATIC void GC_return_mark_stack(mse * low, mse * high)
  942. {
  943. mse * my_top;
  944. mse * my_start;
  945. size_t stack_size;
  946. if ((word)high < (word)low) return;
  947. stack_size = high - low + 1;
  948. GC_acquire_mark_lock();
  949. my_top = GC_mark_stack_top; /* Concurrent modification impossible. */
  950. my_start = my_top + 1;
  951. if ((word)(my_start - GC_mark_stack + stack_size)
  952. > (word)GC_mark_stack_size) {
  953. GC_COND_LOG_PRINTF("No room to copy back mark stack\n");
  954. GC_mark_state = MS_INVALID;
  955. GC_mark_stack_too_small = TRUE;
  956. /* We drop the local mark stack. We'll fix things later. */
  957. } else {
  958. BCOPY(low, my_start, stack_size * sizeof(mse));
  959. GC_ASSERT((mse *)AO_load((volatile AO_t *)(&GC_mark_stack_top))
  960. == my_top);
  961. AO_store_release_write((volatile AO_t *)(&GC_mark_stack_top),
  962. (AO_t)(my_top + stack_size));
  963. /* Ensures visibility of previously written stack contents. */
  964. }
  965. GC_release_mark_lock();
  966. GC_notify_all_marker();
  967. }
  968. #ifndef N_LOCAL_ITERS
  969. # define N_LOCAL_ITERS 1
  970. #endif
  971. /* This function is only called when the local */
  972. /* and the main mark stacks are both empty. */
  973. static GC_bool has_inactive_helpers(void)
  974. {
  975. GC_bool res;
  976. GC_acquire_mark_lock();
  977. res = GC_active_count < GC_helper_count;
  978. GC_release_mark_lock();
  979. return res;
  980. }
  981. /* Mark from the local mark stack. */
  982. /* On return, the local mark stack is empty. */
  983. /* But this may be achieved by copying the */
  984. /* local mark stack back into the global one. */
  985. /* We do not hold the mark lock. */
  986. STATIC void GC_do_local_mark(mse *local_mark_stack, mse *local_top)
  987. {
  988. unsigned n;
  989. for (;;) {
  990. for (n = 0; n < N_LOCAL_ITERS; ++n) {
  991. local_top = GC_mark_from(local_top, local_mark_stack,
  992. local_mark_stack + LOCAL_MARK_STACK_SIZE);
  993. if ((word)local_top < (word)local_mark_stack) return;
  994. if ((word)(local_top - local_mark_stack)
  995. >= LOCAL_MARK_STACK_SIZE / 2) {
  996. GC_return_mark_stack(local_mark_stack, local_top);
  997. return;
  998. }
  999. }
  1000. if ((word)AO_load((volatile AO_t *)&GC_mark_stack_top)
  1001. < (word)AO_load(&GC_first_nonempty)
  1002. && (word)local_top > (word)(local_mark_stack + 1)
  1003. && has_inactive_helpers()) {
  1004. /* Try to share the load, since the main stack is empty, */
  1005. /* and helper threads are waiting for a refill. */
  1006. /* The entries near the bottom of the stack are likely */
  1007. /* to require more work. Thus we return those, even though */
  1008. /* it's harder. */
  1009. mse * new_bottom = local_mark_stack
  1010. + (local_top - local_mark_stack)/2;
  1011. GC_ASSERT((word)new_bottom > (word)local_mark_stack
  1012. && (word)new_bottom < (word)local_top);
  1013. GC_return_mark_stack(local_mark_stack, new_bottom - 1);
  1014. memmove(local_mark_stack, new_bottom,
  1015. (local_top - new_bottom + 1) * sizeof(mse));
  1016. local_top -= (new_bottom - local_mark_stack);
  1017. }
  1018. }
  1019. }
  1020. #ifndef ENTRIES_TO_GET
  1021. # define ENTRIES_TO_GET 5
  1022. #endif
  1023. /* Mark using the local mark stack until the global mark stack is empty */
  1024. /* and there are no active workers. Update GC_first_nonempty to reflect */
  1025. /* progress. Caller holds the mark lock. */
  1026. /* Caller has already incremented GC_helper_count. We decrement it, */
  1027. /* and maintain GC_active_count. */
  1028. STATIC void GC_mark_local(mse *local_mark_stack, int id)
  1029. {
  1030. mse * my_first_nonempty;
  1031. GC_active_count++;
  1032. my_first_nonempty = (mse *)AO_load(&GC_first_nonempty);
  1033. GC_ASSERT((word)GC_mark_stack <= (word)my_first_nonempty);
  1034. GC_ASSERT((word)my_first_nonempty
  1035. <= (word)AO_load((volatile AO_t *)&GC_mark_stack_top) + sizeof(mse));
  1036. GC_VERBOSE_LOG_PRINTF("Starting mark helper %d\n", id);
  1037. GC_release_mark_lock();
  1038. for (;;) {
  1039. size_t n_on_stack;
  1040. unsigned n_to_get;
  1041. mse * my_top;
  1042. mse * local_top;
  1043. mse * global_first_nonempty = (mse *)AO_load(&GC_first_nonempty);
  1044. GC_ASSERT((word)my_first_nonempty >= (word)GC_mark_stack &&
  1045. (word)my_first_nonempty <=
  1046. (word)AO_load((volatile AO_t *)&GC_mark_stack_top)
  1047. + sizeof(mse));
  1048. GC_ASSERT((word)global_first_nonempty >= (word)GC_mark_stack);
  1049. if ((word)my_first_nonempty < (word)global_first_nonempty) {
  1050. my_first_nonempty = global_first_nonempty;
  1051. } else if ((word)global_first_nonempty < (word)my_first_nonempty) {
  1052. (void)AO_compare_and_swap(&GC_first_nonempty,
  1053. (AO_t)global_first_nonempty,
  1054. (AO_t)my_first_nonempty);
  1055. /* If this fails, we just go ahead, without updating */
  1056. /* GC_first_nonempty. */
  1057. }
  1058. /* Perhaps we should also update GC_first_nonempty, if it */
  1059. /* is less. But that would require using atomic updates. */
  1060. my_top = (mse *)AO_load_acquire((volatile AO_t *)(&GC_mark_stack_top));
  1061. if ((word)my_top < (word)my_first_nonempty) {
  1062. GC_acquire_mark_lock();
  1063. my_top = GC_mark_stack_top;
  1064. /* Asynchronous modification impossible here, */
  1065. /* since we hold mark lock. */
  1066. n_on_stack = my_top - my_first_nonempty + 1;
  1067. if (0 == n_on_stack) {
  1068. GC_active_count--;
  1069. GC_ASSERT(GC_active_count <= GC_helper_count);
  1070. /* Other markers may redeposit objects */
  1071. /* on the stack. */
  1072. if (0 == GC_active_count) GC_notify_all_marker();
  1073. while (GC_active_count > 0
  1074. && (word)AO_load(&GC_first_nonempty)
  1075. > (word)GC_mark_stack_top) {
  1076. /* We will be notified if either GC_active_count */
  1077. /* reaches zero, or if more objects are pushed on */
  1078. /* the global mark stack. */
  1079. GC_wait_marker();
  1080. }
  1081. if (GC_active_count == 0
  1082. && (word)AO_load(&GC_first_nonempty)
  1083. > (word)GC_mark_stack_top) {
  1084. GC_bool need_to_notify = FALSE;
  1085. /* The above conditions can't be falsified while we */
  1086. /* hold the mark lock, since neither */
  1087. /* GC_active_count nor GC_mark_stack_top can */
  1088. /* change. GC_first_nonempty can only be */
  1089. /* incremented asynchronously. Thus we know that */
  1090. /* both conditions actually held simultaneously. */
  1091. GC_helper_count--;
  1092. if (0 == GC_helper_count) need_to_notify = TRUE;
  1093. GC_VERBOSE_LOG_PRINTF("Finished mark helper %d\n", id);
  1094. if (need_to_notify) GC_notify_all_marker();
  1095. return;
  1096. }
  1097. /* else there's something on the stack again, or */
  1098. /* another helper may push something. */
  1099. GC_active_count++;
  1100. GC_ASSERT(GC_active_count > 0);
  1101. GC_release_mark_lock();
  1102. continue;
  1103. } else {
  1104. GC_release_mark_lock();
  1105. }
  1106. } else {
  1107. n_on_stack = my_top - my_first_nonempty + 1;
  1108. }
  1109. n_to_get = ENTRIES_TO_GET;
  1110. if (n_on_stack < 2 * ENTRIES_TO_GET) n_to_get = 1;
  1111. local_top = GC_steal_mark_stack(my_first_nonempty, my_top,
  1112. local_mark_stack, n_to_get,
  1113. &my_first_nonempty);
  1114. GC_ASSERT((word)my_first_nonempty >= (word)GC_mark_stack &&
  1115. (word)my_first_nonempty <=
  1116. (word)AO_load((volatile AO_t *)&GC_mark_stack_top)
  1117. + sizeof(mse));
  1118. GC_do_local_mark(local_mark_stack, local_top);
  1119. }
  1120. }
  1121. /* Perform Parallel mark. */
  1122. /* We hold the GC lock, not the mark lock. */
  1123. /* Currently runs until the mark stack is */
  1124. /* empty. */
  1125. STATIC void GC_do_parallel_mark(void)
  1126. {
  1127. GC_acquire_mark_lock();
  1128. GC_ASSERT(I_HOLD_LOCK());
  1129. /* This could be a GC_ASSERT, but it seems safer to keep it on */
  1130. /* all the time, especially since it's cheap. */
  1131. if (GC_help_wanted || GC_active_count != 0 || GC_helper_count != 0)
  1132. ABORT("Tried to start parallel mark in bad state");
  1133. GC_VERBOSE_LOG_PRINTF("Starting marking for mark phase number %lu\n",
  1134. (unsigned long)GC_mark_no);
  1135. GC_first_nonempty = (AO_t)GC_mark_stack;
  1136. GC_active_count = 0;
  1137. GC_helper_count = 1;
  1138. GC_help_wanted = TRUE;
  1139. GC_notify_all_marker();
  1140. /* Wake up potential helpers. */
  1141. GC_mark_local(main_local_mark_stack, 0);
  1142. GC_help_wanted = FALSE;
  1143. /* Done; clean up. */
  1144. while (GC_helper_count > 0) {
  1145. GC_wait_marker();
  1146. }
  1147. /* GC_helper_count cannot be incremented while GC_help_wanted == FALSE */
  1148. GC_VERBOSE_LOG_PRINTF("Finished marking for mark phase number %lu\n",
  1149. (unsigned long)GC_mark_no);
  1150. GC_mark_no++;
  1151. GC_release_mark_lock();
  1152. GC_notify_all_marker();
  1153. }
  1154. /* Try to help out the marker, if it's running. */
  1155. /* We do not hold the GC lock, but the requestor does. */
  1156. /* And we hold the mark lock. */
  1157. GC_INNER void GC_help_marker(word my_mark_no)
  1158. {
  1159. # define my_id my_id_mse.mse_descr.w
  1160. mse my_id_mse; /* align local_mark_stack explicitly */
  1161. mse local_mark_stack[LOCAL_MARK_STACK_SIZE];
  1162. /* Note: local_mark_stack is quite big (up to 128 KiB). */
  1163. GC_ASSERT(GC_parallel);
  1164. while (GC_mark_no < my_mark_no
  1165. || (!GC_help_wanted && GC_mark_no == my_mark_no)) {
  1166. GC_wait_marker();
  1167. }
  1168. my_id = GC_helper_count;
  1169. if (GC_mark_no != my_mark_no || my_id > (unsigned)GC_markers_m1) {
  1170. /* Second test is useful only if original threads can also */
  1171. /* act as helpers. Under Linux they can't. */
  1172. return;
  1173. }
  1174. GC_helper_count = (unsigned)my_id + 1;
  1175. GC_mark_local(local_mark_stack, (int)my_id);
  1176. /* GC_mark_local decrements GC_helper_count. */
  1177. # undef my_id
  1178. }
  1179. #endif /* PARALLEL_MARK */
  1180. GC_INNER void GC_scratch_recycle_inner(void *ptr, size_t bytes)
  1181. {
  1182. if (ptr != NULL) {
  1183. size_t page_offset = (word)ptr & (GC_page_size - 1);
  1184. size_t displ = 0;
  1185. size_t recycled_bytes;
  1186. GC_ASSERT(bytes != 0);
  1187. GC_ASSERT(GC_page_size != 0);
  1188. /* TODO: Assert correct memory flags if GWW_VDB */
  1189. if (page_offset != 0)
  1190. displ = GC_page_size - page_offset;
  1191. recycled_bytes = (bytes - displ) & ~(GC_page_size - 1);
  1192. GC_COND_LOG_PRINTF("Recycle %lu/%lu scratch-allocated bytes at %p\n",
  1193. (unsigned long)recycled_bytes, (unsigned long)bytes,
  1194. ptr);
  1195. if (recycled_bytes > 0)
  1196. GC_add_to_heap((struct hblk *)((word)ptr + displ), recycled_bytes);
  1197. }
  1198. }
  1199. /* Allocate or reallocate space for mark stack of size n entries. */
  1200. /* May silently fail. */
  1201. static void alloc_mark_stack(size_t n)
  1202. {
  1203. mse * new_stack = (mse *)GC_scratch_alloc(n * sizeof(struct GC_ms_entry));
  1204. # ifdef GWW_VDB
  1205. /* Don't recycle a stack segment obtained with the wrong flags. */
  1206. /* Win32 GetWriteWatch requires the right kind of memory. */
  1207. static GC_bool GC_incremental_at_stack_alloc = FALSE;
  1208. GC_bool recycle_old = (!GC_incremental || GC_incremental_at_stack_alloc);
  1209. GC_incremental_at_stack_alloc = GC_incremental;
  1210. # else
  1211. # define recycle_old TRUE
  1212. # endif
  1213. GC_mark_stack_too_small = FALSE;
  1214. if (GC_mark_stack != NULL) {
  1215. if (new_stack != 0) {
  1216. if (recycle_old) {
  1217. /* Recycle old space */
  1218. GC_scratch_recycle_inner(GC_mark_stack,
  1219. GC_mark_stack_size * sizeof(struct GC_ms_entry));
  1220. }
  1221. GC_mark_stack = new_stack;
  1222. GC_mark_stack_size = n;
  1223. /* FIXME: Do we need some way to reset GC_mark_stack_size? */
  1224. GC_mark_stack_limit = new_stack + n;
  1225. GC_COND_LOG_PRINTF("Grew mark stack to %lu frames\n",
  1226. (unsigned long)GC_mark_stack_size);
  1227. } else {
  1228. WARN("Failed to grow mark stack to %" WARN_PRIdPTR " frames\n", n);
  1229. }
  1230. } else {
  1231. if (new_stack == 0) {
  1232. GC_err_printf("No space for mark stack\n");
  1233. EXIT();
  1234. }
  1235. GC_mark_stack = new_stack;
  1236. GC_mark_stack_size = n;
  1237. GC_mark_stack_limit = new_stack + n;
  1238. }
  1239. GC_mark_stack_top = GC_mark_stack-1;
  1240. }
  1241. GC_INNER void GC_mark_init(void)
  1242. {
  1243. alloc_mark_stack(INITIAL_MARK_STACK_SIZE);
  1244. }
  1245. /*
  1246. * Push all locations between b and t onto the mark stack.
  1247. * b is the first location to be checked. t is one past the last
  1248. * location to be checked.
  1249. * Should only be used if there is no possibility of mark stack
  1250. * overflow.
  1251. */
  1252. GC_API void GC_CALL GC_push_all(void *bottom, void *top)
  1253. {
  1254. word length;
  1255. bottom = (void *)(((word)bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
  1256. top = (void *)((word)top & ~(ALIGNMENT-1));
  1257. if ((word)bottom >= (word)top) return;
  1258. GC_mark_stack_top++;
  1259. if ((word)GC_mark_stack_top >= (word)GC_mark_stack_limit) {
  1260. ABORT("Unexpected mark stack overflow");
  1261. }
  1262. length = (word)top - (word)bottom;
  1263. # if GC_DS_TAGS > ALIGNMENT - 1
  1264. length += GC_DS_TAGS;
  1265. length &= ~GC_DS_TAGS;
  1266. # endif
  1267. GC_mark_stack_top -> mse_start = (ptr_t)bottom;
  1268. GC_mark_stack_top -> mse_descr.w = length;
  1269. }
  1270. GC_API void GC_CALL GC_push_proc(GC_word proc , void * start)
  1271. {
  1272. GC_mark_stack_top++;
  1273. if ((word)GC_mark_stack_top >= (word)GC_mark_stack_limit) {
  1274. GC_mark_stack_top = GC_signal_mark_stack_overflow (GC_mark_stack_top);
  1275. }
  1276. GC_mark_stack_top -> mse_descr.w = proc;
  1277. GC_mark_stack_top -> mse_start = (ptr_t)start;
  1278. }
  1279. GC_API struct GC_ms_entry * GC_CALL GC_custom_push_range(void * bottom , void * top,
  1280. struct GC_ms_entry * mark_stack_ptr,
  1281. struct GC_ms_entry * mark_stack_limit)
  1282. {
  1283. word length;
  1284. bottom = (void *)(((word)bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
  1285. top = (void *)((word)top & ~(ALIGNMENT-1));
  1286. if ((word)bottom >= (word)top) return mark_stack_ptr;
  1287. mark_stack_ptr++;
  1288. if ((word)mark_stack_ptr >= (word)mark_stack_limit) {
  1289. mark_stack_ptr = GC_signal_mark_stack_overflow (mark_stack_ptr);
  1290. }
  1291. length = (word)top - (word)bottom;
  1292. # if GC_DS_TAGS > ALIGNMENT - 1
  1293. length += GC_DS_TAGS;
  1294. length &= ~GC_DS_TAGS;
  1295. # endif
  1296. mark_stack_ptr -> mse_start = (ptr_t)bottom;
  1297. mark_stack_ptr -> mse_descr.w = length;
  1298. return mark_stack_ptr;
  1299. }
  1300. GC_API struct GC_ms_entry * GC_CALL GC_custom_push_proc(GC_word proc , void * start,
  1301. struct GC_ms_entry * mark_stack_ptr,
  1302. struct GC_ms_entry * mark_stack_limit)
  1303. {
  1304. mark_stack_ptr++;
  1305. if ((word)mark_stack_ptr >= (word)mark_stack_limit) {
  1306. mark_stack_ptr = GC_signal_mark_stack_overflow (mark_stack_ptr);
  1307. }
  1308. mark_stack_ptr -> mse_descr.w = proc;
  1309. mark_stack_ptr -> mse_start = (ptr_t)start;
  1310. return mark_stack_ptr;
  1311. }
  1312. #ifndef GC_DISABLE_INCREMENTAL
  1313. /* Analogous to the above, but push only those pages h with */
  1314. /* dirty_fn(h) != 0. We use GC_push_all to actually push the block. */
  1315. /* Used both to selectively push dirty pages, or to push a block in */
  1316. /* piecemeal fashion, to allow for more marking concurrency. */
  1317. /* Will not overflow mark stack if GC_push_all pushes a small fixed */
  1318. /* number of entries. (This is invoked only if GC_push_all pushes */
  1319. /* a single entry, or if it marks each object before pushing it, thus */
  1320. /* ensuring progress in the event of a stack overflow.) */
  1321. STATIC void GC_push_selected(ptr_t bottom, ptr_t top,
  1322. GC_bool (*dirty_fn)(struct hblk *))
  1323. {
  1324. struct hblk * h;
  1325. bottom = (ptr_t)(((word) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
  1326. top = (ptr_t)(((word) top) & ~(ALIGNMENT-1));
  1327. if ((word)bottom >= (word)top) return;
  1328. h = HBLKPTR(bottom + HBLKSIZE);
  1329. if ((word)top <= (word)h) {
  1330. if ((*dirty_fn)(h-1)) {
  1331. GC_push_all(bottom, top);
  1332. }
  1333. return;
  1334. }
  1335. if ((*dirty_fn)(h-1)) {
  1336. GC_push_all(bottom, h);
  1337. }
  1338. while ((word)(h+1) <= (word)top) {
  1339. if ((*dirty_fn)(h)) {
  1340. if ((word)(GC_mark_stack_top - GC_mark_stack)
  1341. > 3 * GC_mark_stack_size / 4) {
  1342. /* Danger of mark stack overflow */
  1343. GC_push_all(h, top);
  1344. return;
  1345. } else {
  1346. GC_push_all(h, h + 1);
  1347. }
  1348. }
  1349. h++;
  1350. }
  1351. if ((ptr_t)h != top && (*dirty_fn)(h)) {
  1352. GC_push_all(h, top);
  1353. }
  1354. if ((word)GC_mark_stack_top >= (word)GC_mark_stack_limit) {
  1355. ABORT("Unexpected mark stack overflow");
  1356. }
  1357. }
  1358. GC_API void GC_CALL GC_push_conditional(void *bottom, void *top, int all)
  1359. {
  1360. if (!all) {
  1361. GC_push_selected((ptr_t)bottom, (ptr_t)top, GC_page_was_dirty);
  1362. } else {
  1363. # ifdef PROC_VDB
  1364. if (GC_incremental) {
  1365. /* Pages that were never dirtied cannot contain pointers. */
  1366. GC_push_selected((ptr_t)bottom, (ptr_t)top, GC_page_was_ever_dirty);
  1367. } else
  1368. # endif
  1369. /* else */ {
  1370. GC_push_all(bottom, top);
  1371. }
  1372. }
  1373. }
  1374. #else
  1375. GC_API void GC_CALL GC_push_conditional(void *bottom, void *top,
  1376. int all GC_ATTR_UNUSED)
  1377. {
  1378. GC_push_all(bottom, top);
  1379. }
  1380. #endif /* GC_DISABLE_INCREMENTAL */
  1381. #if defined(MSWIN32) || defined(MSWINCE)
  1382. void __cdecl GC_push_one(word p)
  1383. #else
  1384. void GC_push_one(word p)
  1385. #endif
  1386. {
  1387. GC_PUSH_ONE_STACK(p, MARKED_FROM_REGISTER);
  1388. }
  1389. GC_API struct GC_ms_entry * GC_CALL GC_mark_and_push(void *obj,
  1390. mse *mark_stack_ptr,
  1391. mse *mark_stack_limit,
  1392. void ** src GC_ATTR_UNUSED)
  1393. {
  1394. hdr * hhdr;
  1395. PREFETCH(obj);
  1396. GET_HDR(obj, hhdr);
  1397. if ((EXPECT(IS_FORWARDING_ADDR_OR_NIL(hhdr), FALSE)
  1398. && (!GC_all_interior_pointers
  1399. || NULL == (hhdr = GC_find_header((ptr_t)GC_base(obj)))))
  1400. || EXPECT(HBLK_IS_FREE(hhdr), FALSE)) {
  1401. GC_ADD_TO_BLACK_LIST_NORMAL(obj, (ptr_t)src);
  1402. return mark_stack_ptr;
  1403. }
  1404. PUSH_CONTENTS_HDR(obj, mark_stack_ptr /* modified */, mark_stack_limit,
  1405. (ptr_t)src, hhdr, TRUE);
  1406. return mark_stack_ptr;
  1407. }
  1408. /* Mark and push (i.e. gray) a single object p onto the main */
  1409. /* mark stack. Consider p to be valid if it is an interior */
  1410. /* pointer. */
  1411. /* The object p has passed a preliminary pointer validity */
  1412. /* test, but we do not definitely know whether it is valid. */
  1413. /* Mark bits are NOT atomically updated. Thus this must be the */
  1414. /* only thread setting them. */
  1415. # if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
  1416. GC_INNER void GC_mark_and_push_stack(ptr_t p, ptr_t source)
  1417. # else
  1418. GC_INNER void GC_mark_and_push_stack(ptr_t p)
  1419. # define source ((ptr_t)0)
  1420. # endif
  1421. {
  1422. hdr * hhdr;
  1423. ptr_t r = p;
  1424. PREFETCH(p);
  1425. GET_HDR(p, hhdr);
  1426. if (EXPECT(IS_FORWARDING_ADDR_OR_NIL(hhdr), FALSE)
  1427. && (NULL == hhdr
  1428. || (r = (ptr_t)GC_base(p)) == NULL
  1429. || (hhdr = HDR(r)) == NULL)) {
  1430. GC_ADD_TO_BLACK_LIST_STACK(p, source);
  1431. return;
  1432. }
  1433. if (EXPECT(HBLK_IS_FREE(hhdr), FALSE)) {
  1434. GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
  1435. return;
  1436. }
  1437. # ifdef THREADS
  1438. /* Pointer is on the stack. We may have dirtied the object */
  1439. /* it points to, but have not called GC_dirty yet. */
  1440. GC_dirty(p); /* entire object */
  1441. # endif
  1442. PUSH_CONTENTS_HDR(r, GC_mark_stack_top, GC_mark_stack_limit,
  1443. source, hhdr, FALSE);
  1444. /* We silently ignore pointers to near the end of a block, */
  1445. /* which is very mildly suboptimal. */
  1446. /* FIXME: We should probably add a header word to address */
  1447. /* this. */
  1448. }
  1449. # undef source
  1450. #ifdef TRACE_BUF
  1451. # ifndef TRACE_ENTRIES
  1452. # define TRACE_ENTRIES 1000
  1453. # endif
  1454. struct trace_entry {
  1455. char * kind;
  1456. word gc_no;
  1457. word bytes_allocd;
  1458. word arg1;
  1459. word arg2;
  1460. } GC_trace_buf[TRACE_ENTRIES];
  1461. int GC_trace_buf_ptr = 0;
  1462. void GC_add_trace_entry(char *kind, word arg1, word arg2)
  1463. {
  1464. GC_trace_buf[GC_trace_buf_ptr].kind = kind;
  1465. GC_trace_buf[GC_trace_buf_ptr].gc_no = GC_gc_no;
  1466. GC_trace_buf[GC_trace_buf_ptr].bytes_allocd = GC_bytes_allocd;
  1467. GC_trace_buf[GC_trace_buf_ptr].arg1 = arg1 ^ 0x80000000;
  1468. GC_trace_buf[GC_trace_buf_ptr].arg2 = arg2 ^ 0x80000000;
  1469. GC_trace_buf_ptr++;
  1470. if (GC_trace_buf_ptr >= TRACE_ENTRIES) GC_trace_buf_ptr = 0;
  1471. }
  1472. GC_API void GC_CALL GC_print_trace_inner(word gc_no)
  1473. {
  1474. int i;
  1475. for (i = GC_trace_buf_ptr-1; i != GC_trace_buf_ptr; i--) {
  1476. struct trace_entry *p;
  1477. if (i < 0) i = TRACE_ENTRIES-1;
  1478. p = GC_trace_buf + i;
  1479. if (p -> gc_no < gc_no || p -> kind == 0) {
  1480. return;
  1481. }
  1482. GC_printf("Trace:%s (gc:%u, bytes:%lu) 0x%lX, 0x%lX\n",
  1483. p -> kind, (unsigned)p -> gc_no,
  1484. (unsigned long)p -> bytes_allocd,
  1485. (long)p->arg1 ^ 0x80000000L, (long)p->arg2 ^ 0x80000000L);
  1486. }
  1487. GC_printf("Trace incomplete\n");
  1488. }
  1489. GC_API void GC_CALL GC_print_trace(word gc_no)
  1490. {
  1491. DCL_LOCK_STATE;
  1492. LOCK();
  1493. GC_print_trace_inner(gc_no);
  1494. UNLOCK();
  1495. }
  1496. #endif /* TRACE_BUF */
  1497. /*
  1498. * A version of GC_push_all that treats all interior pointers as valid
  1499. * and scans the entire region immediately, in case the contents
  1500. * change.
  1501. */
  1502. GC_ATTR_NO_SANITIZE_ADDR GC_ATTR_NO_SANITIZE_MEMORY GC_ATTR_NO_SANITIZE_THREAD
  1503. GC_API void GC_CALL GC_push_all_eager(void *bottom, void *top)
  1504. {
  1505. word * b = (word *)(((word) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
  1506. word * t = (word *)(((word) top) & ~(ALIGNMENT-1));
  1507. REGISTER word *p;
  1508. REGISTER word *lim;
  1509. REGISTER ptr_t greatest_ha = (ptr_t)GC_greatest_plausible_heap_addr;
  1510. REGISTER ptr_t least_ha = (ptr_t)GC_least_plausible_heap_addr;
  1511. # define GC_greatest_plausible_heap_addr greatest_ha
  1512. # define GC_least_plausible_heap_addr least_ha
  1513. if (top == 0) return;
  1514. /* check all pointers in range and push if they appear */
  1515. /* to be valid. */
  1516. lim = t - 1 /* longword */;
  1517. for (p = b; (word)p <= (word)lim;
  1518. p = (word *)(((ptr_t)p) + ALIGNMENT)) {
  1519. REGISTER word q = *p;
  1520. GC_PUSH_ONE_STACK(q, p);
  1521. }
  1522. # undef GC_greatest_plausible_heap_addr
  1523. # undef GC_least_plausible_heap_addr
  1524. }
  1525. GC_INNER void GC_push_all_stack(ptr_t bottom, ptr_t top)
  1526. {
  1527. # if defined(THREADS) && defined(MPROTECT_VDB)
  1528. GC_push_all_eager(bottom, top);
  1529. # else
  1530. # ifndef NEED_FIXUP_POINTER
  1531. if (GC_all_interior_pointers) {
  1532. GC_push_all(bottom, top);
  1533. } else
  1534. # endif
  1535. /* else */ {
  1536. GC_push_all_eager(bottom, top);
  1537. }
  1538. # endif
  1539. }
  1540. #if defined(WRAP_MARK_SOME) && defined(PARALLEL_MARK)
  1541. /* Similar to GC_push_conditional but scans the whole region immediately. */
  1542. GC_ATTR_NO_SANITIZE_ADDR GC_ATTR_NO_SANITIZE_MEMORY
  1543. GC_ATTR_NO_SANITIZE_THREAD
  1544. GC_INNER void GC_push_conditional_eager(void *bottom, void *top,
  1545. GC_bool all)
  1546. {
  1547. word * b = (word *)(((word) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
  1548. word * t = (word *)(((word) top) & ~(ALIGNMENT-1));
  1549. REGISTER word *p;
  1550. REGISTER word *lim;
  1551. REGISTER ptr_t greatest_ha = (ptr_t)GC_greatest_plausible_heap_addr;
  1552. REGISTER ptr_t least_ha = (ptr_t)GC_least_plausible_heap_addr;
  1553. # define GC_greatest_plausible_heap_addr greatest_ha
  1554. # define GC_least_plausible_heap_addr least_ha
  1555. if (top == NULL)
  1556. return;
  1557. (void)all; /* TODO: If !all then scan only dirty pages. */
  1558. lim = t - 1;
  1559. for (p = b; (word)p <= (word)lim; p = (word *)((ptr_t)p + ALIGNMENT)) {
  1560. REGISTER word q = *p;
  1561. GC_PUSH_ONE_HEAP(q, p, GC_mark_stack_top);
  1562. }
  1563. # undef GC_greatest_plausible_heap_addr
  1564. # undef GC_least_plausible_heap_addr
  1565. }
  1566. #endif /* WRAP_MARK_SOME && PARALLEL_MARK */
  1567. #if !defined(SMALL_CONFIG) && !defined(USE_MARK_BYTES) && \
  1568. defined(MARK_BIT_PER_GRANULE)
  1569. # if GC_GRANULE_WORDS == 1
  1570. # define USE_PUSH_MARKED_ACCELERATORS
  1571. # define PUSH_GRANULE(q) \
  1572. do { \
  1573. word qcontents = (q)[0]; \
  1574. GC_PUSH_ONE_HEAP(qcontents, q, GC_mark_stack_top); \
  1575. } while (0)
  1576. # elif GC_GRANULE_WORDS == 2
  1577. # define USE_PUSH_MARKED_ACCELERATORS
  1578. # define PUSH_GRANULE(q) \
  1579. do { \
  1580. word qcontents = (q)[0]; \
  1581. GC_PUSH_ONE_HEAP(qcontents, q, GC_mark_stack_top); \
  1582. qcontents = (q)[1]; \
  1583. GC_PUSH_ONE_HEAP(qcontents, (q)+1, GC_mark_stack_top); \
  1584. } while (0)
  1585. # elif GC_GRANULE_WORDS == 4
  1586. # define USE_PUSH_MARKED_ACCELERATORS
  1587. # define PUSH_GRANULE(q) \
  1588. do { \
  1589. word qcontents = (q)[0]; \
  1590. GC_PUSH_ONE_HEAP(qcontents, q, GC_mark_stack_top); \
  1591. qcontents = (q)[1]; \
  1592. GC_PUSH_ONE_HEAP(qcontents, (q)+1, GC_mark_stack_top); \
  1593. qcontents = (q)[2]; \
  1594. GC_PUSH_ONE_HEAP(qcontents, (q)+2, GC_mark_stack_top); \
  1595. qcontents = (q)[3]; \
  1596. GC_PUSH_ONE_HEAP(qcontents, (q)+3, GC_mark_stack_top); \
  1597. } while (0)
  1598. # endif
  1599. #endif /* !USE_MARK_BYTES && MARK_BIT_PER_GRANULE */
  1600. #ifdef USE_PUSH_MARKED_ACCELERATORS
  1601. /* Push all objects reachable from marked objects in the given block */
  1602. /* containing objects of size 1 granule. */
  1603. STATIC void GC_push_marked1(struct hblk *h, hdr *hhdr)
  1604. {
  1605. word * mark_word_addr = &(hhdr->hb_marks[0]);
  1606. word *p;
  1607. word *plim;
  1608. /* Allow registers to be used for some frequently accessed */
  1609. /* global variables. Otherwise aliasing issues are likely */
  1610. /* to prevent that. */
  1611. ptr_t greatest_ha = (ptr_t)GC_greatest_plausible_heap_addr;
  1612. ptr_t least_ha = (ptr_t)GC_least_plausible_heap_addr;
  1613. mse * mark_stack_top = GC_mark_stack_top;
  1614. mse * mark_stack_limit = GC_mark_stack_limit;
  1615. # undef GC_mark_stack_top
  1616. # undef GC_mark_stack_limit
  1617. # define GC_mark_stack_top mark_stack_top
  1618. # define GC_mark_stack_limit mark_stack_limit
  1619. # define GC_greatest_plausible_heap_addr greatest_ha
  1620. # define GC_least_plausible_heap_addr least_ha
  1621. p = (word *)(h->hb_body);
  1622. plim = (word *)(((word)h) + HBLKSIZE);
  1623. /* go through all words in block */
  1624. while ((word)p < (word)plim) {
  1625. word mark_word = *mark_word_addr++;
  1626. word *q = p;
  1627. while(mark_word != 0) {
  1628. if (mark_word & 1) {
  1629. PUSH_GRANULE(q);
  1630. }
  1631. q += GC_GRANULE_WORDS;
  1632. mark_word >>= 1;
  1633. }
  1634. p += WORDSZ*GC_GRANULE_WORDS;
  1635. }
  1636. # undef GC_greatest_plausible_heap_addr
  1637. # undef GC_least_plausible_heap_addr
  1638. # undef GC_mark_stack_top
  1639. # undef GC_mark_stack_limit
  1640. # define GC_mark_stack_limit GC_arrays._mark_stack_limit
  1641. # define GC_mark_stack_top GC_arrays._mark_stack_top
  1642. GC_mark_stack_top = mark_stack_top;
  1643. }
  1644. #ifndef UNALIGNED_PTRS
  1645. /* Push all objects reachable from marked objects in the given block */
  1646. /* of size 2 (granules) objects. */
  1647. STATIC void GC_push_marked2(struct hblk *h, hdr *hhdr)
  1648. {
  1649. word * mark_word_addr = &(hhdr->hb_marks[0]);
  1650. word *p;
  1651. word *plim;
  1652. ptr_t greatest_ha = (ptr_t)GC_greatest_plausible_heap_addr;
  1653. ptr_t least_ha = (ptr_t)GC_least_plausible_heap_addr;
  1654. mse * mark_stack_top = GC_mark_stack_top;
  1655. mse * mark_stack_limit = GC_mark_stack_limit;
  1656. # undef GC_mark_stack_top
  1657. # undef GC_mark_stack_limit
  1658. # define GC_mark_stack_top mark_stack_top
  1659. # define GC_mark_stack_limit mark_stack_limit
  1660. # define GC_greatest_plausible_heap_addr greatest_ha
  1661. # define GC_least_plausible_heap_addr least_ha
  1662. p = (word *)(h->hb_body);
  1663. plim = (word *)(((word)h) + HBLKSIZE);
  1664. /* go through all words in block */
  1665. while ((word)p < (word)plim) {
  1666. word mark_word = *mark_word_addr++;
  1667. word *q = p;
  1668. while(mark_word != 0) {
  1669. if (mark_word & 1) {
  1670. PUSH_GRANULE(q);
  1671. PUSH_GRANULE(q + GC_GRANULE_WORDS);
  1672. }
  1673. q += 2 * GC_GRANULE_WORDS;
  1674. mark_word >>= 2;
  1675. }
  1676. p += WORDSZ*GC_GRANULE_WORDS;
  1677. }
  1678. # undef GC_greatest_plausible_heap_addr
  1679. # undef GC_least_plausible_heap_addr
  1680. # undef GC_mark_stack_top
  1681. # undef GC_mark_stack_limit
  1682. # define GC_mark_stack_limit GC_arrays._mark_stack_limit
  1683. # define GC_mark_stack_top GC_arrays._mark_stack_top
  1684. GC_mark_stack_top = mark_stack_top;
  1685. }
  1686. # if GC_GRANULE_WORDS < 4
  1687. /* Push all objects reachable from marked objects in the given block */
  1688. /* of size 4 (granules) objects. */
  1689. /* There is a risk of mark stack overflow here. But we handle that. */
  1690. /* And only unmarked objects get pushed, so it's not very likely. */
  1691. STATIC void GC_push_marked4(struct hblk *h, hdr *hhdr)
  1692. {
  1693. word * mark_word_addr = &(hhdr->hb_marks[0]);
  1694. word *p;
  1695. word *plim;
  1696. ptr_t greatest_ha = (ptr_t)GC_greatest_plausible_heap_addr;
  1697. ptr_t least_ha = (ptr_t)GC_least_plausible_heap_addr;
  1698. mse * mark_stack_top = GC_mark_stack_top;
  1699. mse * mark_stack_limit = GC_mark_stack_limit;
  1700. # undef GC_mark_stack_top
  1701. # undef GC_mark_stack_limit
  1702. # define GC_mark_stack_top mark_stack_top
  1703. # define GC_mark_stack_limit mark_stack_limit
  1704. # define GC_greatest_plausible_heap_addr greatest_ha
  1705. # define GC_least_plausible_heap_addr least_ha
  1706. p = (word *)(h->hb_body);
  1707. plim = (word *)(((word)h) + HBLKSIZE);
  1708. /* go through all words in block */
  1709. while ((word)p < (word)plim) {
  1710. word mark_word = *mark_word_addr++;
  1711. word *q = p;
  1712. while(mark_word != 0) {
  1713. if (mark_word & 1) {
  1714. PUSH_GRANULE(q);
  1715. PUSH_GRANULE(q + GC_GRANULE_WORDS);
  1716. PUSH_GRANULE(q + 2*GC_GRANULE_WORDS);
  1717. PUSH_GRANULE(q + 3*GC_GRANULE_WORDS);
  1718. }
  1719. q += 4 * GC_GRANULE_WORDS;
  1720. mark_word >>= 4;
  1721. }
  1722. p += WORDSZ*GC_GRANULE_WORDS;
  1723. }
  1724. # undef GC_greatest_plausible_heap_addr
  1725. # undef GC_least_plausible_heap_addr
  1726. # undef GC_mark_stack_top
  1727. # undef GC_mark_stack_limit
  1728. # define GC_mark_stack_limit GC_arrays._mark_stack_limit
  1729. # define GC_mark_stack_top GC_arrays._mark_stack_top
  1730. GC_mark_stack_top = mark_stack_top;
  1731. }
  1732. #endif /* GC_GRANULE_WORDS < 4 */
  1733. #endif /* UNALIGNED_PTRS */
  1734. #endif /* USE_PUSH_MARKED_ACCELERATORS */
  1735. /* Push all objects reachable from marked objects in the given block */
  1736. STATIC void GC_push_marked(struct hblk *h, hdr *hhdr)
  1737. {
  1738. word sz = hhdr -> hb_sz;
  1739. word descr = hhdr -> hb_descr;
  1740. ptr_t p;
  1741. word bit_no;
  1742. ptr_t lim;
  1743. mse * GC_mark_stack_top_reg;
  1744. mse * mark_stack_limit = GC_mark_stack_limit;
  1745. /* Some quick shortcuts: */
  1746. if ((/* 0 | */ GC_DS_LENGTH) == descr) return;
  1747. if (GC_block_empty(hhdr)/* nothing marked */) return;
  1748. # if !defined(GC_DISABLE_INCREMENTAL)
  1749. GC_n_rescuing_pages++;
  1750. # endif
  1751. GC_objects_are_marked = TRUE;
  1752. if (sz > MAXOBJBYTES) {
  1753. lim = h -> hb_body;
  1754. } else {
  1755. lim = (ptr_t)((word)(h + 1)->hb_body - sz);
  1756. }
  1757. switch(BYTES_TO_GRANULES(sz)) {
  1758. # if defined(USE_PUSH_MARKED_ACCELERATORS)
  1759. case 1:
  1760. GC_push_marked1(h, hhdr);
  1761. break;
  1762. # if !defined(UNALIGNED_PTRS)
  1763. case 2:
  1764. GC_push_marked2(h, hhdr);
  1765. break;
  1766. # if GC_GRANULE_WORDS < 4
  1767. case 4:
  1768. GC_push_marked4(h, hhdr);
  1769. break;
  1770. # endif
  1771. # endif
  1772. # endif
  1773. default:
  1774. GC_mark_stack_top_reg = GC_mark_stack_top;
  1775. for (p = h -> hb_body, bit_no = 0; (word)p <= (word)lim;
  1776. p += sz, bit_no += MARK_BIT_OFFSET(sz)) {
  1777. if (mark_bit_from_hdr(hhdr, bit_no)) {
  1778. /* Mark from fields inside the object */
  1779. PUSH_OBJ(p, hhdr, GC_mark_stack_top_reg, mark_stack_limit);
  1780. }
  1781. }
  1782. GC_mark_stack_top = GC_mark_stack_top_reg;
  1783. }
  1784. }
  1785. #ifdef ENABLE_DISCLAIM
  1786. /* Unconditionally mark from all objects which have not been reclaimed. */
  1787. /* This is useful in order to retain pointers which are reachable from */
  1788. /* the disclaim notifiers. */
  1789. /* */
  1790. /* To determine whether an object has been reclaimed, we require that */
  1791. /* any live object has a non-zero as one of the two lowest bits of the */
  1792. /* first word. On the other hand, a reclaimed object is a members of */
  1793. /* free-lists, and thus contains a word-aligned next-pointer as the */
  1794. /* first word. */
  1795. STATIC void GC_push_unconditionally(struct hblk *h, hdr *hhdr)
  1796. {
  1797. word sz = hhdr -> hb_sz;
  1798. word descr = hhdr -> hb_descr;
  1799. ptr_t p;
  1800. ptr_t lim;
  1801. mse * GC_mark_stack_top_reg;
  1802. mse * mark_stack_limit = GC_mark_stack_limit;
  1803. if ((/* 0 | */ GC_DS_LENGTH) == descr)
  1804. return;
  1805. # if !defined(GC_DISABLE_INCREMENTAL)
  1806. GC_n_rescuing_pages++;
  1807. # endif
  1808. GC_objects_are_marked = TRUE;
  1809. if (sz > MAXOBJBYTES)
  1810. lim = h -> hb_body;
  1811. else
  1812. lim = (ptr_t)((word)(h + 1)->hb_body - sz);
  1813. GC_mark_stack_top_reg = GC_mark_stack_top;
  1814. for (p = h -> hb_body; (word)p <= (word)lim; p += sz)
  1815. if ((*(word *)p & 0x3) != 0)
  1816. PUSH_OBJ(p, hhdr, GC_mark_stack_top_reg, mark_stack_limit);
  1817. GC_mark_stack_top = GC_mark_stack_top_reg;
  1818. }
  1819. #endif /* ENABLE_DISCLAIM */
  1820. #ifndef GC_DISABLE_INCREMENTAL
  1821. /* Test whether any page in the given block is dirty. */
  1822. STATIC GC_bool GC_block_was_dirty(struct hblk *h, hdr *hhdr)
  1823. {
  1824. word sz = hhdr -> hb_sz;
  1825. if (sz <= MAXOBJBYTES) {
  1826. return(GC_page_was_dirty(h));
  1827. } else {
  1828. ptr_t p = (ptr_t)h;
  1829. while ((word)p < (word)h + sz) {
  1830. if (GC_page_was_dirty((struct hblk *)p)) return(TRUE);
  1831. p += HBLKSIZE;
  1832. }
  1833. return(FALSE);
  1834. }
  1835. }
  1836. #endif /* GC_DISABLE_INCREMENTAL */
  1837. /* Similar to GC_push_marked, but skip over unallocated blocks */
  1838. /* and return address of next plausible block. */
  1839. STATIC struct hblk * GC_push_next_marked(struct hblk *h)
  1840. {
  1841. hdr * hhdr = HDR(h);
  1842. if (EXPECT(IS_FORWARDING_ADDR_OR_NIL(hhdr) || HBLK_IS_FREE(hhdr), FALSE)) {
  1843. h = GC_next_used_block(h);
  1844. if (h == 0) return(0);
  1845. hhdr = GC_find_header((ptr_t)h);
  1846. } else {
  1847. # ifdef LINT2
  1848. if (NULL == h) ABORT("Bad HDR() definition");
  1849. # endif
  1850. }
  1851. GC_push_marked(h, hhdr);
  1852. return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
  1853. }
  1854. #ifndef GC_DISABLE_INCREMENTAL
  1855. /* Identical to above, but mark only from dirty pages */
  1856. STATIC struct hblk * GC_push_next_marked_dirty(struct hblk *h)
  1857. {
  1858. hdr * hhdr = HDR(h);
  1859. if (!GC_incremental) ABORT("Dirty bits not set up");
  1860. for (;;) {
  1861. if (EXPECT(IS_FORWARDING_ADDR_OR_NIL(hhdr)
  1862. || HBLK_IS_FREE(hhdr), FALSE)) {
  1863. h = GC_next_used_block(h);
  1864. if (h == 0) return(0);
  1865. hhdr = GC_find_header((ptr_t)h);
  1866. } else {
  1867. # ifdef LINT2
  1868. if (NULL == h) ABORT("Bad HDR() definition");
  1869. # endif
  1870. }
  1871. if (GC_block_was_dirty(h, hhdr))
  1872. break;
  1873. h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
  1874. hhdr = HDR(h);
  1875. }
  1876. # ifdef ENABLE_DISCLAIM
  1877. if ((hhdr -> hb_flags & MARK_UNCONDITIONALLY) != 0) {
  1878. GC_push_unconditionally(h, hhdr);
  1879. /* Then we may ask, why not also add the MARK_UNCONDITIONALLY */
  1880. /* case to GC_push_next_marked, which is also applied to */
  1881. /* uncollectible blocks? But it seems to me that the function */
  1882. /* does not need to scan uncollectible (and unconditionally */
  1883. /* marked) blocks since those are already handled in the */
  1884. /* MS_PUSH_UNCOLLECTABLE phase. */
  1885. } else
  1886. # endif
  1887. /* else */ {
  1888. GC_push_marked(h, hhdr);
  1889. }
  1890. return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
  1891. }
  1892. #endif /* !GC_DISABLE_INCREMENTAL */
  1893. /* Similar to above, but for uncollectible pages. Needed since we */
  1894. /* do not clear marks for such pages, even for full collections. */
  1895. STATIC struct hblk * GC_push_next_marked_uncollectable(struct hblk *h)
  1896. {
  1897. hdr * hhdr = HDR(h);
  1898. for (;;) {
  1899. if (EXPECT(IS_FORWARDING_ADDR_OR_NIL(hhdr)
  1900. || HBLK_IS_FREE(hhdr), FALSE)) {
  1901. h = GC_next_used_block(h);
  1902. if (h == 0) return(0);
  1903. hhdr = GC_find_header((ptr_t)h);
  1904. } else {
  1905. # ifdef LINT2
  1906. if (NULL == h) ABORT("Bad HDR() definition");
  1907. # endif
  1908. }
  1909. if (hhdr -> hb_obj_kind == UNCOLLECTABLE) {
  1910. GC_push_marked(h, hhdr);
  1911. break;
  1912. }
  1913. # ifdef ENABLE_DISCLAIM
  1914. if ((hhdr -> hb_flags & MARK_UNCONDITIONALLY) != 0) {
  1915. GC_push_unconditionally(h, hhdr);
  1916. break;
  1917. }
  1918. # endif
  1919. h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
  1920. hhdr = HDR(h);
  1921. }
  1922. return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
  1923. }