weaks.c 9.4 KB

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