gc-mark.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2005, 2006 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. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #ifdef __ia64__
  25. #include <ucontext.h>
  26. extern unsigned long * __libc_ia64_register_backing_store_base;
  27. #endif
  28. #include "libguile/_scm.h"
  29. #include "libguile/eval.h"
  30. #include "libguile/stime.h"
  31. #include "libguile/stackchk.h"
  32. #include "libguile/struct.h"
  33. #include "libguile/smob.h"
  34. #include "libguile/unif.h"
  35. #include "libguile/async.h"
  36. #include "libguile/ports.h"
  37. #include "libguile/root.h"
  38. #include "libguile/strings.h"
  39. #include "libguile/vectors.h"
  40. #include "libguile/weaks.h"
  41. #include "libguile/hashtab.h"
  42. #include "libguile/tags.h"
  43. #include "libguile/private-gc.h"
  44. #include "libguile/validate.h"
  45. #include "libguile/deprecation.h"
  46. #include "libguile/gc.h"
  47. #include "libguile/guardians.h"
  48. #ifdef GUILE_DEBUG_MALLOC
  49. #include "libguile/debug-malloc.h"
  50. #endif
  51. #ifdef HAVE_MALLOC_H
  52. #include <malloc.h>
  53. #endif
  54. #ifdef HAVE_UNISTD_H
  55. #include <unistd.h>
  56. #endif
  57. /*
  58. Entry point for this file.
  59. */
  60. void
  61. scm_mark_all (void)
  62. {
  63. long j;
  64. int loops;
  65. scm_i_init_weak_vectors_for_gc ();
  66. scm_i_init_guardians_for_gc ();
  67. scm_i_clear_mark_space ();
  68. /* Mark every thread's stack and registers */
  69. scm_threads_mark_stacks ();
  70. j = SCM_NUM_PROTECTS;
  71. while (j--)
  72. scm_gc_mark (scm_sys_protects[j]);
  73. /* mark the registered roots */
  74. {
  75. size_t i;
  76. for (i = 0; i < SCM_HASHTABLE_N_BUCKETS (scm_gc_registered_roots); ++i)
  77. {
  78. SCM l = SCM_HASHTABLE_BUCKET (scm_gc_registered_roots, i);
  79. for (; !scm_is_null (l); l = SCM_CDR (l))
  80. {
  81. SCM *p = (SCM *) (scm_to_ulong (SCM_CAAR (l)));
  82. scm_gc_mark (*p);
  83. }
  84. }
  85. }
  86. scm_mark_subr_table ();
  87. loops = 0;
  88. while (1)
  89. {
  90. int again;
  91. loops++;
  92. /* Mark the non-weak references of weak vectors. For a weak key
  93. alist vector, this would mark the values for keys that are
  94. marked. We need to do this in a loop until everything
  95. settles down since the newly marked values might be keys in
  96. other weak key alist vectors, for example.
  97. */
  98. again = scm_i_mark_weak_vectors_non_weaks ();
  99. if (again)
  100. continue;
  101. /* Now we scan all marked guardians and move all unmarked objects
  102. from the accessible to the inaccessible list.
  103. */
  104. scm_i_identify_inaccessible_guardeds ();
  105. /* When we have identified all inaccessible objects, we can mark
  106. them.
  107. */
  108. again = scm_i_mark_inaccessible_guardeds ();
  109. /* This marking might have changed the situation for weak vectors
  110. and might have turned up new guardians that need to be processed,
  111. so we do it all over again.
  112. */
  113. if (again)
  114. continue;
  115. /* Nothing new marked in this round, we are done.
  116. */
  117. break;
  118. }
  119. /* fprintf (stderr, "%d loops\n", loops); */
  120. /* Remove all unmarked entries from the weak vectors.
  121. */
  122. scm_i_remove_weaks_from_weak_vectors ();
  123. /* Bring hashtables upto date.
  124. */
  125. scm_i_scan_weak_hashtables ();
  126. }
  127. /* {Mark/Sweep}
  128. */
  129. /*
  130. Mark an object precisely, then recurse.
  131. */
  132. void
  133. scm_gc_mark (SCM ptr)
  134. {
  135. if (SCM_IMP (ptr))
  136. return;
  137. if (SCM_GC_MARK_P (ptr))
  138. return;
  139. SCM_SET_GC_MARK (ptr);
  140. scm_gc_mark_dependencies (ptr);
  141. }
  142. /*
  143. Mark the dependencies of an object.
  144. Prefetching:
  145. Should prefetch objects before marking, i.e. if marking a cell, we
  146. should prefetch the car, and then mark the cdr. This will improve CPU
  147. cache misses, because the car is more likely to be in core when we
  148. finish the cdr.
  149. See http://www.hpl.hp.com/techreports/2000/HPL-2000-99.pdf, reducing
  150. garbage collector cache misses.
  151. Prefetch is supported on GCC >= 3.1
  152. (Some time later.)
  153. Tried this with GCC 3.1.1 -- the time differences are barely measurable.
  154. Perhaps this would work better with an explicit markstack?
  155. */
  156. void
  157. scm_gc_mark_dependencies (SCM p)
  158. #define FUNC_NAME "scm_gc_mark_dependencies"
  159. {
  160. register long i;
  161. register SCM ptr;
  162. SCM cell_type;
  163. ptr = p;
  164. scm_mark_dependencies_again:
  165. cell_type = SCM_GC_CELL_TYPE (ptr);
  166. switch (SCM_ITAG7 (cell_type))
  167. {
  168. case scm_tcs_cons_nimcar:
  169. if (SCM_IMP (SCM_CDR (ptr)))
  170. {
  171. ptr = SCM_CAR (ptr);
  172. goto gc_mark_nimp;
  173. }
  174. scm_gc_mark (SCM_CAR (ptr));
  175. ptr = SCM_CDR (ptr);
  176. goto gc_mark_nimp;
  177. case scm_tcs_cons_imcar:
  178. ptr = SCM_CDR (ptr);
  179. goto gc_mark_loop;
  180. case scm_tc7_pws:
  181. scm_gc_mark (SCM_SETTER (ptr));
  182. ptr = SCM_PROCEDURE (ptr);
  183. goto gc_mark_loop;
  184. case scm_tcs_struct:
  185. {
  186. /* XXX - use less explicit code. */
  187. scm_t_bits word0 = SCM_CELL_WORD_0 (ptr) - scm_tc3_struct;
  188. scm_t_bits * vtable_data = (scm_t_bits *) word0;
  189. SCM layout = SCM_PACK (vtable_data [scm_vtable_index_layout]);
  190. long len = scm_i_symbol_length (layout);
  191. const char *fields_desc = scm_i_symbol_chars (layout);
  192. scm_t_bits *struct_data = (scm_t_bits *) SCM_STRUCT_DATA (ptr);
  193. if (vtable_data[scm_struct_i_flags] & SCM_STRUCTF_ENTITY)
  194. {
  195. scm_gc_mark (SCM_PACK (struct_data[scm_struct_i_procedure]));
  196. scm_gc_mark (SCM_PACK (struct_data[scm_struct_i_setter]));
  197. }
  198. if (len)
  199. {
  200. long x;
  201. for (x = 0; x < len - 2; x += 2, ++struct_data)
  202. if (fields_desc[x] == 'p')
  203. scm_gc_mark (SCM_PACK (*struct_data));
  204. if (fields_desc[x] == 'p')
  205. {
  206. if (SCM_LAYOUT_TAILP (fields_desc[x + 1]))
  207. for (x = *struct_data++; x; --x, ++struct_data)
  208. scm_gc_mark (SCM_PACK (*struct_data));
  209. else
  210. scm_gc_mark (SCM_PACK (*struct_data));
  211. }
  212. }
  213. /* mark vtable */
  214. ptr = SCM_PACK (vtable_data [scm_vtable_index_vtable]);
  215. goto gc_mark_loop;
  216. }
  217. break;
  218. case scm_tcs_closures:
  219. if (SCM_IMP (SCM_ENV (ptr)))
  220. {
  221. ptr = SCM_CLOSCAR (ptr);
  222. goto gc_mark_nimp;
  223. }
  224. scm_gc_mark (SCM_CLOSCAR (ptr));
  225. ptr = SCM_ENV (ptr);
  226. goto gc_mark_nimp;
  227. case scm_tc7_vector:
  228. i = SCM_SIMPLE_VECTOR_LENGTH (ptr);
  229. if (i == 0)
  230. break;
  231. while (--i > 0)
  232. {
  233. SCM elt = SCM_SIMPLE_VECTOR_REF (ptr, i);
  234. if (SCM_NIMP (elt))
  235. scm_gc_mark (elt);
  236. }
  237. ptr = SCM_SIMPLE_VECTOR_REF (ptr, 0);
  238. goto gc_mark_loop;
  239. #ifdef CCLO
  240. case scm_tc7_cclo:
  241. {
  242. size_t i = SCM_CCLO_LENGTH (ptr);
  243. size_t j;
  244. for (j = 1; j != i; ++j)
  245. {
  246. SCM obj = SCM_CCLO_REF (ptr, j);
  247. if (!SCM_IMP (obj))
  248. scm_gc_mark (obj);
  249. }
  250. ptr = SCM_CCLO_REF (ptr, 0);
  251. goto gc_mark_loop;
  252. }
  253. #endif
  254. case scm_tc7_string:
  255. ptr = scm_i_string_mark (ptr);
  256. goto gc_mark_loop;
  257. case scm_tc7_stringbuf:
  258. ptr = scm_i_stringbuf_mark (ptr);
  259. goto gc_mark_loop;
  260. case scm_tc7_number:
  261. if (SCM_TYP16 (ptr) == scm_tc16_fraction)
  262. {
  263. scm_gc_mark (SCM_CELL_OBJECT_1 (ptr));
  264. ptr = SCM_CELL_OBJECT_2 (ptr);
  265. goto gc_mark_loop;
  266. }
  267. break;
  268. case scm_tc7_wvect:
  269. scm_i_mark_weak_vector (ptr);
  270. break;
  271. case scm_tc7_symbol:
  272. ptr = scm_i_symbol_mark (ptr);
  273. goto gc_mark_loop;
  274. case scm_tc7_variable:
  275. ptr = SCM_CELL_OBJECT_1 (ptr);
  276. goto gc_mark_loop;
  277. case scm_tcs_subrs:
  278. break;
  279. case scm_tc7_port:
  280. i = SCM_PTOBNUM (ptr);
  281. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  282. if (!(i < scm_numptob))
  283. {
  284. fprintf (stderr, "undefined port type");
  285. abort();
  286. }
  287. #endif
  288. if (SCM_PTAB_ENTRY(ptr))
  289. scm_gc_mark (SCM_FILENAME (ptr));
  290. if (scm_ptobs[i].mark)
  291. {
  292. ptr = (scm_ptobs[i].mark) (ptr);
  293. goto gc_mark_loop;
  294. }
  295. else
  296. return;
  297. break;
  298. case scm_tc7_smob:
  299. switch (SCM_TYP16 (ptr))
  300. { /* should be faster than going through scm_smobs */
  301. case scm_tc_free_cell:
  302. /* We have detected a free cell. This can happen if non-object data
  303. * on the C stack points into guile's heap and is scanned during
  304. * conservative marking. */
  305. break;
  306. default:
  307. i = SCM_SMOBNUM (ptr);
  308. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  309. if (!(i < scm_numsmob))
  310. {
  311. fprintf (stderr, "undefined smob type");
  312. abort();
  313. }
  314. #endif
  315. if (scm_smobs[i].mark)
  316. {
  317. ptr = (scm_smobs[i].mark) (ptr);
  318. goto gc_mark_loop;
  319. }
  320. else
  321. return;
  322. }
  323. break;
  324. default:
  325. fprintf (stderr, "unknown type");
  326. abort();
  327. }
  328. /*
  329. If we got here, then exhausted recursion options for PTR. we
  330. return (careful not to mark PTR, it might be the argument that we
  331. were called with.)
  332. */
  333. return ;
  334. gc_mark_loop:
  335. if (SCM_IMP (ptr))
  336. return;
  337. gc_mark_nimp:
  338. {
  339. int valid_cell = CELL_P (ptr);
  340. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  341. if (scm_debug_cell_accesses_p)
  342. {
  343. /* We are in debug mode. Check the ptr exhaustively. */
  344. valid_cell = valid_cell && (scm_i_find_heap_segment_containing_object (ptr) >= 0);
  345. }
  346. #endif
  347. if (!valid_cell)
  348. {
  349. fprintf (stderr, "rogue pointer in heap");
  350. abort();
  351. }
  352. }
  353. if (SCM_GC_MARK_P (ptr))
  354. {
  355. return;
  356. }
  357. SCM_SET_GC_MARK (ptr);
  358. goto scm_mark_dependencies_again;
  359. }
  360. #undef FUNC_NAME
  361. /* Mark a region conservatively */
  362. void
  363. scm_mark_locations (SCM_STACKITEM x[], unsigned long n)
  364. {
  365. unsigned long m;
  366. for (m = 0; m < n; ++m)
  367. {
  368. SCM obj = * (SCM *) &x[m];
  369. long int segment = scm_i_find_heap_segment_containing_object (obj);
  370. if (segment >= 0)
  371. scm_gc_mark (obj);
  372. }
  373. }
  374. /* The function scm_in_heap_p determines whether an SCM value can be regarded as a
  375. * pointer to a cell on the heap.
  376. */
  377. int
  378. scm_in_heap_p (SCM value)
  379. {
  380. long int segment = scm_i_find_heap_segment_containing_object (value);
  381. return (segment >= 0);
  382. }
  383. #if SCM_ENABLE_DEPRECATED == 1
  384. /* If an allocated cell is detected during garbage collection, this
  385. * means that some code has just obtained the object but was preempted
  386. * before the initialization of the object was completed. This meanst
  387. * that some entries of the allocated cell may already contain SCM
  388. * objects. Therefore, allocated cells are scanned conservatively.
  389. */
  390. scm_t_bits scm_tc16_allocated;
  391. static SCM
  392. allocated_mark (SCM cell)
  393. {
  394. unsigned long int cell_segment = scm_i_find_heap_segment_containing_object (cell);
  395. unsigned int span = scm_i_heap_segment_table[cell_segment]->span;
  396. unsigned int i;
  397. for (i = 1; i != span * 2; ++i)
  398. {
  399. SCM obj = SCM_CELL_OBJECT (cell, i);
  400. long int obj_segment = scm_i_find_heap_segment_containing_object (obj);
  401. if (obj_segment >= 0)
  402. scm_gc_mark (obj);
  403. }
  404. return SCM_BOOL_F;
  405. }
  406. SCM
  407. scm_deprecated_newcell (void)
  408. {
  409. scm_c_issue_deprecation_warning
  410. ("SCM_NEWCELL is deprecated. Use `scm_cell' instead.\n");
  411. return scm_cell (scm_tc16_allocated, 0);
  412. }
  413. SCM
  414. scm_deprecated_newcell2 (void)
  415. {
  416. scm_c_issue_deprecation_warning
  417. ("SCM_NEWCELL2 is deprecated. Use `scm_double_cell' instead.\n");
  418. return scm_double_cell (scm_tc16_allocated, 0, 0, 0);
  419. }
  420. #endif /* SCM_ENABLE_DEPRECATED == 1 */
  421. void
  422. scm_gc_init_mark(void)
  423. {
  424. #if SCM_ENABLE_DEPRECATED == 1
  425. scm_tc16_allocated = scm_make_smob_type ("allocated cell", 0);
  426. scm_set_smob_mark (scm_tc16_allocated, allocated_mark);
  427. #endif
  428. }