gc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. /* #define DEBUGINFO */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include "libguile/_scm.h"
  27. #include "libguile/eval.h"
  28. #include "libguile/stime.h"
  29. #include "libguile/stackchk.h"
  30. #include "libguile/struct.h"
  31. #include "libguile/smob.h"
  32. #include "libguile/unif.h"
  33. #include "libguile/async.h"
  34. #include "libguile/ports.h"
  35. #include "libguile/root.h"
  36. #include "libguile/strings.h"
  37. #include "libguile/vectors.h"
  38. #include "libguile/weaks.h"
  39. #include "libguile/hashtab.h"
  40. #include "libguile/tags.h"
  41. #include "libguile/private-gc.h"
  42. #include "libguile/validate.h"
  43. #include "libguile/deprecation.h"
  44. #include "libguile/gc.h"
  45. #include "libguile/dynwind.h"
  46. #ifdef GUILE_DEBUG_MALLOC
  47. #include "libguile/debug-malloc.h"
  48. #endif
  49. #ifdef HAVE_MALLOC_H
  50. #include <malloc.h>
  51. #endif
  52. #ifdef HAVE_UNISTD_H
  53. #include <unistd.h>
  54. #endif
  55. /* Lock this mutex before doing lazy sweeping.
  56. */
  57. scm_i_pthread_mutex_t scm_i_sweep_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  58. /* Set this to != 0 if every cell that is accessed shall be checked:
  59. */
  60. int scm_debug_cell_accesses_p = 0;
  61. int scm_expensive_debug_cell_accesses_p = 0;
  62. /* Set this to 0 if no additional gc's shall be performed, otherwise set it to
  63. * the number of cell accesses after which a gc shall be called.
  64. */
  65. int scm_debug_cells_gc_interval = 0;
  66. /*
  67. Global variable, so you can switch it off at runtime by setting
  68. scm_i_cell_validation_already_running.
  69. */
  70. int scm_i_cell_validation_already_running ;
  71. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  72. /*
  73. Assert that the given object is a valid reference to a valid cell. This
  74. test involves to determine whether the object is a cell pointer, whether
  75. this pointer actually points into a heap segment and whether the cell
  76. pointed to is not a free cell. Further, additional garbage collections may
  77. get executed after a user defined number of cell accesses. This helps to
  78. find places in the C code where references are dropped for extremely short
  79. periods.
  80. */
  81. void
  82. scm_i_expensive_validation_check (SCM cell)
  83. {
  84. if (!scm_in_heap_p (cell))
  85. {
  86. fprintf (stderr, "scm_assert_cell_valid: this object does not live in the heap: %lux\n",
  87. (unsigned long) SCM_UNPACK (cell));
  88. abort ();
  89. }
  90. /* If desired, perform additional garbage collections after a user
  91. * defined number of cell accesses.
  92. */
  93. if (scm_debug_cells_gc_interval)
  94. {
  95. static unsigned int counter = 0;
  96. if (counter != 0)
  97. {
  98. --counter;
  99. }
  100. else
  101. {
  102. counter = scm_debug_cells_gc_interval;
  103. scm_gc ();
  104. }
  105. }
  106. }
  107. void
  108. scm_assert_cell_valid (SCM cell)
  109. {
  110. if (!scm_i_cell_validation_already_running && scm_debug_cell_accesses_p)
  111. {
  112. scm_i_cell_validation_already_running = 1; /* set to avoid recursion */
  113. /*
  114. During GC, no user-code should be run, and the guile core
  115. should use non-protected accessors.
  116. */
  117. if (scm_gc_running_p)
  118. return;
  119. /*
  120. Only scm_in_heap_p and rescanning the heap is wildly
  121. expensive.
  122. */
  123. if (scm_expensive_debug_cell_accesses_p)
  124. scm_i_expensive_validation_check (cell);
  125. #if (SCM_DEBUG_MARKING_API == 0)
  126. if (!SCM_GC_MARK_P (cell))
  127. {
  128. fprintf (stderr,
  129. "scm_assert_cell_valid: this object is unmarked. \n"
  130. "It has been garbage-collected in the last GC run: "
  131. "%lux\n",
  132. (unsigned long) SCM_UNPACK (cell));
  133. abort ();
  134. }
  135. #endif /* SCM_DEBUG_MARKING_API */
  136. scm_i_cell_validation_already_running = 0; /* re-enable */
  137. }
  138. }
  139. SCM_DEFINE (scm_set_debug_cell_accesses_x, "set-debug-cell-accesses!", 1, 0, 0,
  140. (SCM flag),
  141. "If @var{flag} is @code{#f}, cell access checking is disabled.\n"
  142. "If @var{flag} is @code{#t}, cheap cell access checking is enabled,\n"
  143. "but no additional calls to garbage collection are issued.\n"
  144. "If @var{flag} is a number, strict cell access checking is enabled,\n"
  145. "with an additional garbage collection after the given\n"
  146. "number of cell accesses.\n"
  147. "This procedure only exists when the compile-time flag\n"
  148. "@code{SCM_DEBUG_CELL_ACCESSES} was set to 1.")
  149. #define FUNC_NAME s_scm_set_debug_cell_accesses_x
  150. {
  151. if (scm_is_false (flag))
  152. {
  153. scm_debug_cell_accesses_p = 0;
  154. }
  155. else if (scm_is_eq (flag, SCM_BOOL_T))
  156. {
  157. scm_debug_cells_gc_interval = 0;
  158. scm_debug_cell_accesses_p = 1;
  159. scm_expensive_debug_cell_accesses_p = 0;
  160. }
  161. else
  162. {
  163. scm_debug_cells_gc_interval = scm_to_signed_integer (flag, 0, INT_MAX);
  164. scm_debug_cell_accesses_p = 1;
  165. scm_expensive_debug_cell_accesses_p = 1;
  166. }
  167. return SCM_UNSPECIFIED;
  168. }
  169. #undef FUNC_NAME
  170. #endif /* SCM_DEBUG_CELL_ACCESSES == 1 */
  171. /* scm_mtrigger
  172. * is the number of bytes of malloc allocation needed to trigger gc.
  173. */
  174. unsigned long scm_mtrigger;
  175. /* GC Statistics Keeping
  176. */
  177. unsigned long scm_cells_allocated = 0;
  178. unsigned long scm_last_cells_allocated = 0;
  179. unsigned long scm_mallocated = 0;
  180. long int scm_i_find_heap_calls = 0;
  181. /* Global GC sweep statistics since the last full GC. */
  182. scm_t_sweep_statistics scm_i_gc_sweep_stats = { 0, 0 };
  183. /* Total count of cells marked/swept. */
  184. static double scm_gc_cells_marked_acc = 0.;
  185. static double scm_gc_cells_marked_conservatively_acc = 0.;
  186. static double scm_gc_cells_swept_acc = 0.;
  187. static double scm_gc_cells_allocated_acc = 0.;
  188. static unsigned long scm_gc_time_taken = 0;
  189. static unsigned long scm_gc_mark_time_taken = 0;
  190. static unsigned long scm_gc_times = 0;
  191. static int scm_gc_cell_yield_percentage = 0;
  192. static unsigned long protected_obj_count = 0;
  193. /* The following are accessed from `gc-malloc.c' and `gc-card.c'. */
  194. int scm_gc_malloc_yield_percentage = 0;
  195. unsigned long scm_gc_malloc_collected = 0;
  196. SCM_SYMBOL (sym_cells_allocated, "cells-allocated");
  197. SCM_SYMBOL (sym_heap_size, "cell-heap-size");
  198. SCM_SYMBOL (sym_mallocated, "bytes-malloced");
  199. SCM_SYMBOL (sym_mtrigger, "gc-malloc-threshold");
  200. SCM_SYMBOL (sym_heap_segments, "cell-heap-segments");
  201. SCM_SYMBOL (sym_gc_time_taken, "gc-time-taken");
  202. SCM_SYMBOL (sym_gc_mark_time_taken, "gc-mark-time-taken");
  203. SCM_SYMBOL (sym_times, "gc-times");
  204. SCM_SYMBOL (sym_cells_marked, "cells-marked");
  205. SCM_SYMBOL (sym_cells_marked_conservatively, "cells-marked-conservatively");
  206. SCM_SYMBOL (sym_cells_swept, "cells-swept");
  207. SCM_SYMBOL (sym_malloc_yield, "malloc-yield");
  208. SCM_SYMBOL (sym_cell_yield, "cell-yield");
  209. SCM_SYMBOL (sym_protected_objects, "protected-objects");
  210. SCM_SYMBOL (sym_total_cells_allocated, "total-cells-allocated");
  211. /* Number of calls to SCM_NEWCELL since startup. */
  212. unsigned scm_newcell_count;
  213. unsigned scm_newcell2_count;
  214. /* {Scheme Interface to GC}
  215. */
  216. static SCM
  217. tag_table_to_type_alist (void *closure, SCM key, SCM val, SCM acc)
  218. {
  219. if (scm_is_integer (key))
  220. {
  221. int c_tag = scm_to_int (key);
  222. char const * name = scm_i_tag_name (c_tag);
  223. if (name != NULL)
  224. {
  225. key = scm_from_locale_string (name);
  226. }
  227. else
  228. {
  229. char s[100];
  230. sprintf (s, "tag %d", c_tag);
  231. key = scm_from_locale_string (s);
  232. }
  233. }
  234. return scm_cons (scm_cons (key, val), acc);
  235. }
  236. SCM_DEFINE (scm_gc_live_object_stats, "gc-live-object-stats", 0, 0, 0,
  237. (),
  238. "Return an alist of statistics of the current live objects. ")
  239. #define FUNC_NAME s_scm_gc_live_object_stats
  240. {
  241. SCM tab = scm_make_hash_table (scm_from_int (57));
  242. SCM alist;
  243. scm_i_all_segments_statistics (tab);
  244. alist
  245. = scm_internal_hash_fold (&tag_table_to_type_alist, NULL, SCM_EOL, tab);
  246. return alist;
  247. }
  248. #undef FUNC_NAME
  249. extern int scm_gc_malloc_yield_percentage;
  250. SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
  251. (),
  252. "Return an association list of statistics about Guile's current\n"
  253. "use of storage.\n")
  254. #define FUNC_NAME s_scm_gc_stats
  255. {
  256. long i = 0;
  257. SCM heap_segs = SCM_EOL ;
  258. unsigned long int local_scm_mtrigger;
  259. unsigned long int local_scm_mallocated;
  260. unsigned long int local_scm_heap_size;
  261. int local_scm_gc_cell_yield_percentage;
  262. int local_scm_gc_malloc_yield_percentage;
  263. unsigned long int local_scm_cells_allocated;
  264. unsigned long int local_scm_gc_time_taken;
  265. unsigned long int local_scm_gc_times;
  266. unsigned long int local_scm_gc_mark_time_taken;
  267. unsigned long int local_protected_obj_count;
  268. double local_scm_gc_cells_swept;
  269. double local_scm_gc_cells_marked;
  270. double local_scm_gc_cells_marked_conservatively;
  271. double local_scm_total_cells_allocated;
  272. SCM answer;
  273. unsigned long *bounds = 0;
  274. int table_size = 0;
  275. SCM_CRITICAL_SECTION_START;
  276. bounds = scm_i_segment_table_info (&table_size);
  277. /* Below, we cons to produce the resulting list. We want a snapshot of
  278. * the heap situation before consing.
  279. */
  280. local_scm_mtrigger = scm_mtrigger;
  281. local_scm_mallocated = scm_mallocated;
  282. local_scm_heap_size =
  283. (scm_i_master_freelist.heap_total_cells + scm_i_master_freelist2.heap_total_cells);
  284. local_scm_cells_allocated =
  285. scm_cells_allocated + scm_i_gc_sweep_stats.collected;
  286. local_scm_gc_time_taken = scm_gc_time_taken;
  287. local_scm_gc_mark_time_taken = scm_gc_mark_time_taken;
  288. local_scm_gc_times = scm_gc_times;
  289. local_scm_gc_malloc_yield_percentage = scm_gc_malloc_yield_percentage;
  290. local_scm_gc_cell_yield_percentage = scm_gc_cell_yield_percentage;
  291. local_protected_obj_count = protected_obj_count;
  292. local_scm_gc_cells_swept =
  293. (double) scm_gc_cells_swept_acc
  294. + (double) scm_i_gc_sweep_stats.swept;
  295. local_scm_gc_cells_marked = scm_gc_cells_marked_acc
  296. + (double) scm_i_gc_sweep_stats.swept
  297. - (double) scm_i_gc_sweep_stats.collected;
  298. local_scm_gc_cells_marked_conservatively
  299. = scm_gc_cells_marked_conservatively_acc;
  300. local_scm_total_cells_allocated = scm_gc_cells_allocated_acc
  301. + (double) scm_i_gc_sweep_stats.collected;
  302. for (i = table_size; i--;)
  303. {
  304. heap_segs = scm_cons (scm_cons (scm_from_ulong (bounds[2*i]),
  305. scm_from_ulong (bounds[2*i+1])),
  306. heap_segs);
  307. }
  308. /* njrev: can any of these scm_cons's or scm_list_n signal a memory
  309. error? If so we need a frame here. */
  310. answer =
  311. scm_list_n (scm_cons (sym_gc_time_taken,
  312. scm_from_ulong (local_scm_gc_time_taken)),
  313. scm_cons (sym_cells_allocated,
  314. scm_from_ulong (local_scm_cells_allocated)),
  315. scm_cons (sym_total_cells_allocated,
  316. scm_from_double (local_scm_total_cells_allocated)),
  317. scm_cons (sym_heap_size,
  318. scm_from_ulong (local_scm_heap_size)),
  319. scm_cons (sym_cells_marked_conservatively,
  320. scm_from_ulong (local_scm_gc_cells_marked_conservatively)),
  321. scm_cons (sym_mallocated,
  322. scm_from_ulong (local_scm_mallocated)),
  323. scm_cons (sym_mtrigger,
  324. scm_from_ulong (local_scm_mtrigger)),
  325. scm_cons (sym_times,
  326. scm_from_ulong (local_scm_gc_times)),
  327. scm_cons (sym_gc_mark_time_taken,
  328. scm_from_ulong (local_scm_gc_mark_time_taken)),
  329. scm_cons (sym_cells_marked,
  330. scm_from_double (local_scm_gc_cells_marked)),
  331. scm_cons (sym_cells_swept,
  332. scm_from_double (local_scm_gc_cells_swept)),
  333. scm_cons (sym_malloc_yield,
  334. scm_from_long (local_scm_gc_malloc_yield_percentage)),
  335. scm_cons (sym_cell_yield,
  336. scm_from_long (local_scm_gc_cell_yield_percentage)),
  337. scm_cons (sym_protected_objects,
  338. scm_from_ulong (local_protected_obj_count)),
  339. scm_cons (sym_heap_segments, heap_segs),
  340. SCM_UNDEFINED);
  341. SCM_CRITICAL_SECTION_END;
  342. free (bounds);
  343. return answer;
  344. }
  345. #undef FUNC_NAME
  346. /*
  347. Update nice-to-know-statistics.
  348. */
  349. static void
  350. gc_end_stats ()
  351. {
  352. /* CELLS SWEPT is another word for the number of cells that were examined
  353. during GC. YIELD is the number that we cleaned out. MARKED is the number
  354. that weren't cleaned. */
  355. scm_gc_cell_yield_percentage = (scm_i_gc_sweep_stats.collected * 100) /
  356. (scm_i_master_freelist.heap_total_cells + scm_i_master_freelist2.heap_total_cells);
  357. scm_gc_cells_allocated_acc +=
  358. (double) scm_i_gc_sweep_stats.collected;
  359. scm_gc_cells_marked_acc += (double) scm_i_last_marked_cell_count;
  360. scm_gc_cells_marked_conservatively_acc += (double) scm_i_find_heap_calls;
  361. scm_gc_cells_swept_acc += (double) scm_i_gc_sweep_stats.swept;
  362. ++scm_gc_times;
  363. }
  364. SCM_DEFINE (scm_object_address, "object-address", 1, 0, 0,
  365. (SCM obj),
  366. "Return an integer that for the lifetime of @var{obj} is uniquely\n"
  367. "returned by this function for @var{obj}")
  368. #define FUNC_NAME s_scm_object_address
  369. {
  370. return scm_from_ulong (SCM_UNPACK (obj));
  371. }
  372. #undef FUNC_NAME
  373. SCM_DEFINE (scm_gc, "gc", 0, 0, 0,
  374. (),
  375. "Scans all of SCM objects and reclaims for further use those that are\n"
  376. "no longer accessible.")
  377. #define FUNC_NAME s_scm_gc
  378. {
  379. scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
  380. scm_gc_running_p = 1;
  381. scm_i_gc ("call");
  382. /* njrev: It looks as though other places, e.g. scm_realloc,
  383. can call scm_i_gc without acquiring the sweep mutex. Does this
  384. matter? Also scm_i_gc (or its descendants) touch the
  385. scm_sys_protects, which are protected in some cases
  386. (e.g. scm_permobjs above in scm_gc_stats) by a critical section,
  387. not by the sweep mutex. Shouldn't all the GC-relevant objects be
  388. protected in the same way? */
  389. scm_gc_running_p = 0;
  390. scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
  391. scm_c_hook_run (&scm_after_gc_c_hook, 0);
  392. return SCM_UNSPECIFIED;
  393. }
  394. #undef FUNC_NAME
  395. /* The master is global and common while the freelist will be
  396. * individual for each thread.
  397. */
  398. SCM
  399. scm_gc_for_newcell (scm_t_cell_type_statistics *freelist, SCM *free_cells)
  400. {
  401. SCM cell;
  402. int did_gc = 0;
  403. scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
  404. scm_gc_running_p = 1;
  405. *free_cells = scm_i_sweep_for_freelist (freelist);
  406. if (*free_cells == SCM_EOL)
  407. {
  408. float delta = scm_i_gc_heap_size_delta (freelist);
  409. if (delta > 0.0)
  410. {
  411. size_t bytes = ((unsigned long) delta) * sizeof (scm_t_cell);
  412. freelist->heap_segment_idx =
  413. scm_i_get_new_heap_segment (freelist, bytes, abort_on_error);
  414. *free_cells = scm_i_sweep_for_freelist (freelist);
  415. }
  416. }
  417. if (*free_cells == SCM_EOL)
  418. {
  419. /*
  420. out of fresh cells. Try to get some new ones.
  421. */
  422. char reason[] = "0-cells";
  423. reason[0] += freelist->span;
  424. did_gc = 1;
  425. scm_i_gc (reason);
  426. *free_cells = scm_i_sweep_for_freelist (freelist);
  427. }
  428. if (*free_cells == SCM_EOL)
  429. {
  430. /*
  431. failed getting new cells. Get new juice or die.
  432. */
  433. float delta = scm_i_gc_heap_size_delta (freelist);
  434. assert (delta > 0.0);
  435. size_t bytes = ((unsigned long) delta) * sizeof (scm_t_cell);
  436. freelist->heap_segment_idx =
  437. scm_i_get_new_heap_segment (freelist, bytes, abort_on_error);
  438. *free_cells = scm_i_sweep_for_freelist (freelist);
  439. }
  440. if (*free_cells == SCM_EOL)
  441. abort ();
  442. cell = *free_cells;
  443. *free_cells = SCM_FREE_CELL_CDR (cell);
  444. scm_gc_running_p = 0;
  445. scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
  446. if (did_gc)
  447. scm_c_hook_run (&scm_after_gc_c_hook, 0);
  448. return cell;
  449. }
  450. scm_t_c_hook scm_before_gc_c_hook;
  451. scm_t_c_hook scm_before_mark_c_hook;
  452. scm_t_c_hook scm_before_sweep_c_hook;
  453. scm_t_c_hook scm_after_sweep_c_hook;
  454. scm_t_c_hook scm_after_gc_c_hook;
  455. static void
  456. scm_check_deprecated_memory_return ()
  457. {
  458. if (scm_mallocated < scm_i_deprecated_memory_return)
  459. {
  460. /* The byte count of allocated objects has underflowed. This is
  461. probably because you forgot to report the sizes of objects you
  462. have allocated, by calling scm_done_malloc or some such. When
  463. the GC freed them, it subtracted their size from
  464. scm_mallocated, which underflowed. */
  465. fprintf (stderr,
  466. "scm_gc_sweep: Byte count of allocated objects has underflowed.\n"
  467. "This is probably because the GC hasn't been correctly informed\n"
  468. "about object sizes\n");
  469. abort ();
  470. }
  471. scm_mallocated -= scm_i_deprecated_memory_return;
  472. scm_i_deprecated_memory_return = 0;
  473. }
  474. long int scm_i_last_marked_cell_count;
  475. /* Must be called while holding scm_i_sweep_mutex.
  476. This function is fairly long, but it touches various global
  477. variables. To not obscure the side effects on global variables,
  478. this function has not been split up.
  479. */
  480. void
  481. scm_i_gc (const char *what)
  482. {
  483. unsigned long t_before_gc = 0;
  484. scm_i_thread_put_to_sleep ();
  485. scm_c_hook_run (&scm_before_gc_c_hook, 0);
  486. #ifdef DEBUGINFO
  487. fprintf (stderr,"gc reason %s\n", what);
  488. fprintf (stderr,
  489. scm_is_null (*SCM_FREELIST_LOC (scm_i_freelist))
  490. ? "*"
  491. : (scm_is_null (*SCM_FREELIST_LOC (scm_i_freelist2)) ? "o" : "m"));
  492. #endif
  493. t_before_gc = scm_c_get_internal_run_time ();
  494. scm_gc_malloc_collected = 0;
  495. /*
  496. Set freelists to NULL so scm_cons () always triggers gc, causing
  497. the assertion above to fail.
  498. */
  499. *SCM_FREELIST_LOC (scm_i_freelist) = SCM_EOL;
  500. *SCM_FREELIST_LOC (scm_i_freelist2) = SCM_EOL;
  501. /*
  502. Let's finish the sweep. The conservative GC might point into the
  503. garbage, and marking that would create a mess.
  504. */
  505. scm_i_sweep_all_segments ("GC", &scm_i_gc_sweep_stats);
  506. scm_check_deprecated_memory_return ();
  507. #if (SCM_DEBUG_CELL_ACCESSES == 0 && SCM_SIZEOF_UNSIGNED_LONG == 4)
  508. /* Sanity check our numbers. */
  509. /* TODO(hanwen): figure out why the stats are off on x64_64. */
  510. /* If this was not true, someone touched mark bits outside of the
  511. mark phase. */
  512. if (scm_i_last_marked_cell_count != scm_i_marked_count ())
  513. {
  514. static char msg[] =
  515. "The number of marked objects changed since the last GC: %d vs %d.";
  516. /* At some point, we should probably use a deprecation warning. */
  517. fprintf(stderr, msg, scm_i_last_marked_cell_count, scm_i_marked_count ());
  518. }
  519. assert (scm_i_gc_sweep_stats.swept
  520. == (scm_i_master_freelist.heap_total_cells
  521. + scm_i_master_freelist2.heap_total_cells));
  522. assert (scm_i_gc_sweep_stats.collected + scm_i_last_marked_cell_count
  523. == scm_i_gc_sweep_stats.swept);
  524. #endif /* SCM_DEBUG_CELL_ACCESSES */
  525. /* Mark */
  526. scm_c_hook_run (&scm_before_mark_c_hook, 0);
  527. scm_mark_all ();
  528. scm_gc_mark_time_taken += (scm_c_get_internal_run_time () - t_before_gc);
  529. scm_i_last_marked_cell_count = scm_cells_allocated = scm_i_marked_count ();
  530. /* Sweep
  531. TODO: the after_sweep hook should probably be moved to just before
  532. the mark, since that's where the sweep is finished in lazy
  533. sweeping.
  534. MDJ 030219 <djurfeldt@nada.kth.se>: No, probably not. The
  535. original meaning implied at least two things: that it would be
  536. called when
  537. 1. the freelist is re-initialized (no evaluation possible, though)
  538. and
  539. 2. the heap is "fresh"
  540. (it is well-defined what data is used and what is not)
  541. Neither of these conditions would hold just before the mark phase.
  542. Of course, the lazy sweeping has muddled the distinction between
  543. scm_before_sweep_c_hook and scm_after_sweep_c_hook, but even if
  544. there were no difference, it would still be useful to have two
  545. distinct classes of hook functions since this can prevent some
  546. bad interference when several modules adds gc hooks.
  547. */
  548. scm_c_hook_run (&scm_before_sweep_c_hook, 0);
  549. /*
  550. Nothing here: lazy sweeping.
  551. */
  552. scm_i_reset_segments ();
  553. *SCM_FREELIST_LOC (scm_i_freelist) = SCM_EOL;
  554. *SCM_FREELIST_LOC (scm_i_freelist2) = SCM_EOL;
  555. /* Invalidate the freelists of other threads. */
  556. scm_i_thread_invalidate_freelists ();
  557. scm_c_hook_run (&scm_after_sweep_c_hook, 0);
  558. gc_end_stats ();
  559. scm_i_gc_sweep_stats.collected = scm_i_gc_sweep_stats.swept = 0;
  560. scm_i_gc_sweep_freelist_reset (&scm_i_master_freelist);
  561. scm_i_gc_sweep_freelist_reset (&scm_i_master_freelist2);
  562. /* Arguably, this statistic is fairly useless: marking will dominate
  563. the time taken.
  564. */
  565. scm_gc_time_taken += (scm_c_get_internal_run_time () - t_before_gc);
  566. scm_i_thread_wake_up ();
  567. /*
  568. For debugging purposes, you could do
  569. scm_i_sweep_all_segments ("debug"), but then the remains of the
  570. cell aren't left to analyse.
  571. */
  572. }
  573. /* {GC Protection Helper Functions}
  574. */
  575. /*
  576. * If within a function you need to protect one or more scheme objects from
  577. * garbage collection, pass them as parameters to one of the
  578. * scm_remember_upto_here* functions below. These functions don't do
  579. * anything, but since the compiler does not know that they are actually
  580. * no-ops, it will generate code that calls these functions with the given
  581. * parameters. Therefore, you can be sure that the compiler will keep those
  582. * scheme values alive (on the stack or in a register) up to the point where
  583. * scm_remember_upto_here* is called. In other words, place the call to
  584. * scm_remember_upto_here* _behind_ the last code in your function, that
  585. * depends on the scheme object to exist.
  586. *
  587. * Example: We want to make sure that the string object str does not get
  588. * garbage collected during the execution of 'some_function' in the code
  589. * below, because otherwise the characters belonging to str would be freed and
  590. * 'some_function' might access freed memory. To make sure that the compiler
  591. * keeps str alive on the stack or in a register such that it is visible to
  592. * the conservative gc we add the call to scm_remember_upto_here_1 _after_ the
  593. * call to 'some_function'. Note that this would not be necessary if str was
  594. * used anyway after the call to 'some_function'.
  595. * char *chars = scm_i_string_chars (str);
  596. * some_function (chars);
  597. * scm_remember_upto_here_1 (str); // str will be alive up to this point.
  598. */
  599. /* Remove any macro versions of these while defining the functions.
  600. Functions are always included in the library, for upward binary
  601. compatibility and in case combinations of GCC and non-GCC are used. */
  602. #undef scm_remember_upto_here_1
  603. #undef scm_remember_upto_here_2
  604. void
  605. scm_remember_upto_here_1 (SCM obj SCM_UNUSED)
  606. {
  607. /* Empty. Protects a single object from garbage collection. */
  608. }
  609. void
  610. scm_remember_upto_here_2 (SCM obj1 SCM_UNUSED, SCM obj2 SCM_UNUSED)
  611. {
  612. /* Empty. Protects two objects from garbage collection. */
  613. }
  614. void
  615. scm_remember_upto_here (SCM obj SCM_UNUSED, ...)
  616. {
  617. /* Empty. Protects any number of objects from garbage collection. */
  618. }
  619. /*
  620. These crazy functions prevent garbage collection
  621. of arguments after the first argument by
  622. ensuring they remain live throughout the
  623. function because they are used in the last
  624. line of the code block.
  625. It'd be better to have a nice compiler hint to
  626. aid the conservative stack-scanning GC. --03/09/00 gjb */
  627. SCM
  628. scm_return_first (SCM elt, ...)
  629. {
  630. return elt;
  631. }
  632. int
  633. scm_return_first_int (int i, ...)
  634. {
  635. return i;
  636. }
  637. SCM
  638. scm_permanent_object (SCM obj)
  639. {
  640. SCM cell = scm_cons (obj, SCM_EOL);
  641. SCM_CRITICAL_SECTION_START;
  642. SCM_SETCDR (cell, scm_permobjs);
  643. scm_permobjs = cell;
  644. SCM_CRITICAL_SECTION_END;
  645. return obj;
  646. }
  647. /* Protect OBJ from the garbage collector. OBJ will not be freed, even if all
  648. other references are dropped, until the object is unprotected by calling
  649. scm_gc_unprotect_object (OBJ). Calls to scm_gc_protect/unprotect_object nest,
  650. i. e. it is possible to protect the same object several times, but it is
  651. necessary to unprotect the object the same number of times to actually get
  652. the object unprotected. It is an error to unprotect an object more often
  653. than it has been protected before. The function scm_protect_object returns
  654. OBJ.
  655. */
  656. /* Implementation note: For every object X, there is a counter which
  657. scm_gc_protect_object (X) increments and scm_gc_unprotect_object (X) decrements.
  658. */
  659. SCM
  660. scm_gc_protect_object (SCM obj)
  661. {
  662. SCM handle;
  663. /* This critical section barrier will be replaced by a mutex. */
  664. /* njrev: Indeed; if my comment above is correct, there is the same
  665. critsec/mutex inconsistency here. */
  666. SCM_CRITICAL_SECTION_START;
  667. handle = scm_hashq_create_handle_x (scm_protects, obj, scm_from_int (0));
  668. SCM_SETCDR (handle, scm_sum (SCM_CDR (handle), scm_from_int (1)));
  669. protected_obj_count ++;
  670. SCM_CRITICAL_SECTION_END;
  671. return obj;
  672. }
  673. /* Remove any protection for OBJ established by a prior call to
  674. scm_protect_object. This function returns OBJ.
  675. See scm_protect_object for more information. */
  676. SCM
  677. scm_gc_unprotect_object (SCM obj)
  678. {
  679. SCM handle;
  680. /* This critical section barrier will be replaced by a mutex. */
  681. /* njrev: and again. */
  682. SCM_CRITICAL_SECTION_START;
  683. if (scm_gc_running_p)
  684. {
  685. fprintf (stderr, "scm_unprotect_object called during GC.\n");
  686. abort ();
  687. }
  688. handle = scm_hashq_get_handle (scm_protects, obj);
  689. if (scm_is_false (handle))
  690. {
  691. fprintf (stderr, "scm_unprotect_object called on unprotected object\n");
  692. abort ();
  693. }
  694. else
  695. {
  696. SCM count = scm_difference (SCM_CDR (handle), scm_from_int (1));
  697. if (scm_is_eq (count, scm_from_int (0)))
  698. scm_hashq_remove_x (scm_protects, obj);
  699. else
  700. SCM_SETCDR (handle, count);
  701. }
  702. protected_obj_count --;
  703. SCM_CRITICAL_SECTION_END;
  704. return obj;
  705. }
  706. void
  707. scm_gc_register_root (SCM *p)
  708. {
  709. SCM handle;
  710. SCM key = scm_from_ulong ((unsigned long) p);
  711. /* This critical section barrier will be replaced by a mutex. */
  712. /* njrev: and again. */
  713. SCM_CRITICAL_SECTION_START;
  714. handle = scm_hashv_create_handle_x (scm_gc_registered_roots, key,
  715. scm_from_int (0));
  716. /* njrev: note also that the above can probably signal an error */
  717. SCM_SETCDR (handle, scm_sum (SCM_CDR (handle), scm_from_int (1)));
  718. SCM_CRITICAL_SECTION_END;
  719. }
  720. void
  721. scm_gc_unregister_root (SCM *p)
  722. {
  723. SCM handle;
  724. SCM key = scm_from_ulong ((unsigned long) p);
  725. /* This critical section barrier will be replaced by a mutex. */
  726. /* njrev: and again. */
  727. SCM_CRITICAL_SECTION_START;
  728. handle = scm_hashv_get_handle (scm_gc_registered_roots, key);
  729. if (scm_is_false (handle))
  730. {
  731. fprintf (stderr, "scm_gc_unregister_root called on unregistered root\n");
  732. abort ();
  733. }
  734. else
  735. {
  736. SCM count = scm_difference (SCM_CDR (handle), scm_from_int (1));
  737. if (scm_is_eq (count, scm_from_int (0)))
  738. scm_hashv_remove_x (scm_gc_registered_roots, key);
  739. else
  740. SCM_SETCDR (handle, count);
  741. }
  742. SCM_CRITICAL_SECTION_END;
  743. }
  744. void
  745. scm_gc_register_roots (SCM *b, unsigned long n)
  746. {
  747. SCM *p = b;
  748. for (; p < b + n; ++p)
  749. scm_gc_register_root (p);
  750. }
  751. void
  752. scm_gc_unregister_roots (SCM *b, unsigned long n)
  753. {
  754. SCM *p = b;
  755. for (; p < b + n; ++p)
  756. scm_gc_unregister_root (p);
  757. }
  758. int scm_i_terminating;
  759. /*
  760. MOVE THIS FUNCTION. IT DOES NOT HAVE ANYTHING TODO WITH GC.
  761. */
  762. /* Get an integer from an environment variable. */
  763. int
  764. scm_getenv_int (const char *var, int def)
  765. {
  766. char *end = 0;
  767. char *val = getenv (var);
  768. long res = def;
  769. if (!val)
  770. return def;
  771. res = strtol (val, &end, 10);
  772. if (end == val)
  773. return def;
  774. return res;
  775. }
  776. void
  777. scm_storage_prehistory ()
  778. {
  779. scm_c_hook_init (&scm_before_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
  780. scm_c_hook_init (&scm_before_mark_c_hook, 0, SCM_C_HOOK_NORMAL);
  781. scm_c_hook_init (&scm_before_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
  782. scm_c_hook_init (&scm_after_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
  783. scm_c_hook_init (&scm_after_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
  784. }
  785. scm_i_pthread_mutex_t scm_i_gc_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  786. int
  787. scm_init_storage ()
  788. {
  789. size_t j;
  790. j = SCM_NUM_PROTECTS;
  791. while (j)
  792. scm_sys_protects[--j] = SCM_BOOL_F;
  793. scm_gc_init_freelist ();
  794. scm_gc_init_malloc ();
  795. #if 0
  796. /* We can't have a cleanup handler since we have no thread to run it
  797. in. */
  798. #ifdef HAVE_ATEXIT
  799. atexit (cleanup);
  800. #else
  801. #ifdef HAVE_ON_EXIT
  802. on_exit (cleanup, 0);
  803. #endif
  804. #endif
  805. #endif
  806. scm_stand_in_procs = scm_make_weak_key_hash_table (scm_from_int (257));
  807. scm_permobjs = SCM_EOL;
  808. scm_protects = scm_c_make_hash_table (31);
  809. scm_gc_registered_roots = scm_c_make_hash_table (31);
  810. return 0;
  811. }
  812. SCM scm_after_gc_hook;
  813. static SCM gc_async;
  814. /* The function gc_async_thunk causes the execution of the after-gc-hook. It
  815. * is run after the gc, as soon as the asynchronous events are handled by the
  816. * evaluator.
  817. */
  818. static SCM
  819. gc_async_thunk (void)
  820. {
  821. scm_c_run_hook (scm_after_gc_hook, SCM_EOL);
  822. return SCM_UNSPECIFIED;
  823. }
  824. /* The function mark_gc_async is run by the scm_after_gc_c_hook at the end of
  825. * the garbage collection. The only purpose of this function is to mark the
  826. * gc_async (which will eventually lead to the execution of the
  827. * gc_async_thunk).
  828. */
  829. static void *
  830. mark_gc_async (void * hook_data SCM_UNUSED,
  831. void *fn_data SCM_UNUSED,
  832. void *data SCM_UNUSED)
  833. {
  834. /* If cell access debugging is enabled, the user may choose to perform
  835. * additional garbage collections after an arbitrary number of cell
  836. * accesses. We don't want the scheme level after-gc-hook to be performed
  837. * for each of these garbage collections for the following reason: The
  838. * execution of the after-gc-hook causes cell accesses itself. Thus, if the
  839. * after-gc-hook was performed with every gc, and if the gc was performed
  840. * after a very small number of cell accesses, then the number of cell
  841. * accesses during the execution of the after-gc-hook will suffice to cause
  842. * the execution of the next gc. Then, guile would keep executing the
  843. * after-gc-hook over and over again, and would never come to do other
  844. * things.
  845. *
  846. * To overcome this problem, if cell access debugging with additional
  847. * garbage collections is enabled, the after-gc-hook is never run by the
  848. * garbage collecter. When running guile with cell access debugging and the
  849. * execution of the after-gc-hook is desired, then it is necessary to run
  850. * the hook explicitly from the user code. This has the effect, that from
  851. * the scheme level point of view it seems that garbage collection is
  852. * performed with a much lower frequency than it actually is. Obviously,
  853. * this will not work for code that depends on a fixed one to one
  854. * relationship between the execution counts of the C level garbage
  855. * collection hooks and the execution count of the scheme level
  856. * after-gc-hook.
  857. */
  858. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  859. if (scm_debug_cells_gc_interval == 0)
  860. scm_system_async_mark (gc_async);
  861. #else
  862. scm_system_async_mark (gc_async);
  863. #endif
  864. return NULL;
  865. }
  866. void
  867. scm_init_gc ()
  868. {
  869. scm_gc_init_mark ();
  870. scm_after_gc_hook = scm_permanent_object (scm_make_hook (SCM_INUM0));
  871. scm_c_define ("after-gc-hook", scm_after_gc_hook);
  872. gc_async = scm_c_make_subr ("%gc-thunk", scm_tc7_subr_0,
  873. gc_async_thunk);
  874. scm_c_hook_add (&scm_after_gc_c_hook, mark_gc_async, NULL, 0);
  875. #include "libguile/gc.x"
  876. }
  877. #ifdef __ia64__
  878. # ifdef __hpux
  879. # include <sys/param.h>
  880. # include <sys/pstat.h>
  881. void *
  882. scm_ia64_register_backing_store_base (void)
  883. {
  884. struct pst_vm_status vm_status;
  885. int i = 0;
  886. while (pstat_getprocvm (&vm_status, sizeof (vm_status), 0, i++) == 1)
  887. if (vm_status.pst_type == PS_RSESTACK)
  888. return (void *) vm_status.pst_vaddr;
  889. abort ();
  890. }
  891. void *
  892. scm_ia64_ar_bsp (const void *ctx)
  893. {
  894. uint64_t bsp;
  895. __uc_get_ar_bsp (ctx, &bsp);
  896. return (void *) bsp;
  897. }
  898. # endif /* hpux */
  899. # ifdef linux
  900. # include <ucontext.h>
  901. void *
  902. scm_ia64_register_backing_store_base (void)
  903. {
  904. extern void *__libc_ia64_register_backing_store_base;
  905. return __libc_ia64_register_backing_store_base;
  906. }
  907. void *
  908. scm_ia64_ar_bsp (const void *opaque)
  909. {
  910. const ucontext_t *ctx = opaque;
  911. return (void *) ctx->uc_mcontext.sc_ar_bsp;
  912. }
  913. # endif /* linux */
  914. #endif /* __ia64__ */
  915. void
  916. scm_gc_sweep (void)
  917. #define FUNC_NAME "scm_gc_sweep"
  918. {
  919. }
  920. #undef FUNC_NAME
  921. /*
  922. Local Variables:
  923. c-file-style: "gnu"
  924. End:
  925. */