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