1 /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2007 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
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
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.
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 02110-1301 USA
16 */
17
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include "libguile/_scm.h"
24 #include "libguile/async.h"
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"
35
36 #include "libguile/eq.h"
37
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41
42
43
44 static SCM required_vtable_fields = SCM_BOOL_F;
45 SCM scm_struct_table;
46
47
48 SCM_DEFINE (scm_make_struct_layout, "make-struct-layout", 1, 0, 0,
49 (SCM fields),
50 "Return a new structure layout object.\n\n"
51 "@var{fields} must be a string made up of pairs of characters\n"
52 "strung together. The first character of each pair describes a field\n"
53 "type, the second a field protection. Allowed types are 'p' for\n"
54 "GC-protected Scheme data, 'u' for unprotected binary data, and 's' for\n"
55 "a field that points to the structure itself. Allowed protections\n"
56 "are 'w' for mutable fields, 'r' for read-only fields, and 'o' for opaque\n"
57 "fields. The last field protection specification may be capitalized to\n"
58 "indicate that the field is a tail-array.")
59 #define FUNC_NAME s_scm_make_struct_layout
60 {
61 SCM new_sym;
62 SCM_VALIDATE_STRING (1, fields);
63
64 { /* scope */
65 const char * field_desc;
66 size_t len;
67 int x;
68
69 len = scm_i_string_length (fields);
70 if (len % 2 == 1)
71 SCM_MISC_ERROR ("odd length field specification: ~S",
72 scm_list_1 (fields));
73
74 field_desc = scm_i_string_chars (fields);
75
76 for (x = 0; x < len; x += 2)
77 {
78 switch (field_desc[x])
79 {
80 case 'u':
81 case 'p':
82 #if 0
83 case 'i':
84 case 'd':
85 #endif
86 case 's':
87 break;
88 default:
89 SCM_MISC_ERROR ("unrecognized field type: ~S",
90 scm_list_1 (SCM_MAKE_CHAR (field_desc[x])));
91 }
92
93 switch (field_desc[x + 1])
94 {
95 case 'w':
96 if (field_desc[x] == 's')
97 SCM_MISC_ERROR ("self fields not writable", SCM_EOL);
98 case 'r':
99 case 'o':
100 break;
101 case 'R':
102 case 'W':
103 case 'O':
104 if (field_desc[x] == 's')
105 SCM_MISC_ERROR ("self fields not allowed in tail array",
106 SCM_EOL);
107 if (x != len - 2)
108 SCM_MISC_ERROR ("tail array field must be last field in layout",
109 SCM_EOL);
110 break;
111 default:
112 SCM_MISC_ERROR ("unrecognized ref specification: ~S",
113 scm_list_1 (SCM_MAKE_CHAR (field_desc[x + 1])));
114 }
115 #if 0
116 if (field_desc[x] == 'd')
117 {
118 if (field_desc[x + 2] != '-')
119 SCM_MISC_ERROR ("missing dash field at position ~A",
120 scm_list_1 (scm_from_int (x / 2)));
121 x += 2;
122 goto recheck_ref;
123 }
124 #endif
125 }
126 new_sym = scm_string_to_symbol (fields);
127 }
128 scm_remember_upto_here_1 (fields);
129 return new_sym;
130 }
131 #undef FUNC_NAME
132
133
134
135
136
137 static void
scm_struct_init(SCM handle,SCM layout,scm_t_bits * mem,int tail_elts,SCM inits)138 scm_struct_init (SCM handle, SCM layout, scm_t_bits * mem, int tail_elts, SCM inits)
139 {
140 unsigned const char *fields_desc =
141 (unsigned const char *) scm_i_symbol_chars (layout) - 2;
142 unsigned char prot = 0;
143 int n_fields = scm_i_symbol_length (layout) / 2;
144 int tailp = 0;
145
146 while (n_fields)
147 {
148 if (!tailp)
149 {
150 fields_desc += 2;
151 prot = fields_desc[1];
152 if (SCM_LAYOUT_TAILP (prot))
153 {
154 tailp = 1;
155 prot = prot == 'R' ? 'r' : prot == 'W' ? 'w' : 'o';
156 *mem++ = tail_elts;
157 n_fields += tail_elts - 1;
158 if (n_fields == 0)
159 break;
160 }
161 }
162
163 switch (*fields_desc)
164 {
165 #if 0
166 case 'i':
167 if ((prot != 'r' && prot != 'w') || inits == SCM_EOL)
168 *mem = 0;
169 else
170 {
171 *mem = scm_to_long (SCM_CAR (inits));
172 inits = SCM_CDR (inits);
173 }
174 break;
175 #endif
176
177 case 'u':
178 if ((prot != 'r' && prot != 'w') || scm_is_null (inits))
179 *mem = 0;
180 else
181 {
182 *mem = scm_to_ulong (SCM_CAR (inits));
183 inits = SCM_CDR (inits);
184 }
185 break;
186
187 case 'p':
188 if ((prot != 'r' && prot != 'w') || scm_is_null (inits))
189 *mem = SCM_UNPACK (SCM_BOOL_F);
190 else
191 {
192 *mem = SCM_UNPACK (SCM_CAR (inits));
193 inits = SCM_CDR (inits);
194 }
195
196 break;
197
198 #if 0
199 case 'd':
200 if ((prot != 'r' && prot != 'w') || inits == SCM_EOL)
201 *((double *)mem) = 0.0;
202 else
203 {
204 *mem = scm_num2dbl (SCM_CAR (inits), "scm_struct_init");
205 inits = SCM_CDR (inits);
206 }
207 fields_desc += 2;
208 break;
209 #endif
210
211 case 's':
212 *mem = SCM_UNPACK (handle);
213 break;
214 }
215
216 n_fields--;
217 mem++;
218 }
219 }
220
221
222 SCM_DEFINE (scm_struct_p, "struct?", 1, 0, 0,
223 (SCM x),
224 "Return @code{#t} iff @var{x} is a structure object, else\n"
225 "@code{#f}.")
226 #define FUNC_NAME s_scm_struct_p
227 {
228 return scm_from_bool(SCM_STRUCTP (x));
229 }
230 #undef FUNC_NAME
231
232 SCM_DEFINE (scm_struct_vtable_p, "struct-vtable?", 1, 0, 0,
233 (SCM x),
234 "Return @code{#t} iff @var{x} is a vtable structure.")
235 #define FUNC_NAME s_scm_struct_vtable_p
236 {
237 SCM layout;
238 scm_t_bits * mem;
239 int tmp;
240
241 if (!SCM_STRUCTP (x))
242 return SCM_BOOL_F;
243
244 layout = SCM_STRUCT_LAYOUT (x);
245
246 if (scm_i_symbol_length (layout)
247 < scm_i_string_length (required_vtable_fields))
248 return SCM_BOOL_F;
249
250 tmp = strncmp (scm_i_symbol_chars (layout),
251 scm_i_string_chars (required_vtable_fields),
252 scm_i_string_length (required_vtable_fields));
253 scm_remember_upto_here_1 (required_vtable_fields);
254 if (tmp)
255 return SCM_BOOL_F;
256
257 mem = SCM_STRUCT_DATA (x);
258
259 return scm_from_bool (scm_is_symbol (SCM_PACK (mem[scm_vtable_index_layout])));
260 }
261 #undef FUNC_NAME
262
263
264 /* All struct data must be allocated at an address whose bottom three
265 bits are zero. This is because the tag for a struct lives in the
266 bottom three bits of the struct's car, and the upper bits point to
267 the data of its vtable, which is a struct itself. Thus, if the
268 address of that data doesn't end in three zeros, tagging it will
269 destroy the pointer.
270
271 This function allocates a block of memory, and returns a pointer at
272 least scm_struct_n_extra_words words into the block. Furthermore,
273 it guarantees that that pointer's least three significant bits are
274 all zero.
275
276 The argument n_words should be the number of words that should
277 appear after the returned address. (That is, it shouldn't include
278 scm_struct_n_extra_words.)
279
280 This function initializes the following fields of the struct:
281
282 scm_struct_i_ptr --- the actual start of the block of memory; the
283 address you should pass to 'free' to dispose of the block.
284 This field allows us to both guarantee that the returned
285 address is divisible by eight, and allow the GC to free the
286 block.
287
288 scm_struct_i_n_words --- the number of words allocated to the
289 block, including the extra fields. This is used by the GC.
290
291 Ugh. */
292
293
294 scm_t_bits *
scm_alloc_struct(int n_words,int n_extra,const char * what)295 scm_alloc_struct (int n_words, int n_extra, const char *what)
296 {
297 int size = sizeof (scm_t_bits) * (n_words + n_extra) + 7;
298 void * block = scm_gc_malloc (size, what);
299
300 /* Adjust the pointer to hide the extra words. */
301 scm_t_bits * p = (scm_t_bits *) block + n_extra;
302
303 /* Adjust it even further so it's aligned on an eight-byte boundary. */
304 p = (scm_t_bits *) (((scm_t_bits) p + 7) & ~7);
305
306 /* Initialize a few fields as described above. */
307 p[scm_struct_i_free] = (scm_t_bits) scm_struct_free_standard;
308 p[scm_struct_i_ptr] = (scm_t_bits) block;
309 p[scm_struct_i_n_words] = n_words;
310 p[scm_struct_i_flags] = 0;
311
312 return p;
313 }
314
315 void
scm_struct_free_0(scm_t_bits * vtable SCM_UNUSED,scm_t_bits * data SCM_UNUSED)316 scm_struct_free_0 (scm_t_bits * vtable SCM_UNUSED,
317 scm_t_bits * data SCM_UNUSED)
318 {
319 }
320
321 void
scm_struct_free_light(scm_t_bits * vtable,scm_t_bits * data)322 scm_struct_free_light (scm_t_bits * vtable, scm_t_bits * data)
323 {
324 size_t n = vtable [scm_struct_i_size] & ~SCM_STRUCTF_MASK;
325 scm_gc_free (data, n, "struct");
326 }
327
328 void
scm_struct_free_standard(scm_t_bits * vtable SCM_UNUSED,scm_t_bits * data)329 scm_struct_free_standard (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data)
330 {
331 size_t n = (data[scm_struct_i_n_words] + scm_struct_n_extra_words)
332 * sizeof (scm_t_bits) + 7;
333 scm_gc_free ((void *) data[scm_struct_i_ptr], n, "heavy struct");
334 }
335
336 void
scm_struct_free_entity(scm_t_bits * vtable SCM_UNUSED,scm_t_bits * data)337 scm_struct_free_entity (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data)
338 {
339 size_t n = (data[scm_struct_i_n_words] + scm_struct_entity_n_extra_words)
340 * sizeof (scm_t_bits) + 7;
341 scm_gc_free ((void *) data[scm_struct_i_ptr], n, "entity struct");
342 }
343
344 static void *
scm_struct_gc_init(void * dummy1 SCM_UNUSED,void * dummy2 SCM_UNUSED,void * dummy3 SCM_UNUSED)345 scm_struct_gc_init (void *dummy1 SCM_UNUSED,
346 void *dummy2 SCM_UNUSED,
347 void *dummy3 SCM_UNUSED)
348 {
349 scm_i_structs_to_free = SCM_EOL;
350 return 0;
351 }
352
353 static void *
scm_free_structs(void * dummy1 SCM_UNUSED,void * dummy2 SCM_UNUSED,void * dummy3 SCM_UNUSED)354 scm_free_structs (void *dummy1 SCM_UNUSED,
355 void *dummy2 SCM_UNUSED,
356 void *dummy3 SCM_UNUSED)
357 {
358 SCM newchain = scm_i_structs_to_free;
359 do
360 {
361 /* Mark vtables in GC chain. GC mark set means delay freeing. */
362 SCM chain = newchain;
363 while (!scm_is_null (chain))
364 {
365 SCM vtable = SCM_STRUCT_VTABLE (chain);
366 if (SCM_STRUCT_GC_CHAIN (vtable) != 0 && vtable != chain)
367 SCM_SET_GC_MARK (vtable);
368 chain = SCM_STRUCT_GC_CHAIN (chain);
369 }
370 /* Free unmarked structs. */
371 chain = newchain;
372 newchain = SCM_EOL;
373 while (!scm_is_null (chain))
374 {
375 SCM obj = chain;
376 chain = SCM_STRUCT_GC_CHAIN (chain);
377 if (SCM_GC_MARK_P (obj))
378 {
379 SCM_CLEAR_GC_MARK (obj);
380 SCM_SET_STRUCT_GC_CHAIN (obj, newchain);
381 newchain = obj;
382 }
383 else
384 {
385 scm_t_bits * vtable_data = SCM_STRUCT_VTABLE_DATA (obj);
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]);
389 SCM_SET_CELL_TYPE (obj, scm_tc_free_cell);
390 free_struct_data (vtable_data, data);
391 }
392 }
393 }
394 while (!scm_is_null (newchain));
395 return 0;
396 }
397
398 SCM_DEFINE (scm_make_struct, "make-struct", 2, 0, 1,
399 (SCM vtable, SCM tail_array_size, SCM init),
400 "Create a new structure.\n\n"
401 "@var{type} must be a vtable structure (@pxref{Vtables}).\n\n"
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"
405 "The @var{init1}, @dots{} are optional arguments describing how\n"
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"
418 "For more information, see the documentation for @code{make-vtable-vtable}.")
419 #define FUNC_NAME s_scm_make_struct
420 {
421 SCM layout;
422 size_t basic_size;
423 size_t tail_elts;
424 scm_t_bits * data;
425 SCM handle;
426
427 SCM_VALIDATE_VTABLE (1, vtable);
428 SCM_VALIDATE_REST_ARGUMENT (init);
429
430 layout = SCM_PACK (SCM_STRUCT_DATA (vtable) [scm_vtable_index_layout]);
431 basic_size = scm_i_symbol_length (layout) / 2;
432 tail_elts = scm_to_size_t (tail_array_size);
433
434 /* A tail array is only allowed if the layout fields string ends in "R",
435 "W" or "O". */
436 if (tail_elts != 0)
437 {
438 SCM layout_str, last_char;
439
440 if (basic_size == 0)
441 {
442 bad_tail:
443 SCM_MISC_ERROR ("tail array not allowed unless layout ends R, W, or O", SCM_EOL);
444 }
445
446 layout_str = scm_symbol_to_string (layout);
447 last_char = scm_string_ref (layout_str,
448 scm_from_size_t (2 * basic_size - 1));
449 if (! SCM_LAYOUT_TAILP (SCM_CHAR (last_char)))
450 goto bad_tail;
451 }
452
453 /* In guile 1.8.5 and earlier, everything below was covered by a
454 CRITICAL_SECTION lock. This can lead to deadlocks in garbage
455 collection, since other threads might be holding the heap_mutex, while
456 sleeping on the CRITICAL_SECTION lock. There does not seem to be any
457 need for a lock on the section below, as it does not access or update
458 any globals, so the critical section has been removed. */
459
460 if (SCM_STRUCT_DATA (vtable)[scm_struct_i_flags] & SCM_STRUCTF_ENTITY)
461 {
462 data = scm_alloc_struct (basic_size + tail_elts,
463 scm_struct_entity_n_extra_words,
464 "entity struct");
465 data[scm_struct_i_procedure] = SCM_UNPACK (SCM_BOOL_F);
466 data[scm_struct_i_setter] = SCM_UNPACK (SCM_BOOL_F);
467 }
468 else
469 data = scm_alloc_struct (basic_size + tail_elts,
470 scm_struct_n_extra_words,
471 "struct");
472 handle = scm_double_cell ((((scm_t_bits) SCM_STRUCT_DATA (vtable))
473 + scm_tc3_struct),
474 (scm_t_bits) data, 0, 0);
475
476 scm_struct_init (handle, layout, data, tail_elts, init);
477
478 return handle;
479 }
480 #undef FUNC_NAME
481
482
483
484 SCM_DEFINE (scm_make_vtable_vtable, "make-vtable-vtable", 2, 0, 1,
485 (SCM user_fields, SCM tail_array_size, SCM init),
486 "Return a new, self-describing vtable structure.\n\n"
487 "@var{user-fields} is a string describing user defined fields of the\n"
488 "vtable beginning at index @code{vtable-offset-user}\n"
489 "(see @code{make-struct-layout}).\n\n"
490 "@var{tail-size} specifies the size of the tail-array (if any) of\n"
491 "this vtable.\n\n"
492 "@var{init1}, @dots{} are the optional initializers for the fields of\n"
493 "the vtable.\n\n"
494 "Vtables have one initializable system field---the struct printer.\n"
495 "This field comes before the user fields in the initializers passed\n"
496 "to @code{make-vtable-vtable} and @code{make-struct}, and thus works as\n"
497 "a third optional argument to @code{make-vtable-vtable} and a fourth to\n"
498 "@code{make-struct} when creating vtables:\n\n"
499 "If the value is a procedure, it will be called instead of the standard\n"
500 "printer whenever a struct described by this vtable is printed.\n"
501 "The procedure will be called with arguments STRUCT and PORT.\n\n"
502 "The structure of a struct is described by a vtable, so the vtable is\n"
503 "in essence the type of the struct. The vtable is itself a struct with\n"
504 "a vtable. This could go on forever if it weren't for the\n"
505 "vtable-vtables which are self-describing vtables, and thus terminate\n"
506 "the chain.\n\n"
507 "There are several potential ways of using structs, but the standard\n"
508 "one is to use three kinds of structs, together building up a type\n"
509 "sub-system: one vtable-vtable working as the root and one or several\n"
510 "\"types\", each with a set of \"instances\". (The vtable-vtable should be\n"
511 "compared to the class <class> which is the class of itself.)\n\n"
512 "@lisp\n"
513 "(define ball-root (make-vtable-vtable \"pr\" 0))\n\n"
514 "(define (make-ball-type ball-color)\n"
515 " (make-struct ball-root 0\n"
516 " (make-struct-layout \"pw\")\n"
517 " (lambda (ball port)\n"
518 " (format port \"#<a ~A ball owned by ~A>\"\n"
519 " (color ball)\n"
520 " (owner ball)))\n"
521 " ball-color))\n"
522 "(define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))\n"
523 "(define (owner ball) (struct-ref ball 0))\n\n"
524 "(define red (make-ball-type 'red))\n"
525 "(define green (make-ball-type 'green))\n\n"
526 "(define (make-ball type owner) (make-struct type 0 owner))\n\n"
527 "(define ball (make-ball green 'Nisse))\n"
528 "ball @result{} #<a green ball owned by Nisse>\n"
529 "@end lisp")
530 #define FUNC_NAME s_scm_make_vtable_vtable
531 {
532 SCM fields;
533 SCM layout;
534 size_t basic_size;
535 size_t tail_elts;
536 scm_t_bits *data;
537 SCM handle;
538
539 SCM_VALIDATE_STRING (1, user_fields);
540 SCM_VALIDATE_REST_ARGUMENT (init);
541
542 fields = scm_string_append (scm_list_2 (required_vtable_fields,
543 user_fields));
544 layout = scm_make_struct_layout (fields);
545 basic_size = scm_i_symbol_length (layout) / 2;
546 tail_elts = scm_to_size_t (tail_array_size);
547 SCM_CRITICAL_SECTION_START;
548 data = scm_alloc_struct (basic_size + tail_elts,
549 scm_struct_n_extra_words,
550 "struct");
551 handle = scm_double_cell ((scm_t_bits) data + scm_tc3_struct,
552 (scm_t_bits) data, 0, 0);
553 data [scm_vtable_index_layout] = SCM_UNPACK (layout);
554 scm_struct_init (handle, layout, data, tail_elts, scm_cons (layout, init));
555 SCM_CRITICAL_SECTION_END;
556 return handle;
557 }
558 #undef FUNC_NAME
559
560
561 static SCM scm_i_vtable_vtable_no_extra_fields;
562
563 SCM_DEFINE (scm_make_vtable, "make-vtable", 1, 1, 0,
564 (SCM fields, SCM printer),
565 "Create a vtable, for creating structures with the given\n"
566 "@var{fields}.\n"
567 "\n"
568 "The optional @var{printer} argument is a function to be called\n"
569 "@code{(@var{printer} struct port)} on the structures created.\n"
570 "It should look at @var{struct} and write to @var{port}.")
571 #define FUNC_NAME s_scm_make_vtable
572 {
573 if (SCM_UNBNDP (printer))
574 printer = SCM_BOOL_F;
575
576 return scm_make_struct (scm_i_vtable_vtable_no_extra_fields, SCM_INUM0,
577 scm_list_2 (scm_make_struct_layout (fields),
578 printer));
579 }
580 #undef FUNC_NAME
581
582
583 /* Return true if S1 and S2 are equal structures, i.e., if their vtable and
584 contents are the same. Field protections are honored. Thus, it is an
585 error to test the equality of structures that contain opaque fields. */
586 SCM
scm_i_struct_equalp(SCM s1,SCM s2)587 scm_i_struct_equalp (SCM s1, SCM s2)
588 #define FUNC_NAME "scm_i_struct_equalp"
589 {
590 SCM vtable1, vtable2, layout;
591 size_t struct_size, field_num;
592
593 SCM_VALIDATE_STRUCT (1, s1);
594 SCM_VALIDATE_STRUCT (2, s2);
595
596 vtable1 = SCM_STRUCT_VTABLE (s1);
597 vtable2 = SCM_STRUCT_VTABLE (s2);
598
599 if (!scm_is_eq (vtable1, vtable2))
600 return SCM_BOOL_F;
601
602 layout = SCM_STRUCT_LAYOUT (s1);
603 struct_size = scm_i_symbol_length (layout) / 2;
604
605 for (field_num = 0; field_num < struct_size; field_num++)
606 {
607 SCM s_field_num;
608 SCM field1, field2;
609
610 /* We have to use `scm_struct_ref ()' here so that fields are accessed
611 consistently, notably wrt. field types and access rights. */
612 s_field_num = scm_from_size_t (field_num);
613 field1 = scm_struct_ref (s1, s_field_num);
614 field2 = scm_struct_ref (s2, s_field_num);
615
616 /* Self-referencing fields (type `s') must be skipped to avoid infinite
617 recursion. */
618 if (!(scm_is_eq (field1, s1) && (scm_is_eq (field2, s2))))
619 if (scm_is_false (scm_equal_p (field1, field2)))
620 return SCM_BOOL_F;
621 }
622
623 /* FIXME: Tail elements should be tested for equality. */
624
625 return SCM_BOOL_T;
626 }
627 #undef FUNC_NAME
628
629
630
631
632
633 SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0,
634 (SCM handle, SCM pos),
635 "@deffnx {Scheme Procedure} struct-set! struct n value\n"
636 "Access (or modify) the @var{n}th field of @var{struct}.\n\n"
637 "If the field is of type 'p', then it can be set to an arbitrary value.\n\n"
638 "If the field is of type 'u', then it can only be set to a non-negative\n"
639 "integer value small enough to fit in one machine word.")
640 #define FUNC_NAME s_scm_struct_ref
641 {
642 SCM answer = SCM_UNDEFINED;
643 scm_t_bits * data;
644 SCM layout;
645 size_t layout_len;
646 size_t p;
647 scm_t_bits n_fields;
648 const char *fields_desc;
649 char field_type = 0;
650
651
652 SCM_VALIDATE_STRUCT (1, handle);
653
654 layout = SCM_STRUCT_LAYOUT (handle);
655 data = SCM_STRUCT_DATA (handle);
656 p = scm_to_size_t (pos);
657
658 fields_desc = scm_i_symbol_chars (layout);
659 layout_len = scm_i_symbol_length (layout);
660 if (SCM_STRUCT_VTABLE_FLAGS (handle) & SCM_STRUCTF_LIGHT)
661 /* no extra words */
662 n_fields = layout_len / 2;
663 else
664 n_fields = data[scm_struct_i_n_words];
665
666 SCM_ASSERT_RANGE(1, pos, p < n_fields);
667
668 if (p * 2 < layout_len)
669 {
670 char ref;
671 field_type = fields_desc[p * 2];
672 ref = fields_desc[p * 2 + 1];
673 if ((ref != 'r') && (ref != 'w'))
674 {
675 if ((ref == 'R') || (ref == 'W'))
676 field_type = 'u';
677 else
678 SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
679 }
680 }
681 else if (fields_desc[layout_len - 1] != 'O')
682 field_type = fields_desc[layout_len - 2];
683 else
684 SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
685
686 switch (field_type)
687 {
688 case 'u':
689 answer = scm_from_ulong (data[p]);
690 break;
691
692 #if 0
693 case 'i':
694 answer = scm_from_long (data[p]);
695 break;
696
697 case 'd':
698 answer = scm_make_real (*((double *)&(data[p])));
699 break;
700 #endif
701
702 case 's':
703 case 'p':
704 answer = SCM_PACK (data[p]);
705 break;
706
707
708 default:
709 SCM_MISC_ERROR ("unrecognized field type: ~S",
710 scm_list_1 (SCM_MAKE_CHAR (field_type)));
711 }
712
713 return answer;
714 }
715 #undef FUNC_NAME
716
717
718 SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0,
719 (SCM handle, SCM pos, SCM val),
720 "Set the slot of the structure @var{handle} with index @var{pos}\n"
721 "to @var{val}. Signal an error if the slot can not be written\n"
722 "to.")
723 #define FUNC_NAME s_scm_struct_set_x
724 {
725 scm_t_bits * data;
726 SCM layout;
727 size_t layout_len;
728 size_t p;
729 int n_fields;
730 const char *fields_desc;
731 char field_type = 0;
732
733 SCM_VALIDATE_STRUCT (1, handle);
734
735 layout = SCM_STRUCT_LAYOUT (handle);
736 data = SCM_STRUCT_DATA (handle);
737 p = scm_to_size_t (pos);
738
739 fields_desc = scm_i_symbol_chars (layout);
740 layout_len = scm_i_symbol_length (layout);
741 if (SCM_STRUCT_VTABLE_FLAGS (handle) & SCM_STRUCTF_LIGHT)
742 /* no extra words */
743 n_fields = layout_len / 2;
744 else
745 n_fields = data[scm_struct_i_n_words];
746
747 SCM_ASSERT_RANGE (1, pos, p < n_fields);
748
749 if (p * 2 < layout_len)
750 {
751 char set_x;
752 field_type = fields_desc[p * 2];
753 set_x = fields_desc [p * 2 + 1];
754 if (set_x != 'w')
755 SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
756 }
757 else if (fields_desc[layout_len - 1] == 'W')
758 field_type = fields_desc[layout_len - 2];
759 else
760 SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
761
762 switch (field_type)
763 {
764 case 'u':
765 data[p] = SCM_NUM2ULONG (3, val);
766 break;
767
768 #if 0
769 case 'i':
770 data[p] = SCM_NUM2LONG (3, val);
771 break;
772
773 case 'd':
774 *((double *)&(data[p])) = scm_num2dbl (val, (char *)SCM_ARG3);
775 break;
776 #endif
777
778 case 'p':
779 data[p] = SCM_UNPACK (val);
780 break;
781
782 case 's':
783 SCM_MISC_ERROR ("self fields immutable", SCM_EOL);
784
785 default:
786 SCM_MISC_ERROR ("unrecognized field type: ~S",
787 scm_list_1 (SCM_MAKE_CHAR (field_type)));
788 }
789
790 return val;
791 }
792 #undef FUNC_NAME
793
794
795 SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0,
796 (SCM handle),
797 "Return the vtable structure that describes the type of @var{struct}.")
798 #define FUNC_NAME s_scm_struct_vtable
799 {
800 SCM_VALIDATE_STRUCT (1, handle);
801 return SCM_STRUCT_VTABLE (handle);
802 }
803 #undef FUNC_NAME
804
805
806 SCM_DEFINE (scm_struct_vtable_tag, "struct-vtable-tag", 1, 0, 0,
807 (SCM handle),
808 "Return the vtable tag of the structure @var{handle}.")
809 #define FUNC_NAME s_scm_struct_vtable_tag
810 {
811 SCM_VALIDATE_VTABLE (1, handle);
812 return scm_from_ulong (((unsigned long)SCM_STRUCT_DATA (handle)) >> 3);
813 }
814 #undef FUNC_NAME
815
816 /* {Associating names and classes with vtables}
817 *
818 * The name of a vtable should probably be stored as a slot. This is
819 * a backward compatible solution until agreement has been achieved on
820 * how to associate names with vtables.
821 */
822
823 unsigned long
scm_struct_ihashq(SCM obj,unsigned long n)824 scm_struct_ihashq (SCM obj, unsigned long n)
825 {
826 /* The length of the hash table should be a relative prime it's not
827 necessary to shift down the address. */
828 return SCM_UNPACK (obj) % n;
829 }
830
831 SCM
scm_struct_create_handle(SCM obj)832 scm_struct_create_handle (SCM obj)
833 {
834 SCM handle = scm_hash_fn_create_handle_x (scm_struct_table,
835 obj,
836 SCM_BOOL_F,
837 scm_struct_ihashq,
838 scm_sloppy_assq,
839 0);
840 if (scm_is_false (SCM_CDR (handle)))
841 SCM_SETCDR (handle, scm_cons (SCM_BOOL_F, SCM_BOOL_F));
842 return handle;
843 }
844
845 SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0,
846 (SCM vtable),
847 "Return the name of the vtable @var{vtable}.")
848 #define FUNC_NAME s_scm_struct_vtable_name
849 {
850 SCM_VALIDATE_VTABLE (1, vtable);
851 return SCM_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)));
852 }
853 #undef FUNC_NAME
854
855 SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0,
856 (SCM vtable, SCM name),
857 "Set the name of the vtable @var{vtable} to @var{name}.")
858 #define FUNC_NAME s_scm_set_struct_vtable_name_x
859 {
860 SCM_VALIDATE_VTABLE (1, vtable);
861 SCM_VALIDATE_SYMBOL (2, name);
862 SCM_SET_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)),
863 name);
864 return SCM_UNSPECIFIED;
865 }
866 #undef FUNC_NAME
867
868
869
870
871 void
scm_print_struct(SCM exp,SCM port,scm_print_state * pstate)872 scm_print_struct (SCM exp, SCM port, scm_print_state *pstate)
873 {
874 if (scm_is_true (scm_procedure_p (SCM_STRUCT_PRINTER (exp))))
875 scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate);
876 else
877 {
878 SCM vtable = SCM_STRUCT_VTABLE (exp);
879 SCM name = scm_struct_vtable_name (vtable);
880 scm_puts ("#<", port);
881 if (scm_is_true (name))
882 scm_display (name, port);
883 else
884 scm_puts ("struct", port);
885 scm_putc (' ', port);
886 scm_uintprint (SCM_UNPACK (vtable), 16, port);
887 scm_putc (':', port);
888 scm_uintprint (SCM_UNPACK (exp), 16, port);
889 scm_putc ('>', port);
890 }
891 }
892
893 void
scm_struct_prehistory()894 scm_struct_prehistory ()
895 {
896 scm_i_structs_to_free = SCM_EOL;
897 scm_c_hook_add (&scm_before_sweep_c_hook, scm_struct_gc_init, 0, 0);
898 /* With the new lazy sweep GC, the point at which the entire heap is
899 swept is just before the mark phase. */
900 scm_c_hook_add (&scm_before_mark_c_hook, scm_free_structs, 0, 0);
901 }
902
903 void
scm_init_struct()904 scm_init_struct ()
905 {
906 scm_struct_table
907 = scm_permanent_object (scm_make_weak_key_hash_table (scm_from_int (31)));
908 required_vtable_fields = scm_from_locale_string ("prsrpw");
909 scm_permanent_object (required_vtable_fields);
910
911 scm_i_vtable_vtable_no_extra_fields =
912 scm_permanent_object
913 (scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL));
914
915 scm_c_define ("vtable-index-layout", scm_from_int (scm_vtable_index_layout));
916 scm_c_define ("vtable-index-vtable", scm_from_int (scm_vtable_index_vtable));
917 scm_c_define ("vtable-index-printer",
918 scm_from_int (scm_vtable_index_printer));
919 scm_c_define ("vtable-offset-user", scm_from_int (scm_vtable_offset_user));
920 #include "libguile/struct.x"
921 }
922
923 /*
924 Local Variables:
925 c-file-style: "gnu"
926 End:
927 */
928