mark.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  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. # include <stdio.h>
  17. # include "private/gc_pmark.h"
  18. #if defined(MSWIN32) && defined(__GNUC__)
  19. # include <excpt.h>
  20. #endif
  21. /* We put this here to minimize the risk of inlining. */
  22. /*VARARGS*/
  23. #ifdef __WATCOMC__
  24. void GC_noop(void *p, ...) {}
  25. #else
  26. void GC_noop() {}
  27. #endif
  28. /* Single argument version, robust against whole program analysis. */
  29. void GC_noop1(x)
  30. word x;
  31. {
  32. static VOLATILE word sink;
  33. sink = x;
  34. }
  35. /* mark_proc GC_mark_procs[MAX_MARK_PROCS] = {0} -- declared in gc_priv.h */
  36. word GC_n_mark_procs = GC_RESERVED_MARK_PROCS;
  37. /* Initialize GC_obj_kinds properly and standard free lists properly. */
  38. /* This must be done statically since they may be accessed before */
  39. /* GC_init is called. */
  40. /* It's done here, since we need to deal with mark descriptors. */
  41. struct obj_kind GC_obj_kinds[MAXOBJKINDS] = {
  42. /* PTRFREE */ { &GC_aobjfreelist[0], 0 /* filled in dynamically */,
  43. 0 | GC_DS_LENGTH, FALSE, FALSE },
  44. /* NORMAL */ { &GC_objfreelist[0], 0,
  45. 0 | GC_DS_LENGTH, /* Adjusted in GC_init_inner for EXTRA_BYTES */
  46. TRUE /* add length to descr */, TRUE },
  47. /* UNCOLLECTABLE */
  48. { &GC_uobjfreelist[0], 0,
  49. 0 | GC_DS_LENGTH, TRUE /* add length to descr */, TRUE },
  50. # ifdef ATOMIC_UNCOLLECTABLE
  51. /* AUNCOLLECTABLE */
  52. { &GC_auobjfreelist[0], 0,
  53. 0 | GC_DS_LENGTH, FALSE /* add length to descr */, FALSE },
  54. # endif
  55. # ifdef STUBBORN_ALLOC
  56. /*STUBBORN*/ { &GC_sobjfreelist[0], 0,
  57. 0 | GC_DS_LENGTH, TRUE /* add length to descr */, TRUE },
  58. # endif
  59. };
  60. # ifdef ATOMIC_UNCOLLECTABLE
  61. # ifdef STUBBORN_ALLOC
  62. int GC_n_kinds = 5;
  63. # else
  64. int GC_n_kinds = 4;
  65. # endif
  66. # else
  67. # ifdef STUBBORN_ALLOC
  68. int GC_n_kinds = 4;
  69. # else
  70. int GC_n_kinds = 3;
  71. # endif
  72. # endif
  73. # ifndef INITIAL_MARK_STACK_SIZE
  74. # define INITIAL_MARK_STACK_SIZE (1*HBLKSIZE)
  75. /* INITIAL_MARK_STACK_SIZE * sizeof(mse) should be a */
  76. /* multiple of HBLKSIZE. */
  77. /* The incremental collector actually likes a larger */
  78. /* size, since it want to push all marked dirty objs */
  79. /* before marking anything new. Currently we let it */
  80. /* grow dynamically. */
  81. # endif
  82. /*
  83. * Limits of stack for GC_mark routine.
  84. * All ranges between GC_mark_stack(incl.) and GC_mark_stack_top(incl.) still
  85. * need to be marked from.
  86. */
  87. word GC_n_rescuing_pages; /* Number of dirty pages we marked from */
  88. /* excludes ptrfree pages, etc. */
  89. mse * GC_mark_stack;
  90. mse * GC_mark_stack_limit;
  91. word GC_mark_stack_size = 0;
  92. #ifdef PARALLEL_MARK
  93. mse * VOLATILE GC_mark_stack_top;
  94. #else
  95. mse * GC_mark_stack_top;
  96. #endif
  97. static struct hblk * scan_ptr;
  98. mark_state_t GC_mark_state = MS_NONE;
  99. GC_bool GC_mark_stack_too_small = FALSE;
  100. GC_bool GC_objects_are_marked = FALSE; /* Are there collectable marked */
  101. /* objects in the heap? */
  102. /* Is a collection in progress? Note that this can return true in the */
  103. /* nonincremental case, if a collection has been abandoned and the */
  104. /* mark state is now MS_INVALID. */
  105. GC_bool GC_collection_in_progress()
  106. {
  107. return(GC_mark_state != MS_NONE);
  108. }
  109. /* clear all mark bits in the header */
  110. void GC_clear_hdr_marks(hhdr)
  111. register hdr * hhdr;
  112. {
  113. # ifdef USE_MARK_BYTES
  114. BZERO(hhdr -> hb_marks, MARK_BITS_SZ);
  115. # else
  116. BZERO(hhdr -> hb_marks, MARK_BITS_SZ*sizeof(word));
  117. # endif
  118. }
  119. /* Set all mark bits in the header. Used for uncollectable blocks. */
  120. void GC_set_hdr_marks(hhdr)
  121. register hdr * hhdr;
  122. {
  123. register int i;
  124. for (i = 0; i < MARK_BITS_SZ; ++i) {
  125. # ifdef USE_MARK_BYTES
  126. hhdr -> hb_marks[i] = 1;
  127. # else
  128. hhdr -> hb_marks[i] = ONES;
  129. # endif
  130. }
  131. }
  132. /*
  133. * Clear all mark bits associated with block h.
  134. */
  135. /*ARGSUSED*/
  136. # if defined(__STDC__) || defined(__cplusplus)
  137. static void clear_marks_for_block(struct hblk *h, word dummy)
  138. # else
  139. static void clear_marks_for_block(h, dummy)
  140. struct hblk *h;
  141. word dummy;
  142. # endif
  143. {
  144. register hdr * hhdr = HDR(h);
  145. if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) return;
  146. /* Mark bit for these is cleared only once the object is */
  147. /* explicitly deallocated. This either frees the block, or */
  148. /* the bit is cleared once the object is on the free list. */
  149. GC_clear_hdr_marks(hhdr);
  150. }
  151. /* Slow but general routines for setting/clearing/asking about mark bits */
  152. void GC_set_mark_bit(p)
  153. ptr_t p;
  154. {
  155. register struct hblk *h = HBLKPTR(p);
  156. register hdr * hhdr = HDR(h);
  157. register int word_no = (word *)p - (word *)h;
  158. set_mark_bit_from_hdr(hhdr, word_no);
  159. }
  160. void GC_clear_mark_bit(p)
  161. ptr_t p;
  162. {
  163. register struct hblk *h = HBLKPTR(p);
  164. register hdr * hhdr = HDR(h);
  165. register int word_no = (word *)p - (word *)h;
  166. clear_mark_bit_from_hdr(hhdr, word_no);
  167. }
  168. GC_bool GC_is_marked(p)
  169. ptr_t p;
  170. {
  171. register struct hblk *h = HBLKPTR(p);
  172. register hdr * hhdr = HDR(h);
  173. register int word_no = (word *)p - (word *)h;
  174. return(mark_bit_from_hdr(hhdr, word_no));
  175. }
  176. /*
  177. * Clear mark bits in all allocated heap blocks. This invalidates
  178. * the marker invariant, and sets GC_mark_state to reflect this.
  179. * (This implicitly starts marking to reestablish the invariant.)
  180. */
  181. void GC_clear_marks()
  182. {
  183. GC_apply_to_all_blocks(clear_marks_for_block, (word)0);
  184. GC_objects_are_marked = FALSE;
  185. GC_mark_state = MS_INVALID;
  186. scan_ptr = 0;
  187. # ifdef GATHERSTATS
  188. /* Counters reflect currently marked objects: reset here */
  189. GC_composite_in_use = 0;
  190. GC_atomic_in_use = 0;
  191. # endif
  192. }
  193. /* Initiate a garbage collection. Initiates a full collection if the */
  194. /* mark state is invalid. */
  195. /*ARGSUSED*/
  196. void GC_initiate_gc()
  197. {
  198. if (GC_dirty_maintained) GC_read_dirty();
  199. # ifdef STUBBORN_ALLOC
  200. GC_read_changed();
  201. # endif
  202. # ifdef CHECKSUMS
  203. {
  204. extern void GC_check_dirty();
  205. if (GC_dirty_maintained) GC_check_dirty();
  206. }
  207. # endif
  208. GC_n_rescuing_pages = 0;
  209. if (GC_mark_state == MS_NONE) {
  210. GC_mark_state = MS_PUSH_RESCUERS;
  211. } else if (GC_mark_state != MS_INVALID) {
  212. ABORT("unexpected state");
  213. } /* else this is really a full collection, and mark */
  214. /* bits are invalid. */
  215. scan_ptr = 0;
  216. }
  217. static void alloc_mark_stack();
  218. /* Perform a small amount of marking. */
  219. /* We try to touch roughly a page of memory. */
  220. /* Return TRUE if we just finished a mark phase. */
  221. /* Cold_gc_frame is an address inside a GC frame that */
  222. /* remains valid until all marking is complete. */
  223. /* A zero value indicates that it's OK to miss some */
  224. /* register values. */
  225. /* We hold the allocation lock. In the case of */
  226. /* incremental collection, the world may not be stopped.*/
  227. #ifdef MSWIN32
  228. /* For win32, this is called after we establish a structured */
  229. /* exception handler, in case Windows unmaps one of our root */
  230. /* segments. See below. In either case, we acquire the */
  231. /* allocator lock long before we get here. */
  232. GC_bool GC_mark_some_inner(cold_gc_frame)
  233. ptr_t cold_gc_frame;
  234. #else
  235. GC_bool GC_mark_some(cold_gc_frame)
  236. ptr_t cold_gc_frame;
  237. #endif
  238. {
  239. switch(GC_mark_state) {
  240. case MS_NONE:
  241. return(FALSE);
  242. case MS_PUSH_RESCUERS:
  243. if (GC_mark_stack_top
  244. >= GC_mark_stack_limit - INITIAL_MARK_STACK_SIZE/2) {
  245. /* Go ahead and mark, even though that might cause us to */
  246. /* see more marked dirty objects later on. Avoid this */
  247. /* in the future. */
  248. GC_mark_stack_too_small = TRUE;
  249. MARK_FROM_MARK_STACK();
  250. return(FALSE);
  251. } else {
  252. scan_ptr = GC_push_next_marked_dirty(scan_ptr);
  253. if (scan_ptr == 0) {
  254. # ifdef CONDPRINT
  255. if (GC_print_stats) {
  256. GC_printf1("Marked from %lu dirty pages\n",
  257. (unsigned long)GC_n_rescuing_pages);
  258. }
  259. # endif
  260. GC_push_roots(FALSE, cold_gc_frame);
  261. GC_objects_are_marked = TRUE;
  262. if (GC_mark_state != MS_INVALID) {
  263. GC_mark_state = MS_ROOTS_PUSHED;
  264. }
  265. }
  266. }
  267. return(FALSE);
  268. case MS_PUSH_UNCOLLECTABLE:
  269. if (GC_mark_stack_top
  270. >= GC_mark_stack + GC_mark_stack_size/4) {
  271. # ifdef PARALLEL_MARK
  272. /* Avoid this, since we don't parallelize the marker */
  273. /* here. */
  274. if (GC_parallel) GC_mark_stack_too_small = TRUE;
  275. # endif
  276. MARK_FROM_MARK_STACK();
  277. return(FALSE);
  278. } else {
  279. scan_ptr = GC_push_next_marked_uncollectable(scan_ptr);
  280. if (scan_ptr == 0) {
  281. GC_push_roots(TRUE, cold_gc_frame);
  282. GC_objects_are_marked = TRUE;
  283. if (GC_mark_state != MS_INVALID) {
  284. GC_mark_state = MS_ROOTS_PUSHED;
  285. }
  286. }
  287. }
  288. return(FALSE);
  289. case MS_ROOTS_PUSHED:
  290. # ifdef PARALLEL_MARK
  291. /* In the incremental GC case, this currently doesn't */
  292. /* quite do the right thing, since it runs to */
  293. /* completion. On the other hand, starting a */
  294. /* parallel marker is expensive, so perhaps it is */
  295. /* the right thing? */
  296. /* Eventually, incremental marking should run */
  297. /* asynchronously in multiple threads, without grabbing */
  298. /* the allocation lock. */
  299. if (GC_parallel) {
  300. GC_do_parallel_mark();
  301. GC_ASSERT(GC_mark_stack_top < GC_first_nonempty);
  302. GC_mark_stack_top = GC_mark_stack - 1;
  303. if (GC_mark_stack_too_small) {
  304. alloc_mark_stack(2*GC_mark_stack_size);
  305. }
  306. if (GC_mark_state == MS_ROOTS_PUSHED) {
  307. GC_mark_state = MS_NONE;
  308. return(TRUE);
  309. } else {
  310. return(FALSE);
  311. }
  312. }
  313. # endif
  314. if (GC_mark_stack_top >= GC_mark_stack) {
  315. MARK_FROM_MARK_STACK();
  316. return(FALSE);
  317. } else {
  318. GC_mark_state = MS_NONE;
  319. if (GC_mark_stack_too_small) {
  320. alloc_mark_stack(2*GC_mark_stack_size);
  321. }
  322. return(TRUE);
  323. }
  324. case MS_INVALID:
  325. case MS_PARTIALLY_INVALID:
  326. if (!GC_objects_are_marked) {
  327. GC_mark_state = MS_PUSH_UNCOLLECTABLE;
  328. return(FALSE);
  329. }
  330. if (GC_mark_stack_top >= GC_mark_stack) {
  331. MARK_FROM_MARK_STACK();
  332. return(FALSE);
  333. }
  334. if (scan_ptr == 0 && GC_mark_state == MS_INVALID) {
  335. /* About to start a heap scan for marked objects. */
  336. /* Mark stack is empty. OK to reallocate. */
  337. if (GC_mark_stack_too_small) {
  338. alloc_mark_stack(2*GC_mark_stack_size);
  339. }
  340. GC_mark_state = MS_PARTIALLY_INVALID;
  341. }
  342. scan_ptr = GC_push_next_marked(scan_ptr);
  343. if (scan_ptr == 0 && GC_mark_state == MS_PARTIALLY_INVALID) {
  344. GC_push_roots(TRUE, cold_gc_frame);
  345. GC_objects_are_marked = TRUE;
  346. if (GC_mark_state != MS_INVALID) {
  347. GC_mark_state = MS_ROOTS_PUSHED;
  348. }
  349. }
  350. return(FALSE);
  351. default:
  352. ABORT("GC_mark_some: bad state");
  353. return(FALSE);
  354. }
  355. }
  356. #ifdef MSWIN32
  357. # ifdef __GNUC__
  358. typedef struct {
  359. EXCEPTION_REGISTRATION ex_reg;
  360. void *alt_path;
  361. } ext_ex_regn;
  362. static EXCEPTION_DISPOSITION mark_ex_handler(
  363. struct _EXCEPTION_RECORD *ex_rec,
  364. void *est_frame,
  365. struct _CONTEXT *context,
  366. void *disp_ctxt)
  367. {
  368. if (ex_rec->ExceptionCode == STATUS_ACCESS_VIOLATION) {
  369. ext_ex_regn *xer = (ext_ex_regn *)est_frame;
  370. /* Unwind from the inner function assuming the standard */
  371. /* function prologue. */
  372. /* Assumes code has not been compiled with */
  373. /* -fomit-frame-pointer. */
  374. context->Esp = context->Ebp;
  375. context->Ebp = *((DWORD *)context->Esp);
  376. context->Esp = context->Esp - 8;
  377. /* Resume execution at the "real" handler within the */
  378. /* wrapper function. */
  379. context->Eip = (DWORD )(xer->alt_path);
  380. return ExceptionContinueExecution;
  381. } else {
  382. return ExceptionContinueSearch;
  383. }
  384. }
  385. # endif /* __GNUC__ */
  386. GC_bool GC_mark_some(cold_gc_frame)
  387. ptr_t cold_gc_frame;
  388. {
  389. GC_bool ret_val;
  390. # ifndef __GNUC__
  391. /* Windows 98 appears to asynchronously create and remove */
  392. /* writable memory mappings, for reasons we haven't yet */
  393. /* understood. Since we look for writable regions to */
  394. /* determine the root set, we may try to mark from an */
  395. /* address range that disappeared since we started the */
  396. /* collection. Thus we have to recover from faults here. */
  397. /* This code does not appear to be necessary for Windows */
  398. /* 95/NT/2000. Note that this code should never generate */
  399. /* an incremental GC write fault. */
  400. __try {
  401. # else /* __GNUC__ */
  402. /* Manually install an exception handler since GCC does */
  403. /* not yet support Structured Exception Handling (SEH) on */
  404. /* Win32. */
  405. ext_ex_regn er;
  406. er.alt_path = &&handle_ex;
  407. er.ex_reg.handler = mark_ex_handler;
  408. asm volatile ("movl %%fs:0, %0" : "=r" (er.ex_reg.prev));
  409. asm volatile ("movl %0, %%fs:0" : : "r" (&er));
  410. # endif /* __GNUC__ */
  411. ret_val = GC_mark_some_inner(cold_gc_frame);
  412. # ifndef __GNUC__
  413. } __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
  414. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
  415. # else /* __GNUC__ */
  416. /* Prevent GCC from considering the following code unreachable */
  417. /* and thus eliminating it. */
  418. if (er.alt_path != 0)
  419. goto rm_handler;
  420. handle_ex:
  421. /* Execution resumes from here on an access violation. */
  422. # endif /* __GNUC__ */
  423. # ifdef CONDPRINT
  424. if (GC_print_stats) {
  425. GC_printf0("Caught ACCESS_VIOLATION in marker. "
  426. "Memory mapping disappeared.\n");
  427. }
  428. # endif /* CONDPRINT */
  429. /* We have bad roots on the stack. Discard mark stack. */
  430. /* Rescan from marked objects. Redetermine roots. */
  431. GC_invalidate_mark_state();
  432. scan_ptr = 0;
  433. ret_val = FALSE;
  434. # ifndef __GNUC__
  435. }
  436. # else /* __GNUC__ */
  437. rm_handler:
  438. /* Uninstall the exception handler */
  439. asm volatile ("mov %0, %%fs:0" : : "r" (er.ex_reg.prev));
  440. # endif /* __GNUC__ */
  441. return ret_val;
  442. }
  443. #endif /* MSWIN32 */
  444. GC_bool GC_mark_stack_empty()
  445. {
  446. return(GC_mark_stack_top < GC_mark_stack);
  447. }
  448. #ifdef PROF_MARKER
  449. word GC_prof_array[10];
  450. # define PROF(n) GC_prof_array[n]++
  451. #else
  452. # define PROF(n)
  453. #endif
  454. /* Given a pointer to someplace other than a small object page or the */
  455. /* first page of a large object, either: */
  456. /* - return a pointer to somewhere in the first page of the large */
  457. /* object, if current points to a large object. */
  458. /* In this case *hhdr is replaced with a pointer to the header */
  459. /* for the large object. */
  460. /* - just return current if it does not point to a large object. */
  461. /*ARGSUSED*/
  462. ptr_t GC_find_start(current, hhdr, new_hdr_p)
  463. register ptr_t current;
  464. register hdr *hhdr, **new_hdr_p;
  465. {
  466. if (GC_all_interior_pointers) {
  467. if (hhdr != 0) {
  468. register ptr_t orig = current;
  469. current = (ptr_t)HBLKPTR(current);
  470. do {
  471. current = current - HBLKSIZE*(word)hhdr;
  472. hhdr = HDR(current);
  473. } while(IS_FORWARDING_ADDR_OR_NIL(hhdr));
  474. /* current points to near the start of the large object */
  475. if (hhdr -> hb_flags & IGNORE_OFF_PAGE) return(orig);
  476. if ((word *)orig - (word *)current
  477. >= (ptrdiff_t)(hhdr->hb_sz)) {
  478. /* Pointer past the end of the block */
  479. return(orig);
  480. }
  481. *new_hdr_p = hhdr;
  482. return(current);
  483. } else {
  484. return(current);
  485. }
  486. } else {
  487. return(current);
  488. }
  489. }
  490. void GC_invalidate_mark_state()
  491. {
  492. GC_mark_state = MS_INVALID;
  493. GC_mark_stack_top = GC_mark_stack-1;
  494. }
  495. mse * GC_signal_mark_stack_overflow(msp)
  496. mse * msp;
  497. {
  498. GC_mark_state = MS_INVALID;
  499. GC_mark_stack_too_small = TRUE;
  500. # ifdef CONDPRINT
  501. if (GC_print_stats) {
  502. GC_printf1("Mark stack overflow; current size = %lu entries\n",
  503. GC_mark_stack_size);
  504. }
  505. # endif
  506. return(msp - GC_MARK_STACK_DISCARDS);
  507. }
  508. /*
  509. * Mark objects pointed to by the regions described by
  510. * mark stack entries between GC_mark_stack and GC_mark_stack_top,
  511. * inclusive. Assumes the upper limit of a mark stack entry
  512. * is never 0. A mark stack entry never has size 0.
  513. * We try to traverse on the order of a hblk of memory before we return.
  514. * Caller is responsible for calling this until the mark stack is empty.
  515. * Note that this is the most performance critical routine in the
  516. * collector. Hence it contains all sorts of ugly hacks to speed
  517. * things up. In particular, we avoid procedure calls on the common
  518. * path, we take advantage of peculiarities of the mark descriptor
  519. * encoding, we optionally maintain a cache for the block address to
  520. * header mapping, we prefetch when an object is "grayed", etc.
  521. */
  522. mse * GC_mark_from(mark_stack_top, mark_stack, mark_stack_limit)
  523. mse * mark_stack_top;
  524. mse * mark_stack;
  525. mse * mark_stack_limit;
  526. {
  527. int credit = HBLKSIZE; /* Remaining credit for marking work */
  528. register word * current_p; /* Pointer to current candidate ptr. */
  529. register word current; /* Candidate pointer. */
  530. register word * limit; /* (Incl) limit of current candidate */
  531. /* range */
  532. register word descr;
  533. register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
  534. register ptr_t least_ha = GC_least_plausible_heap_addr;
  535. DECLARE_HDR_CACHE;
  536. # define SPLIT_RANGE_WORDS 128 /* Must be power of 2. */
  537. GC_objects_are_marked = TRUE;
  538. INIT_HDR_CACHE;
  539. # ifdef OS2 /* Use untweaked version to circumvent compiler problem */
  540. while (mark_stack_top >= mark_stack && credit >= 0) {
  541. # else
  542. while ((((ptr_t)mark_stack_top - (ptr_t)mark_stack) | credit)
  543. >= 0) {
  544. # endif
  545. current_p = mark_stack_top -> mse_start;
  546. descr = mark_stack_top -> mse_descr;
  547. retry:
  548. /* current_p and descr describe the current object. */
  549. /* *mark_stack_top is vacant. */
  550. /* The following is 0 only for small objects described by a simple */
  551. /* length descriptor. For many applications this is the common */
  552. /* case, so we try to detect it quickly. */
  553. if (descr & ((~(WORDS_TO_BYTES(SPLIT_RANGE_WORDS) - 1)) | GC_DS_TAGS)) {
  554. word tag = descr & GC_DS_TAGS;
  555. switch(tag) {
  556. case GC_DS_LENGTH:
  557. /* Large length. */
  558. /* Process part of the range to avoid pushing too much on the */
  559. /* stack. */
  560. GC_ASSERT(descr < (word)GC_greatest_plausible_heap_addr
  561. - (word)GC_least_plausible_heap_addr);
  562. # ifdef PARALLEL_MARK
  563. # define SHARE_BYTES 2048
  564. if (descr > SHARE_BYTES && GC_parallel
  565. && mark_stack_top < mark_stack_limit - 1) {
  566. int new_size = (descr/2) & ~(sizeof(word)-1);
  567. mark_stack_top -> mse_start = current_p;
  568. mark_stack_top -> mse_descr = new_size + sizeof(word);
  569. /* makes sure we handle */
  570. /* misaligned pointers. */
  571. mark_stack_top++;
  572. current_p = (word *) ((char *)current_p + new_size);
  573. descr -= new_size;
  574. goto retry;
  575. }
  576. # endif /* PARALLEL_MARK */
  577. mark_stack_top -> mse_start =
  578. limit = current_p + SPLIT_RANGE_WORDS-1;
  579. mark_stack_top -> mse_descr =
  580. descr - WORDS_TO_BYTES(SPLIT_RANGE_WORDS-1);
  581. /* Make sure that pointers overlapping the two ranges are */
  582. /* considered. */
  583. limit = (word *)((char *)limit + sizeof(word) - ALIGNMENT);
  584. break;
  585. case GC_DS_BITMAP:
  586. mark_stack_top--;
  587. descr &= ~GC_DS_TAGS;
  588. credit -= WORDS_TO_BYTES(WORDSZ/2); /* guess */
  589. while (descr != 0) {
  590. if ((signed_word)descr < 0) {
  591. current = *current_p;
  592. FIXUP_POINTER(current);
  593. if ((ptr_t)current >= least_ha && (ptr_t)current < greatest_ha) {
  594. PREFETCH((ptr_t)current);
  595. HC_PUSH_CONTENTS((ptr_t)current, mark_stack_top,
  596. mark_stack_limit, current_p, exit1);
  597. }
  598. }
  599. descr <<= 1;
  600. ++ current_p;
  601. }
  602. continue;
  603. case GC_DS_PROC:
  604. mark_stack_top--;
  605. credit -= GC_PROC_BYTES;
  606. mark_stack_top =
  607. (*PROC(descr))
  608. (current_p, mark_stack_top,
  609. mark_stack_limit, ENV(descr));
  610. continue;
  611. case GC_DS_PER_OBJECT:
  612. if ((signed_word)descr >= 0) {
  613. /* Descriptor is in the object. */
  614. descr = *(word *)((ptr_t)current_p + descr - GC_DS_PER_OBJECT);
  615. } else {
  616. /* Descriptor is in type descriptor pointed to by first */
  617. /* word in object. */
  618. ptr_t type_descr = *(ptr_t *)current_p;
  619. /* type_descr is either a valid pointer to the descriptor */
  620. /* structure, or this object was on a free list. If it */
  621. /* it was anything but the last object on the free list, */
  622. /* we will misinterpret the next object on the free list as */
  623. /* the type descriptor, and get a 0 GC descriptor, which */
  624. /* is ideal. Unfortunately, we need to check for the last */
  625. /* object case explicitly. */
  626. if (0 == type_descr) {
  627. /* Rarely executed. */
  628. mark_stack_top--;
  629. continue;
  630. }
  631. descr = *(word *)(type_descr
  632. - (descr - (GC_DS_PER_OBJECT
  633. - GC_INDIR_PER_OBJ_BIAS)));
  634. }
  635. if (0 == descr) {
  636. /* Can happen either because we generated a 0 descriptor */
  637. /* or we saw a pointer to a free object. */
  638. mark_stack_top--;
  639. continue;
  640. }
  641. goto retry;
  642. }
  643. } else /* Small object with length descriptor */ {
  644. mark_stack_top--;
  645. limit = (word *)(((ptr_t)current_p) + (word)descr);
  646. }
  647. /* The simple case in which we're scanning a range. */
  648. GC_ASSERT(!((word)current_p & (ALIGNMENT-1)));
  649. credit -= (ptr_t)limit - (ptr_t)current_p;
  650. limit -= 1;
  651. {
  652. # define PREF_DIST 4
  653. # ifndef SMALL_CONFIG
  654. word deferred;
  655. /* Try to prefetch the next pointer to be examined asap. */
  656. /* Empirically, this also seems to help slightly without */
  657. /* prefetches, at least on linux/X86. Presumably this loop */
  658. /* ends up with less register pressure, and gcc thus ends up */
  659. /* generating slightly better code. Overall gcc code quality */
  660. /* for this loop is still not great. */
  661. for(;;) {
  662. PREFETCH((ptr_t)limit - PREF_DIST*CACHE_LINE_SIZE);
  663. GC_ASSERT(limit >= current_p);
  664. deferred = *limit;
  665. FIXUP_POINTER(deferred);
  666. limit = (word *)((char *)limit - ALIGNMENT);
  667. if ((ptr_t)deferred >= least_ha && (ptr_t)deferred < greatest_ha) {
  668. PREFETCH((ptr_t)deferred);
  669. break;
  670. }
  671. if (current_p > limit) goto next_object;
  672. /* Unroll once, so we don't do too many of the prefetches */
  673. /* based on limit. */
  674. deferred = *limit;
  675. FIXUP_POINTER(deferred);
  676. limit = (word *)((char *)limit - ALIGNMENT);
  677. if ((ptr_t)deferred >= least_ha && (ptr_t)deferred < greatest_ha) {
  678. PREFETCH((ptr_t)deferred);
  679. break;
  680. }
  681. if (current_p > limit) goto next_object;
  682. }
  683. # endif
  684. while (current_p <= limit) {
  685. /* Empirically, unrolling this loop doesn't help a lot. */
  686. /* Since HC_PUSH_CONTENTS expands to a lot of code, */
  687. /* we don't. */
  688. current = *current_p;
  689. FIXUP_POINTER(current);
  690. PREFETCH((ptr_t)current_p + PREF_DIST*CACHE_LINE_SIZE);
  691. if ((ptr_t)current >= least_ha && (ptr_t)current < greatest_ha) {
  692. /* Prefetch the contents of the object we just pushed. It's */
  693. /* likely we will need them soon. */
  694. PREFETCH((ptr_t)current);
  695. HC_PUSH_CONTENTS((ptr_t)current, mark_stack_top,
  696. mark_stack_limit, current_p, exit2);
  697. }
  698. current_p = (word *)((char *)current_p + ALIGNMENT);
  699. }
  700. # ifndef SMALL_CONFIG
  701. /* We still need to mark the entry we previously prefetched. */
  702. /* We alrady know that it passes the preliminary pointer */
  703. /* validity test. */
  704. HC_PUSH_CONTENTS((ptr_t)deferred, mark_stack_top,
  705. mark_stack_limit, current_p, exit4);
  706. next_object:;
  707. # endif
  708. }
  709. }
  710. return mark_stack_top;
  711. }
  712. #ifdef PARALLEL_MARK
  713. /* We assume we have an ANSI C Compiler. */
  714. GC_bool GC_help_wanted = FALSE;
  715. unsigned GC_helper_count = 0;
  716. unsigned GC_active_count = 0;
  717. mse * VOLATILE GC_first_nonempty;
  718. word GC_mark_no = 0;
  719. #define LOCAL_MARK_STACK_SIZE HBLKSIZE
  720. /* Under normal circumstances, this is big enough to guarantee */
  721. /* We don't overflow half of it in a single call to */
  722. /* GC_mark_from. */
  723. /* Steal mark stack entries starting at mse low into mark stack local */
  724. /* until we either steal mse high, or we have max entries. */
  725. /* Return a pointer to the top of the local mark stack. */
  726. /* *next is replaced by a pointer to the next unscanned mark stack */
  727. /* entry. */
  728. mse * GC_steal_mark_stack(mse * low, mse * high, mse * local,
  729. unsigned max, mse **next)
  730. {
  731. mse *p;
  732. mse *top = local - 1;
  733. unsigned i = 0;
  734. /* Make sure that prior writes to the mark stack are visible. */
  735. /* On some architectures, the fact that the reads are */
  736. /* volatile should suffice. */
  737. # if !defined(IA64) && !defined(HP_PA) && !defined(I386)
  738. GC_memory_barrier();
  739. # endif
  740. GC_ASSERT(high >= low-1 && high - low + 1 <= GC_mark_stack_size);
  741. for (p = low; p <= high && i <= max; ++p) {
  742. word descr = *(volatile word *) &(p -> mse_descr);
  743. /* In the IA64 memory model, the following volatile store is */
  744. /* ordered after this read of descr. Thus a thread must read */
  745. /* the original nonzero value. HP_PA appears to be similar, */
  746. /* and if I'm reading the P4 spec correctly, X86 is probably */
  747. /* also OK. In some other cases we need a barrier. */
  748. # if !defined(IA64) && !defined(HP_PA) && !defined(I386)
  749. GC_memory_barrier();
  750. # endif
  751. if (descr != 0) {
  752. *(volatile word *) &(p -> mse_descr) = 0;
  753. /* More than one thread may get this entry, but that's only */
  754. /* a minor performance problem. */
  755. ++top;
  756. top -> mse_descr = descr;
  757. top -> mse_start = p -> mse_start;
  758. GC_ASSERT( (top -> mse_descr & GC_DS_TAGS) != GC_DS_LENGTH ||
  759. top -> mse_descr < (ptr_t)GC_greatest_plausible_heap_addr
  760. - (ptr_t)GC_least_plausible_heap_addr);
  761. /* If this is a big object, count it as */
  762. /* size/256 + 1 objects. */
  763. ++i;
  764. if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) i += (descr >> 8);
  765. }
  766. }
  767. *next = p;
  768. return top;
  769. }
  770. /* Copy back a local mark stack. */
  771. /* low and high are inclusive bounds. */
  772. void GC_return_mark_stack(mse * low, mse * high)
  773. {
  774. mse * my_top;
  775. mse * my_start;
  776. size_t stack_size;
  777. if (high < low) return;
  778. stack_size = high - low + 1;
  779. GC_acquire_mark_lock();
  780. my_top = GC_mark_stack_top;
  781. my_start = my_top + 1;
  782. if (my_start - GC_mark_stack + stack_size > GC_mark_stack_size) {
  783. # ifdef CONDPRINT
  784. if (GC_print_stats) {
  785. GC_printf0("No room to copy back mark stack.");
  786. }
  787. # endif
  788. GC_mark_state = MS_INVALID;
  789. GC_mark_stack_too_small = TRUE;
  790. /* We drop the local mark stack. We'll fix things later. */
  791. } else {
  792. BCOPY(low, my_start, stack_size * sizeof(mse));
  793. GC_ASSERT(GC_mark_stack_top = my_top);
  794. # if !defined(IA64) && !defined(HP_PA)
  795. GC_memory_barrier();
  796. # endif
  797. /* On IA64, the volatile write acts as a release barrier. */
  798. GC_mark_stack_top = my_top + stack_size;
  799. }
  800. GC_release_mark_lock();
  801. GC_notify_all_marker();
  802. }
  803. /* Mark from the local mark stack. */
  804. /* On return, the local mark stack is empty. */
  805. /* But this may be achieved by copying the */
  806. /* local mark stack back into the global one. */
  807. void GC_do_local_mark(mse *local_mark_stack, mse *local_top)
  808. {
  809. unsigned n;
  810. # define N_LOCAL_ITERS 1
  811. # ifdef GC_ASSERTIONS
  812. /* Make sure we don't hold mark lock. */
  813. GC_acquire_mark_lock();
  814. GC_release_mark_lock();
  815. # endif
  816. for (;;) {
  817. for (n = 0; n < N_LOCAL_ITERS; ++n) {
  818. local_top = GC_mark_from(local_top, local_mark_stack,
  819. local_mark_stack + LOCAL_MARK_STACK_SIZE);
  820. if (local_top < local_mark_stack) return;
  821. if (local_top - local_mark_stack >= LOCAL_MARK_STACK_SIZE/2) {
  822. GC_return_mark_stack(local_mark_stack, local_top);
  823. return;
  824. }
  825. }
  826. if (GC_mark_stack_top < GC_first_nonempty &&
  827. GC_active_count < GC_helper_count
  828. && local_top > local_mark_stack + 1) {
  829. /* Try to share the load, since the main stack is empty, */
  830. /* and helper threads are waiting for a refill. */
  831. /* The entries near the bottom of the stack are likely */
  832. /* to require more work. Thus we return those, eventhough */
  833. /* it's harder. */
  834. mse * p;
  835. mse * new_bottom = local_mark_stack
  836. + (local_top - local_mark_stack)/2;
  837. GC_ASSERT(new_bottom > local_mark_stack
  838. && new_bottom < local_top);
  839. GC_return_mark_stack(local_mark_stack, new_bottom - 1);
  840. memmove(local_mark_stack, new_bottom,
  841. (local_top - new_bottom + 1) * sizeof(mse));
  842. local_top -= (new_bottom - local_mark_stack);
  843. }
  844. }
  845. }
  846. #define ENTRIES_TO_GET 5
  847. long GC_markers = 2; /* Normally changed by thread-library- */
  848. /* -specific code. */
  849. /* Mark using the local mark stack until the global mark stack is empty */
  850. /* and there are no active workers. Update GC_first_nonempty to reflect */
  851. /* progress. */
  852. /* Caller does not hold mark lock. */
  853. /* Caller has already incremented GC_helper_count. We decrement it, */
  854. /* and maintain GC_active_count. */
  855. void GC_mark_local(mse *local_mark_stack, int id)
  856. {
  857. mse * my_first_nonempty;
  858. GC_acquire_mark_lock();
  859. GC_active_count++;
  860. my_first_nonempty = GC_first_nonempty;
  861. GC_ASSERT(GC_first_nonempty >= GC_mark_stack &&
  862. GC_first_nonempty <= GC_mark_stack_top + 1);
  863. # ifdef PRINTSTATS
  864. GC_printf1("Starting mark helper %lu\n", (unsigned long)id);
  865. # endif
  866. GC_release_mark_lock();
  867. for (;;) {
  868. size_t n_on_stack;
  869. size_t n_to_get;
  870. mse *next;
  871. mse * my_top;
  872. mse * local_top;
  873. mse * global_first_nonempty = GC_first_nonempty;
  874. GC_ASSERT(my_first_nonempty >= GC_mark_stack &&
  875. my_first_nonempty <= GC_mark_stack_top + 1);
  876. GC_ASSERT(global_first_nonempty >= GC_mark_stack &&
  877. global_first_nonempty <= GC_mark_stack_top + 1);
  878. if (my_first_nonempty < global_first_nonempty) {
  879. my_first_nonempty = global_first_nonempty;
  880. } else if (global_first_nonempty < my_first_nonempty) {
  881. GC_compare_and_exchange((word *)(&GC_first_nonempty),
  882. (word) global_first_nonempty,
  883. (word) my_first_nonempty);
  884. /* If this fails, we just go ahead, without updating */
  885. /* GC_first_nonempty. */
  886. }
  887. /* Perhaps we should also update GC_first_nonempty, if it */
  888. /* is less. But that would require using atomic updates. */
  889. my_top = GC_mark_stack_top;
  890. n_on_stack = my_top - my_first_nonempty + 1;
  891. if (0 == n_on_stack) {
  892. GC_acquire_mark_lock();
  893. my_top = GC_mark_stack_top;
  894. n_on_stack = my_top - my_first_nonempty + 1;
  895. if (0 == n_on_stack) {
  896. GC_active_count--;
  897. GC_ASSERT(GC_active_count <= GC_helper_count);
  898. /* Other markers may redeposit objects */
  899. /* on the stack. */
  900. if (0 == GC_active_count) GC_notify_all_marker();
  901. while (GC_active_count > 0
  902. && GC_first_nonempty > GC_mark_stack_top) {
  903. /* We will be notified if either GC_active_count */
  904. /* reaches zero, or if more objects are pushed on */
  905. /* the global mark stack. */
  906. GC_wait_marker();
  907. }
  908. if (GC_active_count == 0 &&
  909. GC_first_nonempty > GC_mark_stack_top) {
  910. GC_bool need_to_notify = FALSE;
  911. /* The above conditions can't be falsified while we */
  912. /* hold the mark lock, since neither */
  913. /* GC_active_count nor GC_mark_stack_top can */
  914. /* change. GC_first_nonempty can only be */
  915. /* incremented asynchronously. Thus we know that */
  916. /* both conditions actually held simultaneously. */
  917. GC_helper_count--;
  918. if (0 == GC_helper_count) need_to_notify = TRUE;
  919. # ifdef PRINTSTATS
  920. GC_printf1(
  921. "Finished mark helper %lu\n", (unsigned long)id);
  922. # endif
  923. GC_release_mark_lock();
  924. if (need_to_notify) GC_notify_all_marker();
  925. return;
  926. }
  927. /* else there's something on the stack again, or */
  928. /* another helper may push something. */
  929. GC_active_count++;
  930. GC_ASSERT(GC_active_count > 0);
  931. GC_release_mark_lock();
  932. continue;
  933. } else {
  934. GC_release_mark_lock();
  935. }
  936. }
  937. n_to_get = ENTRIES_TO_GET;
  938. if (n_on_stack < 2 * ENTRIES_TO_GET) n_to_get = 1;
  939. local_top = GC_steal_mark_stack(my_first_nonempty, my_top,
  940. local_mark_stack, n_to_get,
  941. &my_first_nonempty);
  942. GC_ASSERT(my_first_nonempty >= GC_mark_stack &&
  943. my_first_nonempty <= GC_mark_stack_top + 1);
  944. GC_do_local_mark(local_mark_stack, local_top);
  945. }
  946. }
  947. /* Perform Parallel mark. */
  948. /* We hold the GC lock, not the mark lock. */
  949. /* Currently runs until the mark stack is */
  950. /* empty. */
  951. void GC_do_parallel_mark()
  952. {
  953. mse local_mark_stack[LOCAL_MARK_STACK_SIZE];
  954. mse * local_top;
  955. mse * my_top;
  956. GC_acquire_mark_lock();
  957. GC_ASSERT(I_HOLD_LOCK());
  958. /* This could be a GC_ASSERT, but it seems safer to keep it on */
  959. /* all the time, especially since it's cheap. */
  960. if (GC_help_wanted || GC_active_count != 0 || GC_helper_count != 0)
  961. ABORT("Tried to start parallel mark in bad state");
  962. # ifdef PRINTSTATS
  963. GC_printf1("Starting marking for mark phase number %lu\n",
  964. (unsigned long)GC_mark_no);
  965. # endif
  966. GC_first_nonempty = GC_mark_stack;
  967. GC_active_count = 0;
  968. GC_helper_count = 1;
  969. GC_help_wanted = TRUE;
  970. GC_release_mark_lock();
  971. GC_notify_all_marker();
  972. /* Wake up potential helpers. */
  973. GC_mark_local(local_mark_stack, 0);
  974. GC_acquire_mark_lock();
  975. GC_help_wanted = FALSE;
  976. /* Done; clean up. */
  977. while (GC_helper_count > 0) GC_wait_marker();
  978. /* GC_helper_count cannot be incremented while GC_help_wanted == FALSE */
  979. # ifdef PRINTSTATS
  980. GC_printf1(
  981. "Finished marking for mark phase number %lu\n",
  982. (unsigned long)GC_mark_no);
  983. # endif
  984. GC_mark_no++;
  985. GC_release_mark_lock();
  986. GC_notify_all_marker();
  987. }
  988. /* Try to help out the marker, if it's running. */
  989. /* We do not hold the GC lock, but the requestor does. */
  990. void GC_help_marker(word my_mark_no)
  991. {
  992. mse local_mark_stack[LOCAL_MARK_STACK_SIZE];
  993. unsigned my_id;
  994. mse * my_first_nonempty;
  995. if (!GC_parallel) return;
  996. GC_acquire_mark_lock();
  997. while (GC_mark_no < my_mark_no
  998. || !GC_help_wanted && GC_mark_no == my_mark_no) {
  999. GC_wait_marker();
  1000. }
  1001. my_id = GC_helper_count;
  1002. if (GC_mark_no != my_mark_no || my_id >= GC_markers) {
  1003. /* Second test is useful only if original threads can also */
  1004. /* act as helpers. Under Linux they can't. */
  1005. GC_release_mark_lock();
  1006. return;
  1007. }
  1008. GC_helper_count = my_id + 1;
  1009. GC_release_mark_lock();
  1010. GC_mark_local(local_mark_stack, my_id);
  1011. /* GC_mark_local decrements GC_helper_count. */
  1012. }
  1013. #endif /* PARALLEL_MARK */
  1014. /* Allocate or reallocate space for mark stack of size s words */
  1015. /* May silently fail. */
  1016. static void alloc_mark_stack(n)
  1017. word n;
  1018. {
  1019. mse * new_stack = (mse *)GC_scratch_alloc(n * sizeof(struct GC_ms_entry));
  1020. GC_mark_stack_too_small = FALSE;
  1021. if (GC_mark_stack_size != 0) {
  1022. if (new_stack != 0) {
  1023. word displ = (word)GC_mark_stack & (GC_page_size - 1);
  1024. signed_word size = GC_mark_stack_size * sizeof(struct GC_ms_entry);
  1025. /* Recycle old space */
  1026. if (0 != displ) displ = GC_page_size - displ;
  1027. size = (size - displ) & ~(GC_page_size - 1);
  1028. if (size > 0) {
  1029. GC_add_to_heap((struct hblk *)
  1030. ((word)GC_mark_stack + displ), (word)size);
  1031. }
  1032. GC_mark_stack = new_stack;
  1033. GC_mark_stack_size = n;
  1034. GC_mark_stack_limit = new_stack + n;
  1035. # ifdef CONDPRINT
  1036. if (GC_print_stats) {
  1037. GC_printf1("Grew mark stack to %lu frames\n",
  1038. (unsigned long) GC_mark_stack_size);
  1039. }
  1040. # endif
  1041. } else {
  1042. # ifdef CONDPRINT
  1043. if (GC_print_stats) {
  1044. GC_printf1("Failed to grow mark stack to %lu frames\n",
  1045. (unsigned long) n);
  1046. }
  1047. # endif
  1048. }
  1049. } else {
  1050. if (new_stack == 0) {
  1051. GC_err_printf0("No space for mark stack\n");
  1052. EXIT();
  1053. }
  1054. GC_mark_stack = new_stack;
  1055. GC_mark_stack_size = n;
  1056. GC_mark_stack_limit = new_stack + n;
  1057. }
  1058. GC_mark_stack_top = GC_mark_stack-1;
  1059. }
  1060. void GC_mark_init()
  1061. {
  1062. alloc_mark_stack(INITIAL_MARK_STACK_SIZE);
  1063. }
  1064. /*
  1065. * Push all locations between b and t onto the mark stack.
  1066. * b is the first location to be checked. t is one past the last
  1067. * location to be checked.
  1068. * Should only be used if there is no possibility of mark stack
  1069. * overflow.
  1070. */
  1071. void GC_push_all(bottom, top)
  1072. ptr_t bottom;
  1073. ptr_t top;
  1074. {
  1075. register word length;
  1076. bottom = (ptr_t)(((word) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
  1077. top = (ptr_t)(((word) top) & ~(ALIGNMENT-1));
  1078. if (top == 0 || bottom == top) return;
  1079. GC_mark_stack_top++;
  1080. if (GC_mark_stack_top >= GC_mark_stack_limit) {
  1081. ABORT("unexpected mark stack overflow");
  1082. }
  1083. length = top - bottom;
  1084. # if GC_DS_TAGS > ALIGNMENT - 1
  1085. length += GC_DS_TAGS;
  1086. length &= ~GC_DS_TAGS;
  1087. # endif
  1088. GC_mark_stack_top -> mse_start = (word *)bottom;
  1089. GC_mark_stack_top -> mse_descr = length;
  1090. }
  1091. /*
  1092. * Analogous to the above, but push only those pages h with dirty_fn(h) != 0.
  1093. * We use push_fn to actually push the block.
  1094. * Used both to selectively push dirty pages, or to push a block
  1095. * in piecemeal fashion, to allow for more marking concurrency.
  1096. * Will not overflow mark stack if push_fn pushes a small fixed number
  1097. * of entries. (This is invoked only if push_fn pushes a single entry,
  1098. * or if it marks each object before pushing it, thus ensuring progress
  1099. * in the event of a stack overflow.)
  1100. */
  1101. void GC_push_selected(bottom, top, dirty_fn, push_fn)
  1102. ptr_t bottom;
  1103. ptr_t top;
  1104. int (*dirty_fn) GC_PROTO((struct hblk * h));
  1105. void (*push_fn) GC_PROTO((ptr_t bottom, ptr_t top));
  1106. {
  1107. register struct hblk * h;
  1108. bottom = (ptr_t)(((long) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
  1109. top = (ptr_t)(((long) top) & ~(ALIGNMENT-1));
  1110. if (top == 0 || bottom == top) return;
  1111. h = HBLKPTR(bottom + HBLKSIZE);
  1112. if (top <= (ptr_t) h) {
  1113. if ((*dirty_fn)(h-1)) {
  1114. (*push_fn)(bottom, top);
  1115. }
  1116. return;
  1117. }
  1118. if ((*dirty_fn)(h-1)) {
  1119. (*push_fn)(bottom, (ptr_t)h);
  1120. }
  1121. while ((ptr_t)(h+1) <= top) {
  1122. if ((*dirty_fn)(h)) {
  1123. if ((word)(GC_mark_stack_top - GC_mark_stack)
  1124. > 3 * GC_mark_stack_size / 4) {
  1125. /* Danger of mark stack overflow */
  1126. (*push_fn)((ptr_t)h, top);
  1127. return;
  1128. } else {
  1129. (*push_fn)((ptr_t)h, (ptr_t)(h+1));
  1130. }
  1131. }
  1132. h++;
  1133. }
  1134. if ((ptr_t)h != top) {
  1135. if ((*dirty_fn)(h)) {
  1136. (*push_fn)((ptr_t)h, top);
  1137. }
  1138. }
  1139. if (GC_mark_stack_top >= GC_mark_stack_limit) {
  1140. ABORT("unexpected mark stack overflow");
  1141. }
  1142. }
  1143. # ifndef SMALL_CONFIG
  1144. #ifdef PARALLEL_MARK
  1145. /* Break up root sections into page size chunks to better spread */
  1146. /* out work. */
  1147. GC_bool GC_true_func(struct hblk *h) { return TRUE; }
  1148. # define GC_PUSH_ALL(b,t) GC_push_selected(b,t,GC_true_func,GC_push_all);
  1149. #else
  1150. # define GC_PUSH_ALL(b,t) GC_push_all(b,t);
  1151. #endif
  1152. void GC_push_conditional(bottom, top, all)
  1153. ptr_t bottom;
  1154. ptr_t top;
  1155. int all;
  1156. {
  1157. if (all) {
  1158. if (GC_dirty_maintained) {
  1159. # ifdef PROC_VDB
  1160. /* Pages that were never dirtied cannot contain pointers */
  1161. GC_push_selected(bottom, top, GC_page_was_ever_dirty, GC_push_all);
  1162. # else
  1163. GC_push_all(bottom, top);
  1164. # endif
  1165. } else {
  1166. GC_push_all(bottom, top);
  1167. }
  1168. } else {
  1169. GC_push_selected(bottom, top, GC_page_was_dirty, GC_push_all);
  1170. }
  1171. }
  1172. #endif
  1173. # if defined(MSWIN32) || defined(MSWINCE)
  1174. void __cdecl GC_push_one(p)
  1175. # else
  1176. void GC_push_one(p)
  1177. # endif
  1178. word p;
  1179. {
  1180. GC_PUSH_ONE_STACK(p, MARKED_FROM_REGISTER);
  1181. }
  1182. struct GC_ms_entry *GC_mark_and_push(obj, mark_stack_ptr, mark_stack_limit, src)
  1183. GC_PTR obj;
  1184. struct GC_ms_entry * mark_stack_ptr;
  1185. struct GC_ms_entry * mark_stack_limit;
  1186. GC_PTR *src;
  1187. {
  1188. PREFETCH(obj);
  1189. PUSH_CONTENTS(obj, mark_stack_ptr /* modified */, mark_stack_limit, src,
  1190. was_marked /* internally generated exit label */);
  1191. return mark_stack_ptr;
  1192. }
  1193. # ifdef __STDC__
  1194. # define BASE(p) (word)GC_base((void *)(p))
  1195. # else
  1196. # define BASE(p) (word)GC_base((char *)(p))
  1197. # endif
  1198. /* Mark and push (i.e. gray) a single object p onto the main */
  1199. /* mark stack. Consider p to be valid if it is an interior */
  1200. /* pointer. */
  1201. /* The object p has passed a preliminary pointer validity */
  1202. /* test, but we do not definitely know whether it is valid. */
  1203. /* Mark bits are NOT atomically updated. Thus this must be the */
  1204. /* only thread setting them. */
  1205. # if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
  1206. void GC_mark_and_push_stack(p, source)
  1207. ptr_t source;
  1208. # else
  1209. void GC_mark_and_push_stack(p)
  1210. # define source 0
  1211. # endif
  1212. register word p;
  1213. {
  1214. register word r;
  1215. register hdr * hhdr;
  1216. register int displ;
  1217. GET_HDR(p, hhdr);
  1218. if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
  1219. if (hhdr != 0) {
  1220. r = BASE(p);
  1221. hhdr = HDR(r);
  1222. displ = BYTES_TO_WORDS(HBLKDISPL(r));
  1223. }
  1224. } else {
  1225. register map_entry_type map_entry;
  1226. displ = HBLKDISPL(p);
  1227. map_entry = MAP_ENTRY((hhdr -> hb_map), displ);
  1228. if (map_entry >= MAX_OFFSET) {
  1229. if (map_entry == OFFSET_TOO_BIG || !GC_all_interior_pointers) {
  1230. r = BASE(p);
  1231. displ = BYTES_TO_WORDS(HBLKDISPL(r));
  1232. if (r == 0) hhdr = 0;
  1233. } else {
  1234. /* Offset invalid, but map reflects interior pointers */
  1235. hhdr = 0;
  1236. }
  1237. } else {
  1238. displ = BYTES_TO_WORDS(displ);
  1239. displ -= map_entry;
  1240. r = (word)((word *)(HBLKPTR(p)) + displ);
  1241. }
  1242. }
  1243. /* If hhdr != 0 then r == GC_base(p), only we did it faster. */
  1244. /* displ is the word index within the block. */
  1245. if (hhdr == 0) {
  1246. # ifdef PRINT_BLACK_LIST
  1247. GC_add_to_black_list_stack(p, source);
  1248. # else
  1249. GC_add_to_black_list_stack(p);
  1250. # endif
  1251. # undef source /* In case we had to define it. */
  1252. } else {
  1253. if (!mark_bit_from_hdr(hhdr, displ)) {
  1254. set_mark_bit_from_hdr(hhdr, displ);
  1255. GC_STORE_BACK_PTR(source, (ptr_t)r);
  1256. PUSH_OBJ((word *)r, hhdr, GC_mark_stack_top,
  1257. GC_mark_stack_limit);
  1258. }
  1259. }
  1260. }
  1261. # ifdef TRACE_BUF
  1262. # define TRACE_ENTRIES 1000
  1263. struct trace_entry {
  1264. char * kind;
  1265. word gc_no;
  1266. word words_allocd;
  1267. word arg1;
  1268. word arg2;
  1269. } GC_trace_buf[TRACE_ENTRIES];
  1270. int GC_trace_buf_ptr = 0;
  1271. void GC_add_trace_entry(char *kind, word arg1, word arg2)
  1272. {
  1273. GC_trace_buf[GC_trace_buf_ptr].kind = kind;
  1274. GC_trace_buf[GC_trace_buf_ptr].gc_no = GC_gc_no;
  1275. GC_trace_buf[GC_trace_buf_ptr].words_allocd = GC_words_allocd;
  1276. GC_trace_buf[GC_trace_buf_ptr].arg1 = arg1 ^ 0x80000000;
  1277. GC_trace_buf[GC_trace_buf_ptr].arg2 = arg2 ^ 0x80000000;
  1278. GC_trace_buf_ptr++;
  1279. if (GC_trace_buf_ptr >= TRACE_ENTRIES) GC_trace_buf_ptr = 0;
  1280. }
  1281. void GC_print_trace(word gc_no, GC_bool lock)
  1282. {
  1283. int i;
  1284. struct trace_entry *p;
  1285. if (lock) LOCK();
  1286. for (i = GC_trace_buf_ptr-1; i != GC_trace_buf_ptr; i--) {
  1287. if (i < 0) i = TRACE_ENTRIES-1;
  1288. p = GC_trace_buf + i;
  1289. if (p -> gc_no < gc_no || p -> kind == 0) return;
  1290. printf("Trace:%s (gc:%d,words:%d) 0x%X, 0x%X\n",
  1291. p -> kind, p -> gc_no, p -> words_allocd,
  1292. (p -> arg1) ^ 0x80000000, (p -> arg2) ^ 0x80000000);
  1293. }
  1294. printf("Trace incomplete\n");
  1295. if (lock) UNLOCK();
  1296. }
  1297. # endif /* TRACE_BUF */
  1298. /*
  1299. * A version of GC_push_all that treats all interior pointers as valid
  1300. * and scans the entire region immediately, in case the contents
  1301. * change.
  1302. */
  1303. void GC_push_all_eager(bottom, top)
  1304. ptr_t bottom;
  1305. ptr_t top;
  1306. {
  1307. word * b = (word *)(((word) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
  1308. word * t = (word *)(((word) top) & ~(ALIGNMENT-1));
  1309. register word *p;
  1310. register word q;
  1311. register word *lim;
  1312. register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
  1313. register ptr_t least_ha = GC_least_plausible_heap_addr;
  1314. # define GC_greatest_plausible_heap_addr greatest_ha
  1315. # define GC_least_plausible_heap_addr least_ha
  1316. if (top == 0) return;
  1317. /* check all pointers in range and push if they appear */
  1318. /* to be valid. */
  1319. lim = t - 1 /* longword */;
  1320. for (p = b; p <= lim; p = (word *)(((char *)p) + ALIGNMENT)) {
  1321. q = *p;
  1322. GC_PUSH_ONE_STACK(q, p);
  1323. }
  1324. # undef GC_greatest_plausible_heap_addr
  1325. # undef GC_least_plausible_heap_addr
  1326. }
  1327. #ifndef THREADS
  1328. /*
  1329. * A version of GC_push_all that treats all interior pointers as valid
  1330. * and scans part of the area immediately, to make sure that saved
  1331. * register values are not lost.
  1332. * Cold_gc_frame delimits the stack section that must be scanned
  1333. * eagerly. A zero value indicates that no eager scanning is needed.
  1334. */
  1335. void GC_push_all_stack_partially_eager(bottom, top, cold_gc_frame)
  1336. ptr_t bottom;
  1337. ptr_t top;
  1338. ptr_t cold_gc_frame;
  1339. {
  1340. if (!NEED_FIXUP_POINTER && GC_all_interior_pointers) {
  1341. # define EAGER_BYTES 1024
  1342. /* Push the hot end of the stack eagerly, so that register values */
  1343. /* saved inside GC frames are marked before they disappear. */
  1344. /* The rest of the marking can be deferred until later. */
  1345. if (0 == cold_gc_frame) {
  1346. GC_push_all_stack(bottom, top);
  1347. return;
  1348. }
  1349. GC_ASSERT(bottom <= cold_gc_frame && cold_gc_frame <= top);
  1350. # ifdef STACK_GROWS_DOWN
  1351. GC_push_all(cold_gc_frame - sizeof(ptr_t), top);
  1352. GC_push_all_eager(bottom, cold_gc_frame);
  1353. # else /* STACK_GROWS_UP */
  1354. GC_push_all(bottom, cold_gc_frame + sizeof(ptr_t));
  1355. GC_push_all_eager(cold_gc_frame, top);
  1356. # endif /* STACK_GROWS_UP */
  1357. } else {
  1358. GC_push_all_eager(bottom, top);
  1359. }
  1360. # ifdef TRACE_BUF
  1361. GC_add_trace_entry("GC_push_all_stack", bottom, top);
  1362. # endif
  1363. }
  1364. #endif /* !THREADS */
  1365. void GC_push_all_stack(bottom, top)
  1366. ptr_t bottom;
  1367. ptr_t top;
  1368. {
  1369. if (!NEED_FIXUP_POINTER && GC_all_interior_pointers) {
  1370. GC_push_all(bottom, top);
  1371. } else {
  1372. GC_push_all_eager(bottom, top);
  1373. }
  1374. }
  1375. #if !defined(SMALL_CONFIG) && !defined(USE_MARK_BYTES)
  1376. /* Push all objects reachable from marked objects in the given block */
  1377. /* of size 1 objects. */
  1378. void GC_push_marked1(h, hhdr)
  1379. struct hblk *h;
  1380. register hdr * hhdr;
  1381. {
  1382. word * mark_word_addr = &(hhdr->hb_marks[0]);
  1383. register word *p;
  1384. word *plim;
  1385. register int i;
  1386. register word q;
  1387. register word mark_word;
  1388. register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
  1389. register ptr_t least_ha = GC_least_plausible_heap_addr;
  1390. register mse * mark_stack_top = GC_mark_stack_top;
  1391. register mse * mark_stack_limit = GC_mark_stack_limit;
  1392. # define GC_mark_stack_top mark_stack_top
  1393. # define GC_mark_stack_limit mark_stack_limit
  1394. # define GC_greatest_plausible_heap_addr greatest_ha
  1395. # define GC_least_plausible_heap_addr least_ha
  1396. p = (word *)(h->hb_body);
  1397. plim = (word *)(((word)h) + HBLKSIZE);
  1398. /* go through all words in block */
  1399. while( p < plim ) {
  1400. mark_word = *mark_word_addr++;
  1401. i = 0;
  1402. while(mark_word != 0) {
  1403. if (mark_word & 1) {
  1404. q = p[i];
  1405. GC_PUSH_ONE_HEAP(q, p + i);
  1406. }
  1407. i++;
  1408. mark_word >>= 1;
  1409. }
  1410. p += WORDSZ;
  1411. }
  1412. # undef GC_greatest_plausible_heap_addr
  1413. # undef GC_least_plausible_heap_addr
  1414. # undef GC_mark_stack_top
  1415. # undef GC_mark_stack_limit
  1416. GC_mark_stack_top = mark_stack_top;
  1417. }
  1418. #ifndef UNALIGNED
  1419. /* Push all objects reachable from marked objects in the given block */
  1420. /* of size 2 objects. */
  1421. void GC_push_marked2(h, hhdr)
  1422. struct hblk *h;
  1423. register hdr * hhdr;
  1424. {
  1425. word * mark_word_addr = &(hhdr->hb_marks[0]);
  1426. register word *p;
  1427. word *plim;
  1428. register int i;
  1429. register word q;
  1430. register word mark_word;
  1431. register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
  1432. register ptr_t least_ha = GC_least_plausible_heap_addr;
  1433. register mse * mark_stack_top = GC_mark_stack_top;
  1434. register mse * mark_stack_limit = GC_mark_stack_limit;
  1435. # define GC_mark_stack_top mark_stack_top
  1436. # define GC_mark_stack_limit mark_stack_limit
  1437. # define GC_greatest_plausible_heap_addr greatest_ha
  1438. # define GC_least_plausible_heap_addr least_ha
  1439. p = (word *)(h->hb_body);
  1440. plim = (word *)(((word)h) + HBLKSIZE);
  1441. /* go through all words in block */
  1442. while( p < plim ) {
  1443. mark_word = *mark_word_addr++;
  1444. i = 0;
  1445. while(mark_word != 0) {
  1446. if (mark_word & 1) {
  1447. q = p[i];
  1448. GC_PUSH_ONE_HEAP(q, p + i);
  1449. q = p[i+1];
  1450. GC_PUSH_ONE_HEAP(q, p + i);
  1451. }
  1452. i += 2;
  1453. mark_word >>= 2;
  1454. }
  1455. p += WORDSZ;
  1456. }
  1457. # undef GC_greatest_plausible_heap_addr
  1458. # undef GC_least_plausible_heap_addr
  1459. # undef GC_mark_stack_top
  1460. # undef GC_mark_stack_limit
  1461. GC_mark_stack_top = mark_stack_top;
  1462. }
  1463. /* Push all objects reachable from marked objects in the given block */
  1464. /* of size 4 objects. */
  1465. /* There is a risk of mark stack overflow here. But we handle that. */
  1466. /* And only unmarked objects get pushed, so it's not very likely. */
  1467. void GC_push_marked4(h, hhdr)
  1468. struct hblk *h;
  1469. register hdr * hhdr;
  1470. {
  1471. word * mark_word_addr = &(hhdr->hb_marks[0]);
  1472. register word *p;
  1473. word *plim;
  1474. register int i;
  1475. register word q;
  1476. register word mark_word;
  1477. register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
  1478. register ptr_t least_ha = GC_least_plausible_heap_addr;
  1479. register mse * mark_stack_top = GC_mark_stack_top;
  1480. register mse * mark_stack_limit = GC_mark_stack_limit;
  1481. # define GC_mark_stack_top mark_stack_top
  1482. # define GC_mark_stack_limit mark_stack_limit
  1483. # define GC_greatest_plausible_heap_addr greatest_ha
  1484. # define GC_least_plausible_heap_addr least_ha
  1485. p = (word *)(h->hb_body);
  1486. plim = (word *)(((word)h) + HBLKSIZE);
  1487. /* go through all words in block */
  1488. while( p < plim ) {
  1489. mark_word = *mark_word_addr++;
  1490. i = 0;
  1491. while(mark_word != 0) {
  1492. if (mark_word & 1) {
  1493. q = p[i];
  1494. GC_PUSH_ONE_HEAP(q, p + i);
  1495. q = p[i+1];
  1496. GC_PUSH_ONE_HEAP(q, p + i + 1);
  1497. q = p[i+2];
  1498. GC_PUSH_ONE_HEAP(q, p + i + 2);
  1499. q = p[i+3];
  1500. GC_PUSH_ONE_HEAP(q, p + i + 3);
  1501. }
  1502. i += 4;
  1503. mark_word >>= 4;
  1504. }
  1505. p += WORDSZ;
  1506. }
  1507. # undef GC_greatest_plausible_heap_addr
  1508. # undef GC_least_plausible_heap_addr
  1509. # undef GC_mark_stack_top
  1510. # undef GC_mark_stack_limit
  1511. GC_mark_stack_top = mark_stack_top;
  1512. }
  1513. #endif /* UNALIGNED */
  1514. #endif /* SMALL_CONFIG */
  1515. /* Push all objects reachable from marked objects in the given block */
  1516. void GC_push_marked(h, hhdr)
  1517. struct hblk *h;
  1518. register hdr * hhdr;
  1519. {
  1520. register int sz = hhdr -> hb_sz;
  1521. register int descr = hhdr -> hb_descr;
  1522. register word * p;
  1523. register int word_no;
  1524. register word * lim;
  1525. register mse * GC_mark_stack_top_reg;
  1526. register mse * mark_stack_limit = GC_mark_stack_limit;
  1527. /* Some quick shortcuts: */
  1528. if ((0 | GC_DS_LENGTH) == descr) return;
  1529. if (GC_block_empty(hhdr)/* nothing marked */) return;
  1530. GC_n_rescuing_pages++;
  1531. GC_objects_are_marked = TRUE;
  1532. if (sz > MAXOBJSZ) {
  1533. lim = (word *)h;
  1534. } else {
  1535. lim = (word *)(h + 1) - sz;
  1536. }
  1537. switch(sz) {
  1538. # if !defined(SMALL_CONFIG) && !defined(USE_MARK_BYTES)
  1539. case 1:
  1540. GC_push_marked1(h, hhdr);
  1541. break;
  1542. # endif
  1543. # if !defined(SMALL_CONFIG) && !defined(UNALIGNED) && \
  1544. !defined(USE_MARK_BYTES)
  1545. case 2:
  1546. GC_push_marked2(h, hhdr);
  1547. break;
  1548. case 4:
  1549. GC_push_marked4(h, hhdr);
  1550. break;
  1551. # endif
  1552. default:
  1553. GC_mark_stack_top_reg = GC_mark_stack_top;
  1554. for (p = (word *)h, word_no = 0; p <= lim; p += sz, word_no += sz) {
  1555. if (mark_bit_from_hdr(hhdr, word_no)) {
  1556. /* Mark from fields inside the object */
  1557. PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top_reg, mark_stack_limit);
  1558. # ifdef GATHERSTATS
  1559. /* Subtract this object from total, since it was */
  1560. /* added in twice. */
  1561. GC_composite_in_use -= sz;
  1562. # endif
  1563. }
  1564. }
  1565. GC_mark_stack_top = GC_mark_stack_top_reg;
  1566. }
  1567. }
  1568. #ifndef SMALL_CONFIG
  1569. /* Test whether any page in the given block is dirty */
  1570. GC_bool GC_block_was_dirty(h, hhdr)
  1571. struct hblk *h;
  1572. register hdr * hhdr;
  1573. {
  1574. register int sz = hhdr -> hb_sz;
  1575. if (sz <= MAXOBJSZ) {
  1576. return(GC_page_was_dirty(h));
  1577. } else {
  1578. register ptr_t p = (ptr_t)h;
  1579. sz = WORDS_TO_BYTES(sz);
  1580. while (p < (ptr_t)h + sz) {
  1581. if (GC_page_was_dirty((struct hblk *)p)) return(TRUE);
  1582. p += HBLKSIZE;
  1583. }
  1584. return(FALSE);
  1585. }
  1586. }
  1587. #endif /* SMALL_CONFIG */
  1588. /* Similar to GC_push_next_marked, but return address of next block */
  1589. struct hblk * GC_push_next_marked(h)
  1590. struct hblk *h;
  1591. {
  1592. register hdr * hhdr;
  1593. h = GC_next_used_block(h);
  1594. if (h == 0) return(0);
  1595. hhdr = HDR(h);
  1596. GC_push_marked(h, hhdr);
  1597. return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
  1598. }
  1599. #ifndef SMALL_CONFIG
  1600. /* Identical to above, but mark only from dirty pages */
  1601. struct hblk * GC_push_next_marked_dirty(h)
  1602. struct hblk *h;
  1603. {
  1604. register hdr * hhdr;
  1605. if (!GC_dirty_maintained) { ABORT("dirty bits not set up"); }
  1606. for (;;) {
  1607. h = GC_next_used_block(h);
  1608. if (h == 0) return(0);
  1609. hhdr = HDR(h);
  1610. # ifdef STUBBORN_ALLOC
  1611. if (hhdr -> hb_obj_kind == STUBBORN) {
  1612. if (GC_page_was_changed(h) && GC_block_was_dirty(h, hhdr)) {
  1613. break;
  1614. }
  1615. } else {
  1616. if (GC_block_was_dirty(h, hhdr)) break;
  1617. }
  1618. # else
  1619. if (GC_block_was_dirty(h, hhdr)) break;
  1620. # endif
  1621. h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
  1622. }
  1623. GC_push_marked(h, hhdr);
  1624. return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
  1625. }
  1626. #endif
  1627. /* Similar to above, but for uncollectable pages. Needed since we */
  1628. /* do not clear marks for such pages, even for full collections. */
  1629. struct hblk * GC_push_next_marked_uncollectable(h)
  1630. struct hblk *h;
  1631. {
  1632. register hdr * hhdr = HDR(h);
  1633. for (;;) {
  1634. h = GC_next_used_block(h);
  1635. if (h == 0) return(0);
  1636. hhdr = HDR(h);
  1637. if (hhdr -> hb_obj_kind == UNCOLLECTABLE) break;
  1638. h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
  1639. }
  1640. GC_push_marked(h, hhdr);
  1641. return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
  1642. }