Include <config.h> in all C files; use `#ifdef HAVE_CONFIG_H' rather than `#if'.
[bpt/guile.git] / libguile / struct.c
CommitLineData
651f2cd2 1/* Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2007 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
dbb605f5 19#ifdef HAVE_CONFIG_H
a6f7f57d
RB
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
d15ad007
LC
36#include "libguile/eq.h"
37
95b88819
GH
38#ifdef HAVE_STRING_H
39#include <string.h>
40#endif
41
0f2d19dd
JB
42\f
43
44static SCM required_vtable_fields = SCM_BOOL_F;
98d5f601 45SCM scm_struct_table;
0f2d19dd
JB
46
47\f
a1ec6916 48SCM_DEFINE (scm_make_struct_layout, "make-struct-layout", 1, 0, 0,
1bbd0b84 49 (SCM fields),
b380b885 50 "Return a new structure layout object.\n\n"
7c31152f 51 "@var{fields} must be a string made up of pairs of characters\n"
b380b885
MD
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"
04323af4 55 "a field that points to the structure itself. Allowed protections\n"
9401323e 56 "are 'w' for mutable fields, 'r' for read-only fields, and 'o' for opaque\n"
b380b885
MD
57 "fields. The last field protection specification may be capitalized to\n"
58 "indicate that the field is a tail-array.")
1bbd0b84 59#define FUNC_NAME s_scm_make_struct_layout
0f2d19dd
JB
60{
61 SCM new_sym;
d1ca2c64 62 SCM_VALIDATE_STRING (1, fields);
2ade72d7 63
1bbd0b84 64 { /* scope */
cc95e00a 65 const char * field_desc;
1be6b49c 66 size_t len;
0f2d19dd
JB
67 int x;
68
cc95e00a 69 len = scm_i_string_length (fields);
2ade72d7
DH
70 if (len % 2 == 1)
71 SCM_MISC_ERROR ("odd length field specification: ~S",
1afff620 72 scm_list_1 (fields));
2ade72d7 73
cc95e00a 74 field_desc = scm_i_string_chars (fields);
0f2d19dd
JB
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:
2ade72d7 89 SCM_MISC_ERROR ("unrecognized field type: ~S",
1afff620 90 scm_list_1 (SCM_MAKE_CHAR (field_desc[x])));
0f2d19dd
JB
91 }
92
93 switch (field_desc[x + 1])
94 {
95 case 'w':
2ade72d7
DH
96 if (field_desc[x] == 's')
97 SCM_MISC_ERROR ("self fields not writable", SCM_EOL);
0f2d19dd
JB
98 case 'r':
99 case 'o':
100 break;
2c36c351
MD
101 case 'R':
102 case 'W':
103 case 'O':
2ade72d7
DH
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);
2c36c351 110 break;
0f2d19dd 111 default:
2ade72d7 112 SCM_MISC_ERROR ("unrecognized ref specification: ~S",
1afff620 113 scm_list_1 (SCM_MAKE_CHAR (field_desc[x + 1])));
0f2d19dd
JB
114 }
115#if 0
116 if (field_desc[x] == 'd')
117 {
2ade72d7
DH
118 if (field_desc[x + 2] != '-')
119 SCM_MISC_ERROR ("missing dash field at position ~A",
e11e83f3 120 scm_list_1 (scm_from_int (x / 2)));
0f2d19dd
JB
121 x += 2;
122 goto recheck_ref;
123 }
124#endif
125 }
cc95e00a 126 new_sym = scm_string_to_symbol (fields);
0f2d19dd 127 }
8824ac88
MV
128 scm_remember_upto_here_1 (fields);
129 return new_sym;
0f2d19dd 130}
1bbd0b84 131#undef FUNC_NAME
0f2d19dd
JB
132
133\f
134
135
1cc91f1b 136
f7620510 137static void
92c2555f 138scm_struct_init (SCM handle, SCM layout, scm_t_bits * mem, int tail_elts, SCM inits)
0f2d19dd 139{
cc95e00a
MV
140 unsigned const char *fields_desc =
141 (unsigned const char *) scm_i_symbol_chars (layout) - 2;
35de7ebe 142 unsigned char prot = 0;
cc95e00a 143 int n_fields = scm_i_symbol_length (layout) / 2;
2c36c351 144 int tailp = 0;
d8c40b9f 145
0f2d19dd
JB
146 while (n_fields)
147 {
2c36c351
MD
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';
d8c40b9f 156 *mem++ = tail_elts;
2c36c351
MD
157 n_fields += tail_elts - 1;
158 if (n_fields == 0)
159 break;
160 }
161 }
162
0f2d19dd
JB
163 switch (*fields_desc)
164 {
165#if 0
166 case 'i':
2c36c351 167 if ((prot != 'r' && prot != 'w') || inits == SCM_EOL)
0f2d19dd
JB
168 *mem = 0;
169 else
170 {
b9bd8526 171 *mem = scm_to_long (SCM_CAR (inits));
0f2d19dd
JB
172 inits = SCM_CDR (inits);
173 }
174 break;
175#endif
176
177 case 'u':
d2e53ed6 178 if ((prot != 'r' && prot != 'w') || scm_is_null (inits))
0f2d19dd
JB
179 *mem = 0;
180 else
181 {
b9bd8526 182 *mem = scm_to_ulong (SCM_CAR (inits));
0f2d19dd
JB
183 inits = SCM_CDR (inits);
184 }
185 break;
186
187 case 'p':
d2e53ed6 188 if ((prot != 'r' && prot != 'w') || scm_is_null (inits))
d8c40b9f 189 *mem = SCM_UNPACK (SCM_BOOL_F);
0f2d19dd
JB
190 else
191 {
d8c40b9f 192 *mem = SCM_UNPACK (SCM_CAR (inits));
0f2d19dd
JB
193 inits = SCM_CDR (inits);
194 }
195
196 break;
197
198#if 0
199 case 'd':
2c36c351 200 if ((prot != 'r' && prot != 'w') || inits == SCM_EOL)
0f2d19dd
JB
201 *((double *)mem) = 0.0;
202 else
203 {
a5bfe84d 204 *mem = scm_num2dbl (SCM_CAR (inits), "scm_struct_init");
0f2d19dd
JB
205 inits = SCM_CDR (inits);
206 }
207 fields_desc += 2;
208 break;
209#endif
210
211 case 's':
d8c40b9f 212 *mem = SCM_UNPACK (handle);
0f2d19dd
JB
213 break;
214 }
215
0f2d19dd
JB
216 n_fields--;
217 mem++;
218 }
219}
220
221
a1ec6916 222SCM_DEFINE (scm_struct_p, "struct?", 1, 0, 0,
1bbd0b84 223 (SCM x),
0233bfc1 224 "Return @code{#t} iff @var{x} is a structure object, else\n"
942e5b91 225 "@code{#f}.")
1bbd0b84 226#define FUNC_NAME s_scm_struct_p
0f2d19dd 227{
7888309b 228 return scm_from_bool(SCM_STRUCTP (x));
0f2d19dd 229}
1bbd0b84 230#undef FUNC_NAME
0f2d19dd 231
a1ec6916 232SCM_DEFINE (scm_struct_vtable_p, "struct-vtable?", 1, 0, 0,
1bbd0b84 233 (SCM x),
0233bfc1 234 "Return @code{#t} iff @var{x} is a vtable structure.")
1bbd0b84 235#define FUNC_NAME s_scm_struct_vtable_p
0f2d19dd
JB
236{
237 SCM layout;
92c2555f 238 scm_t_bits * mem;
8824ac88 239 int tmp;
0f2d19dd
JB
240
241 if (!SCM_STRUCTP (x))
242 return SCM_BOOL_F;
243
244 layout = SCM_STRUCT_LAYOUT (x);
245
cc95e00a
MV
246 if (scm_i_symbol_length (layout)
247 < scm_i_string_length (required_vtable_fields))
0f2d19dd
JB
248 return SCM_BOOL_F;
249
cc95e00a
MV
250 tmp = strncmp (scm_i_symbol_chars (layout),
251 scm_i_string_chars (required_vtable_fields),
252 scm_i_string_length (required_vtable_fields));
8824ac88
MV
253 scm_remember_upto_here_1 (required_vtable_fields);
254 if (tmp)
0f2d19dd
JB
255 return SCM_BOOL_F;
256
257 mem = SCM_STRUCT_DATA (x);
258
cc95e00a 259 return scm_from_bool (scm_is_symbol (SCM_PACK (mem[scm_vtable_index_layout])));
0f2d19dd 260}
1bbd0b84 261#undef FUNC_NAME
0f2d19dd 262
14d1400f
JB
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
ad196599 282 scm_struct_i_ptr --- the actual start of the block of memory; the
14d1400f
JB
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
14d1400f
JB
291 Ugh. */
292
293
92c2555f 294scm_t_bits *
4c9419ac 295scm_alloc_struct (int n_words, int n_extra, const char *what)
14d1400f 296{
92c2555f 297 int size = sizeof (scm_t_bits) * (n_words + n_extra) + 7;
4c9419ac 298 void * block = scm_gc_malloc (size, what);
14d1400f
JB
299
300 /* Adjust the pointer to hide the extra words. */
92c2555f 301 scm_t_bits * p = (scm_t_bits *) block + n_extra;
14d1400f
JB
302
303 /* Adjust it even further so it's aligned on an eight-byte boundary. */
92c2555f 304 p = (scm_t_bits *) (((scm_t_bits) p + 7) & ~7);
14d1400f 305
ad196599 306 /* Initialize a few fields as described above. */
92c2555f
MV
307 p[scm_struct_i_free] = (scm_t_bits) scm_struct_free_standard;
308 p[scm_struct_i_ptr] = (scm_t_bits) block;
c8045e8d 309 p[scm_struct_i_n_words] = n_words;
ad196599 310 p[scm_struct_i_flags] = 0;
14d1400f
JB
311
312 return p;
313}
314
4c9419ac 315void
92c2555f
MV
316scm_struct_free_0 (scm_t_bits * vtable SCM_UNUSED,
317 scm_t_bits * data SCM_UNUSED)
ad196599 318{
ad196599
MD
319}
320
4c9419ac 321void
92c2555f 322scm_struct_free_light (scm_t_bits * vtable, scm_t_bits * data)
ad196599 323{
4c9419ac
MV
324 size_t n = vtable [scm_struct_i_size] & ~SCM_STRUCTF_MASK;
325 scm_gc_free (data, n, "struct");
ad196599
MD
326}
327
4c9419ac 328void
92c2555f 329scm_struct_free_standard (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data)
ad196599 330{
c8045e8d 331 size_t n = (data[scm_struct_i_n_words] + scm_struct_n_extra_words)
92c2555f 332 * sizeof (scm_t_bits) + 7;
4c9419ac 333 scm_gc_free ((void *) data[scm_struct_i_ptr], n, "heavy struct");
ad196599
MD
334}
335
4c9419ac 336void
92c2555f 337scm_struct_free_entity (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data)
ad196599 338{
c8045e8d 339 size_t n = (data[scm_struct_i_n_words] + scm_struct_entity_n_extra_words)
92c2555f 340 * sizeof (scm_t_bits) + 7;
4c9419ac 341 scm_gc_free ((void *) data[scm_struct_i_ptr], n, "entity struct");
ad196599 342}
14d1400f 343
b4a1358c
MD
344static void *
345scm_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
08c880a3 353static void *
e81d98ec
DH
354scm_free_structs (void *dummy1 SCM_UNUSED,
355 void *dummy2 SCM_UNUSED,
356 void *dummy3 SCM_UNUSED)
08c880a3 357{
ffd72400 358 SCM newchain = scm_i_structs_to_free;
08c880a3
MD
359 do
360 {
361 /* Mark vtables in GC chain. GC mark set means delay freeing. */
362 SCM chain = newchain;
d2e53ed6 363 while (!scm_is_null (chain))
08c880a3
MD
364 {
365 SCM vtable = SCM_STRUCT_VTABLE (chain);
366 if (SCM_STRUCT_GC_CHAIN (vtable) != 0 && vtable != chain)
569aa529 367 SCM_SET_STRUCT_MARK (vtable);
08c880a3
MD
368 chain = SCM_STRUCT_GC_CHAIN (chain);
369 }
370 /* Free unmarked structs. */
371 chain = newchain;
372 newchain = SCM_EOL;
d2e53ed6 373 while (!scm_is_null (chain))
08c880a3
MD
374 {
375 SCM obj = chain;
376 chain = SCM_STRUCT_GC_CHAIN (chain);
569aa529 377 if (SCM_STRUCT_MARK_P (obj))
08c880a3 378 {
569aa529 379 SCM_CLEAR_STRUCT_MARK (obj);
08c880a3
MD
380 SCM_SET_STRUCT_GC_CHAIN (obj, newchain);
381 newchain = obj;
382 }
383 else
384 {
d15ad007 385 scm_t_bits * vtable_data = SCM_STRUCT_VTABLE_DATA (obj);
92c2555f
MV
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 398SCM_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);
651f2cd2
KR
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
9de87eea 453 SCM_CRITICAL_SECTION_START;
d8c40b9f 454 if (SCM_STRUCT_DATA (vtable)[scm_struct_i_flags] & SCM_STRUCTF_ENTITY)
a5bfe84d
MD
455 {
456 data = scm_alloc_struct (basic_size + tail_elts,
98d5f601 457 scm_struct_entity_n_extra_words,
4c9419ac 458 "entity struct");
c8045e8d
DH
459 data[scm_struct_i_procedure] = SCM_UNPACK (SCM_BOOL_F);
460 data[scm_struct_i_setter] = SCM_UNPACK (SCM_BOOL_F);
a5bfe84d
MD
461 }
462 else
463 data = scm_alloc_struct (basic_size + tail_elts,
464 scm_struct_n_extra_words,
4c9419ac 465 "struct");
228a24ef
DH
466 handle = scm_double_cell ((((scm_t_bits) SCM_STRUCT_DATA (vtable))
467 + scm_tc3_struct),
468 (scm_t_bits) data, 0, 0);
9de87eea 469 SCM_CRITICAL_SECTION_END;
651f2cd2
KR
470
471 /* In guile 1.8.1 and earlier, the SCM_CRITICAL_SECTION_END above covered
472 also the following scm_struct_init. But that meant if scm_struct_init
473 finds an invalid type for a "u" field then there's an error throw in a
474 critical section, which results in an abort(). Not sure if we need any
475 protection across scm_struct_init. The data array contains garbage at
476 this point, but until we return it's not visible to anyone except
477 `gc'. */
478 scm_struct_init (handle, layout, data, tail_elts, init);
479
0f2d19dd
JB
480 return handle;
481}
1bbd0b84 482#undef FUNC_NAME
0f2d19dd
JB
483
484
485
a1ec6916 486SCM_DEFINE (scm_make_vtable_vtable, "make-vtable-vtable", 2, 0, 1,
04323af4 487 (SCM user_fields, SCM tail_array_size, SCM init),
b380b885 488 "Return a new, self-describing vtable structure.\n\n"
04323af4
MD
489 "@var{user-fields} is a string describing user defined fields of the\n"
490 "vtable beginning at index @code{vtable-offset-user}\n"
491 "(see @code{make-struct-layout}).\n\n"
b380b885
MD
492 "@var{tail-size} specifies the size of the tail-array (if any) of\n"
493 "this vtable.\n\n"
6386e25c 494 "@var{init1}, @dots{} are the optional initializers for the fields of\n"
04323af4
MD
495 "the vtable.\n\n"
496 "Vtables have one initializable system field---the struct printer.\n"
497 "This field comes before the user fields in the initializers passed\n"
498 "to @code{make-vtable-vtable} and @code{make-struct}, and thus works as\n"
499 "a third optional argument to @code{make-vtable-vtable} and a fourth to\n"
500 "@code{make-struct} when creating vtables:\n\n"
501 "If the value is a procedure, it will be called instead of the standard\n"
502 "printer whenever a struct described by this vtable is printed.\n"
503 "The procedure will be called with arguments STRUCT and PORT.\n\n"
504 "The structure of a struct is described by a vtable, so the vtable is\n"
505 "in essence the type of the struct. The vtable is itself a struct with\n"
506 "a vtable. This could go on forever if it weren't for the\n"
29b4f9fb 507 "vtable-vtables which are self-describing vtables, and thus terminate\n"
04323af4
MD
508 "the chain.\n\n"
509 "There are several potential ways of using structs, but the standard\n"
510 "one is to use three kinds of structs, together building up a type\n"
511 "sub-system: one vtable-vtable working as the root and one or several\n"
512 "\"types\", each with a set of \"instances\". (The vtable-vtable should be\n"
29b4f9fb 513 "compared to the class <class> which is the class of itself.)\n\n"
1e6808ea 514 "@lisp\n"
04323af4
MD
515 "(define ball-root (make-vtable-vtable \"pr\" 0))\n\n"
516 "(define (make-ball-type ball-color)\n"
517 " (make-struct ball-root 0\n"
518 " (make-struct-layout \"pw\")\n"
519 " (lambda (ball port)\n"
520 " (format port \"#<a ~A ball owned by ~A>\"\n"
521 " (color ball)\n"
522 " (owner ball)))\n"
523 " ball-color))\n"
524 "(define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))\n"
525 "(define (owner ball) (struct-ref ball 0))\n\n"
526 "(define red (make-ball-type 'red))\n"
527 "(define green (make-ball-type 'green))\n\n"
528 "(define (make-ball type owner) (make-struct type 0 owner))\n\n"
529 "(define ball (make-ball green 'Nisse))\n"
530 "ball @result{} #<a green ball owned by Nisse>\n"
9401323e 531 "@end lisp")
1bbd0b84 532#define FUNC_NAME s_scm_make_vtable_vtable
0f2d19dd
JB
533{
534 SCM fields;
535 SCM layout;
a55c2b68
MV
536 size_t basic_size;
537 size_t tail_elts;
538 scm_t_bits *data;
0f2d19dd
JB
539 SCM handle;
540
d1ca2c64 541 SCM_VALIDATE_STRING (1, user_fields);
af45e3b0 542 SCM_VALIDATE_REST_ARGUMENT (init);
0f2d19dd 543
1afff620
KN
544 fields = scm_string_append (scm_list_2 (required_vtable_fields,
545 user_fields));
0f2d19dd 546 layout = scm_make_struct_layout (fields);
cc95e00a 547 basic_size = scm_i_symbol_length (layout) / 2;
a55c2b68 548 tail_elts = scm_to_size_t (tail_array_size);
9de87eea 549 SCM_CRITICAL_SECTION_START;
a5bfe84d
MD
550 data = scm_alloc_struct (basic_size + tail_elts,
551 scm_struct_n_extra_words,
4c9419ac 552 "struct");
228a24ef
DH
553 handle = scm_double_cell ((scm_t_bits) data + scm_tc3_struct,
554 (scm_t_bits) data, 0, 0);
f7620510
DH
555 data [scm_vtable_index_layout] = SCM_UNPACK (layout);
556 scm_struct_init (handle, layout, data, tail_elts, scm_cons (layout, init));
9de87eea 557 SCM_CRITICAL_SECTION_END;
0f2d19dd
JB
558 return handle;
559}
1bbd0b84 560#undef FUNC_NAME
0f2d19dd 561
d15ad007 562
651f2cd2
KR
563static SCM scm_i_vtable_vtable_no_extra_fields;
564
565SCM_DEFINE (scm_make_vtable, "make-vtable", 1, 1, 0,
566 (SCM fields, SCM printer),
567 "Create a vtable, for creating structures with the given\n"
568 "@var{fields}.\n"
569 "\n"
570 "The optional @var{printer} argument is a function to be called\n"
571 "@code{(@var{printer} struct port)} on the structures created.\n"
572 "It should look at @var{struct} and write to @var{port}.")
573#define FUNC_NAME s_scm_make_vtable
574{
575 if (SCM_UNBNDP (printer))
576 printer = SCM_BOOL_F;
577
578 return scm_make_struct (scm_i_vtable_vtable_no_extra_fields, SCM_INUM0,
579 scm_list_2 (scm_make_struct_layout (fields),
580 printer));
581}
582#undef FUNC_NAME
583
584
d15ad007
LC
585/* Return true if S1 and S2 are equal structures, i.e., if their vtable and
586 contents are the same. Field protections are honored. Thus, it is an
587 error to test the equality of structures that contain opaque fields. */
588SCM
589scm_i_struct_equalp (SCM s1, SCM s2)
590#define FUNC_NAME "scm_i_struct_equalp"
591{
592 SCM vtable1, vtable2, layout;
593 size_t struct_size, field_num;
594
595 SCM_VALIDATE_STRUCT (1, s1);
596 SCM_VALIDATE_STRUCT (2, s2);
597
598 vtable1 = SCM_STRUCT_VTABLE (s1);
599 vtable2 = SCM_STRUCT_VTABLE (s2);
600
601 if (!scm_is_eq (vtable1, vtable2))
602 return SCM_BOOL_F;
603
604 layout = SCM_STRUCT_LAYOUT (s1);
605 struct_size = scm_i_symbol_length (layout) / 2;
606
607 for (field_num = 0; field_num < struct_size; field_num++)
608 {
609 SCM s_field_num;
610 SCM field1, field2;
611
612 /* We have to use `scm_struct_ref ()' here so that fields are accessed
613 consistently, notably wrt. field types and access rights. */
614 s_field_num = scm_from_size_t (field_num);
615 field1 = scm_struct_ref (s1, s_field_num);
616 field2 = scm_struct_ref (s2, s_field_num);
617
42ddb3cb
LC
618 /* Self-referencing fields (type `s') must be skipped to avoid infinite
619 recursion. */
620 if (!(scm_is_eq (field1, s1) && (scm_is_eq (field2, s2))))
621 if (scm_is_false (scm_equal_p (field1, field2)))
622 return SCM_BOOL_F;
d15ad007
LC
623 }
624
42ddb3cb
LC
625 /* FIXME: Tail elements should be tested for equality. */
626
d15ad007
LC
627 return SCM_BOOL_T;
628}
629#undef FUNC_NAME
630
631
0f2d19dd
JB
632\f
633
634
a1ec6916 635SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0,
1bbd0b84 636 (SCM handle, SCM pos),
8f85c0c6 637 "@deffnx {Scheme Procedure} struct-set! struct n value\n"
b380b885
MD
638 "Access (or modify) the @var{n}th field of @var{struct}.\n\n"
639 "If the field is of type 'p', then it can be set to an arbitrary value.\n\n"
640 "If the field is of type 'u', then it can only be set to a non-negative\n"
641 "integer value small enough to fit in one machine word.")
1bbd0b84 642#define FUNC_NAME s_scm_struct_ref
0f2d19dd 643{
5e840c2e 644 SCM answer = SCM_UNDEFINED;
92c2555f 645 scm_t_bits * data;
0f2d19dd 646 SCM layout;
cc95e00a 647 size_t layout_len;
a55c2b68 648 size_t p;
92c2555f 649 scm_t_bits n_fields;
cc95e00a 650 const char *fields_desc;
e51fe79c 651 char field_type = 0;
0f2d19dd
JB
652
653
34d19ef6 654 SCM_VALIDATE_STRUCT (1, handle);
0f2d19dd
JB
655
656 layout = SCM_STRUCT_LAYOUT (handle);
657 data = SCM_STRUCT_DATA (handle);
a55c2b68 658 p = scm_to_size_t (pos);
0f2d19dd 659
cc95e00a
MV
660 fields_desc = scm_i_symbol_chars (layout);
661 layout_len = scm_i_symbol_length (layout);
4650d115
AW
662 if (SCM_STRUCT_VTABLE_FLAGS (handle) & SCM_STRUCTF_LIGHT)
663 /* no extra words */
664 n_fields = layout_len / 2;
665 else
666 n_fields = data[scm_struct_i_n_words];
2c36c351 667
34d19ef6 668 SCM_ASSERT_RANGE(1, pos, p < n_fields);
0f2d19dd 669
cc95e00a 670 if (p * 2 < layout_len)
2c36c351 671 {
e51fe79c 672 char ref;
2c36c351
MD
673 field_type = fields_desc[p * 2];
674 ref = fields_desc[p * 2 + 1];
675 if ((ref != 'r') && (ref != 'w'))
676 {
677 if ((ref == 'R') || (ref == 'W'))
678 field_type = 'u';
679 else
1afff620 680 SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
2c36c351
MD
681 }
682 }
cc95e00a
MV
683 else if (fields_desc[layout_len - 1] != 'O')
684 field_type = fields_desc[layout_len - 2];
2c36c351 685 else
1afff620 686 SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
2c36c351 687
0f2d19dd
JB
688 switch (field_type)
689 {
690 case 'u':
b9bd8526 691 answer = scm_from_ulong (data[p]);
0f2d19dd
JB
692 break;
693
694#if 0
695 case 'i':
b9bd8526 696 answer = scm_from_long (data[p]);
0f2d19dd
JB
697 break;
698
699 case 'd':
f8de44c1 700 answer = scm_make_real (*((double *)&(data[p])));
0f2d19dd
JB
701 break;
702#endif
703
704 case 's':
705 case 'p':
d8c40b9f 706 answer = SCM_PACK (data[p]);
0f2d19dd
JB
707 break;
708
709
710 default:
2ade72d7 711 SCM_MISC_ERROR ("unrecognized field type: ~S",
1afff620 712 scm_list_1 (SCM_MAKE_CHAR (field_type)));
0f2d19dd
JB
713 }
714
715 return answer;
716}
1bbd0b84 717#undef FUNC_NAME
0f2d19dd
JB
718
719
a1ec6916 720SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0,
1bbd0b84 721 (SCM handle, SCM pos, SCM val),
e3239868
DH
722 "Set the slot of the structure @var{handle} with index @var{pos}\n"
723 "to @var{val}. Signal an error if the slot can not be written\n"
724 "to.")
1bbd0b84 725#define FUNC_NAME s_scm_struct_set_x
0f2d19dd 726{
92c2555f 727 scm_t_bits * data;
0f2d19dd 728 SCM layout;
cc95e00a 729 size_t layout_len;
a55c2b68 730 size_t p;
0f2d19dd 731 int n_fields;
cc95e00a 732 const char *fields_desc;
e51fe79c 733 char field_type = 0;
0f2d19dd 734
34d19ef6 735 SCM_VALIDATE_STRUCT (1, handle);
0f2d19dd
JB
736
737 layout = SCM_STRUCT_LAYOUT (handle);
738 data = SCM_STRUCT_DATA (handle);
a55c2b68 739 p = scm_to_size_t (pos);
0f2d19dd 740
cc95e00a
MV
741 fields_desc = scm_i_symbol_chars (layout);
742 layout_len = scm_i_symbol_length (layout);
4650d115
AW
743 if (SCM_STRUCT_VTABLE_FLAGS (handle) & SCM_STRUCTF_LIGHT)
744 /* no extra words */
745 n_fields = layout_len / 2;
746 else
747 n_fields = data[scm_struct_i_n_words];
0f2d19dd 748
34d19ef6 749 SCM_ASSERT_RANGE (1, pos, p < n_fields);
0f2d19dd 750
cc95e00a 751 if (p * 2 < layout_len)
2c36c351 752 {
e51fe79c 753 char set_x;
2c36c351
MD
754 field_type = fields_desc[p * 2];
755 set_x = fields_desc [p * 2 + 1];
756 if (set_x != 'w')
1afff620 757 SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
2c36c351 758 }
cc95e00a
MV
759 else if (fields_desc[layout_len - 1] == 'W')
760 field_type = fields_desc[layout_len - 2];
2c36c351 761 else
1afff620 762 SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
2c36c351 763
0f2d19dd
JB
764 switch (field_type)
765 {
766 case 'u':
d8c40b9f 767 data[p] = SCM_NUM2ULONG (3, val);
0f2d19dd
JB
768 break;
769
770#if 0
771 case 'i':
e4b265d8 772 data[p] = SCM_NUM2LONG (3, val);
0f2d19dd
JB
773 break;
774
775 case 'd':
776 *((double *)&(data[p])) = scm_num2dbl (val, (char *)SCM_ARG3);
777 break;
778#endif
779
780 case 'p':
d8c40b9f 781 data[p] = SCM_UNPACK (val);
0f2d19dd
JB
782 break;
783
784 case 's':
2ade72d7 785 SCM_MISC_ERROR ("self fields immutable", SCM_EOL);
0f2d19dd
JB
786
787 default:
2ade72d7 788 SCM_MISC_ERROR ("unrecognized field type: ~S",
1afff620 789 scm_list_1 (SCM_MAKE_CHAR (field_type)));
0f2d19dd
JB
790 }
791
792 return val;
793}
1bbd0b84 794#undef FUNC_NAME
0f2d19dd
JB
795
796
a1ec6916 797SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0,
1bbd0b84 798 (SCM handle),
b380b885 799 "Return the vtable structure that describes the type of @var{struct}.")
1bbd0b84 800#define FUNC_NAME s_scm_struct_vtable
0f2d19dd 801{
34d19ef6 802 SCM_VALIDATE_STRUCT (1, handle);
0f2d19dd
JB
803 return SCM_STRUCT_VTABLE (handle);
804}
1bbd0b84 805#undef FUNC_NAME
0f2d19dd
JB
806
807
a1ec6916 808SCM_DEFINE (scm_struct_vtable_tag, "struct-vtable-tag", 1, 0, 0,
1bbd0b84 809 (SCM handle),
e3239868 810 "Return the vtable tag of the structure @var{handle}.")
1bbd0b84 811#define FUNC_NAME s_scm_struct_vtable_tag
0f2d19dd 812{
34d19ef6 813 SCM_VALIDATE_VTABLE (1, handle);
b9bd8526 814 return scm_from_ulong (((unsigned long)SCM_STRUCT_DATA (handle)) >> 3);
98d5f601 815}
1bbd0b84 816#undef FUNC_NAME
98d5f601
MD
817
818/* {Associating names and classes with vtables}
819 *
820 * The name of a vtable should probably be stored as a slot. This is
821 * a backward compatible solution until agreement has been achieved on
822 * how to associate names with vtables.
823 */
824
c014a02e
ML
825unsigned long
826scm_struct_ihashq (SCM obj, unsigned long n)
98d5f601 827{
ad196599
MD
828 /* The length of the hash table should be a relative prime it's not
829 necessary to shift down the address. */
f1267706 830 return SCM_UNPACK (obj) % n;
98d5f601
MD
831}
832
833SCM
834scm_struct_create_handle (SCM obj)
835{
836 SCM handle = scm_hash_fn_create_handle_x (scm_struct_table,
837 obj,
838 SCM_BOOL_F,
839 scm_struct_ihashq,
840 scm_sloppy_assq,
841 0);
7888309b 842 if (scm_is_false (SCM_CDR (handle)))
98d5f601
MD
843 SCM_SETCDR (handle, scm_cons (SCM_BOOL_F, SCM_BOOL_F));
844 return handle;
845}
846
a1ec6916 847SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0,
1bbd0b84 848 (SCM vtable),
e3239868 849 "Return the name of the vtable @var{vtable}.")
1bbd0b84 850#define FUNC_NAME s_scm_struct_vtable_name
98d5f601 851{
34d19ef6 852 SCM_VALIDATE_VTABLE (1, vtable);
98d5f601
MD
853 return SCM_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)));
854}
1bbd0b84 855#undef FUNC_NAME
98d5f601 856
a1ec6916 857SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0,
1bbd0b84 858 (SCM vtable, SCM name),
e3239868 859 "Set the name of the vtable @var{vtable} to @var{name}.")
1bbd0b84 860#define FUNC_NAME s_scm_set_struct_vtable_name_x
98d5f601 861{
34d19ef6
HWN
862 SCM_VALIDATE_VTABLE (1, vtable);
863 SCM_VALIDATE_SYMBOL (2, name);
98d5f601
MD
864 SCM_SET_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)),
865 name);
866 return SCM_UNSPECIFIED;
0f2d19dd 867}
1bbd0b84 868#undef FUNC_NAME
0f2d19dd
JB
869
870
871\f
872
bafcafb2 873void
1bbd0b84 874scm_print_struct (SCM exp, SCM port, scm_print_state *pstate)
bafcafb2 875{
7888309b 876 if (scm_is_true (scm_procedure_p (SCM_STRUCT_PRINTER (exp))))
4bfdf158
MD
877 scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate);
878 else
bafcafb2 879 {
a1ae1799
MD
880 SCM vtable = SCM_STRUCT_VTABLE (exp);
881 SCM name = scm_struct_vtable_name (vtable);
882 scm_puts ("#<", port);
7888309b 883 if (scm_is_true (name))
a1ae1799
MD
884 scm_display (name, port);
885 else
886 scm_puts ("struct", port);
887 scm_putc (' ', port);
0345e278 888 scm_uintprint (SCM_UNPACK (vtable), 16, port);
b7f3516f 889 scm_putc (':', port);
0345e278 890 scm_uintprint (SCM_UNPACK (exp), 16, port);
b7f3516f 891 scm_putc ('>', port);
bafcafb2 892 }
bafcafb2 893}
1cc91f1b 894
08c880a3
MD
895void
896scm_struct_prehistory ()
897{
ea5c9285 898 scm_i_structs_to_free = SCM_EOL;
b4a1358c 899 scm_c_hook_add (&scm_before_sweep_c_hook, scm_struct_gc_init, 0, 0);
569aa529
HWN
900 /* With lazy sweep GC, the point at which the entire heap is swept
901 is just before the mark phase. */
53af8255 902 scm_c_hook_add (&scm_before_mark_c_hook, scm_free_structs, 0, 0);
08c880a3
MD
903}
904
0f2d19dd
JB
905void
906scm_init_struct ()
0f2d19dd 907{
98d5f601 908 scm_struct_table
e11e83f3 909 = scm_permanent_object (scm_make_weak_key_hash_table (scm_from_int (31)));
cc95e00a 910 required_vtable_fields = scm_from_locale_string ("prsrpw");
0f2d19dd 911 scm_permanent_object (required_vtable_fields);
651f2cd2
KR
912
913 scm_i_vtable_vtable_no_extra_fields =
914 scm_permanent_object
915 (scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL));
916
e11e83f3
MV
917 scm_c_define ("vtable-index-layout", scm_from_int (scm_vtable_index_layout));
918 scm_c_define ("vtable-index-vtable", scm_from_int (scm_vtable_index_vtable));
86d31dfe 919 scm_c_define ("vtable-index-printer",
e11e83f3
MV
920 scm_from_int (scm_vtable_index_printer));
921 scm_c_define ("vtable-offset-user", scm_from_int (scm_vtable_offset_user));
a0599745 922#include "libguile/struct.x"
0f2d19dd 923}
89e00824
ML
924
925/*
926 Local Variables:
927 c-file-style: "gnu"
928 End:
929*/