Commit | Line | Data |
---|---|---|
cc95e00a | 1 | /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
73be1d9e MV |
8 | * This library is distributed in the hope that it will be useful, |
9 | * but 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. | |
0f2d19dd | 12 | * |
73be1d9e MV |
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 | |
92205699 | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
73be1d9e | 16 | */ |
1bbd0b84 | 17 | |
0f2d19dd | 18 | \f |
a6f7f57d RB |
19 | #if HAVE_CONFIG_H |
20 | # include <config.h> | |
21 | #endif | |
0f2d19dd | 22 | |
a0599745 | 23 | #include "libguile/_scm.h" |
4e047c3e | 24 | #include "libguile/async.h" |
a0599745 MD |
25 | #include "libguile/chars.h" |
26 | #include "libguile/eval.h" | |
27 | #include "libguile/alist.h" | |
28 | #include "libguile/weaks.h" | |
29 | #include "libguile/hashtab.h" | |
30 | #include "libguile/ports.h" | |
31 | #include "libguile/strings.h" | |
32 | ||
33 | #include "libguile/validate.h" | |
34 | #include "libguile/struct.h" | |
0f2d19dd | 35 | |
95b88819 GH |
36 | #ifdef HAVE_STRING_H |
37 | #include <string.h> | |
38 | #endif | |
39 | ||
0f2d19dd JB |
40 | \f |
41 | ||
42 | static SCM required_vtable_fields = SCM_BOOL_F; | |
98d5f601 | 43 | SCM scm_struct_table; |
0f2d19dd JB |
44 | |
45 | \f | |
a1ec6916 | 46 | SCM_DEFINE (scm_make_struct_layout, "make-struct-layout", 1, 0, 0, |
1bbd0b84 | 47 | (SCM fields), |
b380b885 | 48 | "Return a new structure layout object.\n\n" |
7c31152f | 49 | "@var{fields} must be a string made up of pairs of characters\n" |
b380b885 MD |
50 | "strung together. The first character of each pair describes a field\n" |
51 | "type, the second a field protection. Allowed types are 'p' for\n" | |
52 | "GC-protected Scheme data, 'u' for unprotected binary data, and 's' for\n" | |
04323af4 | 53 | "a field that points to the structure itself. Allowed protections\n" |
9401323e | 54 | "are 'w' for mutable fields, 'r' for read-only fields, and 'o' for opaque\n" |
b380b885 MD |
55 | "fields. The last field protection specification may be capitalized to\n" |
56 | "indicate that the field is a tail-array.") | |
1bbd0b84 | 57 | #define FUNC_NAME s_scm_make_struct_layout |
0f2d19dd JB |
58 | { |
59 | SCM new_sym; | |
d1ca2c64 | 60 | SCM_VALIDATE_STRING (1, fields); |
2ade72d7 | 61 | |
1bbd0b84 | 62 | { /* scope */ |
cc95e00a | 63 | const char * field_desc; |
1be6b49c | 64 | size_t len; |
0f2d19dd JB |
65 | int x; |
66 | ||
cc95e00a | 67 | len = scm_i_string_length (fields); |
2ade72d7 DH |
68 | if (len % 2 == 1) |
69 | SCM_MISC_ERROR ("odd length field specification: ~S", | |
1afff620 | 70 | scm_list_1 (fields)); |
2ade72d7 | 71 | |
cc95e00a | 72 | field_desc = scm_i_string_chars (fields); |
0f2d19dd JB |
73 | |
74 | for (x = 0; x < len; x += 2) | |
75 | { | |
76 | switch (field_desc[x]) | |
77 | { | |
78 | case 'u': | |
79 | case 'p': | |
80 | #if 0 | |
81 | case 'i': | |
82 | case 'd': | |
83 | #endif | |
84 | case 's': | |
85 | break; | |
86 | default: | |
2ade72d7 | 87 | SCM_MISC_ERROR ("unrecognized field type: ~S", |
1afff620 | 88 | scm_list_1 (SCM_MAKE_CHAR (field_desc[x]))); |
0f2d19dd JB |
89 | } |
90 | ||
91 | switch (field_desc[x + 1]) | |
92 | { | |
93 | case 'w': | |
2ade72d7 DH |
94 | if (field_desc[x] == 's') |
95 | SCM_MISC_ERROR ("self fields not writable", SCM_EOL); | |
0f2d19dd JB |
96 | case 'r': |
97 | case 'o': | |
98 | break; | |
2c36c351 MD |
99 | case 'R': |
100 | case 'W': | |
101 | case 'O': | |
2ade72d7 DH |
102 | if (field_desc[x] == 's') |
103 | SCM_MISC_ERROR ("self fields not allowed in tail array", | |
104 | SCM_EOL); | |
105 | if (x != len - 2) | |
106 | SCM_MISC_ERROR ("tail array field must be last field in layout", | |
107 | SCM_EOL); | |
2c36c351 | 108 | break; |
0f2d19dd | 109 | default: |
2ade72d7 | 110 | SCM_MISC_ERROR ("unrecognized ref specification: ~S", |
1afff620 | 111 | scm_list_1 (SCM_MAKE_CHAR (field_desc[x + 1]))); |
0f2d19dd JB |
112 | } |
113 | #if 0 | |
114 | if (field_desc[x] == 'd') | |
115 | { | |
2ade72d7 DH |
116 | if (field_desc[x + 2] != '-') |
117 | SCM_MISC_ERROR ("missing dash field at position ~A", | |
e11e83f3 | 118 | scm_list_1 (scm_from_int (x / 2))); |
0f2d19dd JB |
119 | x += 2; |
120 | goto recheck_ref; | |
121 | } | |
122 | #endif | |
123 | } | |
cc95e00a | 124 | new_sym = scm_string_to_symbol (fields); |
0f2d19dd | 125 | } |
8824ac88 MV |
126 | scm_remember_upto_here_1 (fields); |
127 | return new_sym; | |
0f2d19dd | 128 | } |
1bbd0b84 | 129 | #undef FUNC_NAME |
0f2d19dd JB |
130 | |
131 | \f | |
132 | ||
133 | ||
1cc91f1b | 134 | |
f7620510 | 135 | static void |
92c2555f | 136 | scm_struct_init (SCM handle, SCM layout, scm_t_bits * mem, int tail_elts, SCM inits) |
0f2d19dd | 137 | { |
cc95e00a MV |
138 | unsigned const char *fields_desc = |
139 | (unsigned const char *) scm_i_symbol_chars (layout) - 2; | |
35de7ebe | 140 | unsigned char prot = 0; |
cc95e00a | 141 | int n_fields = scm_i_symbol_length (layout) / 2; |
2c36c351 | 142 | int tailp = 0; |
d8c40b9f | 143 | |
0f2d19dd JB |
144 | while (n_fields) |
145 | { | |
2c36c351 MD |
146 | if (!tailp) |
147 | { | |
148 | fields_desc += 2; | |
149 | prot = fields_desc[1]; | |
150 | if (SCM_LAYOUT_TAILP (prot)) | |
151 | { | |
152 | tailp = 1; | |
153 | prot = prot == 'R' ? 'r' : prot == 'W' ? 'w' : 'o'; | |
d8c40b9f | 154 | *mem++ = tail_elts; |
2c36c351 MD |
155 | n_fields += tail_elts - 1; |
156 | if (n_fields == 0) | |
157 | break; | |
158 | } | |
159 | } | |
160 | ||
0f2d19dd JB |
161 | switch (*fields_desc) |
162 | { | |
163 | #if 0 | |
164 | case 'i': | |
2c36c351 | 165 | if ((prot != 'r' && prot != 'w') || inits == SCM_EOL) |
0f2d19dd JB |
166 | *mem = 0; |
167 | else | |
168 | { | |
b9bd8526 | 169 | *mem = scm_to_long (SCM_CAR (inits)); |
0f2d19dd JB |
170 | inits = SCM_CDR (inits); |
171 | } | |
172 | break; | |
173 | #endif | |
174 | ||
175 | case 'u': | |
d2e53ed6 | 176 | if ((prot != 'r' && prot != 'w') || scm_is_null (inits)) |
0f2d19dd JB |
177 | *mem = 0; |
178 | else | |
179 | { | |
b9bd8526 | 180 | *mem = scm_to_ulong (SCM_CAR (inits)); |
0f2d19dd JB |
181 | inits = SCM_CDR (inits); |
182 | } | |
183 | break; | |
184 | ||
185 | case 'p': | |
d2e53ed6 | 186 | if ((prot != 'r' && prot != 'w') || scm_is_null (inits)) |
d8c40b9f | 187 | *mem = SCM_UNPACK (SCM_BOOL_F); |
0f2d19dd JB |
188 | else |
189 | { | |
d8c40b9f | 190 | *mem = SCM_UNPACK (SCM_CAR (inits)); |
0f2d19dd JB |
191 | inits = SCM_CDR (inits); |
192 | } | |
193 | ||
194 | break; | |
195 | ||
196 | #if 0 | |
197 | case 'd': | |
2c36c351 | 198 | if ((prot != 'r' && prot != 'w') || inits == SCM_EOL) |
0f2d19dd JB |
199 | *((double *)mem) = 0.0; |
200 | else | |
201 | { | |
a5bfe84d | 202 | *mem = scm_num2dbl (SCM_CAR (inits), "scm_struct_init"); |
0f2d19dd JB |
203 | inits = SCM_CDR (inits); |
204 | } | |
205 | fields_desc += 2; | |
206 | break; | |
207 | #endif | |
208 | ||
209 | case 's': | |
d8c40b9f | 210 | *mem = SCM_UNPACK (handle); |
0f2d19dd JB |
211 | break; |
212 | } | |
213 | ||
0f2d19dd JB |
214 | n_fields--; |
215 | mem++; | |
216 | } | |
217 | } | |
218 | ||
219 | ||
a1ec6916 | 220 | SCM_DEFINE (scm_struct_p, "struct?", 1, 0, 0, |
1bbd0b84 | 221 | (SCM x), |
0233bfc1 | 222 | "Return @code{#t} iff @var{x} is a structure object, else\n" |
942e5b91 | 223 | "@code{#f}.") |
1bbd0b84 | 224 | #define FUNC_NAME s_scm_struct_p |
0f2d19dd | 225 | { |
7888309b | 226 | return scm_from_bool(SCM_STRUCTP (x)); |
0f2d19dd | 227 | } |
1bbd0b84 | 228 | #undef FUNC_NAME |
0f2d19dd | 229 | |
a1ec6916 | 230 | SCM_DEFINE (scm_struct_vtable_p, "struct-vtable?", 1, 0, 0, |
1bbd0b84 | 231 | (SCM x), |
0233bfc1 | 232 | "Return @code{#t} iff @var{x} is a vtable structure.") |
1bbd0b84 | 233 | #define FUNC_NAME s_scm_struct_vtable_p |
0f2d19dd JB |
234 | { |
235 | SCM layout; | |
92c2555f | 236 | scm_t_bits * mem; |
8824ac88 | 237 | int tmp; |
0f2d19dd JB |
238 | |
239 | if (!SCM_STRUCTP (x)) | |
240 | return SCM_BOOL_F; | |
241 | ||
242 | layout = SCM_STRUCT_LAYOUT (x); | |
243 | ||
cc95e00a MV |
244 | if (scm_i_symbol_length (layout) |
245 | < scm_i_string_length (required_vtable_fields)) | |
0f2d19dd JB |
246 | return SCM_BOOL_F; |
247 | ||
cc95e00a MV |
248 | tmp = strncmp (scm_i_symbol_chars (layout), |
249 | scm_i_string_chars (required_vtable_fields), | |
250 | scm_i_string_length (required_vtable_fields)); | |
8824ac88 MV |
251 | scm_remember_upto_here_1 (required_vtable_fields); |
252 | if (tmp) | |
0f2d19dd JB |
253 | return SCM_BOOL_F; |
254 | ||
255 | mem = SCM_STRUCT_DATA (x); | |
256 | ||
cc95e00a | 257 | return scm_from_bool (scm_is_symbol (SCM_PACK (mem[scm_vtable_index_layout]))); |
0f2d19dd | 258 | } |
1bbd0b84 | 259 | #undef FUNC_NAME |
0f2d19dd | 260 | |
14d1400f JB |
261 | |
262 | /* All struct data must be allocated at an address whose bottom three | |
263 | bits are zero. This is because the tag for a struct lives in the | |
264 | bottom three bits of the struct's car, and the upper bits point to | |
265 | the data of its vtable, which is a struct itself. Thus, if the | |
266 | address of that data doesn't end in three zeros, tagging it will | |
267 | destroy the pointer. | |
268 | ||
269 | This function allocates a block of memory, and returns a pointer at | |
270 | least scm_struct_n_extra_words words into the block. Furthermore, | |
271 | it guarantees that that pointer's least three significant bits are | |
272 | all zero. | |
273 | ||
274 | The argument n_words should be the number of words that should | |
275 | appear after the returned address. (That is, it shouldn't include | |
276 | scm_struct_n_extra_words.) | |
277 | ||
278 | This function initializes the following fields of the struct: | |
279 | ||
ad196599 | 280 | scm_struct_i_ptr --- the actual start of the block of memory; the |
14d1400f JB |
281 | address you should pass to 'free' to dispose of the block. |
282 | This field allows us to both guarantee that the returned | |
283 | address is divisible by eight, and allow the GC to free the | |
284 | block. | |
285 | ||
286 | scm_struct_i_n_words --- the number of words allocated to the | |
287 | block, including the extra fields. This is used by the GC. | |
288 | ||
14d1400f JB |
289 | Ugh. */ |
290 | ||
291 | ||
92c2555f | 292 | scm_t_bits * |
4c9419ac | 293 | scm_alloc_struct (int n_words, int n_extra, const char *what) |
14d1400f | 294 | { |
92c2555f | 295 | int size = sizeof (scm_t_bits) * (n_words + n_extra) + 7; |
4c9419ac | 296 | void * block = scm_gc_malloc (size, what); |
14d1400f JB |
297 | |
298 | /* Adjust the pointer to hide the extra words. */ | |
92c2555f | 299 | scm_t_bits * p = (scm_t_bits *) block + n_extra; |
14d1400f JB |
300 | |
301 | /* Adjust it even further so it's aligned on an eight-byte boundary. */ | |
92c2555f | 302 | p = (scm_t_bits *) (((scm_t_bits) p + 7) & ~7); |
14d1400f | 303 | |
ad196599 | 304 | /* Initialize a few fields as described above. */ |
92c2555f MV |
305 | p[scm_struct_i_free] = (scm_t_bits) scm_struct_free_standard; |
306 | p[scm_struct_i_ptr] = (scm_t_bits) block; | |
c8045e8d | 307 | p[scm_struct_i_n_words] = n_words; |
ad196599 | 308 | p[scm_struct_i_flags] = 0; |
14d1400f JB |
309 | |
310 | return p; | |
311 | } | |
312 | ||
4c9419ac | 313 | void |
92c2555f MV |
314 | scm_struct_free_0 (scm_t_bits * vtable SCM_UNUSED, |
315 | scm_t_bits * data SCM_UNUSED) | |
ad196599 | 316 | { |
ad196599 MD |
317 | } |
318 | ||
4c9419ac | 319 | void |
92c2555f | 320 | scm_struct_free_light (scm_t_bits * vtable, scm_t_bits * data) |
ad196599 | 321 | { |
4c9419ac MV |
322 | size_t n = vtable [scm_struct_i_size] & ~SCM_STRUCTF_MASK; |
323 | scm_gc_free (data, n, "struct"); | |
ad196599 MD |
324 | } |
325 | ||
4c9419ac | 326 | void |
92c2555f | 327 | scm_struct_free_standard (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data) |
ad196599 | 328 | { |
c8045e8d | 329 | size_t n = (data[scm_struct_i_n_words] + scm_struct_n_extra_words) |
92c2555f | 330 | * sizeof (scm_t_bits) + 7; |
4c9419ac | 331 | scm_gc_free ((void *) data[scm_struct_i_ptr], n, "heavy struct"); |
ad196599 MD |
332 | } |
333 | ||
4c9419ac | 334 | void |
92c2555f | 335 | scm_struct_free_entity (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data) |
ad196599 | 336 | { |
c8045e8d | 337 | size_t n = (data[scm_struct_i_n_words] + scm_struct_entity_n_extra_words) |
92c2555f | 338 | * sizeof (scm_t_bits) + 7; |
4c9419ac | 339 | scm_gc_free ((void *) data[scm_struct_i_ptr], n, "entity struct"); |
ad196599 | 340 | } |
14d1400f | 341 | |
b4a1358c MD |
342 | static void * |
343 | scm_struct_gc_init (void *dummy1 SCM_UNUSED, | |
344 | void *dummy2 SCM_UNUSED, | |
345 | void *dummy3 SCM_UNUSED) | |
346 | { | |
347 | scm_i_structs_to_free = SCM_EOL; | |
348 | return 0; | |
349 | } | |
350 | ||
08c880a3 | 351 | static void * |
e81d98ec DH |
352 | scm_free_structs (void *dummy1 SCM_UNUSED, |
353 | void *dummy2 SCM_UNUSED, | |
354 | void *dummy3 SCM_UNUSED) | |
08c880a3 | 355 | { |
ffd72400 | 356 | SCM newchain = scm_i_structs_to_free; |
08c880a3 MD |
357 | do |
358 | { | |
359 | /* Mark vtables in GC chain. GC mark set means delay freeing. */ | |
360 | SCM chain = newchain; | |
d2e53ed6 | 361 | while (!scm_is_null (chain)) |
08c880a3 MD |
362 | { |
363 | SCM vtable = SCM_STRUCT_VTABLE (chain); | |
364 | if (SCM_STRUCT_GC_CHAIN (vtable) != 0 && vtable != chain) | |
c8a1bdc4 | 365 | SCM_SET_GC_MARK (vtable); |
08c880a3 MD |
366 | chain = SCM_STRUCT_GC_CHAIN (chain); |
367 | } | |
368 | /* Free unmarked structs. */ | |
369 | chain = newchain; | |
370 | newchain = SCM_EOL; | |
d2e53ed6 | 371 | while (!scm_is_null (chain)) |
08c880a3 MD |
372 | { |
373 | SCM obj = chain; | |
374 | chain = SCM_STRUCT_GC_CHAIN (chain); | |
c8a1bdc4 | 375 | if (SCM_GC_MARK_P (obj)) |
08c880a3 | 376 | { |
c8a1bdc4 | 377 | SCM_CLEAR_GC_MARK (obj); |
08c880a3 MD |
378 | SCM_SET_STRUCT_GC_CHAIN (obj, newchain); |
379 | newchain = obj; | |
380 | } | |
381 | else | |
382 | { | |
904a077d MV |
383 | /* XXX - use less explicit code. */ |
384 | scm_t_bits word0 = SCM_CELL_WORD_0 (obj) - scm_tc3_struct; | |
92c2555f MV |
385 | scm_t_bits * vtable_data = (scm_t_bits *) word0; |
386 | scm_t_bits * data = SCM_STRUCT_DATA (obj); | |
387 | scm_t_struct_free free_struct_data | |
388 | = ((scm_t_struct_free) vtable_data[scm_struct_i_free]); | |
08c880a3 MD |
389 | SCM_SET_CELL_TYPE (obj, scm_tc_free_cell); |
390 | free_struct_data (vtable_data, data); | |
391 | } | |
392 | } | |
393 | } | |
d2e53ed6 | 394 | while (!scm_is_null (newchain)); |
08c880a3 MD |
395 | return 0; |
396 | } | |
397 | ||
a1ec6916 | 398 | SCM_DEFINE (scm_make_struct, "make-struct", 2, 0, 1, |
1bbd0b84 | 399 | (SCM vtable, SCM tail_array_size, SCM init), |
b380b885 | 400 | "Create a new structure.\n\n" |
1bee0e70 | 401 | "@var{type} must be a vtable structure (@pxref{Vtables}).\n\n" |
b380b885 MD |
402 | "@var{tail-elts} must be a non-negative integer. If the layout\n" |
403 | "specification indicated by @var{type} includes a tail-array,\n" | |
404 | "this is the number of elements allocated to that array.\n\n" | |
6386e25c | 405 | "The @var{init1}, @dots{} are optional arguments describing how\n" |
04323af4 MD |
406 | "successive fields of the structure should be initialized. Only fields\n" |
407 | "with protection 'r' or 'w' can be initialized, except for fields of\n" | |
408 | "type 's', which are automatically initialized to point to the new\n" | |
409 | "structure itself; fields with protection 'o' can not be initialized by\n" | |
410 | "Scheme programs.\n\n" | |
411 | "If fewer optional arguments than initializable fields are supplied,\n" | |
412 | "fields of type 'p' get default value #f while fields of type 'u' are\n" | |
413 | "initialized to 0.\n\n" | |
414 | "Structs are currently the basic representation for record-like data\n" | |
415 | "structures in Guile. The plan is to eventually replace them with a\n" | |
416 | "new representation which will at the same time be easier to use and\n" | |
417 | "more powerful.\n\n" | |
6386e25c | 418 | "For more information, see the documentation for @code{make-vtable-vtable}.") |
1bbd0b84 | 419 | #define FUNC_NAME s_scm_make_struct |
0f2d19dd JB |
420 | { |
421 | SCM layout; | |
a55c2b68 MV |
422 | size_t basic_size; |
423 | size_t tail_elts; | |
92c2555f | 424 | scm_t_bits * data; |
0f2d19dd JB |
425 | SCM handle; |
426 | ||
34d19ef6 | 427 | SCM_VALIDATE_VTABLE (1, vtable); |
af45e3b0 | 428 | SCM_VALIDATE_REST_ARGUMENT (init); |
0f2d19dd | 429 | |
d8c40b9f | 430 | layout = SCM_PACK (SCM_STRUCT_DATA (vtable) [scm_vtable_index_layout]); |
cc95e00a | 431 | basic_size = scm_i_symbol_length (layout) / 2; |
a55c2b68 | 432 | tail_elts = scm_to_size_t (tail_array_size); |
9de87eea | 433 | SCM_CRITICAL_SECTION_START; |
d8c40b9f | 434 | if (SCM_STRUCT_DATA (vtable)[scm_struct_i_flags] & SCM_STRUCTF_ENTITY) |
a5bfe84d MD |
435 | { |
436 | data = scm_alloc_struct (basic_size + tail_elts, | |
98d5f601 | 437 | scm_struct_entity_n_extra_words, |
4c9419ac | 438 | "entity struct"); |
c8045e8d DH |
439 | data[scm_struct_i_procedure] = SCM_UNPACK (SCM_BOOL_F); |
440 | data[scm_struct_i_setter] = SCM_UNPACK (SCM_BOOL_F); | |
a5bfe84d MD |
441 | } |
442 | else | |
443 | data = scm_alloc_struct (basic_size + tail_elts, | |
444 | scm_struct_n_extra_words, | |
4c9419ac | 445 | "struct"); |
228a24ef DH |
446 | handle = scm_double_cell ((((scm_t_bits) SCM_STRUCT_DATA (vtable)) |
447 | + scm_tc3_struct), | |
448 | (scm_t_bits) data, 0, 0); | |
f7620510 | 449 | scm_struct_init (handle, layout, data, tail_elts, init); |
9de87eea | 450 | SCM_CRITICAL_SECTION_END; |
0f2d19dd JB |
451 | return handle; |
452 | } | |
1bbd0b84 | 453 | #undef FUNC_NAME |
0f2d19dd JB |
454 | |
455 | ||
456 | ||
a1ec6916 | 457 | SCM_DEFINE (scm_make_vtable_vtable, "make-vtable-vtable", 2, 0, 1, |
04323af4 | 458 | (SCM user_fields, SCM tail_array_size, SCM init), |
b380b885 | 459 | "Return a new, self-describing vtable structure.\n\n" |
04323af4 MD |
460 | "@var{user-fields} is a string describing user defined fields of the\n" |
461 | "vtable beginning at index @code{vtable-offset-user}\n" | |
462 | "(see @code{make-struct-layout}).\n\n" | |
b380b885 MD |
463 | "@var{tail-size} specifies the size of the tail-array (if any) of\n" |
464 | "this vtable.\n\n" | |
6386e25c | 465 | "@var{init1}, @dots{} are the optional initializers for the fields of\n" |
04323af4 MD |
466 | "the vtable.\n\n" |
467 | "Vtables have one initializable system field---the struct printer.\n" | |
468 | "This field comes before the user fields in the initializers passed\n" | |
469 | "to @code{make-vtable-vtable} and @code{make-struct}, and thus works as\n" | |
470 | "a third optional argument to @code{make-vtable-vtable} and a fourth to\n" | |
471 | "@code{make-struct} when creating vtables:\n\n" | |
472 | "If the value is a procedure, it will be called instead of the standard\n" | |
473 | "printer whenever a struct described by this vtable is printed.\n" | |
474 | "The procedure will be called with arguments STRUCT and PORT.\n\n" | |
475 | "The structure of a struct is described by a vtable, so the vtable is\n" | |
476 | "in essence the type of the struct. The vtable is itself a struct with\n" | |
477 | "a vtable. This could go on forever if it weren't for the\n" | |
29b4f9fb | 478 | "vtable-vtables which are self-describing vtables, and thus terminate\n" |
04323af4 MD |
479 | "the chain.\n\n" |
480 | "There are several potential ways of using structs, but the standard\n" | |
481 | "one is to use three kinds of structs, together building up a type\n" | |
482 | "sub-system: one vtable-vtable working as the root and one or several\n" | |
483 | "\"types\", each with a set of \"instances\". (The vtable-vtable should be\n" | |
29b4f9fb | 484 | "compared to the class <class> which is the class of itself.)\n\n" |
1e6808ea | 485 | "@lisp\n" |
04323af4 MD |
486 | "(define ball-root (make-vtable-vtable \"pr\" 0))\n\n" |
487 | "(define (make-ball-type ball-color)\n" | |
488 | " (make-struct ball-root 0\n" | |
489 | " (make-struct-layout \"pw\")\n" | |
490 | " (lambda (ball port)\n" | |
491 | " (format port \"#<a ~A ball owned by ~A>\"\n" | |
492 | " (color ball)\n" | |
493 | " (owner ball)))\n" | |
494 | " ball-color))\n" | |
495 | "(define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))\n" | |
496 | "(define (owner ball) (struct-ref ball 0))\n\n" | |
497 | "(define red (make-ball-type 'red))\n" | |
498 | "(define green (make-ball-type 'green))\n\n" | |
499 | "(define (make-ball type owner) (make-struct type 0 owner))\n\n" | |
500 | "(define ball (make-ball green 'Nisse))\n" | |
501 | "ball @result{} #<a green ball owned by Nisse>\n" | |
9401323e | 502 | "@end lisp") |
1bbd0b84 | 503 | #define FUNC_NAME s_scm_make_vtable_vtable |
0f2d19dd JB |
504 | { |
505 | SCM fields; | |
506 | SCM layout; | |
a55c2b68 MV |
507 | size_t basic_size; |
508 | size_t tail_elts; | |
509 | scm_t_bits *data; | |
0f2d19dd JB |
510 | SCM handle; |
511 | ||
d1ca2c64 | 512 | SCM_VALIDATE_STRING (1, user_fields); |
af45e3b0 | 513 | SCM_VALIDATE_REST_ARGUMENT (init); |
0f2d19dd | 514 | |
1afff620 KN |
515 | fields = scm_string_append (scm_list_2 (required_vtable_fields, |
516 | user_fields)); | |
0f2d19dd | 517 | layout = scm_make_struct_layout (fields); |
cc95e00a | 518 | basic_size = scm_i_symbol_length (layout) / 2; |
a55c2b68 | 519 | tail_elts = scm_to_size_t (tail_array_size); |
9de87eea | 520 | SCM_CRITICAL_SECTION_START; |
a5bfe84d MD |
521 | data = scm_alloc_struct (basic_size + tail_elts, |
522 | scm_struct_n_extra_words, | |
4c9419ac | 523 | "struct"); |
228a24ef DH |
524 | handle = scm_double_cell ((scm_t_bits) data + scm_tc3_struct, |
525 | (scm_t_bits) data, 0, 0); | |
f7620510 DH |
526 | data [scm_vtable_index_layout] = SCM_UNPACK (layout); |
527 | scm_struct_init (handle, layout, data, tail_elts, scm_cons (layout, init)); | |
9de87eea | 528 | SCM_CRITICAL_SECTION_END; |
0f2d19dd JB |
529 | return handle; |
530 | } | |
1bbd0b84 | 531 | #undef FUNC_NAME |
0f2d19dd JB |
532 | |
533 | \f | |
534 | ||
535 | ||
a1ec6916 | 536 | SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0, |
1bbd0b84 | 537 | (SCM handle, SCM pos), |
8f85c0c6 | 538 | "@deffnx {Scheme Procedure} struct-set! struct n value\n" |
b380b885 MD |
539 | "Access (or modify) the @var{n}th field of @var{struct}.\n\n" |
540 | "If the field is of type 'p', then it can be set to an arbitrary value.\n\n" | |
541 | "If the field is of type 'u', then it can only be set to a non-negative\n" | |
542 | "integer value small enough to fit in one machine word.") | |
1bbd0b84 | 543 | #define FUNC_NAME s_scm_struct_ref |
0f2d19dd | 544 | { |
5e840c2e | 545 | SCM answer = SCM_UNDEFINED; |
92c2555f | 546 | scm_t_bits * data; |
0f2d19dd | 547 | SCM layout; |
cc95e00a | 548 | size_t layout_len; |
a55c2b68 | 549 | size_t p; |
92c2555f | 550 | scm_t_bits n_fields; |
cc95e00a | 551 | const char *fields_desc; |
e51fe79c | 552 | char field_type = 0; |
0f2d19dd JB |
553 | |
554 | ||
34d19ef6 | 555 | SCM_VALIDATE_STRUCT (1, handle); |
0f2d19dd JB |
556 | |
557 | layout = SCM_STRUCT_LAYOUT (handle); | |
558 | data = SCM_STRUCT_DATA (handle); | |
a55c2b68 | 559 | p = scm_to_size_t (pos); |
0f2d19dd | 560 | |
cc95e00a MV |
561 | fields_desc = scm_i_symbol_chars (layout); |
562 | layout_len = scm_i_symbol_length (layout); | |
d8c40b9f | 563 | n_fields = data[scm_struct_i_n_words]; |
2c36c351 | 564 | |
34d19ef6 | 565 | SCM_ASSERT_RANGE(1, pos, p < n_fields); |
0f2d19dd | 566 | |
cc95e00a | 567 | if (p * 2 < layout_len) |
2c36c351 | 568 | { |
e51fe79c | 569 | char ref; |
2c36c351 MD |
570 | field_type = fields_desc[p * 2]; |
571 | ref = fields_desc[p * 2 + 1]; | |
572 | if ((ref != 'r') && (ref != 'w')) | |
573 | { | |
574 | if ((ref == 'R') || (ref == 'W')) | |
575 | field_type = 'u'; | |
576 | else | |
1afff620 | 577 | SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos)); |
2c36c351 MD |
578 | } |
579 | } | |
cc95e00a MV |
580 | else if (fields_desc[layout_len - 1] != 'O') |
581 | field_type = fields_desc[layout_len - 2]; | |
2c36c351 | 582 | else |
1afff620 | 583 | SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos)); |
2c36c351 | 584 | |
0f2d19dd JB |
585 | switch (field_type) |
586 | { | |
587 | case 'u': | |
b9bd8526 | 588 | answer = scm_from_ulong (data[p]); |
0f2d19dd JB |
589 | break; |
590 | ||
591 | #if 0 | |
592 | case 'i': | |
b9bd8526 | 593 | answer = scm_from_long (data[p]); |
0f2d19dd JB |
594 | break; |
595 | ||
596 | case 'd': | |
f8de44c1 | 597 | answer = scm_make_real (*((double *)&(data[p]))); |
0f2d19dd JB |
598 | break; |
599 | #endif | |
600 | ||
601 | case 's': | |
602 | case 'p': | |
d8c40b9f | 603 | answer = SCM_PACK (data[p]); |
0f2d19dd JB |
604 | break; |
605 | ||
606 | ||
607 | default: | |
2ade72d7 | 608 | SCM_MISC_ERROR ("unrecognized field type: ~S", |
1afff620 | 609 | scm_list_1 (SCM_MAKE_CHAR (field_type))); |
0f2d19dd JB |
610 | } |
611 | ||
612 | return answer; | |
613 | } | |
1bbd0b84 | 614 | #undef FUNC_NAME |
0f2d19dd JB |
615 | |
616 | ||
a1ec6916 | 617 | SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0, |
1bbd0b84 | 618 | (SCM handle, SCM pos, SCM val), |
e3239868 DH |
619 | "Set the slot of the structure @var{handle} with index @var{pos}\n" |
620 | "to @var{val}. Signal an error if the slot can not be written\n" | |
621 | "to.") | |
1bbd0b84 | 622 | #define FUNC_NAME s_scm_struct_set_x |
0f2d19dd | 623 | { |
92c2555f | 624 | scm_t_bits * data; |
0f2d19dd | 625 | SCM layout; |
cc95e00a | 626 | size_t layout_len; |
a55c2b68 | 627 | size_t p; |
0f2d19dd | 628 | int n_fields; |
cc95e00a | 629 | const char *fields_desc; |
e51fe79c | 630 | char field_type = 0; |
0f2d19dd | 631 | |
34d19ef6 | 632 | SCM_VALIDATE_STRUCT (1, handle); |
0f2d19dd JB |
633 | |
634 | layout = SCM_STRUCT_LAYOUT (handle); | |
635 | data = SCM_STRUCT_DATA (handle); | |
a55c2b68 | 636 | p = scm_to_size_t (pos); |
0f2d19dd | 637 | |
cc95e00a MV |
638 | fields_desc = scm_i_symbol_chars (layout); |
639 | layout_len = scm_i_symbol_length (layout); | |
d8c40b9f | 640 | n_fields = data[scm_struct_i_n_words]; |
0f2d19dd | 641 | |
34d19ef6 | 642 | SCM_ASSERT_RANGE (1, pos, p < n_fields); |
0f2d19dd | 643 | |
cc95e00a | 644 | if (p * 2 < layout_len) |
2c36c351 | 645 | { |
e51fe79c | 646 | char set_x; |
2c36c351 MD |
647 | field_type = fields_desc[p * 2]; |
648 | set_x = fields_desc [p * 2 + 1]; | |
649 | if (set_x != 'w') | |
1afff620 | 650 | SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos)); |
2c36c351 | 651 | } |
cc95e00a MV |
652 | else if (fields_desc[layout_len - 1] == 'W') |
653 | field_type = fields_desc[layout_len - 2]; | |
2c36c351 | 654 | else |
1afff620 | 655 | SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos)); |
2c36c351 | 656 | |
0f2d19dd JB |
657 | switch (field_type) |
658 | { | |
659 | case 'u': | |
d8c40b9f | 660 | data[p] = SCM_NUM2ULONG (3, val); |
0f2d19dd JB |
661 | break; |
662 | ||
663 | #if 0 | |
664 | case 'i': | |
e4b265d8 | 665 | data[p] = SCM_NUM2LONG (3, val); |
0f2d19dd JB |
666 | break; |
667 | ||
668 | case 'd': | |
669 | *((double *)&(data[p])) = scm_num2dbl (val, (char *)SCM_ARG3); | |
670 | break; | |
671 | #endif | |
672 | ||
673 | case 'p': | |
d8c40b9f | 674 | data[p] = SCM_UNPACK (val); |
0f2d19dd JB |
675 | break; |
676 | ||
677 | case 's': | |
2ade72d7 | 678 | SCM_MISC_ERROR ("self fields immutable", SCM_EOL); |
0f2d19dd JB |
679 | |
680 | default: | |
2ade72d7 | 681 | SCM_MISC_ERROR ("unrecognized field type: ~S", |
1afff620 | 682 | scm_list_1 (SCM_MAKE_CHAR (field_type))); |
0f2d19dd JB |
683 | } |
684 | ||
685 | return val; | |
686 | } | |
1bbd0b84 | 687 | #undef FUNC_NAME |
0f2d19dd JB |
688 | |
689 | ||
a1ec6916 | 690 | SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0, |
1bbd0b84 | 691 | (SCM handle), |
b380b885 | 692 | "Return the vtable structure that describes the type of @var{struct}.") |
1bbd0b84 | 693 | #define FUNC_NAME s_scm_struct_vtable |
0f2d19dd | 694 | { |
34d19ef6 | 695 | SCM_VALIDATE_STRUCT (1, handle); |
0f2d19dd JB |
696 | return SCM_STRUCT_VTABLE (handle); |
697 | } | |
1bbd0b84 | 698 | #undef FUNC_NAME |
0f2d19dd JB |
699 | |
700 | ||
a1ec6916 | 701 | SCM_DEFINE (scm_struct_vtable_tag, "struct-vtable-tag", 1, 0, 0, |
1bbd0b84 | 702 | (SCM handle), |
e3239868 | 703 | "Return the vtable tag of the structure @var{handle}.") |
1bbd0b84 | 704 | #define FUNC_NAME s_scm_struct_vtable_tag |
0f2d19dd | 705 | { |
34d19ef6 | 706 | SCM_VALIDATE_VTABLE (1, handle); |
b9bd8526 | 707 | return scm_from_ulong (((unsigned long)SCM_STRUCT_DATA (handle)) >> 3); |
98d5f601 | 708 | } |
1bbd0b84 | 709 | #undef FUNC_NAME |
98d5f601 MD |
710 | |
711 | /* {Associating names and classes with vtables} | |
712 | * | |
713 | * The name of a vtable should probably be stored as a slot. This is | |
714 | * a backward compatible solution until agreement has been achieved on | |
715 | * how to associate names with vtables. | |
716 | */ | |
717 | ||
c014a02e ML |
718 | unsigned long |
719 | scm_struct_ihashq (SCM obj, unsigned long n) | |
98d5f601 | 720 | { |
ad196599 MD |
721 | /* The length of the hash table should be a relative prime it's not |
722 | necessary to shift down the address. */ | |
f1267706 | 723 | return SCM_UNPACK (obj) % n; |
98d5f601 MD |
724 | } |
725 | ||
726 | SCM | |
727 | scm_struct_create_handle (SCM obj) | |
728 | { | |
729 | SCM handle = scm_hash_fn_create_handle_x (scm_struct_table, | |
730 | obj, | |
731 | SCM_BOOL_F, | |
732 | scm_struct_ihashq, | |
733 | scm_sloppy_assq, | |
734 | 0); | |
7888309b | 735 | if (scm_is_false (SCM_CDR (handle))) |
98d5f601 MD |
736 | SCM_SETCDR (handle, scm_cons (SCM_BOOL_F, SCM_BOOL_F)); |
737 | return handle; | |
738 | } | |
739 | ||
a1ec6916 | 740 | SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0, |
1bbd0b84 | 741 | (SCM vtable), |
e3239868 | 742 | "Return the name of the vtable @var{vtable}.") |
1bbd0b84 | 743 | #define FUNC_NAME s_scm_struct_vtable_name |
98d5f601 | 744 | { |
34d19ef6 | 745 | SCM_VALIDATE_VTABLE (1, vtable); |
98d5f601 MD |
746 | return SCM_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable))); |
747 | } | |
1bbd0b84 | 748 | #undef FUNC_NAME |
98d5f601 | 749 | |
a1ec6916 | 750 | SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0, |
1bbd0b84 | 751 | (SCM vtable, SCM name), |
e3239868 | 752 | "Set the name of the vtable @var{vtable} to @var{name}.") |
1bbd0b84 | 753 | #define FUNC_NAME s_scm_set_struct_vtable_name_x |
98d5f601 | 754 | { |
34d19ef6 HWN |
755 | SCM_VALIDATE_VTABLE (1, vtable); |
756 | SCM_VALIDATE_SYMBOL (2, name); | |
98d5f601 MD |
757 | SCM_SET_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)), |
758 | name); | |
759 | return SCM_UNSPECIFIED; | |
0f2d19dd | 760 | } |
1bbd0b84 | 761 | #undef FUNC_NAME |
0f2d19dd JB |
762 | |
763 | ||
764 | \f | |
765 | ||
bafcafb2 | 766 | void |
1bbd0b84 | 767 | scm_print_struct (SCM exp, SCM port, scm_print_state *pstate) |
bafcafb2 | 768 | { |
7888309b | 769 | if (scm_is_true (scm_procedure_p (SCM_STRUCT_PRINTER (exp)))) |
4bfdf158 MD |
770 | scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate); |
771 | else | |
bafcafb2 | 772 | { |
a1ae1799 MD |
773 | SCM vtable = SCM_STRUCT_VTABLE (exp); |
774 | SCM name = scm_struct_vtable_name (vtable); | |
775 | scm_puts ("#<", port); | |
7888309b | 776 | if (scm_is_true (name)) |
a1ae1799 MD |
777 | scm_display (name, port); |
778 | else | |
779 | scm_puts ("struct", port); | |
780 | scm_putc (' ', port); | |
0345e278 | 781 | scm_uintprint (SCM_UNPACK (vtable), 16, port); |
b7f3516f | 782 | scm_putc (':', port); |
0345e278 | 783 | scm_uintprint (SCM_UNPACK (exp), 16, port); |
b7f3516f | 784 | scm_putc ('>', port); |
bafcafb2 | 785 | } |
bafcafb2 | 786 | } |
1cc91f1b | 787 | |
08c880a3 MD |
788 | void |
789 | scm_struct_prehistory () | |
790 | { | |
ea5c9285 | 791 | scm_i_structs_to_free = SCM_EOL; |
b4a1358c | 792 | scm_c_hook_add (&scm_before_sweep_c_hook, scm_struct_gc_init, 0, 0); |
53af8255 MD |
793 | /* With the new lazy sweep GC, the point at which the entire heap is |
794 | swept is just before the mark phase. */ | |
795 | scm_c_hook_add (&scm_before_mark_c_hook, scm_free_structs, 0, 0); | |
08c880a3 MD |
796 | } |
797 | ||
0f2d19dd JB |
798 | void |
799 | scm_init_struct () | |
0f2d19dd | 800 | { |
98d5f601 | 801 | scm_struct_table |
e11e83f3 | 802 | = scm_permanent_object (scm_make_weak_key_hash_table (scm_from_int (31))); |
cc95e00a | 803 | required_vtable_fields = scm_from_locale_string ("prsrpw"); |
0f2d19dd | 804 | scm_permanent_object (required_vtable_fields); |
e11e83f3 MV |
805 | scm_c_define ("vtable-index-layout", scm_from_int (scm_vtable_index_layout)); |
806 | scm_c_define ("vtable-index-vtable", scm_from_int (scm_vtable_index_vtable)); | |
86d31dfe | 807 | scm_c_define ("vtable-index-printer", |
e11e83f3 MV |
808 | scm_from_int (scm_vtable_index_printer)); |
809 | scm_c_define ("vtable-offset-user", scm_from_int (scm_vtable_offset_user)); | |
a0599745 | 810 | #include "libguile/struct.x" |
0f2d19dd | 811 | } |
89e00824 ML |
812 | |
813 | /* | |
814 | Local Variables: | |
815 | c-file-style: "gnu" | |
816 | End: | |
817 | */ |