weaks.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* Copyright (C) 1995,1996,1998,2000,2001, 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. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <stdio.h>
  21. #include "libguile/_scm.h"
  22. #include "libguile/vectors.h"
  23. #include "libguile/lang.h"
  24. #include "libguile/hashtab.h"
  25. #include "libguile/validate.h"
  26. #include "libguile/weaks.h"
  27. /* 1. The current hash table implementation in hashtab.c uses weak alist
  28. * vectors (formerly called weak hash tables) internally.
  29. *
  30. * 2. All hash table operations still work on alist vectors.
  31. *
  32. * 3. The weak vector and alist vector Scheme API is accessed through
  33. * the module (ice-9 weak-vector).
  34. */
  35. /* {Weak Vectors}
  36. */
  37. SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0,
  38. (SCM size, SCM fill),
  39. "Return a weak vector with @var{size} elements. If the optional\n"
  40. "argument @var{fill} is given, all entries in the vector will be\n"
  41. "set to @var{fill}. The default value for @var{fill} is the\n"
  42. "empty list.")
  43. #define FUNC_NAME s_scm_make_weak_vector
  44. {
  45. return scm_i_allocate_weak_vector (0, size, fill);
  46. }
  47. #undef FUNC_NAME
  48. SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector);
  49. SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1,
  50. (SCM l),
  51. "@deffnx {Scheme Procedure} list->weak-vector l\n"
  52. "Construct a weak vector from a list: @code{weak-vector} uses\n"
  53. "the list of its arguments while @code{list->weak-vector} uses\n"
  54. "its only argument @var{l} (a list) to construct a weak vector\n"
  55. "the same way @code{list->vector} would.")
  56. #define FUNC_NAME s_scm_weak_vector
  57. {
  58. scm_t_array_handle handle;
  59. SCM res, *data;
  60. long i;
  61. i = scm_ilength (l);
  62. SCM_ASSERT (i >= 0, l, SCM_ARG1, FUNC_NAME);
  63. res = scm_make_weak_vector (scm_from_int (i), SCM_UNSPECIFIED);
  64. data = scm_vector_writable_elements (res, &handle, NULL, NULL);
  65. while (scm_is_pair (l) && i > 0)
  66. {
  67. *data++ = SCM_CAR (l);
  68. l = SCM_CDR (l);
  69. i--;
  70. }
  71. scm_array_handle_release (&handle);
  72. return res;
  73. }
  74. #undef FUNC_NAME
  75. SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0,
  76. (SCM obj),
  77. "Return @code{#t} if @var{obj} is a weak vector. Note that all\n"
  78. "weak hashes are also weak vectors.")
  79. #define FUNC_NAME s_scm_weak_vector_p
  80. {
  81. return scm_from_bool (SCM_I_WVECTP (obj) && !SCM_IS_WHVEC (obj));
  82. }
  83. #undef FUNC_NAME
  84. SCM_DEFINE (scm_make_weak_key_alist_vector, "make-weak-key-alist-vector", 0, 1, 0,
  85. (SCM size),
  86. "@deffnx {Scheme Procedure} make-weak-value-alist-vector size\n"
  87. "@deffnx {Scheme Procedure} make-doubly-weak-alist-vector size\n"
  88. "Return a weak hash table with @var{size} buckets. As with any\n"
  89. "hash table, choosing a good size for the table requires some\n"
  90. "caution.\n"
  91. "\n"
  92. "You can modify weak hash tables in exactly the same way you\n"
  93. "would modify regular hash tables. (@pxref{Hash Tables})")
  94. #define FUNC_NAME s_scm_make_weak_key_alist_vector
  95. {
  96. return scm_i_allocate_weak_vector
  97. (1, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL);
  98. }
  99. #undef FUNC_NAME
  100. SCM_DEFINE (scm_make_weak_value_alist_vector, "make-weak-value-alist-vector", 0, 1, 0,
  101. (SCM size),
  102. "Return a hash table with weak values with @var{size} buckets.\n"
  103. "(@pxref{Hash Tables})")
  104. #define FUNC_NAME s_scm_make_weak_value_alist_vector
  105. {
  106. return scm_i_allocate_weak_vector
  107. (2, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL);
  108. }
  109. #undef FUNC_NAME
  110. SCM_DEFINE (scm_make_doubly_weak_alist_vector, "make-doubly-weak-alist-vector", 1, 0, 0,
  111. (SCM size),
  112. "Return a hash table with weak keys and values with @var{size}\n"
  113. "buckets. (@pxref{Hash Tables})")
  114. #define FUNC_NAME s_scm_make_doubly_weak_alist_vector
  115. {
  116. return scm_i_allocate_weak_vector
  117. (3, SCM_UNBNDP (size) ? scm_from_int (31) : size, SCM_EOL);
  118. }
  119. #undef FUNC_NAME
  120. SCM_DEFINE (scm_weak_key_alist_vector_p, "weak-key-alist-vector?", 1, 0, 0,
  121. (SCM obj),
  122. "@deffnx {Scheme Procedure} weak-value-alist-vector? obj\n"
  123. "@deffnx {Scheme Procedure} doubly-weak-alist-vector? obj\n"
  124. "Return @code{#t} if @var{obj} is the specified weak hash\n"
  125. "table. Note that a doubly weak hash table is neither a weak key\n"
  126. "nor a weak value hash table.")
  127. #define FUNC_NAME s_scm_weak_key_alist_vector_p
  128. {
  129. return scm_from_bool (SCM_I_WVECTP (obj) && SCM_IS_WHVEC (obj));
  130. }
  131. #undef FUNC_NAME
  132. SCM_DEFINE (scm_weak_value_alist_vector_p, "weak-value-alist-vector?", 1, 0, 0,
  133. (SCM obj),
  134. "Return @code{#t} if @var{obj} is a weak value hash table.")
  135. #define FUNC_NAME s_scm_weak_value_alist_vector_p
  136. {
  137. return scm_from_bool (SCM_I_WVECTP (obj) && SCM_IS_WHVEC_V (obj));
  138. }
  139. #undef FUNC_NAME
  140. SCM_DEFINE (scm_doubly_weak_alist_vector_p, "doubly-weak-alist-vector?", 1, 0, 0,
  141. (SCM obj),
  142. "Return @code{#t} if @var{obj} is a doubly weak hash table.")
  143. #define FUNC_NAME s_scm_doubly_weak_alist_vector_p
  144. {
  145. return scm_from_bool (SCM_I_WVECTP (obj) && SCM_IS_WHVEC_B (obj));
  146. }
  147. #undef FUNC_NAME
  148. #define UNMARKED_CELL_P(x) (SCM_NIMP(x) && !SCM_GC_MARK_P (x))
  149. static SCM weak_vectors;
  150. void
  151. scm_i_init_weak_vectors_for_gc ()
  152. {
  153. weak_vectors = SCM_EOL;
  154. }
  155. void
  156. scm_i_mark_weak_vector (SCM w)
  157. {
  158. SCM_I_SET_WVECT_GC_CHAIN (w, weak_vectors);
  159. weak_vectors = w;
  160. }
  161. static int
  162. scm_i_mark_weak_vector_non_weaks (SCM w)
  163. {
  164. int again = 0;
  165. if (SCM_IS_WHVEC_ANY (w))
  166. {
  167. SCM *ptr;
  168. long n = SCM_I_WVECT_LENGTH (w);
  169. long j;
  170. int weak_keys = SCM_IS_WHVEC (w) || SCM_IS_WHVEC_B (w);
  171. int weak_values = SCM_IS_WHVEC_V (w) || SCM_IS_WHVEC_B (w);
  172. ptr = SCM_I_WVECT_GC_WVELTS (w);
  173. for (j = 0; j < n; ++j)
  174. {
  175. SCM alist, slow_alist;
  176. int slow_toggle = 0;
  177. /* We do not set the mark bits of the alist spine cells here
  178. since we do not want to ever create the situation where a
  179. marked cell references an unmarked cell (except in
  180. scm_gc_mark, where the referenced cells will be marked
  181. immediately). Thus, we can not use mark bits to stop us
  182. from looping indefinitely over a cyclic alist. Instead,
  183. we use the standard tortoise and hare trick to catch
  184. cycles. The fast walker does the work, and stops when it
  185. catches the slow walker to ensure that the whole cycle
  186. has been worked on.
  187. */
  188. alist = slow_alist = ptr[j];
  189. while (scm_is_pair (alist))
  190. {
  191. SCM elt = SCM_CAR (alist);
  192. if (UNMARKED_CELL_P (elt))
  193. {
  194. if (scm_is_pair (elt))
  195. {
  196. SCM key = SCM_CAR (elt);
  197. SCM value = SCM_CDR (elt);
  198. if (!((weak_keys && UNMARKED_CELL_P (key))
  199. || (weak_values && UNMARKED_CELL_P (value))))
  200. {
  201. /* The item should be kept. We need to mark it
  202. recursively.
  203. */
  204. scm_gc_mark (elt);
  205. again = 1;
  206. }
  207. }
  208. else
  209. {
  210. /* A non-pair cell element. This should not
  211. appear in a real alist, but when it does, we
  212. need to keep it.
  213. */
  214. scm_gc_mark (elt);
  215. again = 1;
  216. }
  217. }
  218. alist = SCM_CDR (alist);
  219. if (slow_toggle && scm_is_pair (slow_alist))
  220. {
  221. slow_alist = SCM_CDR (slow_alist);
  222. slow_toggle = !slow_toggle;
  223. if (scm_is_eq (slow_alist, alist))
  224. break;
  225. }
  226. }
  227. if (!scm_is_pair (alist))
  228. scm_gc_mark (alist);
  229. }
  230. }
  231. return again;
  232. }
  233. int
  234. scm_i_mark_weak_vectors_non_weaks ()
  235. {
  236. int again = 0;
  237. SCM w = weak_vectors;
  238. while (!scm_is_null (w))
  239. {
  240. if (scm_i_mark_weak_vector_non_weaks (w))
  241. again = 1;
  242. w = SCM_I_WVECT_GC_CHAIN (w);
  243. }
  244. return again;
  245. }
  246. static void
  247. scm_i_remove_weaks (SCM w)
  248. {
  249. SCM *ptr = SCM_I_WVECT_GC_WVELTS (w);
  250. size_t n = SCM_I_WVECT_LENGTH (w);
  251. size_t i;
  252. if (!SCM_IS_WHVEC_ANY (w))
  253. {
  254. for (i = 0; i < n; ++i)
  255. if (UNMARKED_CELL_P (ptr[i]))
  256. ptr[i] = SCM_BOOL_F;
  257. }
  258. else
  259. {
  260. size_t delta = 0;
  261. for (i = 0; i < n; ++i)
  262. {
  263. SCM alist, *fixup;
  264. fixup = ptr + i;
  265. alist = *fixup;
  266. while (scm_is_pair (alist) && !SCM_GC_MARK_P (alist))
  267. {
  268. if (UNMARKED_CELL_P (SCM_CAR (alist)))
  269. {
  270. *fixup = SCM_CDR (alist);
  271. delta++;
  272. }
  273. else
  274. {
  275. SCM_SET_GC_MARK (alist);
  276. fixup = SCM_CDRLOC (alist);
  277. }
  278. alist = *fixup;
  279. }
  280. }
  281. #if 0
  282. if (delta)
  283. fprintf (stderr, "vector %p, delta %d\n", w, delta);
  284. #endif
  285. SCM_I_SET_WVECT_DELTA (w, delta);
  286. }
  287. }
  288. void
  289. scm_i_remove_weaks_from_weak_vectors ()
  290. {
  291. SCM w = weak_vectors;
  292. while (!scm_is_null (w))
  293. {
  294. scm_i_remove_weaks (w);
  295. w = SCM_I_WVECT_GC_CHAIN (w);
  296. }
  297. }
  298. SCM
  299. scm_init_weaks_builtins ()
  300. {
  301. #include "libguile/weaks.x"
  302. return SCM_UNSPECIFIED;
  303. }
  304. void
  305. scm_init_weaks ()
  306. {
  307. scm_c_define_gsubr ("%init-weaks-builtins", 0, 0, 0,
  308. scm_init_weaks_builtins);
  309. }
  310. /*
  311. Local Variables:
  312. c-file-style: "gnu"
  313. End:
  314. */