Commit | Line | Data |
---|---|---|
789dd40b | 1 | /* Copyright (C) 1995, 1996, 1998, 2000, 2001, 2003, 2006, 2008, 2009, |
c9647bfb | 2 | * 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc. |
a141db86 AW |
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 | ||
20 | ||
21 | \f | |
22 | #ifdef HAVE_CONFIG_H | |
23 | # include <config.h> | |
24 | #endif | |
25 | ||
26 | #include <stdio.h> | |
27 | ||
28 | #include "libguile/_scm.h" | |
29 | #include "libguile/vectors.h" | |
30 | ||
31 | #include "libguile/validate.h" | |
32 | ||
33 | \f | |
34 | ||
35 | /* {Weak Vectors} | |
36 | */ | |
37 | ||
38 | #define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8) | |
39 | ||
c9647bfb AW |
40 | SCM |
41 | scm_c_make_weak_vector (size_t len, SCM fill) | |
a141db86 AW |
42 | #define FUNC_NAME "make-weak-vector" |
43 | { | |
44 | SCM wv; | |
45 | size_t j; | |
46 | ||
47 | SCM_ASSERT_RANGE (1, scm_from_size_t (len), len <= VECTOR_MAX_LENGTH); | |
48 | ||
49 | if (SCM_UNBNDP (fill)) | |
50 | fill = SCM_UNSPECIFIED; | |
51 | ||
21041372 | 52 | wv = SCM_PACK_POINTER (scm_gc_malloc_pointerless ((len + 1) * sizeof (SCM), |
a141db86 AW |
53 | "weak vector")); |
54 | ||
55 | SCM_SET_CELL_WORD_0 (wv, (len << 8) | scm_tc7_wvect); | |
56 | ||
8c5bb729 | 57 | if (SCM_HEAP_OBJECT_P (fill)) |
a141db86 AW |
58 | { |
59 | memset (SCM_I_VECTOR_WELTS (wv), 0, len * sizeof (SCM)); | |
60 | for (j = 0; j < len; j++) | |
61 | scm_c_weak_vector_set_x (wv, j, fill); | |
62 | } | |
63 | else | |
64 | for (j = 0; j < len; j++) | |
65 | SCM_SIMPLE_VECTOR_SET (wv, j, fill); | |
66 | ||
67 | return wv; | |
68 | } | |
69 | #undef FUNC_NAME | |
70 | ||
71 | SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0, | |
72 | (SCM size, SCM fill), | |
73 | "Return a weak vector with @var{size} elements. If the optional\n" | |
74 | "argument @var{fill} is given, all entries in the vector will be\n" | |
75 | "set to @var{fill}. The default value for @var{fill} is the\n" | |
76 | "empty list.") | |
77 | #define FUNC_NAME s_scm_make_weak_vector | |
78 | { | |
c9647bfb | 79 | return scm_c_make_weak_vector (scm_to_size_t (size), fill); |
a141db86 AW |
80 | } |
81 | #undef FUNC_NAME | |
82 | ||
83 | ||
84 | SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector); | |
85 | ||
86 | SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1, | |
87 | (SCM lst), | |
88 | "@deffnx {Scheme Procedure} list->weak-vector lst\n" | |
89 | "Construct a weak vector from a list: @code{weak-vector} uses\n" | |
90 | "the list of its arguments while @code{list->weak-vector} uses\n" | |
91 | "its only argument @var{l} (a list) to construct a weak vector\n" | |
92 | "the same way @code{list->vector} would.") | |
93 | #define FUNC_NAME s_scm_weak_vector | |
94 | { | |
95 | SCM wv; | |
96 | size_t i; | |
97 | long c_size; | |
98 | ||
99 | SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, lst, c_size); | |
100 | ||
c9647bfb | 101 | wv = scm_c_make_weak_vector ((size_t) c_size, SCM_BOOL_F); |
a141db86 AW |
102 | |
103 | for (i = 0; scm_is_pair (lst); lst = SCM_CDR (lst), i++) | |
104 | scm_c_weak_vector_set_x (wv, i, SCM_CAR (lst)); | |
105 | ||
106 | return wv; | |
107 | } | |
108 | #undef FUNC_NAME | |
109 | ||
110 | ||
111 | SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0, | |
112 | (SCM obj), | |
113 | "Return @code{#t} if @var{obj} is a weak vector. Note that all\n" | |
114 | "weak hashes are also weak vectors.") | |
115 | #define FUNC_NAME s_scm_weak_vector_p | |
116 | { | |
c9647bfb AW |
117 | return scm_from_bool (scm_is_weak_vector (obj)); |
118 | } | |
119 | #undef FUNC_NAME | |
120 | ||
121 | ||
122 | int | |
123 | scm_is_weak_vector (SCM obj) | |
124 | #define FUNC_NAME s_scm_weak_vector_p | |
125 | { | |
126 | return SCM_I_WVECTP (obj); | |
127 | } | |
128 | #undef FUNC_NAME | |
129 | ||
130 | ||
131 | #define SCM_VALIDATE_WEAK_VECTOR(pos, var) \ | |
132 | SCM_I_MAKE_VALIDATE_MSG2 (pos, var, SCM_I_WVECTP, "weak vector") | |
133 | ||
134 | ||
135 | SCM_DEFINE (scm_weak_vector_length, "weak-vector-length", 1, 0, 0, | |
136 | (SCM wvect), | |
137 | "Like @code{vector-length}, but for weak vectors.") | |
138 | #define FUNC_NAME s_scm_weak_vector_length | |
139 | { | |
140 | return scm_from_size_t (scm_c_weak_vector_length (wvect)); | |
141 | } | |
142 | #undef FUNC_NAME | |
143 | ||
144 | ||
145 | size_t | |
146 | scm_c_weak_vector_length (SCM wvect) | |
147 | #define FUNC_NAME s_scm_weak_vector_length | |
148 | { | |
149 | SCM_VALIDATE_WEAK_VECTOR (1, wvect); | |
150 | return SCM_I_VECTOR_LENGTH (wvect); | |
151 | } | |
152 | #undef FUNC_NAME | |
153 | ||
154 | ||
155 | SCM_DEFINE (scm_weak_vector_ref, "weak-vector-ref", 2, 0, 0, | |
156 | (SCM wvect, SCM k), | |
157 | "Like @code{vector-ref}, but for weak vectors.") | |
158 | #define FUNC_NAME s_scm_weak_vector_ref | |
159 | { | |
160 | return scm_c_weak_vector_ref (wvect, scm_to_size_t (k)); | |
a141db86 AW |
161 | } |
162 | #undef FUNC_NAME | |
163 | ||
164 | ||
165 | struct weak_vector_ref_data | |
166 | { | |
167 | SCM wv; | |
168 | size_t k; | |
169 | }; | |
170 | ||
171 | static void* | |
172 | weak_vector_ref (void *data) | |
173 | { | |
174 | struct weak_vector_ref_data *d = data; | |
175 | ||
789dd40b | 176 | return (void *) SCM_UNPACK (SCM_SIMPLE_VECTOR_REF (d->wv, d->k)); |
a141db86 AW |
177 | } |
178 | ||
179 | SCM | |
180 | scm_c_weak_vector_ref (SCM wv, size_t k) | |
c9647bfb | 181 | #define FUNC_NAME s_scm_weak_vector_ref |
a141db86 AW |
182 | { |
183 | struct weak_vector_ref_data d; | |
184 | void *ret; | |
185 | ||
c9647bfb AW |
186 | SCM_VALIDATE_WEAK_VECTOR (1, wv); |
187 | ||
a141db86 AW |
188 | d.wv = wv; |
189 | d.k = k; | |
190 | ||
191 | if (k >= SCM_I_VECTOR_LENGTH (wv)) | |
adf06a72 | 192 | scm_out_of_range ("weak-vector-ref", scm_from_size_t (k)); |
a141db86 AW |
193 | |
194 | ret = GC_call_with_alloc_lock (weak_vector_ref, &d); | |
195 | ||
196 | if (ret) | |
21041372 | 197 | return SCM_PACK_POINTER (ret); |
a141db86 AW |
198 | else |
199 | return SCM_BOOL_F; | |
200 | } | |
c9647bfb AW |
201 | #undef FUNC_NAME |
202 | ||
203 | ||
204 | SCM_DEFINE (scm_weak_vector_set_x, "weak-vector-set!", 3, 0, 0, | |
205 | (SCM wvect, SCM k, SCM obj), | |
206 | "Like @code{vector-set!}, but for weak vectors.") | |
207 | #define FUNC_NAME s_scm_weak_vector_set_x | |
208 | { | |
209 | scm_c_weak_vector_set_x (wvect, scm_to_size_t (k), obj); | |
210 | ||
211 | return SCM_UNSPECIFIED; | |
212 | } | |
213 | #undef FUNC_NAME | |
a141db86 AW |
214 | |
215 | ||
216 | void | |
217 | scm_c_weak_vector_set_x (SCM wv, size_t k, SCM x) | |
c9647bfb | 218 | #define FUNC_NAME s_scm_weak_vector_set_x |
a141db86 AW |
219 | { |
220 | SCM *elts; | |
221 | struct weak_vector_ref_data d; | |
222 | void *prev; | |
223 | ||
c9647bfb AW |
224 | SCM_VALIDATE_WEAK_VECTOR (1, wv); |
225 | ||
a141db86 AW |
226 | d.wv = wv; |
227 | d.k = k; | |
228 | ||
229 | if (k >= SCM_I_VECTOR_LENGTH (wv)) | |
adf06a72 | 230 | scm_out_of_range ("weak-vector-set!", scm_from_size_t (k)); |
a141db86 AW |
231 | |
232 | prev = GC_call_with_alloc_lock (weak_vector_ref, &d); | |
233 | ||
234 | elts = SCM_I_VECTOR_WELTS (wv); | |
235 | ||
8c5bb729 | 236 | if (prev && SCM_HEAP_OBJECT_P (SCM_PACK_POINTER (prev))) |
2aed2667 | 237 | GC_unregister_disappearing_link ((void **) &elts[k]); |
a141db86 AW |
238 | |
239 | elts[k] = x; | |
240 | ||
8c5bb729 | 241 | if (SCM_HEAP_OBJECT_P (x)) |
2aed2667 AW |
242 | SCM_I_REGISTER_DISAPPEARING_LINK ((void **) &elts[k], |
243 | SCM2PTR (x)); | |
a141db86 | 244 | } |
c9647bfb | 245 | #undef FUNC_NAME |
a141db86 AW |
246 | |
247 | ||
248 | \f | |
249 | static void | |
250 | scm_init_weak_vector_builtins (void) | |
251 | { | |
252 | #ifndef SCM_MAGIC_SNARFER | |
253 | #include "libguile/weak-vector.x" | |
254 | #endif | |
255 | } | |
256 | ||
257 | void | |
258 | scm_init_weak_vectors () | |
259 | { | |
260 | scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION, | |
261 | "scm_init_weak_vector_builtins", | |
262 | (scm_t_extension_init_func)scm_init_weak_vector_builtins, | |
263 | NULL); | |
264 | } | |
265 | ||
266 | ||
267 | /* | |
268 | Local Variables: | |
269 | c-file-style: "gnu" | |
270 | End: | |
271 | */ |