weak-vector.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Copyright (C) 1995,1996,1998,2000,2001, 2003, 2006, 2008, 2009, 2010, 2011 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/validate.h"
  25. /* {Weak Vectors}
  26. */
  27. #define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
  28. static SCM
  29. make_weak_vector (size_t len, SCM fill)
  30. #define FUNC_NAME "make-weak-vector"
  31. {
  32. SCM wv;
  33. size_t j;
  34. SCM_ASSERT_RANGE (1, scm_from_size_t (len), len <= VECTOR_MAX_LENGTH);
  35. if (SCM_UNBNDP (fill))
  36. fill = SCM_UNSPECIFIED;
  37. wv = SCM_PACK_POINTER (scm_gc_malloc_pointerless ((len + 1) * sizeof (SCM),
  38. "weak vector"));
  39. SCM_SET_CELL_WORD_0 (wv, (len << 8) | scm_tc7_wvect);
  40. if (SCM_HEAP_OBJECT_P (fill))
  41. {
  42. memset (SCM_I_VECTOR_WELTS (wv), 0, len * sizeof (SCM));
  43. for (j = 0; j < len; j++)
  44. scm_c_weak_vector_set_x (wv, j, fill);
  45. }
  46. else
  47. for (j = 0; j < len; j++)
  48. SCM_SIMPLE_VECTOR_SET (wv, j, fill);
  49. return wv;
  50. }
  51. #undef FUNC_NAME
  52. SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0,
  53. (SCM size, SCM fill),
  54. "Return a weak vector with @var{size} elements. If the optional\n"
  55. "argument @var{fill} is given, all entries in the vector will be\n"
  56. "set to @var{fill}. The default value for @var{fill} is the\n"
  57. "empty list.")
  58. #define FUNC_NAME s_scm_make_weak_vector
  59. {
  60. return make_weak_vector (scm_to_size_t (size), fill);
  61. }
  62. #undef FUNC_NAME
  63. SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector);
  64. SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1,
  65. (SCM lst),
  66. "@deffnx {Scheme Procedure} list->weak-vector lst\n"
  67. "Construct a weak vector from a list: @code{weak-vector} uses\n"
  68. "the list of its arguments while @code{list->weak-vector} uses\n"
  69. "its only argument @var{l} (a list) to construct a weak vector\n"
  70. "the same way @code{list->vector} would.")
  71. #define FUNC_NAME s_scm_weak_vector
  72. {
  73. SCM wv;
  74. size_t i;
  75. long c_size;
  76. SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, lst, c_size);
  77. wv = make_weak_vector ((size_t) c_size, SCM_BOOL_F);
  78. for (i = 0; scm_is_pair (lst); lst = SCM_CDR (lst), i++)
  79. scm_c_weak_vector_set_x (wv, i, SCM_CAR (lst));
  80. return wv;
  81. }
  82. #undef FUNC_NAME
  83. SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0,
  84. (SCM obj),
  85. "Return @code{#t} if @var{obj} is a weak vector. Note that all\n"
  86. "weak hashes are also weak vectors.")
  87. #define FUNC_NAME s_scm_weak_vector_p
  88. {
  89. return scm_from_bool (SCM_I_WVECTP (obj));
  90. }
  91. #undef FUNC_NAME
  92. struct weak_vector_ref_data
  93. {
  94. SCM wv;
  95. size_t k;
  96. };
  97. static void*
  98. weak_vector_ref (void *data)
  99. {
  100. struct weak_vector_ref_data *d = data;
  101. return SCM_SIMPLE_VECTOR_REF (d->wv, d->k);
  102. }
  103. SCM
  104. scm_c_weak_vector_ref (SCM wv, size_t k)
  105. {
  106. struct weak_vector_ref_data d;
  107. void *ret;
  108. d.wv = wv;
  109. d.k = k;
  110. if (k >= SCM_I_VECTOR_LENGTH (wv))
  111. scm_out_of_range (NULL, scm_from_size_t (k));
  112. ret = GC_call_with_alloc_lock (weak_vector_ref, &d);
  113. if (ret)
  114. return SCM_PACK_POINTER (ret);
  115. else
  116. return SCM_BOOL_F;
  117. }
  118. void
  119. scm_c_weak_vector_set_x (SCM wv, size_t k, SCM x)
  120. {
  121. SCM *elts;
  122. struct weak_vector_ref_data d;
  123. void *prev;
  124. d.wv = wv;
  125. d.k = k;
  126. if (k >= SCM_I_VECTOR_LENGTH (wv))
  127. scm_out_of_range (NULL, scm_from_size_t (k));
  128. prev = GC_call_with_alloc_lock (weak_vector_ref, &d);
  129. elts = SCM_I_VECTOR_WELTS (wv);
  130. if (prev && SCM_HEAP_OBJECT_P (SCM_PACK_POINTER (prev)))
  131. GC_unregister_disappearing_link ((GC_PTR) &elts[k]);
  132. elts[k] = x;
  133. if (SCM_HEAP_OBJECT_P (x))
  134. SCM_I_REGISTER_DISAPPEARING_LINK ((GC_PTR) &elts[k],
  135. (GC_PTR) SCM2PTR (x));
  136. }
  137. static void
  138. scm_init_weak_vector_builtins (void)
  139. {
  140. #ifndef SCM_MAGIC_SNARFER
  141. #include "libguile/weak-vector.x"
  142. #endif
  143. }
  144. void
  145. scm_init_weak_vectors ()
  146. {
  147. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  148. "scm_init_weak_vector_builtins",
  149. (scm_t_extension_init_func)scm_init_weak_vector_builtins,
  150. NULL);
  151. }
  152. /*
  153. Local Variables:
  154. c-file-style: "gnu"
  155. End:
  156. */