weak-vector.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* Copyright (C) 1995, 1996, 1998, 2000, 2001, 2003, 2006, 2008, 2009,
  2. * 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <stdio.h>
  23. #include "libguile/_scm.h"
  24. #include "libguile/vectors.h"
  25. #include "libguile/validate.h"
  26. /* {Weak Vectors}
  27. */
  28. #define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
  29. SCM
  30. scm_c_make_weak_vector (size_t len, SCM fill)
  31. #define FUNC_NAME "make-weak-vector"
  32. {
  33. SCM wv;
  34. size_t j;
  35. SCM_ASSERT_RANGE (1, scm_from_size_t (len), len <= VECTOR_MAX_LENGTH);
  36. if (SCM_UNBNDP (fill))
  37. fill = SCM_UNSPECIFIED;
  38. wv = SCM_PACK_POINTER (scm_gc_malloc_pointerless ((len + 1) * sizeof (SCM),
  39. "weak vector"));
  40. SCM_SET_CELL_WORD_0 (wv, (len << 8) | scm_tc7_wvect);
  41. if (SCM_HEAP_OBJECT_P (fill))
  42. {
  43. memset (SCM_I_VECTOR_WELTS (wv), 0, len * sizeof (SCM));
  44. for (j = 0; j < len; j++)
  45. scm_c_weak_vector_set_x (wv, j, fill);
  46. }
  47. else
  48. for (j = 0; j < len; j++)
  49. SCM_SIMPLE_VECTOR_SET (wv, j, fill);
  50. return wv;
  51. }
  52. #undef FUNC_NAME
  53. SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0,
  54. (SCM size, SCM fill),
  55. "Return a weak vector with @var{size} elements. If the optional\n"
  56. "argument @var{fill} is given, all entries in the vector will be\n"
  57. "set to @var{fill}. The default value for @var{fill} is the\n"
  58. "empty list.")
  59. #define FUNC_NAME s_scm_make_weak_vector
  60. {
  61. return scm_c_make_weak_vector (scm_to_size_t (size), fill);
  62. }
  63. #undef FUNC_NAME
  64. SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector);
  65. SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1,
  66. (SCM lst),
  67. "@deffnx {Scheme Procedure} list->weak-vector lst\n"
  68. "Construct a weak vector from a list: @code{weak-vector} uses\n"
  69. "the list of its arguments while @code{list->weak-vector} uses\n"
  70. "its only argument @var{l} (a list) to construct a weak vector\n"
  71. "the same way @code{list->vector} would.")
  72. #define FUNC_NAME s_scm_weak_vector
  73. {
  74. SCM wv;
  75. size_t i;
  76. long c_size;
  77. SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, lst, c_size);
  78. wv = scm_c_make_weak_vector ((size_t) c_size, SCM_BOOL_F);
  79. for (i = 0; scm_is_pair (lst); lst = SCM_CDR (lst), i++)
  80. scm_c_weak_vector_set_x (wv, i, SCM_CAR (lst));
  81. return wv;
  82. }
  83. #undef FUNC_NAME
  84. SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0,
  85. (SCM obj),
  86. "Return @code{#t} if @var{obj} is a weak vector. Note that all\n"
  87. "weak hashes are also weak vectors.")
  88. #define FUNC_NAME s_scm_weak_vector_p
  89. {
  90. return scm_from_bool (scm_is_weak_vector (obj));
  91. }
  92. #undef FUNC_NAME
  93. int
  94. scm_is_weak_vector (SCM obj)
  95. #define FUNC_NAME s_scm_weak_vector_p
  96. {
  97. return SCM_I_WVECTP (obj);
  98. }
  99. #undef FUNC_NAME
  100. #define SCM_VALIDATE_WEAK_VECTOR(pos, var) \
  101. SCM_I_MAKE_VALIDATE_MSG2 (pos, var, SCM_I_WVECTP, "weak vector")
  102. SCM_DEFINE (scm_weak_vector_length, "weak-vector-length", 1, 0, 0,
  103. (SCM wvect),
  104. "Like @code{vector-length}, but for weak vectors.")
  105. #define FUNC_NAME s_scm_weak_vector_length
  106. {
  107. return scm_from_size_t (scm_c_weak_vector_length (wvect));
  108. }
  109. #undef FUNC_NAME
  110. size_t
  111. scm_c_weak_vector_length (SCM wvect)
  112. #define FUNC_NAME s_scm_weak_vector_length
  113. {
  114. SCM_VALIDATE_WEAK_VECTOR (1, wvect);
  115. return SCM_I_VECTOR_LENGTH (wvect);
  116. }
  117. #undef FUNC_NAME
  118. SCM_DEFINE (scm_weak_vector_ref, "weak-vector-ref", 2, 0, 0,
  119. (SCM wvect, SCM k),
  120. "Like @code{vector-ref}, but for weak vectors.")
  121. #define FUNC_NAME s_scm_weak_vector_ref
  122. {
  123. return scm_c_weak_vector_ref (wvect, scm_to_size_t (k));
  124. }
  125. #undef FUNC_NAME
  126. struct weak_vector_ref_data
  127. {
  128. SCM wv;
  129. size_t k;
  130. };
  131. static void*
  132. weak_vector_ref (void *data)
  133. {
  134. struct weak_vector_ref_data *d = data;
  135. return (void *) SCM_UNPACK (SCM_SIMPLE_VECTOR_REF (d->wv, d->k));
  136. }
  137. SCM
  138. scm_c_weak_vector_ref (SCM wv, size_t k)
  139. #define FUNC_NAME s_scm_weak_vector_ref
  140. {
  141. struct weak_vector_ref_data d;
  142. void *ret;
  143. SCM_VALIDATE_WEAK_VECTOR (1, wv);
  144. d.wv = wv;
  145. d.k = k;
  146. if (k >= SCM_I_VECTOR_LENGTH (wv))
  147. scm_out_of_range ("weak-vector-ref", scm_from_size_t (k));
  148. ret = GC_call_with_alloc_lock (weak_vector_ref, &d);
  149. if (ret)
  150. return SCM_PACK_POINTER (ret);
  151. else
  152. return SCM_BOOL_F;
  153. }
  154. #undef FUNC_NAME
  155. SCM_DEFINE (scm_weak_vector_set_x, "weak-vector-set!", 3, 0, 0,
  156. (SCM wvect, SCM k, SCM obj),
  157. "Like @code{vector-set!}, but for weak vectors.")
  158. #define FUNC_NAME s_scm_weak_vector_set_x
  159. {
  160. scm_c_weak_vector_set_x (wvect, scm_to_size_t (k), obj);
  161. return SCM_UNSPECIFIED;
  162. }
  163. #undef FUNC_NAME
  164. void
  165. scm_c_weak_vector_set_x (SCM wv, size_t k, SCM x)
  166. #define FUNC_NAME s_scm_weak_vector_set_x
  167. {
  168. SCM *elts;
  169. struct weak_vector_ref_data d;
  170. void *prev;
  171. SCM_VALIDATE_WEAK_VECTOR (1, wv);
  172. d.wv = wv;
  173. d.k = k;
  174. if (k >= SCM_I_VECTOR_LENGTH (wv))
  175. scm_out_of_range ("weak-vector-set!", scm_from_size_t (k));
  176. prev = GC_call_with_alloc_lock (weak_vector_ref, &d);
  177. elts = SCM_I_VECTOR_WELTS (wv);
  178. if (prev && SCM_HEAP_OBJECT_P (SCM_PACK_POINTER (prev)))
  179. GC_unregister_disappearing_link ((void **) &elts[k]);
  180. elts[k] = x;
  181. if (SCM_HEAP_OBJECT_P (x))
  182. SCM_I_REGISTER_DISAPPEARING_LINK ((void **) &elts[k],
  183. SCM2PTR (x));
  184. }
  185. #undef FUNC_NAME
  186. static void
  187. scm_init_weak_vector_builtins (void)
  188. {
  189. #ifndef SCM_MAGIC_SNARFER
  190. #include "libguile/weak-vector.x"
  191. #endif
  192. }
  193. void
  194. scm_init_weak_vectors ()
  195. {
  196. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  197. "scm_init_weak_vector_builtins",
  198. (scm_t_extension_init_func)scm_init_weak_vector_builtins,
  199. NULL);
  200. }
  201. /*
  202. Local Variables:
  203. c-file-style: "gnu"
  204. End:
  205. */