weak-vector.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Copyright (C) 1995, 1996, 1998, 2000, 2001, 2003, 2006, 2008, 2009,
  2. * 2010, 2011, 2012, 2013 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. static SCM
  30. 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 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 = 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_I_WVECTP (obj));
  91. }
  92. #undef FUNC_NAME
  93. struct weak_vector_ref_data
  94. {
  95. SCM wv;
  96. size_t k;
  97. };
  98. static void*
  99. weak_vector_ref (void *data)
  100. {
  101. struct weak_vector_ref_data *d = data;
  102. return (void *) SCM_UNPACK (SCM_SIMPLE_VECTOR_REF (d->wv, d->k));
  103. }
  104. SCM
  105. scm_c_weak_vector_ref (SCM wv, size_t k)
  106. {
  107. struct weak_vector_ref_data d;
  108. void *ret;
  109. d.wv = wv;
  110. d.k = k;
  111. if (k >= SCM_I_VECTOR_LENGTH (wv))
  112. scm_out_of_range (NULL, scm_from_size_t (k));
  113. ret = GC_call_with_alloc_lock (weak_vector_ref, &d);
  114. if (ret)
  115. return SCM_PACK_POINTER (ret);
  116. else
  117. return SCM_BOOL_F;
  118. }
  119. void
  120. scm_c_weak_vector_set_x (SCM wv, size_t k, SCM x)
  121. {
  122. SCM *elts;
  123. struct weak_vector_ref_data d;
  124. void *prev;
  125. d.wv = wv;
  126. d.k = k;
  127. if (k >= SCM_I_VECTOR_LENGTH (wv))
  128. scm_out_of_range (NULL, scm_from_size_t (k));
  129. prev = GC_call_with_alloc_lock (weak_vector_ref, &d);
  130. elts = SCM_I_VECTOR_WELTS (wv);
  131. if (prev && SCM_HEAP_OBJECT_P (SCM_PACK_POINTER (prev)))
  132. GC_unregister_disappearing_link ((void **) &elts[k]);
  133. elts[k] = x;
  134. if (SCM_HEAP_OBJECT_P (x))
  135. SCM_I_REGISTER_DISAPPEARING_LINK ((void **) &elts[k],
  136. SCM2PTR (x));
  137. }
  138. static void
  139. scm_init_weak_vector_builtins (void)
  140. {
  141. #ifndef SCM_MAGIC_SNARFER
  142. #include "libguile/weak-vector.x"
  143. #endif
  144. }
  145. void
  146. scm_init_weak_vectors ()
  147. {
  148. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  149. "scm_init_weak_vector_builtins",
  150. (scm_t_extension_init_func)scm_init_weak_vector_builtins,
  151. NULL);
  152. }
  153. /*
  154. Local Variables:
  155. c-file-style: "gnu"
  156. End:
  157. */