weak-vector.c 6.3 KB

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