alloc.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3. * Copyright (c) 1991-1996 by Xerox Corporation. All rights reserved.
  4. * Copyright (c) 1998 by Silicon Graphics. All rights reserved.
  5. * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
  6. *
  7. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  8. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  9. *
  10. * Permission is hereby granted to use or copy this program
  11. * for any purpose, provided the above notices are retained on all copies.
  12. * Permission to modify the code and to distribute modified code is granted,
  13. * provided the above notices are retained, and a notice that the code was
  14. * modified is included with the above copyright notice.
  15. *
  16. */
  17. # include "private/gc_priv.h"
  18. # include <stdio.h>
  19. # if !defined(MACOS) && !defined(MSWINCE)
  20. # include <signal.h>
  21. # include <sys/types.h>
  22. # endif
  23. /*
  24. * Separate free lists are maintained for different sized objects
  25. * up to MAXOBJSZ.
  26. * The call GC_allocobj(i,k) ensures that the freelist for
  27. * kind k objects of size i points to a non-empty
  28. * free list. It returns a pointer to the first entry on the free list.
  29. * In a single-threaded world, GC_allocobj may be called to allocate
  30. * an object of (small) size i as follows:
  31. *
  32. * opp = &(GC_objfreelist[i]);
  33. * if (*opp == 0) GC_allocobj(i, NORMAL);
  34. * ptr = *opp;
  35. * *opp = obj_link(ptr);
  36. *
  37. * Note that this is very fast if the free list is non-empty; it should
  38. * only involve the execution of 4 or 5 simple instructions.
  39. * All composite objects on freelists are cleared, except for
  40. * their first word.
  41. */
  42. /*
  43. * The allocator uses GC_allochblk to allocate large chunks of objects.
  44. * These chunks all start on addresses which are multiples of
  45. * HBLKSZ. Each allocated chunk has an associated header,
  46. * which can be located quickly based on the address of the chunk.
  47. * (See headers.c for details.)
  48. * This makes it possible to check quickly whether an
  49. * arbitrary address corresponds to an object administered by the
  50. * allocator.
  51. */
  52. word GC_non_gc_bytes = 0; /* Number of bytes not intended to be collected */
  53. word GC_gc_no = 0;
  54. #ifndef SMALL_CONFIG
  55. int GC_incremental = 0; /* By default, stop the world. */
  56. #endif
  57. int GC_parallel = FALSE; /* By default, parallel GC is off. */
  58. int GC_full_freq = 19; /* Every 20th collection is a full */
  59. /* collection, whether we need it */
  60. /* or not. */
  61. GC_bool GC_need_full_gc = FALSE;
  62. /* Need full GC do to heap growth. */
  63. #ifdef THREADS
  64. GC_bool GC_world_stopped = FALSE;
  65. # define IF_THREADS(x) x
  66. #else
  67. # define IF_THREADS(x)
  68. #endif
  69. word GC_used_heap_size_after_full = 0;
  70. char * GC_copyright[] =
  71. {"Copyright 1988,1989 Hans-J. Boehm and Alan J. Demers ",
  72. "Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved. ",
  73. "Copyright (c) 1996-1998 by Silicon Graphics. All rights reserved. ",
  74. "Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved. ",
  75. "THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY",
  76. " EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.",
  77. "See source code for details." };
  78. # include "version.h"
  79. #if defined(SAVE_CALL_CHAIN) && \
  80. !(defined(REDIRECT_MALLOC) && defined(GC_HAVE_BUILTIN_BACKTRACE))
  81. # define SAVE_CALL_CHAIN_IN_GC
  82. /* This is only safe if the call chain save mechanism won't end up */
  83. /* calling GC_malloc. The GNU C library documentation suggests */
  84. /* that backtrace doesn't use malloc, but at least the initial */
  85. /* call in some versions does seem to invoke the dynamic linker, */
  86. /* which uses malloc. */
  87. #endif
  88. /* some more variables */
  89. extern signed_word GC_mem_found; /* Number of reclaimed longwords */
  90. /* after garbage collection */
  91. GC_bool GC_dont_expand = 0;
  92. word GC_free_space_divisor = 3;
  93. extern GC_bool GC_collection_in_progress();
  94. /* Collection is in progress, or was abandoned. */
  95. int GC_never_stop_func GC_PROTO((void)) { return(0); }
  96. unsigned long GC_time_limit = TIME_LIMIT;
  97. CLOCK_TYPE GC_start_time; /* Time at which we stopped world. */
  98. /* used only in GC_timeout_stop_func. */
  99. int GC_n_attempts = 0; /* Number of attempts at finishing */
  100. /* collection within GC_time_limit. */
  101. #if defined(SMALL_CONFIG) || defined(NO_CLOCK)
  102. # define GC_timeout_stop_func GC_never_stop_func
  103. #else
  104. int GC_timeout_stop_func GC_PROTO((void))
  105. {
  106. CLOCK_TYPE current_time;
  107. static unsigned count = 0;
  108. unsigned long time_diff;
  109. if ((count++ & 3) != 0) return(0);
  110. GET_TIME(current_time);
  111. time_diff = MS_TIME_DIFF(current_time,GC_start_time);
  112. if (time_diff >= GC_time_limit) {
  113. # ifdef CONDPRINT
  114. if (GC_print_stats) {
  115. GC_printf0("Abandoning stopped marking after ");
  116. GC_printf1("%lu msecs", (unsigned long)time_diff);
  117. GC_printf1("(attempt %ld)\n", (unsigned long) GC_n_attempts);
  118. }
  119. # endif
  120. return(1);
  121. }
  122. return(0);
  123. }
  124. #endif /* !SMALL_CONFIG */
  125. /* Return the minimum number of words that must be allocated between */
  126. /* collections to amortize the collection cost. */
  127. static word min_words_allocd()
  128. {
  129. # ifdef THREADS
  130. /* We punt, for now. */
  131. register signed_word stack_size = 10000;
  132. # else
  133. int dummy;
  134. register signed_word stack_size = (ptr_t)(&dummy) - GC_stackbottom;
  135. # endif
  136. word total_root_size; /* includes double stack size, */
  137. /* since the stack is expensive */
  138. /* to scan. */
  139. word scan_size; /* Estimate of memory to be scanned */
  140. /* during normal GC. */
  141. if (stack_size < 0) stack_size = -stack_size;
  142. total_root_size = 2 * stack_size + GC_root_size;
  143. scan_size = BYTES_TO_WORDS(GC_heapsize - GC_large_free_bytes
  144. + (GC_large_free_bytes >> 2)
  145. /* use a bit more of large empty heap */
  146. + total_root_size);
  147. if (TRUE_INCREMENTAL) {
  148. return scan_size / (2 * GC_free_space_divisor);
  149. } else {
  150. return scan_size / GC_free_space_divisor;
  151. }
  152. }
  153. /* Return the number of words allocated, adjusted for explicit storage */
  154. /* management, etc.. This number is used in deciding when to trigger */
  155. /* collections. */
  156. word GC_adj_words_allocd()
  157. {
  158. register signed_word result;
  159. register signed_word expl_managed =
  160. BYTES_TO_WORDS((long)GC_non_gc_bytes
  161. - (long)GC_non_gc_bytes_at_gc);
  162. /* Don't count what was explicitly freed, or newly allocated for */
  163. /* explicit management. Note that deallocating an explicitly */
  164. /* managed object should not alter result, assuming the client */
  165. /* is playing by the rules. */
  166. result = (signed_word)GC_words_allocd
  167. - (signed_word)GC_mem_freed
  168. + (signed_word)GC_finalizer_mem_freed - expl_managed;
  169. if (result > (signed_word)GC_words_allocd) {
  170. result = GC_words_allocd;
  171. /* probably client bug or unfortunate scheduling */
  172. }
  173. result += GC_words_finalized;
  174. /* We count objects enqueued for finalization as though they */
  175. /* had been reallocated this round. Finalization is user */
  176. /* visible progress. And if we don't count this, we have */
  177. /* stability problems for programs that finalize all objects. */
  178. if ((GC_words_wasted >> 3) < result)
  179. result += GC_words_wasted;
  180. /* This doesn't reflect useful work. But if there is lots of */
  181. /* new fragmentation, the same is probably true of the heap, */
  182. /* and the collection will be correspondingly cheaper. */
  183. if (result < (signed_word)(GC_words_allocd >> 3)) {
  184. /* Always count at least 1/8 of the allocations. We don't want */
  185. /* to collect too infrequently, since that would inhibit */
  186. /* coalescing of free storage blocks. */
  187. /* This also makes us partially robust against client bugs. */
  188. return(GC_words_allocd >> 3);
  189. } else {
  190. return(result);
  191. }
  192. }
  193. /* Clear up a few frames worth of garbage left at the top of the stack. */
  194. /* This is used to prevent us from accidentally treating garbade left */
  195. /* on the stack by other parts of the collector as roots. This */
  196. /* differs from the code in misc.c, which actually tries to keep the */
  197. /* stack clear of long-lived, client-generated garbage. */
  198. void GC_clear_a_few_frames()
  199. {
  200. # define NWORDS 64
  201. word frames[NWORDS];
  202. /* Some compilers will warn that frames was set but never used. */
  203. /* That's the whole idea ... */
  204. register int i;
  205. for (i = 0; i < NWORDS; i++) frames[i] = 0;
  206. }
  207. /* Heap size at which we need a collection to avoid expanding past */
  208. /* limits used by blacklisting. */
  209. static word GC_collect_at_heapsize = (word)(-1);
  210. /* Have we allocated enough to amortize a collection? */
  211. GC_bool GC_should_collect()
  212. {
  213. return(GC_adj_words_allocd() >= min_words_allocd()
  214. || GC_heapsize >= GC_collect_at_heapsize);
  215. }
  216. void GC_notify_full_gc()
  217. {
  218. if (GC_start_call_back != (void (*) GC_PROTO((void)))0) {
  219. (*GC_start_call_back)();
  220. }
  221. }
  222. GC_bool GC_is_full_gc = FALSE;
  223. /*
  224. * Initiate a garbage collection if appropriate.
  225. * Choose judiciously
  226. * between partial, full, and stop-world collections.
  227. * Assumes lock held, signals disabled.
  228. */
  229. void GC_maybe_gc()
  230. {
  231. static int n_partial_gcs = 0;
  232. if (GC_should_collect()) {
  233. if (!GC_incremental) {
  234. GC_gcollect_inner();
  235. n_partial_gcs = 0;
  236. return;
  237. } else {
  238. # ifdef PARALLEL_MARK
  239. GC_wait_for_reclaim();
  240. # endif
  241. if (GC_need_full_gc || n_partial_gcs >= GC_full_freq) {
  242. # ifdef CONDPRINT
  243. if (GC_print_stats) {
  244. GC_printf2(
  245. "***>Full mark for collection %lu after %ld allocd bytes\n",
  246. (unsigned long) GC_gc_no+1,
  247. (long)WORDS_TO_BYTES(GC_words_allocd));
  248. }
  249. # endif
  250. GC_promote_black_lists();
  251. (void)GC_reclaim_all((GC_stop_func)0, TRUE);
  252. GC_clear_marks();
  253. n_partial_gcs = 0;
  254. GC_notify_full_gc();
  255. GC_is_full_gc = TRUE;
  256. } else {
  257. n_partial_gcs++;
  258. }
  259. }
  260. /* We try to mark with the world stopped. */
  261. /* If we run out of time, this turns into */
  262. /* incremental marking. */
  263. # ifndef NO_CLOCK
  264. if (GC_time_limit != GC_TIME_UNLIMITED) { GET_TIME(GC_start_time); }
  265. # endif
  266. if (GC_stopped_mark(GC_time_limit == GC_TIME_UNLIMITED?
  267. GC_never_stop_func : GC_timeout_stop_func)) {
  268. # ifdef SAVE_CALL_CHAIN_IN_GC
  269. GC_save_callers(GC_last_stack);
  270. # endif
  271. GC_finish_collection();
  272. } else {
  273. if (!GC_is_full_gc) {
  274. /* Count this as the first attempt */
  275. GC_n_attempts++;
  276. }
  277. }
  278. }
  279. }
  280. /*
  281. * Stop the world garbage collection. Assumes lock held, signals disabled.
  282. * If stop_func is not GC_never_stop_func, then abort if stop_func returns TRUE.
  283. * Return TRUE if we successfully completed the collection.
  284. */
  285. GC_bool GC_try_to_collect_inner(stop_func)
  286. GC_stop_func stop_func;
  287. {
  288. # ifdef CONDPRINT
  289. CLOCK_TYPE start_time, current_time;
  290. # endif
  291. if (GC_dont_gc) return FALSE;
  292. if (GC_incremental && GC_collection_in_progress()) {
  293. # ifdef CONDPRINT
  294. if (GC_print_stats) {
  295. GC_printf0(
  296. "GC_try_to_collect_inner: finishing collection in progress\n");
  297. }
  298. # endif /* CONDPRINT */
  299. /* Just finish collection already in progress. */
  300. while(GC_collection_in_progress()) {
  301. if (stop_func()) return(FALSE);
  302. GC_collect_a_little_inner(1);
  303. }
  304. }
  305. if (stop_func == GC_never_stop_func) GC_notify_full_gc();
  306. # ifdef CONDPRINT
  307. if (GC_print_stats) {
  308. if (GC_print_stats) GET_TIME(start_time);
  309. GC_printf2(
  310. "Initiating full world-stop collection %lu after %ld allocd bytes\n",
  311. (unsigned long) GC_gc_no+1,
  312. (long)WORDS_TO_BYTES(GC_words_allocd));
  313. }
  314. # endif
  315. GC_promote_black_lists();
  316. /* Make sure all blocks have been reclaimed, so sweep routines */
  317. /* don't see cleared mark bits. */
  318. /* If we're guaranteed to finish, then this is unnecessary. */
  319. /* In the find_leak case, we have to finish to guarantee that */
  320. /* previously unmarked objects are not reported as leaks. */
  321. # ifdef PARALLEL_MARK
  322. GC_wait_for_reclaim();
  323. # endif
  324. if ((GC_find_leak || stop_func != GC_never_stop_func)
  325. && !GC_reclaim_all(stop_func, FALSE)) {
  326. /* Aborted. So far everything is still consistent. */
  327. return(FALSE);
  328. }
  329. GC_invalidate_mark_state(); /* Flush mark stack. */
  330. GC_clear_marks();
  331. # ifdef SAVE_CALL_CHAIN_IN_GC
  332. GC_save_callers(GC_last_stack);
  333. # endif
  334. GC_is_full_gc = TRUE;
  335. if (!GC_stopped_mark(stop_func)) {
  336. if (!GC_incremental) {
  337. /* We're partially done and have no way to complete or use */
  338. /* current work. Reestablish invariants as cheaply as */
  339. /* possible. */
  340. GC_invalidate_mark_state();
  341. GC_unpromote_black_lists();
  342. } /* else we claim the world is already still consistent. We'll */
  343. /* finish incrementally. */
  344. return(FALSE);
  345. }
  346. GC_finish_collection();
  347. # if defined(CONDPRINT)
  348. if (GC_print_stats) {
  349. GET_TIME(current_time);
  350. GC_printf1("Complete collection took %lu msecs\n",
  351. MS_TIME_DIFF(current_time,start_time));
  352. }
  353. # endif
  354. return(TRUE);
  355. }
  356. /*
  357. * Perform n units of garbage collection work. A unit is intended to touch
  358. * roughly GC_RATE pages. Every once in a while, we do more than that.
  359. * This needa to be a fairly large number with our current incremental
  360. * GC strategy, since otherwise we allocate too much during GC, and the
  361. * cleanup gets expensive.
  362. */
  363. # define GC_RATE 10
  364. # define MAX_PRIOR_ATTEMPTS 1
  365. /* Maximum number of prior attempts at world stop marking */
  366. /* A value of 1 means that we finish the second time, no matter */
  367. /* how long it takes. Doesn't count the initial root scan */
  368. /* for a full GC. */
  369. int GC_deficit = 0; /* The number of extra calls to GC_mark_some */
  370. /* that we have made. */
  371. void GC_collect_a_little_inner(n)
  372. int n;
  373. {
  374. register int i;
  375. if (GC_dont_gc) return;
  376. if (GC_incremental && GC_collection_in_progress()) {
  377. for (i = GC_deficit; i < GC_RATE*n; i++) {
  378. if (GC_mark_some((ptr_t)0)) {
  379. /* Need to finish a collection */
  380. # ifdef SAVE_CALL_CHAIN_IN_GC
  381. GC_save_callers(GC_last_stack);
  382. # endif
  383. # ifdef PARALLEL_MARK
  384. GC_wait_for_reclaim();
  385. # endif
  386. if (GC_n_attempts < MAX_PRIOR_ATTEMPTS
  387. && GC_time_limit != GC_TIME_UNLIMITED) {
  388. GET_TIME(GC_start_time);
  389. if (!GC_stopped_mark(GC_timeout_stop_func)) {
  390. GC_n_attempts++;
  391. break;
  392. }
  393. } else {
  394. (void)GC_stopped_mark(GC_never_stop_func);
  395. }
  396. GC_finish_collection();
  397. break;
  398. }
  399. }
  400. if (GC_deficit > 0) GC_deficit -= GC_RATE*n;
  401. if (GC_deficit < 0) GC_deficit = 0;
  402. } else {
  403. GC_maybe_gc();
  404. }
  405. }
  406. int GC_collect_a_little GC_PROTO(())
  407. {
  408. int result;
  409. DCL_LOCK_STATE;
  410. DISABLE_SIGNALS();
  411. LOCK();
  412. GC_collect_a_little_inner(1);
  413. result = (int)GC_collection_in_progress();
  414. UNLOCK();
  415. ENABLE_SIGNALS();
  416. if (!result && GC_debugging_started) GC_print_all_smashed();
  417. return(result);
  418. }
  419. /*
  420. * Assumes lock is held, signals are disabled.
  421. * We stop the world.
  422. * If stop_func() ever returns TRUE, we may fail and return FALSE.
  423. * Increment GC_gc_no if we succeed.
  424. */
  425. GC_bool GC_stopped_mark(stop_func)
  426. GC_stop_func stop_func;
  427. {
  428. register int i;
  429. int dummy;
  430. # if defined(PRINTTIMES) || defined(CONDPRINT)
  431. CLOCK_TYPE start_time, current_time;
  432. # endif
  433. # ifdef PRINTTIMES
  434. GET_TIME(start_time);
  435. # endif
  436. # if defined(CONDPRINT) && !defined(PRINTTIMES)
  437. if (GC_print_stats) GET_TIME(start_time);
  438. # endif
  439. # if defined(REGISTER_LIBRARIES_EARLY)
  440. GC_cond_register_dynamic_libraries();
  441. # endif
  442. STOP_WORLD();
  443. IF_THREADS(GC_world_stopped = TRUE);
  444. # ifdef CONDPRINT
  445. if (GC_print_stats) {
  446. GC_printf1("--> Marking for collection %lu ",
  447. (unsigned long) GC_gc_no + 1);
  448. GC_printf2("after %lu allocd bytes + %lu wasted bytes\n",
  449. (unsigned long) WORDS_TO_BYTES(GC_words_allocd),
  450. (unsigned long) WORDS_TO_BYTES(GC_words_wasted));
  451. }
  452. # endif
  453. # ifdef MAKE_BACK_GRAPH
  454. if (GC_print_back_height) {
  455. GC_build_back_graph();
  456. }
  457. # endif
  458. /* Mark from all roots. */
  459. /* Minimize junk left in my registers and on the stack */
  460. GC_clear_a_few_frames();
  461. GC_noop(0,0,0,0,0,0);
  462. GC_initiate_gc();
  463. for(i = 0;;i++) {
  464. if ((*stop_func)()) {
  465. # ifdef CONDPRINT
  466. if (GC_print_stats) {
  467. GC_printf0("Abandoned stopped marking after ");
  468. GC_printf1("%lu iterations\n",
  469. (unsigned long)i);
  470. }
  471. # endif
  472. GC_deficit = i; /* Give the mutator a chance. */
  473. IF_THREADS(GC_world_stopped = FALSE);
  474. START_WORLD();
  475. return(FALSE);
  476. }
  477. if (GC_mark_some((ptr_t)(&dummy))) break;
  478. }
  479. GC_gc_no++;
  480. # ifdef PRINTSTATS
  481. GC_printf2("Collection %lu reclaimed %ld bytes",
  482. (unsigned long) GC_gc_no - 1,
  483. (long)WORDS_TO_BYTES(GC_mem_found));
  484. # else
  485. # ifdef CONDPRINT
  486. if (GC_print_stats) {
  487. GC_printf1("Collection %lu finished", (unsigned long) GC_gc_no - 1);
  488. }
  489. # endif
  490. # endif /* !PRINTSTATS */
  491. # ifdef CONDPRINT
  492. if (GC_print_stats) {
  493. GC_printf1(" ---> heapsize = %lu bytes\n",
  494. (unsigned long) GC_heapsize);
  495. /* Printf arguments may be pushed in funny places. Clear the */
  496. /* space. */
  497. GC_printf0("");
  498. }
  499. # endif /* CONDPRINT */
  500. /* Check all debugged objects for consistency */
  501. if (GC_debugging_started) {
  502. (*GC_check_heap)();
  503. }
  504. IF_THREADS(GC_world_stopped = FALSE);
  505. START_WORLD();
  506. # ifdef PRINTTIMES
  507. GET_TIME(current_time);
  508. GC_printf1("World-stopped marking took %lu msecs\n",
  509. MS_TIME_DIFF(current_time,start_time));
  510. # else
  511. # ifdef CONDPRINT
  512. if (GC_print_stats) {
  513. GET_TIME(current_time);
  514. GC_printf1("World-stopped marking took %lu msecs\n",
  515. MS_TIME_DIFF(current_time,start_time));
  516. }
  517. # endif
  518. # endif
  519. return(TRUE);
  520. }
  521. /* Set all mark bits for the free list whose first entry is q */
  522. #ifdef __STDC__
  523. void GC_set_fl_marks(ptr_t q)
  524. #else
  525. void GC_set_fl_marks(q)
  526. ptr_t q;
  527. #endif
  528. {
  529. ptr_t p;
  530. struct hblk * h, * last_h = 0;
  531. hdr *hhdr;
  532. int word_no;
  533. for (p = q; p != 0; p = obj_link(p)){
  534. h = HBLKPTR(p);
  535. if (h != last_h) {
  536. last_h = h;
  537. hhdr = HDR(h);
  538. }
  539. word_no = (((word *)p) - ((word *)h));
  540. set_mark_bit_from_hdr(hhdr, word_no);
  541. }
  542. }
  543. /* Clear all mark bits for the free list whose first entry is q */
  544. /* Decrement GC_mem_found by number of words on free list. */
  545. #ifdef __STDC__
  546. void GC_clear_fl_marks(ptr_t q)
  547. #else
  548. void GC_clear_fl_marks(q)
  549. ptr_t q;
  550. #endif
  551. {
  552. ptr_t p;
  553. struct hblk * h, * last_h = 0;
  554. hdr *hhdr;
  555. int word_no;
  556. for (p = q; p != 0; p = obj_link(p)){
  557. h = HBLKPTR(p);
  558. if (h != last_h) {
  559. last_h = h;
  560. hhdr = HDR(h);
  561. }
  562. word_no = (((word *)p) - ((word *)h));
  563. clear_mark_bit_from_hdr(hhdr, word_no);
  564. # ifdef GATHERSTATS
  565. GC_mem_found -= hhdr -> hb_sz;
  566. # endif
  567. }
  568. }
  569. /* Finish up a collection. Assumes lock is held, signals are disabled, */
  570. /* but the world is otherwise running. */
  571. void GC_finish_collection()
  572. {
  573. # ifdef PRINTTIMES
  574. CLOCK_TYPE start_time;
  575. CLOCK_TYPE finalize_time;
  576. CLOCK_TYPE done_time;
  577. GET_TIME(start_time);
  578. finalize_time = start_time;
  579. # endif
  580. # ifdef GATHERSTATS
  581. GC_mem_found = 0;
  582. # endif
  583. # if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
  584. if (getenv("GC_PRINT_ADDRESS_MAP") != 0) {
  585. GC_print_address_map();
  586. }
  587. # endif
  588. COND_DUMP;
  589. if (GC_find_leak) {
  590. /* Mark all objects on the free list. All objects should be */
  591. /* marked when we're done. */
  592. {
  593. register word size; /* current object size */
  594. int kind;
  595. ptr_t q;
  596. for (kind = 0; kind < GC_n_kinds; kind++) {
  597. for (size = 1; size <= MAXOBJSZ; size++) {
  598. q = GC_obj_kinds[kind].ok_freelist[size];
  599. if (q != 0) GC_set_fl_marks(q);
  600. }
  601. }
  602. }
  603. GC_start_reclaim(TRUE);
  604. /* The above just checks; it doesn't really reclaim anything. */
  605. }
  606. GC_finalize();
  607. # ifdef STUBBORN_ALLOC
  608. GC_clean_changing_list();
  609. # endif
  610. # ifdef PRINTTIMES
  611. GET_TIME(finalize_time);
  612. # endif
  613. if (GC_print_back_height) {
  614. # ifdef MAKE_BACK_GRAPH
  615. GC_traverse_back_graph();
  616. # else
  617. # ifndef SMALL_CONFIG
  618. GC_err_printf0("Back height not available: "
  619. "Rebuild collector with -DMAKE_BACK_GRAPH\n");
  620. # endif
  621. # endif
  622. }
  623. /* Clear free list mark bits, in case they got accidentally marked */
  624. /* (or GC_find_leak is set and they were intentionally marked). */
  625. /* Also subtract memory remaining from GC_mem_found count. */
  626. /* Note that composite objects on free list are cleared. */
  627. /* Thus accidentally marking a free list is not a problem; only */
  628. /* objects on the list itself will be marked, and that's fixed here. */
  629. {
  630. register word size; /* current object size */
  631. register ptr_t q; /* pointer to current object */
  632. int kind;
  633. for (kind = 0; kind < GC_n_kinds; kind++) {
  634. for (size = 1; size <= MAXOBJSZ; size++) {
  635. q = GC_obj_kinds[kind].ok_freelist[size];
  636. if (q != 0) GC_clear_fl_marks(q);
  637. }
  638. }
  639. }
  640. # ifdef PRINTSTATS
  641. GC_printf1("Bytes recovered before sweep - f.l. count = %ld\n",
  642. (long)WORDS_TO_BYTES(GC_mem_found));
  643. # endif
  644. /* Reconstruct free lists to contain everything not marked */
  645. GC_start_reclaim(FALSE);
  646. if (GC_is_full_gc) {
  647. GC_used_heap_size_after_full = USED_HEAP_SIZE;
  648. GC_need_full_gc = FALSE;
  649. } else {
  650. GC_need_full_gc =
  651. BYTES_TO_WORDS(USED_HEAP_SIZE - GC_used_heap_size_after_full)
  652. > min_words_allocd();
  653. }
  654. # ifdef PRINTSTATS
  655. GC_printf2(
  656. "Immediately reclaimed %ld bytes in heap of size %lu bytes",
  657. (long)WORDS_TO_BYTES(GC_mem_found),
  658. (unsigned long)GC_heapsize);
  659. # ifdef USE_MUNMAP
  660. GC_printf1("(%lu unmapped)", GC_unmapped_bytes);
  661. # endif
  662. GC_printf2(
  663. "\n%lu (atomic) + %lu (composite) collectable bytes in use\n",
  664. (unsigned long)WORDS_TO_BYTES(GC_atomic_in_use),
  665. (unsigned long)WORDS_TO_BYTES(GC_composite_in_use));
  666. # endif
  667. GC_n_attempts = 0;
  668. GC_is_full_gc = FALSE;
  669. /* Reset or increment counters for next cycle */
  670. GC_words_allocd_before_gc += GC_words_allocd;
  671. GC_non_gc_bytes_at_gc = GC_non_gc_bytes;
  672. GC_words_allocd = 0;
  673. GC_words_wasted = 0;
  674. GC_mem_freed = 0;
  675. GC_finalizer_mem_freed = 0;
  676. # ifdef USE_MUNMAP
  677. GC_unmap_old();
  678. # endif
  679. # ifdef PRINTTIMES
  680. GET_TIME(done_time);
  681. GC_printf2("Finalize + initiate sweep took %lu + %lu msecs\n",
  682. MS_TIME_DIFF(finalize_time,start_time),
  683. MS_TIME_DIFF(done_time,finalize_time));
  684. # endif
  685. }
  686. /* Externally callable routine to invoke full, stop-world collection */
  687. # if defined(__STDC__) || defined(__cplusplus)
  688. int GC_try_to_collect(GC_stop_func stop_func)
  689. # else
  690. int GC_try_to_collect(stop_func)
  691. GC_stop_func stop_func;
  692. # endif
  693. {
  694. int result;
  695. DCL_LOCK_STATE;
  696. if (GC_debugging_started) GC_print_all_smashed();
  697. GC_INVOKE_FINALIZERS();
  698. DISABLE_SIGNALS();
  699. LOCK();
  700. ENTER_GC();
  701. if (!GC_is_initialized) GC_init_inner();
  702. /* Minimize junk left in my registers */
  703. GC_noop(0,0,0,0,0,0);
  704. result = (int)GC_try_to_collect_inner(stop_func);
  705. EXIT_GC();
  706. UNLOCK();
  707. ENABLE_SIGNALS();
  708. if(result) {
  709. if (GC_debugging_started) GC_print_all_smashed();
  710. GC_INVOKE_FINALIZERS();
  711. }
  712. return(result);
  713. }
  714. void GC_gcollect GC_PROTO(())
  715. {
  716. (void)GC_try_to_collect(GC_never_stop_func);
  717. if (GC_have_errors) GC_print_all_errors();
  718. }
  719. word GC_n_heap_sects = 0; /* Number of sections currently in heap. */
  720. /*
  721. * Use the chunk of memory starting at p of size bytes as part of the heap.
  722. * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
  723. */
  724. void GC_add_to_heap(p, bytes)
  725. struct hblk *p;
  726. word bytes;
  727. {
  728. word words;
  729. hdr * phdr;
  730. if (GC_n_heap_sects >= MAX_HEAP_SECTS) {
  731. ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
  732. }
  733. phdr = GC_install_header(p);
  734. if (0 == phdr) {
  735. /* This is extremely unlikely. Can't add it. This will */
  736. /* almost certainly result in a 0 return from the allocator, */
  737. /* which is entirely appropriate. */
  738. return;
  739. }
  740. GC_heap_sects[GC_n_heap_sects].hs_start = (ptr_t)p;
  741. GC_heap_sects[GC_n_heap_sects].hs_bytes = bytes;
  742. GC_n_heap_sects++;
  743. words = BYTES_TO_WORDS(bytes);
  744. phdr -> hb_sz = words;
  745. phdr -> hb_map = (unsigned char *)1; /* A value != GC_invalid_map */
  746. phdr -> hb_flags = 0;
  747. GC_freehblk(p);
  748. GC_heapsize += bytes;
  749. if ((ptr_t)p <= (ptr_t)GC_least_plausible_heap_addr
  750. || GC_least_plausible_heap_addr == 0) {
  751. GC_least_plausible_heap_addr = (GC_PTR)((ptr_t)p - sizeof(word));
  752. /* Making it a little smaller than necessary prevents */
  753. /* us from getting a false hit from the variable */
  754. /* itself. There's some unintentional reflection */
  755. /* here. */
  756. }
  757. if ((ptr_t)p + bytes >= (ptr_t)GC_greatest_plausible_heap_addr) {
  758. GC_greatest_plausible_heap_addr = (GC_PTR)((ptr_t)p + bytes);
  759. }
  760. }
  761. # if !defined(NO_DEBUGGING)
  762. void GC_print_heap_sects()
  763. {
  764. register unsigned i;
  765. GC_printf1("Total heap size: %lu\n", (unsigned long) GC_heapsize);
  766. for (i = 0; i < GC_n_heap_sects; i++) {
  767. unsigned long start = (unsigned long) GC_heap_sects[i].hs_start;
  768. unsigned long len = (unsigned long) GC_heap_sects[i].hs_bytes;
  769. struct hblk *h;
  770. unsigned nbl = 0;
  771. GC_printf3("Section %ld from 0x%lx to 0x%lx ", (unsigned long)i,
  772. start, (unsigned long)(start + len));
  773. for (h = (struct hblk *)start; h < (struct hblk *)(start + len); h++) {
  774. if (GC_is_black_listed(h, HBLKSIZE)) nbl++;
  775. }
  776. GC_printf2("%lu/%lu blacklisted\n", (unsigned long)nbl,
  777. (unsigned long)(len/HBLKSIZE));
  778. }
  779. }
  780. # endif
  781. GC_PTR GC_least_plausible_heap_addr = (GC_PTR)ONES;
  782. GC_PTR GC_greatest_plausible_heap_addr = 0;
  783. ptr_t GC_max(x,y)
  784. ptr_t x, y;
  785. {
  786. return(x > y? x : y);
  787. }
  788. ptr_t GC_min(x,y)
  789. ptr_t x, y;
  790. {
  791. return(x < y? x : y);
  792. }
  793. # if defined(__STDC__) || defined(__cplusplus)
  794. void GC_set_max_heap_size(GC_word n)
  795. # else
  796. void GC_set_max_heap_size(n)
  797. GC_word n;
  798. # endif
  799. {
  800. GC_max_heapsize = n;
  801. }
  802. GC_word GC_max_retries = 0;
  803. /*
  804. * this explicitly increases the size of the heap. It is used
  805. * internally, but may also be invoked from GC_expand_hp by the user.
  806. * The argument is in units of HBLKSIZE.
  807. * Tiny values of n are rounded up.
  808. * Returns FALSE on failure.
  809. */
  810. GC_bool GC_expand_hp_inner(n)
  811. word n;
  812. {
  813. word bytes;
  814. struct hblk * space;
  815. word expansion_slop; /* Number of bytes by which we expect the */
  816. /* heap to expand soon. */
  817. if (n < MINHINCR) n = MINHINCR;
  818. bytes = n * HBLKSIZE;
  819. /* Make sure bytes is a multiple of GC_page_size */
  820. {
  821. word mask = GC_page_size - 1;
  822. bytes += mask;
  823. bytes &= ~mask;
  824. }
  825. if (GC_max_heapsize != 0 && GC_heapsize + bytes > GC_max_heapsize) {
  826. /* Exceeded self-imposed limit */
  827. return(FALSE);
  828. }
  829. space = GET_MEM(bytes);
  830. if( space == 0 ) {
  831. # ifdef CONDPRINT
  832. if (GC_print_stats) {
  833. GC_printf1("Failed to expand heap by %ld bytes\n",
  834. (unsigned long)bytes);
  835. }
  836. # endif
  837. return(FALSE);
  838. }
  839. # ifdef CONDPRINT
  840. if (GC_print_stats) {
  841. GC_printf2("Increasing heap size by %lu after %lu allocated bytes\n",
  842. (unsigned long)bytes,
  843. (unsigned long)WORDS_TO_BYTES(GC_words_allocd));
  844. # ifdef UNDEFINED
  845. GC_printf1("Root size = %lu\n", GC_root_size);
  846. GC_print_block_list(); GC_print_hblkfreelist();
  847. GC_printf0("\n");
  848. # endif
  849. }
  850. # endif
  851. expansion_slop = WORDS_TO_BYTES(min_words_allocd()) + 4*MAXHINCR*HBLKSIZE;
  852. if (GC_last_heap_addr == 0 && !((word)space & SIGNB)
  853. || (GC_last_heap_addr != 0 && GC_last_heap_addr < (ptr_t)space)) {
  854. /* Assume the heap is growing up */
  855. GC_greatest_plausible_heap_addr =
  856. (GC_PTR)GC_max((ptr_t)GC_greatest_plausible_heap_addr,
  857. (ptr_t)space + bytes + expansion_slop);
  858. } else {
  859. /* Heap is growing down */
  860. GC_least_plausible_heap_addr =
  861. (GC_PTR)GC_min((ptr_t)GC_least_plausible_heap_addr,
  862. (ptr_t)space - expansion_slop);
  863. }
  864. # if defined(LARGE_CONFIG)
  865. if (((ptr_t)GC_greatest_plausible_heap_addr <= (ptr_t)space + bytes
  866. || (ptr_t)GC_least_plausible_heap_addr >= (ptr_t)space)
  867. && GC_heapsize > 0) {
  868. /* GC_add_to_heap will fix this, but ... */
  869. WARN("Too close to address space limit: blacklisting ineffective\n", 0);
  870. }
  871. # endif
  872. GC_prev_heap_addr = GC_last_heap_addr;
  873. GC_last_heap_addr = (ptr_t)space;
  874. GC_add_to_heap(space, bytes);
  875. /* Force GC before we are likely to allocate past expansion_slop */
  876. GC_collect_at_heapsize =
  877. GC_heapsize + expansion_slop - 2*MAXHINCR*HBLKSIZE;
  878. # if defined(LARGE_CONFIG)
  879. if (GC_collect_at_heapsize < GC_heapsize /* wrapped */)
  880. GC_collect_at_heapsize = (word)(-1);
  881. # endif
  882. return(TRUE);
  883. }
  884. /* Really returns a bool, but it's externally visible, so that's clumsy. */
  885. /* Arguments is in bytes. */
  886. # if defined(__STDC__) || defined(__cplusplus)
  887. int GC_expand_hp(size_t bytes)
  888. # else
  889. int GC_expand_hp(bytes)
  890. size_t bytes;
  891. # endif
  892. {
  893. int result;
  894. DCL_LOCK_STATE;
  895. DISABLE_SIGNALS();
  896. LOCK();
  897. if (!GC_is_initialized) GC_init_inner();
  898. result = (int)GC_expand_hp_inner(divHBLKSZ((word)bytes));
  899. if (result) GC_requested_heapsize += bytes;
  900. UNLOCK();
  901. ENABLE_SIGNALS();
  902. return(result);
  903. }
  904. unsigned GC_fail_count = 0;
  905. /* How many consecutive GC/expansion failures? */
  906. /* Reset by GC_allochblk. */
  907. GC_bool GC_collect_or_expand(needed_blocks, ignore_off_page)
  908. word needed_blocks;
  909. GC_bool ignore_off_page;
  910. {
  911. if (!GC_incremental && !GC_dont_gc &&
  912. ((GC_dont_expand && GC_words_allocd > 0) || GC_should_collect())) {
  913. GC_gcollect_inner();
  914. } else {
  915. word blocks_to_get = GC_heapsize/(HBLKSIZE*GC_free_space_divisor)
  916. + needed_blocks;
  917. if (blocks_to_get > MAXHINCR) {
  918. word slop;
  919. /* Get the minimum required to make it likely that we */
  920. /* can satisfy the current request in the presence of black- */
  921. /* listing. This will probably be more than MAXHINCR. */
  922. if (ignore_off_page) {
  923. slop = 4;
  924. } else {
  925. slop = 2*divHBLKSZ(BL_LIMIT);
  926. if (slop > needed_blocks) slop = needed_blocks;
  927. }
  928. if (needed_blocks + slop > MAXHINCR) {
  929. blocks_to_get = needed_blocks + slop;
  930. } else {
  931. blocks_to_get = MAXHINCR;
  932. }
  933. }
  934. if (!GC_expand_hp_inner(blocks_to_get)
  935. && !GC_expand_hp_inner(needed_blocks)) {
  936. if (GC_fail_count++ < GC_max_retries) {
  937. WARN("Out of Memory! Trying to continue ...\n", 0);
  938. GC_gcollect_inner();
  939. } else {
  940. # if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
  941. WARN("Out of Memory! Returning NIL!\n", 0);
  942. # endif
  943. return(FALSE);
  944. }
  945. } else {
  946. # ifdef CONDPRINT
  947. if (GC_fail_count && GC_print_stats) {
  948. GC_printf0("Memory available again ...\n");
  949. }
  950. # endif
  951. }
  952. }
  953. return(TRUE);
  954. }
  955. /*
  956. * Make sure the object free list for sz is not empty.
  957. * Return a pointer to the first object on the free list.
  958. * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
  959. * Assumes we hold the allocator lock and signals are disabled.
  960. *
  961. */
  962. ptr_t GC_allocobj(sz, kind)
  963. word sz;
  964. int kind;
  965. {
  966. ptr_t * flh = &(GC_obj_kinds[kind].ok_freelist[sz]);
  967. GC_bool tried_minor = FALSE;
  968. if (sz == 0) return(0);
  969. while (*flh == 0) {
  970. ENTER_GC();
  971. /* Do our share of marking work */
  972. if(TRUE_INCREMENTAL) GC_collect_a_little_inner(1);
  973. /* Sweep blocks for objects of this size */
  974. GC_continue_reclaim(sz, kind);
  975. EXIT_GC();
  976. if (*flh == 0) {
  977. GC_new_hblk(sz, kind);
  978. }
  979. if (*flh == 0) {
  980. ENTER_GC();
  981. if (GC_incremental && GC_time_limit == GC_TIME_UNLIMITED
  982. && ! tried_minor ) {
  983. GC_collect_a_little_inner(1);
  984. tried_minor = TRUE;
  985. } else {
  986. if (!GC_collect_or_expand((word)1,FALSE)) {
  987. EXIT_GC();
  988. return(0);
  989. }
  990. }
  991. EXIT_GC();
  992. }
  993. }
  994. /* Successful allocation; reset failure count. */
  995. GC_fail_count = 0;
  996. return(*flh);
  997. }