alloc.c 31 KB

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