gc.c 30 KB

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