gc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006,
  2. * 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. /* #define DEBUGINFO */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include "libguile/gen-scmconfig.h"
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <math.h>
  29. #ifdef __ia64__
  30. #include <ucontext.h>
  31. extern unsigned long * __libc_ia64_register_backing_store_base;
  32. #endif
  33. #include "libguile/_scm.h"
  34. #include "libguile/eval.h"
  35. #include "libguile/stime.h"
  36. #include "libguile/stackchk.h"
  37. #include "libguile/struct.h"
  38. #include "libguile/smob.h"
  39. #include "libguile/arrays.h"
  40. #include "libguile/async.h"
  41. #include "libguile/ports.h"
  42. #include "libguile/root.h"
  43. #include "libguile/simpos.h"
  44. #include "libguile/strings.h"
  45. #include "libguile/vectors.h"
  46. #include "libguile/hashtab.h"
  47. #include "libguile/tags.h"
  48. #include "libguile/validate.h"
  49. #include "libguile/deprecation.h"
  50. #include "libguile/gc.h"
  51. #include "libguile/dynwind.h"
  52. #include "libguile/bdw-gc.h"
  53. /* For GC_set_start_callback. */
  54. #include <gc/gc_mark.h>
  55. #ifdef GUILE_DEBUG_MALLOC
  56. #include "libguile/debug-malloc.h"
  57. #endif
  58. #include <unistd.h>
  59. /* Size in bytes of the initial heap. This should be about the size of
  60. result of 'guile -c "(display (assq-ref (gc-stats)
  61. 'heap-total-allocated))"'. */
  62. #define DEFAULT_INITIAL_HEAP_SIZE (128 * 1024 * SIZEOF_SCM_T_BITS)
  63. /* Set this to != 0 if every cell that is accessed shall be checked:
  64. */
  65. int scm_debug_cell_accesses_p = 0;
  66. int scm_expensive_debug_cell_accesses_p = 0;
  67. /* Set this to 0 if no additional gc's shall be performed, otherwise set it to
  68. * the number of cell accesses after which a gc shall be called.
  69. */
  70. int scm_debug_cells_gc_interval = 0;
  71. /* Hash table that keeps a reference to objects the user wants to protect from
  72. garbage collection. */
  73. static SCM scm_protects;
  74. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  75. /*
  76. Assert that the given object is a valid reference to a valid cell. This
  77. test involves to determine whether the object is a cell pointer, whether
  78. this pointer actually points into a heap segment and whether the cell
  79. pointed to is not a free cell. Further, additional garbage collections may
  80. get executed after a user defined number of cell accesses. This helps to
  81. find places in the C code where references are dropped for extremely short
  82. periods.
  83. */
  84. void
  85. scm_i_expensive_validation_check (SCM cell)
  86. {
  87. /* If desired, perform additional garbage collections after a user
  88. * defined number of cell accesses.
  89. */
  90. if (scm_debug_cells_gc_interval)
  91. {
  92. static unsigned int counter = 0;
  93. if (counter != 0)
  94. {
  95. --counter;
  96. }
  97. else
  98. {
  99. counter = scm_debug_cells_gc_interval;
  100. scm_gc ();
  101. }
  102. }
  103. }
  104. /* Whether cell validation is already running. */
  105. static int scm_i_cell_validation_already_running = 0;
  106. void
  107. scm_assert_cell_valid (SCM cell)
  108. {
  109. if (!scm_i_cell_validation_already_running && scm_debug_cell_accesses_p)
  110. {
  111. scm_i_cell_validation_already_running = 1; /* set to avoid recursion */
  112. /*
  113. During GC, no user-code should be run, and the guile core
  114. should use non-protected accessors.
  115. */
  116. if (scm_gc_running_p)
  117. return;
  118. /*
  119. Only scm_in_heap_p and rescanning the heap is wildly
  120. expensive.
  121. */
  122. if (scm_expensive_debug_cell_accesses_p)
  123. scm_i_expensive_validation_check (cell);
  124. scm_i_cell_validation_already_running = 0; /* re-enable */
  125. }
  126. }
  127. SCM_DEFINE (scm_set_debug_cell_accesses_x, "set-debug-cell-accesses!", 1, 0, 0,
  128. (SCM flag),
  129. "If @var{flag} is @code{#f}, cell access checking is disabled.\n"
  130. "If @var{flag} is @code{#t}, cheap cell access checking is enabled,\n"
  131. "but no additional calls to garbage collection are issued.\n"
  132. "If @var{flag} is a number, strict cell access checking is enabled,\n"
  133. "with an additional garbage collection after the given\n"
  134. "number of cell accesses.\n"
  135. "This procedure only exists when the compile-time flag\n"
  136. "@code{SCM_DEBUG_CELL_ACCESSES} was set to 1.")
  137. #define FUNC_NAME s_scm_set_debug_cell_accesses_x
  138. {
  139. if (scm_is_false (flag))
  140. {
  141. scm_debug_cell_accesses_p = 0;
  142. }
  143. else if (scm_is_eq (flag, SCM_BOOL_T))
  144. {
  145. scm_debug_cells_gc_interval = 0;
  146. scm_debug_cell_accesses_p = 1;
  147. scm_expensive_debug_cell_accesses_p = 0;
  148. }
  149. else
  150. {
  151. scm_debug_cells_gc_interval = scm_to_signed_integer (flag, 0, INT_MAX);
  152. scm_debug_cell_accesses_p = 1;
  153. scm_expensive_debug_cell_accesses_p = 1;
  154. }
  155. return SCM_UNSPECIFIED;
  156. }
  157. #undef FUNC_NAME
  158. #endif /* SCM_DEBUG_CELL_ACCESSES == 1 */
  159. static int needs_gc_after_nonlocal_exit = 0;
  160. /* Arrange to throw an exception on failed allocations. */
  161. static void*
  162. scm_oom_fn (size_t nbytes)
  163. {
  164. needs_gc_after_nonlocal_exit = 1;
  165. scm_report_out_of_memory ();
  166. return NULL;
  167. }
  168. /* Called within GC -- cannot allocate GC memory. */
  169. static void
  170. scm_gc_warn_proc (char *fmt, GC_word arg)
  171. {
  172. SCM port;
  173. FILE *stream = NULL;
  174. port = scm_current_warning_port ();
  175. if (!SCM_OPPORTP (port))
  176. return;
  177. if (SCM_FPORTP (port))
  178. {
  179. int fd;
  180. scm_force_output (port);
  181. if (!SCM_OPPORTP (port))
  182. return;
  183. fd = dup (SCM_FPORT_FDES (port));
  184. if (fd == -1)
  185. perror ("Failed to dup warning port fd");
  186. else
  187. {
  188. stream = fdopen (fd, "a");
  189. if (!stream)
  190. {
  191. perror ("Failed to open stream for warning port");
  192. close (fd);
  193. }
  194. }
  195. }
  196. fprintf (stream ? stream : stderr, fmt, arg);
  197. if (stream)
  198. fclose (stream);
  199. }
  200. void
  201. scm_gc_after_nonlocal_exit (void)
  202. {
  203. if (needs_gc_after_nonlocal_exit)
  204. {
  205. needs_gc_after_nonlocal_exit = 0;
  206. GC_gcollect_and_unmap ();
  207. }
  208. }
  209. /* Hooks. */
  210. scm_t_c_hook scm_before_gc_c_hook;
  211. scm_t_c_hook scm_before_mark_c_hook;
  212. scm_t_c_hook scm_before_sweep_c_hook;
  213. scm_t_c_hook scm_after_sweep_c_hook;
  214. scm_t_c_hook scm_after_gc_c_hook;
  215. static void
  216. run_before_gc_c_hook (void)
  217. {
  218. if (!SCM_I_CURRENT_THREAD)
  219. /* GC while a thread is spinning up; punt. */
  220. return;
  221. scm_c_hook_run (&scm_before_gc_c_hook, NULL);
  222. }
  223. /* GC Statistics Keeping
  224. */
  225. unsigned long scm_gc_ports_collected = 0;
  226. static long gc_time_taken = 0;
  227. static long gc_start_time = 0;
  228. static unsigned long free_space_divisor;
  229. static unsigned long minimum_free_space_divisor;
  230. static double target_free_space_divisor;
  231. static unsigned long protected_obj_count = 0;
  232. SCM_SYMBOL (sym_gc_time_taken, "gc-time-taken");
  233. SCM_SYMBOL (sym_heap_size, "heap-size");
  234. SCM_SYMBOL (sym_heap_free_size, "heap-free-size");
  235. SCM_SYMBOL (sym_heap_total_allocated, "heap-total-allocated");
  236. SCM_SYMBOL (sym_heap_allocated_since_gc, "heap-allocated-since-gc");
  237. SCM_SYMBOL (sym_protected_objects, "protected-objects");
  238. SCM_SYMBOL (sym_times, "gc-times");
  239. /* {Scheme Interface to GC}
  240. */
  241. extern int scm_gc_malloc_yield_percentage;
  242. SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
  243. (),
  244. "Return an association list of statistics about Guile's current\n"
  245. "use of storage.\n")
  246. #define FUNC_NAME s_scm_gc_stats
  247. {
  248. SCM answer;
  249. GC_word heap_size, free_bytes, unmapped_bytes, bytes_since_gc, total_bytes;
  250. size_t gc_times;
  251. GC_get_heap_usage_safe (&heap_size, &free_bytes, &unmapped_bytes,
  252. &bytes_since_gc, &total_bytes);
  253. gc_times = GC_get_gc_no ();
  254. answer =
  255. scm_list_n (scm_cons (sym_gc_time_taken, scm_from_long (gc_time_taken)),
  256. scm_cons (sym_heap_size, scm_from_size_t (heap_size)),
  257. scm_cons (sym_heap_free_size, scm_from_size_t (free_bytes)),
  258. scm_cons (sym_heap_total_allocated,
  259. scm_from_size_t (total_bytes)),
  260. scm_cons (sym_heap_allocated_since_gc,
  261. scm_from_size_t (bytes_since_gc)),
  262. scm_cons (sym_protected_objects,
  263. scm_from_ulong (protected_obj_count)),
  264. scm_cons (sym_times, scm_from_size_t (gc_times)),
  265. SCM_UNDEFINED);
  266. return answer;
  267. }
  268. #undef FUNC_NAME
  269. SCM_DEFINE (scm_gc_dump, "gc-dump", 0, 0, 0,
  270. (void),
  271. "Dump information about the garbage collector's internal data "
  272. "structures and memory usage to the standard output.")
  273. #define FUNC_NAME s_scm_gc_dump
  274. {
  275. GC_dump ();
  276. return SCM_UNSPECIFIED;
  277. }
  278. #undef FUNC_NAME
  279. SCM_DEFINE (scm_object_address, "object-address", 1, 0, 0,
  280. (SCM obj),
  281. "Return an integer that for the lifetime of @var{obj} is uniquely\n"
  282. "returned by this function for @var{obj}")
  283. #define FUNC_NAME s_scm_object_address
  284. {
  285. return scm_from_ulong (SCM_UNPACK (obj));
  286. }
  287. #undef FUNC_NAME
  288. SCM_DEFINE (scm_gc_disable, "gc-disable", 0, 0, 0,
  289. (),
  290. "Disables the garbage collector. Nested calls are permitted. "
  291. "GC is re-enabled once @code{gc-enable} has been called the "
  292. "same number of times @code{gc-disable} was called.")
  293. #define FUNC_NAME s_scm_gc_disable
  294. {
  295. GC_disable ();
  296. return SCM_UNSPECIFIED;
  297. }
  298. #undef FUNC_NAME
  299. SCM_DEFINE (scm_gc_enable, "gc-enable", 0, 0, 0,
  300. (),
  301. "Enables the garbage collector.")
  302. #define FUNC_NAME s_scm_gc_enable
  303. {
  304. GC_enable ();
  305. return SCM_UNSPECIFIED;
  306. }
  307. #undef FUNC_NAME
  308. SCM_DEFINE (scm_gc, "gc", 0, 0, 0,
  309. (),
  310. "Scans all of SCM objects and reclaims for further use those that are\n"
  311. "no longer accessible.")
  312. #define FUNC_NAME s_scm_gc
  313. {
  314. scm_i_gc ("call");
  315. /* If you're calling scm_gc(), you probably want synchronous
  316. finalization. */
  317. GC_invoke_finalizers ();
  318. return SCM_UNSPECIFIED;
  319. }
  320. #undef FUNC_NAME
  321. void
  322. scm_i_gc (const char *what)
  323. {
  324. GC_gcollect ();
  325. }
  326. /* {GC Protection Helper Functions}
  327. */
  328. /*
  329. * If within a function you need to protect one or more scheme objects from
  330. * garbage collection, pass them as parameters to one of the
  331. * scm_remember_upto_here* functions below. These functions don't do
  332. * anything, but since the compiler does not know that they are actually
  333. * no-ops, it will generate code that calls these functions with the given
  334. * parameters. Therefore, you can be sure that the compiler will keep those
  335. * scheme values alive (on the stack or in a register) up to the point where
  336. * scm_remember_upto_here* is called. In other words, place the call to
  337. * scm_remember_upto_here* _behind_ the last code in your function, that
  338. * depends on the scheme object to exist.
  339. *
  340. * Example: We want to make sure that the string object str does not get
  341. * garbage collected during the execution of 'some_function' in the code
  342. * below, because otherwise the characters belonging to str would be freed and
  343. * 'some_function' might access freed memory. To make sure that the compiler
  344. * keeps str alive on the stack or in a register such that it is visible to
  345. * the conservative gc we add the call to scm_remember_upto_here_1 _after_ the
  346. * call to 'some_function'. Note that this would not be necessary if str was
  347. * used anyway after the call to 'some_function'.
  348. * char *chars = scm_i_string_chars (str);
  349. * some_function (chars);
  350. * scm_remember_upto_here_1 (str); // str will be alive up to this point.
  351. */
  352. /* Remove any macro versions of these while defining the functions.
  353. Functions are always included in the library, for upward binary
  354. compatibility and in case combinations of GCC and non-GCC are used. */
  355. #undef scm_remember_upto_here_1
  356. #undef scm_remember_upto_here_2
  357. void
  358. scm_remember_upto_here_1 (SCM obj SCM_UNUSED)
  359. {
  360. /* Empty. Protects a single object from garbage collection. */
  361. }
  362. void
  363. scm_remember_upto_here_2 (SCM obj1 SCM_UNUSED, SCM obj2 SCM_UNUSED)
  364. {
  365. /* Empty. Protects two objects from garbage collection. */
  366. }
  367. void
  368. scm_remember_upto_here (SCM obj SCM_UNUSED, ...)
  369. {
  370. /* Empty. Protects any number of objects from garbage collection. */
  371. }
  372. /*
  373. These crazy functions prevent garbage collection
  374. of arguments after the first argument by
  375. ensuring they remain live throughout the
  376. function because they are used in the last
  377. line of the code block.
  378. It'd be better to have a nice compiler hint to
  379. aid the conservative stack-scanning GC. --03/09/00 gjb */
  380. SCM
  381. scm_return_first (SCM elt, ...)
  382. {
  383. return elt;
  384. }
  385. int
  386. scm_return_first_int (int i, ...)
  387. {
  388. return i;
  389. }
  390. SCM
  391. scm_permanent_object (SCM obj)
  392. {
  393. return (scm_gc_protect_object (obj));
  394. }
  395. /* Protect OBJ from the garbage collector. OBJ will not be freed, even if all
  396. other references are dropped, until the object is unprotected by calling
  397. scm_gc_unprotect_object (OBJ). Calls to scm_gc_protect/unprotect_object nest,
  398. i. e. it is possible to protect the same object several times, but it is
  399. necessary to unprotect the object the same number of times to actually get
  400. the object unprotected. It is an error to unprotect an object more often
  401. than it has been protected before. The function scm_protect_object returns
  402. OBJ.
  403. */
  404. /* Implementation note: For every object X, there is a counter which
  405. scm_gc_protect_object (X) increments and scm_gc_unprotect_object (X) decrements.
  406. */
  407. SCM
  408. scm_gc_protect_object (SCM obj)
  409. {
  410. SCM handle;
  411. /* This critical section barrier will be replaced by a mutex. */
  412. /* njrev: Indeed; if my comment above is correct, there is the same
  413. critsec/mutex inconsistency here. */
  414. SCM_CRITICAL_SECTION_START;
  415. handle = scm_hashq_create_handle_x (scm_protects, obj, scm_from_int (0));
  416. SCM_SETCDR (handle, scm_sum (SCM_CDR (handle), scm_from_int (1)));
  417. protected_obj_count ++;
  418. SCM_CRITICAL_SECTION_END;
  419. return obj;
  420. }
  421. /* Remove any protection for OBJ established by a prior call to
  422. scm_protect_object. This function returns OBJ.
  423. See scm_protect_object for more information. */
  424. SCM
  425. scm_gc_unprotect_object (SCM obj)
  426. {
  427. SCM handle;
  428. /* This critical section barrier will be replaced by a mutex. */
  429. /* njrev: and again. */
  430. SCM_CRITICAL_SECTION_START;
  431. if (scm_gc_running_p)
  432. {
  433. fprintf (stderr, "scm_unprotect_object called during GC.\n");
  434. abort ();
  435. }
  436. handle = scm_hashq_get_handle (scm_protects, obj);
  437. if (scm_is_false (handle))
  438. {
  439. fprintf (stderr, "scm_unprotect_object called on unprotected object\n");
  440. abort ();
  441. }
  442. else
  443. {
  444. SCM count = scm_difference (SCM_CDR (handle), scm_from_int (1));
  445. if (scm_is_eq (count, scm_from_int (0)))
  446. scm_hashq_remove_x (scm_protects, obj);
  447. else
  448. SCM_SETCDR (handle, count);
  449. }
  450. protected_obj_count --;
  451. SCM_CRITICAL_SECTION_END;
  452. return obj;
  453. }
  454. void
  455. scm_gc_register_root (SCM *p)
  456. {
  457. /* Nothing. */
  458. }
  459. void
  460. scm_gc_unregister_root (SCM *p)
  461. {
  462. /* Nothing. */
  463. }
  464. void
  465. scm_gc_register_roots (SCM *b, unsigned long n)
  466. {
  467. SCM *p = b;
  468. for (; p < b + n; ++p)
  469. scm_gc_register_root (p);
  470. }
  471. void
  472. scm_gc_unregister_roots (SCM *b, unsigned long n)
  473. {
  474. SCM *p = b;
  475. for (; p < b + n; ++p)
  476. scm_gc_unregister_root (p);
  477. }
  478. void
  479. scm_storage_prehistory ()
  480. {
  481. GC_set_all_interior_pointers (0);
  482. free_space_divisor = scm_getenv_int ("GC_FREE_SPACE_DIVISOR", 3);
  483. minimum_free_space_divisor = free_space_divisor;
  484. target_free_space_divisor = free_space_divisor;
  485. GC_set_free_space_divisor (free_space_divisor);
  486. GC_set_finalize_on_demand (1);
  487. #if (GC_VERSION_MAJOR == 7 && GC_VERSION_MINOR == 4 \
  488. && GC_VERSION_MICRO == 0)
  489. /* BDW-GC 7.4.0 has a bug making it loop indefinitely when using more
  490. than one marker thread: <https://github.com/ivmai/bdwgc/pull/30>.
  491. Work around it by asking for one marker thread. */
  492. setenv ("GC_MARKERS", "1", 1);
  493. #endif
  494. GC_INIT ();
  495. GC_expand_hp (DEFAULT_INITIAL_HEAP_SIZE);
  496. /* We only need to register a displacement for those types for which the
  497. higher bits of the type tag are used to store a pointer (that is, a
  498. pointer to an 8-octet aligned region). For `scm_tc3_struct', this is
  499. handled in `scm_alloc_struct ()'. */
  500. GC_REGISTER_DISPLACEMENT (scm_tc3_cons);
  501. /* GC_REGISTER_DISPLACEMENT (scm_tc3_unused); */
  502. /* Sanity check. */
  503. if (!GC_is_visible (&scm_protects))
  504. abort ();
  505. scm_c_hook_init (&scm_before_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
  506. scm_c_hook_init (&scm_before_mark_c_hook, 0, SCM_C_HOOK_NORMAL);
  507. scm_c_hook_init (&scm_before_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
  508. scm_c_hook_init (&scm_after_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
  509. scm_c_hook_init (&scm_after_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
  510. }
  511. scm_i_pthread_mutex_t scm_i_gc_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  512. void
  513. scm_init_gc_protect_object ()
  514. {
  515. scm_protects = scm_c_make_hash_table (31);
  516. #if 0
  517. /* We can't have a cleanup handler since we have no thread to run it
  518. in. */
  519. #ifdef HAVE_ATEXIT
  520. atexit (cleanup);
  521. #else
  522. #ifdef HAVE_ON_EXIT
  523. on_exit (cleanup, 0);
  524. #endif
  525. #endif
  526. #endif
  527. }
  528. SCM scm_after_gc_hook;
  529. static SCM after_gc_async_cell;
  530. /* The function after_gc_async_thunk causes the execution of the
  531. * after-gc-hook. It is run after the gc, as soon as the asynchronous
  532. * events are handled by the evaluator.
  533. */
  534. static SCM
  535. after_gc_async_thunk (void)
  536. {
  537. /* Fun, no? Hook-run *and* run-hook? */
  538. scm_c_hook_run (&scm_after_gc_c_hook, NULL);
  539. scm_c_run_hook (scm_after_gc_hook, SCM_EOL);
  540. return SCM_UNSPECIFIED;
  541. }
  542. /* The function queue_after_gc_hook is run by the scm_before_gc_c_hook
  543. * at the end of the garbage collection. The only purpose of this
  544. * function is to mark the after_gc_async (which will eventually lead to
  545. * the execution of the after_gc_async_thunk).
  546. */
  547. static void *
  548. queue_after_gc_hook (void * hook_data SCM_UNUSED,
  549. void *fn_data SCM_UNUSED,
  550. void *data SCM_UNUSED)
  551. {
  552. /* If cell access debugging is enabled, the user may choose to perform
  553. * additional garbage collections after an arbitrary number of cell
  554. * accesses. We don't want the scheme level after-gc-hook to be performed
  555. * for each of these garbage collections for the following reason: The
  556. * execution of the after-gc-hook causes cell accesses itself. Thus, if the
  557. * after-gc-hook was performed with every gc, and if the gc was performed
  558. * after a very small number of cell accesses, then the number of cell
  559. * accesses during the execution of the after-gc-hook will suffice to cause
  560. * the execution of the next gc. Then, guile would keep executing the
  561. * after-gc-hook over and over again, and would never come to do other
  562. * things.
  563. *
  564. * To overcome this problem, if cell access debugging with additional
  565. * garbage collections is enabled, the after-gc-hook is never run by the
  566. * garbage collecter. When running guile with cell access debugging and the
  567. * execution of the after-gc-hook is desired, then it is necessary to run
  568. * the hook explicitly from the user code. This has the effect, that from
  569. * the scheme level point of view it seems that garbage collection is
  570. * performed with a much lower frequency than it actually is. Obviously,
  571. * this will not work for code that depends on a fixed one to one
  572. * relationship between the execution counts of the C level garbage
  573. * collection hooks and the execution count of the scheme level
  574. * after-gc-hook.
  575. */
  576. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  577. if (scm_debug_cells_gc_interval == 0)
  578. #endif
  579. {
  580. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  581. if (scm_is_false (SCM_CDR (after_gc_async_cell)))
  582. {
  583. SCM_SETCDR (after_gc_async_cell, t->active_asyncs);
  584. t->active_asyncs = after_gc_async_cell;
  585. t->pending_asyncs = 1;
  586. }
  587. }
  588. return NULL;
  589. }
  590. static void *
  591. start_gc_timer (void * hook_data SCM_UNUSED,
  592. void *fn_data SCM_UNUSED,
  593. void *data SCM_UNUSED)
  594. {
  595. if (!gc_start_time)
  596. gc_start_time = scm_c_get_internal_run_time ();
  597. return NULL;
  598. }
  599. static void *
  600. accumulate_gc_timer (void * hook_data SCM_UNUSED,
  601. void *fn_data SCM_UNUSED,
  602. void *data SCM_UNUSED)
  603. {
  604. if (gc_start_time)
  605. {
  606. long now = scm_c_get_internal_run_time ();
  607. gc_time_taken += now - gc_start_time;
  608. gc_start_time = 0;
  609. }
  610. return NULL;
  611. }
  612. static size_t bytes_until_gc = DEFAULT_INITIAL_HEAP_SIZE;
  613. static scm_i_pthread_mutex_t bytes_until_gc_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  614. void
  615. scm_gc_register_allocation (size_t size)
  616. {
  617. scm_i_pthread_mutex_lock (&bytes_until_gc_lock);
  618. if (bytes_until_gc - size > bytes_until_gc)
  619. {
  620. bytes_until_gc = GC_get_heap_size ();
  621. scm_i_pthread_mutex_unlock (&bytes_until_gc_lock);
  622. GC_gcollect ();
  623. }
  624. else
  625. {
  626. bytes_until_gc -= size;
  627. scm_i_pthread_mutex_unlock (&bytes_until_gc_lock);
  628. }
  629. }
  630. void
  631. scm_init_gc ()
  632. {
  633. /* `GC_INIT ()' was invoked in `scm_storage_prehistory ()'. */
  634. scm_after_gc_hook = scm_make_hook (SCM_INUM0);
  635. scm_c_define ("after-gc-hook", scm_after_gc_hook);
  636. /* When the async is to run, the cdr of the gc_async pair gets set to
  637. the asyncs queue of the current thread. */
  638. after_gc_async_cell = scm_cons (scm_c_make_gsubr ("%after-gc-thunk", 0, 0, 0,
  639. after_gc_async_thunk),
  640. SCM_BOOL_F);
  641. scm_c_hook_add (&scm_before_gc_c_hook, queue_after_gc_hook, NULL, 0);
  642. scm_c_hook_add (&scm_before_gc_c_hook, start_gc_timer, NULL, 0);
  643. scm_c_hook_add (&scm_after_gc_c_hook, accumulate_gc_timer, NULL, 0);
  644. GC_set_oom_fn (scm_oom_fn);
  645. GC_set_warn_proc (scm_gc_warn_proc);
  646. GC_set_start_callback (run_before_gc_c_hook);
  647. #include "libguile/gc.x"
  648. }
  649. void
  650. scm_gc_sweep (void)
  651. #define FUNC_NAME "scm_gc_sweep"
  652. {
  653. /* FIXME */
  654. fprintf (stderr, "%s: doing nothing\n", FUNC_NAME);
  655. }
  656. #undef FUNC_NAME
  657. /*
  658. Local Variables:
  659. c-file-style: "gnu"
  660. End:
  661. */