alloc.c 32 KB

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