Commit | Line | Data |
---|---|---|
789dd40b MW |
1 | /* Copyright (C) 1995, 1996, 1998, 2000, 2001, 2003, 2006, 2008, 2009, |
2 | * 2010, 2011, 2012, 2013 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 | ||
40 | static SCM | |
41 | make_weak_vector (size_t len, SCM fill) | |
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 | { | |
79 | return make_weak_vector (scm_to_size_t (size), fill); | |
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 | ||
101 | wv = make_weak_vector ((size_t) c_size, SCM_BOOL_F); | |
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 | { | |
117 | return scm_from_bool (SCM_I_WVECTP (obj)); | |
118 | } | |
119 | #undef FUNC_NAME | |
120 | ||
121 | ||
122 | struct weak_vector_ref_data | |
123 | { | |
124 | SCM wv; | |
125 | size_t k; | |
126 | }; | |
127 | ||
128 | static void* | |
129 | weak_vector_ref (void *data) | |
130 | { | |
131 | struct weak_vector_ref_data *d = data; | |
132 | ||
789dd40b | 133 | return (void *) SCM_UNPACK (SCM_SIMPLE_VECTOR_REF (d->wv, d->k)); |
a141db86 AW |
134 | } |
135 | ||
136 | SCM | |
137 | scm_c_weak_vector_ref (SCM wv, size_t k) | |
138 | { | |
139 | struct weak_vector_ref_data d; | |
140 | void *ret; | |
141 | ||
142 | d.wv = wv; | |
143 | d.k = k; | |
144 | ||
145 | if (k >= SCM_I_VECTOR_LENGTH (wv)) | |
146 | scm_out_of_range (NULL, scm_from_size_t (k)); | |
147 | ||
148 | ret = GC_call_with_alloc_lock (weak_vector_ref, &d); | |
149 | ||
150 | if (ret) | |
21041372 | 151 | return SCM_PACK_POINTER (ret); |
a141db86 AW |
152 | else |
153 | return SCM_BOOL_F; | |
154 | } | |
155 | ||
156 | ||
157 | void | |
158 | scm_c_weak_vector_set_x (SCM wv, size_t k, SCM x) | |
159 | { | |
160 | SCM *elts; | |
161 | struct weak_vector_ref_data d; | |
162 | void *prev; | |
163 | ||
164 | d.wv = wv; | |
165 | d.k = k; | |
166 | ||
167 | if (k >= SCM_I_VECTOR_LENGTH (wv)) | |
168 | scm_out_of_range (NULL, scm_from_size_t (k)); | |
169 | ||
170 | prev = GC_call_with_alloc_lock (weak_vector_ref, &d); | |
171 | ||
172 | elts = SCM_I_VECTOR_WELTS (wv); | |
173 | ||
8c5bb729 | 174 | if (prev && SCM_HEAP_OBJECT_P (SCM_PACK_POINTER (prev))) |
2aed2667 | 175 | GC_unregister_disappearing_link ((void **) &elts[k]); |
a141db86 AW |
176 | |
177 | elts[k] = x; | |
178 | ||
8c5bb729 | 179 | if (SCM_HEAP_OBJECT_P (x)) |
2aed2667 AW |
180 | SCM_I_REGISTER_DISAPPEARING_LINK ((void **) &elts[k], |
181 | SCM2PTR (x)); | |
a141db86 AW |
182 | } |
183 | ||
184 | ||
185 | \f | |
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 | ||
194 | void | |
195 | scm_init_weak_vectors () | |
196 | { | |
197 | scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION, | |
198 | "scm_init_weak_vector_builtins", | |
199 | (scm_t_extension_init_func)scm_init_weak_vector_builtins, | |
200 | NULL); | |
201 | } | |
202 | ||
203 | ||
204 | /* | |
205 | Local Variables: | |
206 | c-file-style: "gnu" | |
207 | End: | |
208 | */ |