gc-card.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2004, 2005, 2006, 2007, 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. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <stdio.h>
  21. #include <gmp.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/eval.h"
  24. #include "libguile/numbers.h"
  25. #include "libguile/stime.h"
  26. #include "libguile/stackchk.h"
  27. #include "libguile/struct.h"
  28. #include "libguile/smob.h"
  29. #include "libguile/unif.h"
  30. #include "libguile/async.h"
  31. #include "libguile/ports.h"
  32. #include "libguile/root.h"
  33. #include "libguile/strings.h"
  34. #include "libguile/vectors.h"
  35. #include "libguile/weaks.h"
  36. #include "libguile/hashtab.h"
  37. #include "libguile/tags.h"
  38. #include "libguile/private-gc.h"
  39. #include "libguile/validate.h"
  40. #include "libguile/deprecation.h"
  41. #include "libguile/gc.h"
  42. #include "libguile/srfi-4.h"
  43. #include "libguile/private-gc.h"
  44. long int scm_i_deprecated_memory_return;
  45. /* During collection, this accumulates structures which are to be freed.
  46. */
  47. SCM scm_i_structs_to_free;
  48. /*
  49. Init all the free cells in CARD, prepending to *FREE_LIST.
  50. Return: number of free cells found in this card.
  51. It would be cleaner to have a separate function sweep_value(), but
  52. that is too slow (functions with switch statements can't be
  53. inlined).
  54. NOTE:
  55. This function is quite efficient. However, for many types of cells,
  56. allocation and a de-allocation involves calling malloc() and
  57. free().
  58. This is costly for small objects (due to malloc/free overhead.)
  59. (should measure this).
  60. It might also be bad for threads: if several threads are allocating
  61. strings concurrently, then mallocs for both threads may have to
  62. fiddle with locks.
  63. It might be interesting to add a separate memory pool for small
  64. objects to each freelist.
  65. --hwn.
  66. */
  67. int
  68. scm_i_sweep_card (scm_t_cell * p, SCM *free_list, scm_t_heap_segment*seg)
  69. #define FUNC_NAME "sweep_card"
  70. {
  71. scm_t_c_bvec_long *bitvec = SCM_GC_CARD_BVEC(p);
  72. scm_t_cell * end = p + SCM_GC_CARD_N_CELLS;
  73. int span = seg->span;
  74. int offset =SCM_MAX (SCM_GC_CARD_N_HEADER_CELLS, span);
  75. int free_count = 0;
  76. /*
  77. I tried something fancy with shifting by one bit every word from
  78. the bitvec in turn, but it wasn't any faster, but quite a bit
  79. hairier.
  80. */
  81. for (p += offset; p < end; p += span, offset += span)
  82. {
  83. SCM scmptr = PTR2SCM (p);
  84. if (SCM_C_BVEC_GET (bitvec, offset))
  85. continue;
  86. switch (SCM_TYP7 (scmptr))
  87. {
  88. case scm_tcs_struct:
  89. /* The card can be swept more than once. Check that it's
  90. * the first time!
  91. */
  92. if (!SCM_STRUCT_GC_CHAIN (scmptr))
  93. {
  94. /* Structs need to be freed in a special order.
  95. * This is handled by GC C hooks in struct.c.
  96. */
  97. SCM_SET_STRUCT_GC_CHAIN (scmptr, scm_i_structs_to_free);
  98. scm_i_structs_to_free = scmptr;
  99. }
  100. continue;
  101. case scm_tcs_cons_imcar:
  102. case scm_tcs_cons_nimcar:
  103. case scm_tcs_closures:
  104. case scm_tc7_pws:
  105. break;
  106. case scm_tc7_wvect:
  107. case scm_tc7_vector:
  108. scm_i_vector_free (scmptr);
  109. break;
  110. #ifdef CCLO
  111. case scm_tc7_cclo:
  112. scm_gc_free (SCM_CCLO_BASE (scmptr),
  113. SCM_CCLO_LENGTH (scmptr) * sizeof (SCM),
  114. "compiled closure");
  115. break;
  116. #endif
  117. case scm_tc7_number:
  118. switch SCM_TYP16 (scmptr)
  119. {
  120. case scm_tc16_real:
  121. break;
  122. case scm_tc16_big:
  123. mpz_clear (SCM_I_BIG_MPZ (scmptr));
  124. /* nothing else to do here since the mpz is in a double cell */
  125. break;
  126. case scm_tc16_complex:
  127. scm_gc_free (SCM_COMPLEX_MEM (scmptr), sizeof (scm_t_complex),
  128. "complex");
  129. break;
  130. case scm_tc16_fraction:
  131. /* nothing to do here since the num/denum of a fraction
  132. are proper SCM objects themselves. */
  133. break;
  134. }
  135. break;
  136. case scm_tc7_string:
  137. scm_i_string_free (scmptr);
  138. break;
  139. case scm_tc7_stringbuf:
  140. scm_i_stringbuf_free (scmptr);
  141. break;
  142. case scm_tc7_symbol:
  143. scm_i_symbol_free (scmptr);
  144. break;
  145. case scm_tc7_variable:
  146. break;
  147. case scm_tcs_subrs:
  148. /* the various "subrs" (primitives) are never freed */
  149. continue;
  150. case scm_tc7_port:
  151. if SCM_OPENP (scmptr)
  152. {
  153. int k = SCM_PTOBNUM (scmptr);
  154. size_t mm;
  155. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  156. if (!(k < scm_numptob))
  157. {
  158. fprintf (stderr, "undefined port type");
  159. abort();
  160. }
  161. #endif
  162. /* Keep "revealed" ports alive. */
  163. if (scm_revealed_count (scmptr) > 0)
  164. continue;
  165. /* Yes, I really do mean scm_ptobs[k].free */
  166. /* rather than ftobs[k].close. .close */
  167. /* is for explicit CLOSE-PORT by user */
  168. mm = scm_ptobs[k].free (scmptr);
  169. if (mm != 0)
  170. {
  171. #if SCM_ENABLE_DEPRECATED == 1
  172. scm_c_issue_deprecation_warning
  173. ("Returning non-0 from a port free function is "
  174. "deprecated. Use scm_gc_free et al instead.");
  175. scm_c_issue_deprecation_warning_fmt
  176. ("(You just returned non-0 while freeing a %s.)",
  177. SCM_PTOBNAME (k));
  178. scm_i_deprecated_memory_return += mm;
  179. #else
  180. abort ();
  181. #endif
  182. }
  183. SCM_SETSTREAM (scmptr, 0);
  184. scm_remove_from_port_table (scmptr);
  185. scm_gc_ports_collected++;
  186. SCM_CLR_PORT_OPEN_FLAG (scmptr);
  187. }
  188. break;
  189. case scm_tc7_smob:
  190. switch SCM_TYP16 (scmptr)
  191. {
  192. case scm_tc_free_cell:
  193. free_count --;
  194. break;
  195. default:
  196. {
  197. int k;
  198. k = SCM_SMOBNUM (scmptr);
  199. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  200. if (!(k < scm_numsmob))
  201. {
  202. fprintf (stderr, "undefined smob type");
  203. abort();
  204. }
  205. #endif
  206. if (scm_smobs[k].free)
  207. {
  208. size_t mm;
  209. mm = scm_smobs[k].free (scmptr);
  210. if (mm != 0)
  211. {
  212. #if SCM_ENABLE_DEPRECATED == 1
  213. scm_c_issue_deprecation_warning
  214. ("Returning non-0 from a smob free function is "
  215. "deprecated. Use scm_gc_free et al instead.");
  216. scm_c_issue_deprecation_warning_fmt
  217. ("(You just returned non-0 while freeing a %s.)",
  218. SCM_SMOBNAME (k));
  219. scm_i_deprecated_memory_return += mm;
  220. #else
  221. abort();
  222. #endif
  223. }
  224. }
  225. break;
  226. }
  227. }
  228. break;
  229. default:
  230. fprintf (stderr, "unknown type");
  231. abort();
  232. }
  233. SCM_GC_SET_CELL_WORD (scmptr, 0, scm_tc_free_cell);
  234. SCM_SET_FREE_CELL_CDR (scmptr, PTR2SCM (*free_list));
  235. *free_list = scmptr;
  236. free_count ++;
  237. }
  238. return free_count;
  239. }
  240. #undef FUNC_NAME
  241. /*
  242. Like sweep, but no complicated logic to do the sweeping.
  243. */
  244. int
  245. scm_i_init_card_freelist (scm_t_cell * card, SCM *free_list,
  246. scm_t_heap_segment*seg)
  247. {
  248. int span = seg->span;
  249. scm_t_cell *end = card + SCM_GC_CARD_N_CELLS;
  250. scm_t_cell *p = end - span;
  251. scm_t_c_bvec_long * bvec_ptr = (scm_t_c_bvec_long* ) seg->bounds[1];
  252. int idx = (card - seg->bounds[0]) / SCM_GC_CARD_N_CELLS;
  253. bvec_ptr += idx *SCM_GC_CARD_BVEC_SIZE_IN_LONGS;
  254. SCM_GC_SET_CELL_BVEC (card, bvec_ptr);
  255. /*
  256. ASSUMPTION: n_header_cells <= 2.
  257. */
  258. for (; p > card; p -= span)
  259. {
  260. const SCM scmptr = PTR2SCM (p);
  261. SCM_GC_SET_CELL_WORD (scmptr, 0, scm_tc_free_cell);
  262. SCM_SET_FREE_CELL_CDR (scmptr, PTR2SCM (*free_list));
  263. *free_list = scmptr;
  264. }
  265. return SCM_GC_CARD_N_CELLS - SCM_MAX(span, SCM_GC_CARD_N_HEADER_CELLS);
  266. }
  267. void
  268. scm_i_card_statistics (scm_t_cell *p, SCM hashtab, scm_t_heap_segment *seg)
  269. {
  270. scm_t_c_bvec_long *bitvec = SCM_GC_CARD_BVEC(p);
  271. scm_t_cell * end = p + SCM_GC_CARD_N_CELLS;
  272. int span = seg->span;
  273. int offset = SCM_MAX (SCM_GC_CARD_N_HEADER_CELLS, span);
  274. if (!bitvec)
  275. /* Card P hasn't been initialized yet by `scm_i_init_card_freelist ()'. */
  276. return;
  277. for (p += offset; p < end; p += span, offset += span)
  278. {
  279. scm_t_bits tag = -1;
  280. SCM scmptr = PTR2SCM (p);
  281. if (!SCM_C_BVEC_GET (bitvec, offset))
  282. continue;
  283. tag = SCM_TYP7 (scmptr);
  284. if (tag == scm_tc7_smob || tag == scm_tc7_number)
  285. {
  286. /* Record smobs and numbers under 16 bits of the tag, so the
  287. different smob objects are distinguished, and likewise the
  288. different numbers big, real, complex and fraction. */
  289. tag = SCM_TYP16(scmptr);
  290. }
  291. else
  292. switch (tag)
  293. {
  294. case scm_tcs_cons_imcar:
  295. tag = scm_tc2_int;
  296. break;
  297. case scm_tcs_cons_nimcar:
  298. tag = scm_tc3_cons;
  299. break;
  300. case scm_tcs_struct:
  301. tag = scm_tc3_struct;
  302. break;
  303. case scm_tcs_closures:
  304. tag = scm_tc3_closure;
  305. break;
  306. case scm_tcs_subrs:
  307. tag = scm_tc7_asubr;
  308. break;
  309. }
  310. {
  311. SCM handle = scm_hashq_create_handle_x (hashtab,
  312. scm_from_int (tag), SCM_INUM0);
  313. SCM_SETCDR (handle, scm_from_int (scm_to_int (SCM_CDR (handle)) + 1));
  314. }
  315. }
  316. }
  317. /* TAG is the tag word of a cell, return a string which is its name, or NULL
  318. if unknown. Currently this is only used by gc-live-object-stats and the
  319. distinctions between types are oriented towards what that code records
  320. while scanning what's alive. */
  321. char const *
  322. scm_i_tag_name (scm_t_bits tag)
  323. {
  324. switch (tag & 0x7F) /* 7 bits */
  325. {
  326. case scm_tcs_struct:
  327. return "struct";
  328. case scm_tcs_cons_imcar:
  329. return "cons (immediate car)";
  330. case scm_tcs_cons_nimcar:
  331. return "cons (non-immediate car)";
  332. case scm_tcs_closures:
  333. return "closures";
  334. case scm_tc7_pws:
  335. return "pws";
  336. case scm_tc7_wvect:
  337. return "weak vector";
  338. case scm_tc7_vector:
  339. return "vector";
  340. #ifdef CCLO
  341. case scm_tc7_cclo:
  342. return "compiled closure";
  343. #endif
  344. case scm_tc7_number:
  345. switch (tag)
  346. {
  347. case scm_tc16_real:
  348. return "real";
  349. case scm_tc16_big:
  350. return "bignum";
  351. case scm_tc16_complex:
  352. return "complex number";
  353. case scm_tc16_fraction:
  354. return "fraction";
  355. }
  356. /* shouldn't reach here unless there's a new class of numbers */
  357. return "number";
  358. case scm_tc7_string:
  359. return "string";
  360. case scm_tc7_stringbuf:
  361. return "string buffer";
  362. case scm_tc7_symbol:
  363. return "symbol";
  364. case scm_tc7_variable:
  365. return "variable";
  366. case scm_tcs_subrs:
  367. return "subrs";
  368. case scm_tc7_port:
  369. return "port";
  370. case scm_tc7_smob:
  371. /* scm_tc_free_cell is smob 0, the name field in that scm_smobs[]
  372. entry should be ok for our return here */
  373. return scm_smobs[SCM_TC2SMOBNUM(tag)].name;
  374. }
  375. return NULL;
  376. }
  377. #if (SCM_DEBUG_DEBUGGING_SUPPORT == 1)
  378. typedef struct scm_dbg_t_list_cell {
  379. scm_t_bits car;
  380. struct scm_dbg_t_list_cell * cdr;
  381. } scm_dbg_t_list_cell;
  382. typedef struct scm_dbg_t_double_cell {
  383. scm_t_bits word_0;
  384. scm_t_bits word_1;
  385. scm_t_bits word_2;
  386. scm_t_bits word_3;
  387. } scm_dbg_t_double_cell;
  388. int scm_dbg_gc_marked_p (SCM obj);
  389. scm_t_cell * scm_dbg_gc_get_card (SCM obj);
  390. scm_t_c_bvec_long * scm_dbg_gc_get_bvec (SCM obj);
  391. int
  392. scm_dbg_gc_marked_p (SCM obj)
  393. {
  394. if (!SCM_IMP (obj))
  395. return SCM_GC_MARK_P(obj);
  396. else
  397. return 0;
  398. }
  399. scm_t_cell *
  400. scm_dbg_gc_get_card (SCM obj)
  401. {
  402. if (!SCM_IMP (obj))
  403. return SCM_GC_CELL_CARD(obj);
  404. else
  405. return NULL;
  406. }
  407. scm_t_c_bvec_long *
  408. scm_dbg_gc_get_bvec (SCM obj)
  409. {
  410. if (!SCM_IMP (obj))
  411. return SCM_GC_CARD_BVEC (SCM_GC_CELL_CARD (obj));
  412. else
  413. return NULL;
  414. }
  415. #endif