Test suite for applicable smobs.
[bpt/guile.git] / libguile / smob.c
CommitLineData
16d35552 1/* Copyright (C) 1995, 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
47#include <stdio.h>
a0599745 48#include "libguile/_scm.h"
20e6290e 49
a0599745
MD
50#include "libguile/objects.h"
51#include "libguile/ports.h"
d7ec6b9f 52
0f2d19dd
JB
53#ifdef HAVE_MALLOC_H
54#include <malloc.h>
55#endif
56
a0599745 57#include "libguile/smob.h"
9dd5943c 58
0f2d19dd
JB
59\f
60
61/* scm_smobs scm_numsmob
62 * implement a dynamicly resized array of smob records.
63 * Indexes into this table are used when generating type
64 * tags for smobjects (if you know a tag you can get an index and conversely).
65 */
4e6e2119 66int scm_numsmob;
9dd5943c 67scm_smob_descriptor *scm_smobs;
0f2d19dd 68
9dd5943c
MD
69/* {Mark}
70 */
71
72/* This function is vestigial. It used to be the mark function's
73 responsibility to set the mark bit on the smob or port, but now the
74 generic marking routine in gc.c takes care of that, and a zero
75 pointer for a mark function means "don't bother". So you never
76 need scm_mark0.
77
78 However, we leave it here because it's harmless to call it, and
79 people out there have smob code that uses it, and there's no reason
80 to make their links fail. */
81
82SCM
6e8d25a6 83scm_mark0 (SCM ptr)
9dd5943c
MD
84{
85 return SCM_BOOL_F;
86}
87
88SCM
6e8d25a6 89scm_markcdr (SCM ptr)
9dd5943c
MD
90{
91 return SCM_CDR (ptr);
92}
93
94/* {Free}
95 */
96
97scm_sizet
6e8d25a6 98scm_free0 (SCM ptr)
9dd5943c
MD
99{
100 return 0;
101}
102
103scm_sizet
104scm_smob_free (SCM obj)
105{
54778cd3 106 scm_must_free ((char *) SCM_CELL_WORD_1 (obj));
9dd5943c
MD
107 return scm_smobs[SCM_SMOBNUM (obj)].size;
108}
109
110/* {Print}
111 */
112
113int
114scm_smob_print (SCM exp, SCM port, scm_print_state *pstate)
115{
116 int n = SCM_SMOBNUM (exp);
117 scm_puts ("#<", port);
2c16a78a 118 scm_puts (SCM_SMOBNAME (n) ? SCM_SMOBNAME (n) : "smob", port);
9dd5943c 119 scm_putc (' ', port);
f1267706 120 scm_intprint (SCM_UNPACK (scm_smobs[n].size ? SCM_CDR (exp) : exp), 16, port);
9dd5943c
MD
121 scm_putc ('>', port);
122 return 1;
123}
1cc91f1b 124
0717dfd8
KN
125/* {Apply}
126 */
127
cb1c46c5
KN
128#define SCM_SMOB_APPLY0(SMOB) \
129 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB)
130#define SCM_SMOB_APPLY1(SMOB,A1) \
131 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1)
132#define SCM_SMOB_APPLY2(SMOB,A1,A2) \
133 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2)
134#define SCM_SMOB_APPLY3(SMOB,A1,A2,A3) \
135 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2, A3)
136
137static SCM
138scm_smob_apply_0_000 (SCM smob)
139{
140 return SCM_SMOB_APPLY0 (smob);
141}
29a34ff6 142
cb1c46c5
KN
143static SCM
144scm_smob_apply_0_010 (SCM smob)
0717dfd8 145{
cb1c46c5 146 return SCM_SMOB_APPLY1 (smob, SCM_UNDEFINED);
0717dfd8
KN
147}
148
cb1c46c5
KN
149static SCM
150scm_smob_apply_0_020 (SCM smob)
0717dfd8 151{
cb1c46c5 152 return SCM_SMOB_APPLY2 (smob, SCM_UNDEFINED, SCM_UNDEFINED);
0717dfd8
KN
153}
154
cb1c46c5
KN
155static SCM
156scm_smob_apply_0_030 (SCM smob)
0717dfd8 157{
cb1c46c5 158 return SCM_SMOB_APPLY3 (smob, SCM_UNDEFINED, SCM_UNDEFINED, SCM_UNDEFINED);
0717dfd8
KN
159}
160
cb1c46c5
KN
161static SCM
162scm_smob_apply_0_001 (SCM smob)
0717dfd8 163{
cb1c46c5
KN
164 return SCM_SMOB_APPLY1 (smob, SCM_EOL);
165}
166
167static SCM
168scm_smob_apply_0_011 (SCM smob)
169{
170 return SCM_SMOB_APPLY2 (smob, SCM_UNDEFINED, SCM_EOL);
171}
172
173static SCM
174scm_smob_apply_0_021 (SCM smob)
175{
176 return SCM_SMOB_APPLY3 (smob, SCM_UNDEFINED, SCM_UNDEFINED, SCM_EOL);
177}
178
179static SCM
180scm_smob_apply_0_error (SCM smob)
181{
182 scm_wrong_num_args (smob);
183}
184
185static SCM
186scm_smob_apply_1_010 (SCM smob, SCM a1)
187{
188 return SCM_SMOB_APPLY1 (smob, a1);
189}
190
191static SCM
192scm_smob_apply_1_020 (SCM smob, SCM a1)
193{
194 return SCM_SMOB_APPLY2 (smob, a1, SCM_UNDEFINED);
195}
196
197static SCM
198scm_smob_apply_1_030 (SCM smob, SCM a1)
199{
200 return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_UNDEFINED);
201}
202
203static SCM
204scm_smob_apply_1_001 (SCM smob, SCM a1)
205{
206 return SCM_SMOB_APPLY1 (smob, SCM_LIST1 (a1));
207}
208
209static SCM
210scm_smob_apply_1_011 (SCM smob, SCM a1)
211{
212 return SCM_SMOB_APPLY2 (smob, a1, SCM_EOL);
213}
214
215static SCM
216scm_smob_apply_1_021 (SCM smob, SCM a1)
217{
218 return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_EOL);
219}
220
221static SCM
222scm_smob_apply_1_error (SCM smob, SCM a1)
223{
224 scm_wrong_num_args (smob);
225}
226
227static SCM
228scm_smob_apply_2_020 (SCM smob, SCM a1, SCM a2)
229{
230 return SCM_SMOB_APPLY2 (smob, a1, a2);
231}
232
233static SCM
234scm_smob_apply_2_030 (SCM smob, SCM a1, SCM a2)
235{
236 return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_UNDEFINED);
237}
238
239static SCM
240scm_smob_apply_2_001 (SCM smob, SCM a1, SCM a2)
241{
242 return SCM_SMOB_APPLY1 (smob, SCM_LIST2 (a1, a2));
0717dfd8 243 }
cb1c46c5
KN
244
245static SCM
246scm_smob_apply_2_011 (SCM smob, SCM a1, SCM a2)
247{
248 return SCM_SMOB_APPLY2 (smob, a1, SCM_LIST1 (a2));
249}
250
251static SCM
252scm_smob_apply_2_021 (SCM smob, SCM a1, SCM a2)
253{
254 return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_EOL);
255}
256
257static SCM
258scm_smob_apply_2_error (SCM smob, SCM a1, SCM a2)
259{
260 scm_wrong_num_args (smob);
261}
262
263static SCM
264scm_smob_apply_3_030 (SCM smob, SCM a1, SCM a2, SCM rst)
265{
266 if (!SCM_NULLP (SCM_CDR (rst)))
267 scm_wrong_num_args (smob);
268 return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_CAR (rst));
269}
270
271static SCM
272scm_smob_apply_3_001 (SCM smob, SCM a1, SCM a2, SCM rst)
273{
274 return SCM_SMOB_APPLY1 (smob, scm_cons2 (a1, a2, rst));
0717dfd8
KN
275}
276
cb1c46c5
KN
277static SCM
278scm_smob_apply_3_011 (SCM smob, SCM a1, SCM a2, SCM rst)
279{
280 return SCM_SMOB_APPLY2 (smob, a1, scm_cons (a2, rst));
281}
282
283static SCM
284scm_smob_apply_3_021 (SCM smob, SCM a1, SCM a2, SCM rst)
285{
286 return SCM_SMOB_APPLY3 (smob, a1, a2, rst);
287}
288
289static SCM
290scm_smob_apply_3_error (SCM smob, SCM a1, SCM a2, SCM rst)
291{
292 scm_wrong_num_args (smob);
293}
294
295\f
0f2d19dd 296long
9dd5943c 297scm_make_smob_type (char *name, scm_sizet size)
0f2d19dd
JB
298{
299 char *tmp;
300 if (255 <= scm_numsmob)
301 goto smoberr;
302 SCM_DEFER_INTS;
9dd5943c
MD
303 SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_smobs,
304 (1 + scm_numsmob)
305 * sizeof (scm_smob_descriptor)));
0f2d19dd
JB
306 if (tmp)
307 {
9dd5943c
MD
308 scm_smobs = (scm_smob_descriptor *) tmp;
309 scm_smobs[scm_numsmob].name = name;
310 scm_smobs[scm_numsmob].size = size;
311 scm_smobs[scm_numsmob].mark = 0;
312 scm_smobs[scm_numsmob].free = (size == 0 ? scm_free0 : scm_smob_free);
313 scm_smobs[scm_numsmob].print = scm_smob_print;
314 scm_smobs[scm_numsmob].equalp = 0;
0717dfd8 315 scm_smobs[scm_numsmob].apply = 0;
cb1c46c5
KN
316 scm_smobs[scm_numsmob].apply_0 = 0;
317 scm_smobs[scm_numsmob].apply_1 = 0;
318 scm_smobs[scm_numsmob].apply_2 = 0;
319 scm_smobs[scm_numsmob].apply_3 = 0;
0f2d19dd
JB
320 scm_numsmob++;
321 }
322 SCM_ALLOW_INTS;
2500356c
DH
323 if (!tmp)
324 {
325 smoberr:
326 scm_memory_error ("scm_make_smob_type");
327 }
d7ec6b9f
MD
328 /* Make a class object if Goops is present. */
329 if (scm_smob_class)
330 scm_smob_class[scm_numsmob - 1]
331 = scm_make_extended_class (SCM_SMOBNAME (scm_numsmob - 1));
0f2d19dd
JB
332 return scm_tc7_smob + (scm_numsmob - 1) * 256;
333}
334
23a62151
MD
335long
336scm_make_smob_type_mfpe (char *name, scm_sizet size,
337 SCM (*mark) (SCM),
338 scm_sizet (*free) (SCM),
339 int (*print) (SCM, SCM, scm_print_state *),
340 SCM (*equalp) (SCM, SCM))
341{
342 long answer = scm_make_smob_type (name, size);
343 scm_set_smob_mfpe (answer, mark, free, print, equalp);
344 return answer;
345}
346
9dd5943c
MD
347void
348scm_set_smob_mark (long tc, SCM (*mark) (SCM))
349{
350 scm_smobs[SCM_TC2SMOBNUM (tc)].mark = mark;
351}
352
353void
354scm_set_smob_free (long tc, scm_sizet (*free) (SCM))
355{
356 scm_smobs[SCM_TC2SMOBNUM (tc)].free = free;
357}
358
359void
360scm_set_smob_print (long tc, int (*print) (SCM, SCM, scm_print_state*))
361{
362 scm_smobs[SCM_TC2SMOBNUM (tc)].print = print;
363}
364
365void
366scm_set_smob_equalp (long tc, SCM (*equalp) (SCM, SCM))
367{
368 scm_smobs[SCM_TC2SMOBNUM (tc)].equalp = equalp;
369}
370
0717dfd8
KN
371void
372scm_set_smob_apply (long tc, SCM (*apply) (), int req, int opt, int rst)
373{
cb1c46c5
KN
374 SCM (*apply_0) (SCM);
375 SCM (*apply_1) (SCM, SCM);
376 SCM (*apply_2) (SCM, SCM, SCM);
377 SCM (*apply_3) (SCM, SCM, SCM, SCM);
378 int type = SCM_GSUBR_MAKTYPE (req, opt, rst);
379
380 if (!(req >= 0 && opt >= 0 && (rst == 0 || rst == 1)
381 && req + opt + rst <= 3))
382 {
383 puts ("Unsupported smob application type");
384 abort ();
385 }
386
387 switch (type)
388 {
389 case SCM_GSUBR_MAKTYPE (0, 0, 0):
390 apply_0 = scm_smob_apply_0_000; break;
391 case SCM_GSUBR_MAKTYPE (0, 1, 0):
392 apply_0 = scm_smob_apply_0_010; break;
393 case SCM_GSUBR_MAKTYPE (0, 2, 0):
394 apply_0 = scm_smob_apply_0_020; break;
395 case SCM_GSUBR_MAKTYPE (0, 3, 0):
396 apply_0 = scm_smob_apply_0_030; break;
397 case SCM_GSUBR_MAKTYPE (0, 0, 1):
398 apply_0 = scm_smob_apply_0_001; break;
399 case SCM_GSUBR_MAKTYPE (0, 1, 1):
400 apply_0 = scm_smob_apply_0_011; break;
401 case SCM_GSUBR_MAKTYPE (0, 2, 1):
402 apply_0 = scm_smob_apply_0_021; break;
403 default:
404 apply_0 = scm_smob_apply_0_error; break;
405 }
406
407 switch (type)
408 {
409 case SCM_GSUBR_MAKTYPE (1, 0, 0):
410 case SCM_GSUBR_MAKTYPE (0, 1, 0):
411 apply_1 = scm_smob_apply_1_010; break;
412 case SCM_GSUBR_MAKTYPE (1, 1, 0):
413 case SCM_GSUBR_MAKTYPE (0, 2, 0):
414 apply_1 = scm_smob_apply_1_020; break;
415 case SCM_GSUBR_MAKTYPE (1, 2, 0):
416 case SCM_GSUBR_MAKTYPE (0, 3, 0):
417 apply_1 = scm_smob_apply_1_030; break;
418 case SCM_GSUBR_MAKTYPE (0, 0, 1):
419 apply_1 = scm_smob_apply_1_001; break;
420 case SCM_GSUBR_MAKTYPE (1, 0, 1):
421 case SCM_GSUBR_MAKTYPE (0, 1, 1):
422 apply_1 = scm_smob_apply_1_011; break;
423 case SCM_GSUBR_MAKTYPE (1, 1, 1):
424 case SCM_GSUBR_MAKTYPE (0, 2, 1):
425 apply_1 = scm_smob_apply_1_021; break;
426 default:
427 apply_1 = scm_smob_apply_1_error; break;
428 }
429
430 switch (type)
431 {
432 case SCM_GSUBR_MAKTYPE (2, 0, 0):
433 case SCM_GSUBR_MAKTYPE (1, 1, 0):
434 case SCM_GSUBR_MAKTYPE (0, 2, 0):
435 apply_2 = scm_smob_apply_2_020; break;
436 case SCM_GSUBR_MAKTYPE (2, 1, 0):
437 case SCM_GSUBR_MAKTYPE (1, 2, 0):
438 case SCM_GSUBR_MAKTYPE (0, 3, 0):
439 apply_2 = scm_smob_apply_2_030; break;
440 case SCM_GSUBR_MAKTYPE (0, 0, 1):
441 apply_2 = scm_smob_apply_2_001; break;
442 case SCM_GSUBR_MAKTYPE (1, 0, 1):
443 case SCM_GSUBR_MAKTYPE (0, 1, 1):
444 apply_2 = scm_smob_apply_2_011; break;
445 case SCM_GSUBR_MAKTYPE (2, 0, 1):
446 case SCM_GSUBR_MAKTYPE (1, 1, 1):
447 case SCM_GSUBR_MAKTYPE (0, 2, 1):
448 apply_2 = scm_smob_apply_2_021; break;
449 default:
450 apply_2 = scm_smob_apply_2_error; break;
451 }
452
453 switch (type)
454 {
455 case SCM_GSUBR_MAKTYPE (3, 0, 0):
456 case SCM_GSUBR_MAKTYPE (2, 1, 0):
457 case SCM_GSUBR_MAKTYPE (1, 2, 0):
458 case SCM_GSUBR_MAKTYPE (0, 3, 0):
459 apply_3 = scm_smob_apply_3_030; break;
460 case SCM_GSUBR_MAKTYPE (0, 0, 1):
461 apply_3 = scm_smob_apply_3_001; break;
462 case SCM_GSUBR_MAKTYPE (1, 0, 1):
463 case SCM_GSUBR_MAKTYPE (0, 1, 1):
464 apply_3 = scm_smob_apply_3_011; break;
465 case SCM_GSUBR_MAKTYPE (2, 0, 1):
466 case SCM_GSUBR_MAKTYPE (1, 1, 1):
467 case SCM_GSUBR_MAKTYPE (0, 2, 1):
468 apply_3 = scm_smob_apply_3_021; break;
469 default:
470 apply_3 = scm_smob_apply_3_error; break;
471 }
472
473 scm_smobs[SCM_TC2SMOBNUM (tc)].gsubr_type = type; /* Used in procprop.c */
0717dfd8 474 scm_smobs[SCM_TC2SMOBNUM (tc)].apply = apply;
cb1c46c5
KN
475 scm_smobs[SCM_TC2SMOBNUM (tc)].apply_0 = apply_0;
476 scm_smobs[SCM_TC2SMOBNUM (tc)].apply_1 = apply_1;
477 scm_smobs[SCM_TC2SMOBNUM (tc)].apply_2 = apply_2;
478 scm_smobs[SCM_TC2SMOBNUM (tc)].apply_3 = apply_3;
0717dfd8
KN
479}
480
23a62151
MD
481void
482scm_set_smob_mfpe (long tc,
483 SCM (*mark) (SCM),
484 scm_sizet (*free) (SCM),
485 int (*print) (SCM, SCM, scm_print_state *),
486 SCM (*equalp) (SCM, SCM))
487{
488 if (mark) scm_set_smob_mark (tc, mark);
489 if (free) scm_set_smob_free (tc, free);
490 if (print) scm_set_smob_print (tc, print);
491 if (equalp) scm_set_smob_equalp (tc, equalp);
492}
493
f5f2dcff 494
9dd5943c
MD
495SCM
496scm_make_smob (long tc)
497{
498 int n = SCM_TC2SMOBNUM (tc);
499 scm_sizet size = scm_smobs[n].size;
500 SCM z;
501 SCM_NEWCELL (z);
502 if (size != 0)
503 {
504#if 0
505 SCM_ASSERT (scm_smobs[n].mark == 0,
506 0,
507 "forbidden operation for smobs with GC data, use SCM_NEWSMOB",
508 SCM_SMOBNAME (n));
509#endif
510 SCM_SET_SMOB_DATA (z, scm_must_malloc (size, SCM_SMOBNAME (n)));
511 }
54778cd3 512 SCM_SET_CELL_TYPE (z, tc);
9dd5943c
MD
513 return z;
514}
515
ceef3208 516\f
0f2d19dd
JB
517/* {Initialization for i/o types, float, bignum, the type of free cells}
518 */
519
ceef3208
JB
520static int
521freeprint (SCM exp,
522 SCM port,
523 scm_print_state *pstate)
524{
525 char buf[100];
526
54778cd3 527 sprintf (buf, "#<freed cell %p; GC missed a reference>", (void *) SCM_UNPACK (exp));
ceef3208
JB
528 scm_puts (buf, port);
529
530 return 1;
531}
532
533
0f2d19dd
JB
534void
535scm_smob_prehistory ()
0f2d19dd
JB
536{
537 scm_numsmob = 0;
9dd5943c
MD
538 scm_smobs = ((scm_smob_descriptor *)
539 malloc (7 * sizeof (scm_smob_descriptor)));
540
541 /* WARNING: These scm_make_smob_type calls must be done in this order */
23a62151
MD
542 scm_make_smob_type_mfpe ("free", 0,
543 NULL, NULL, freeprint, NULL);
544
16d35552 545 scm_make_smob_type_mfpe ("big", 0, /* freed in gc */
23a62151
MD
546 NULL, NULL, scm_bigprint, scm_bigequal);
547
16d35552
MD
548 scm_make_smob_type_mfpe ("real", 0, /* freed in gc */
549 NULL, NULL, scm_print_real, scm_real_equalp);
550
551 scm_make_smob_type_mfpe ("complex", 0, /* freed in gc */
552 NULL, NULL, scm_print_complex, scm_complex_equalp);
0f2d19dd 553}
89e00824
ML
554
555/*
556 Local Variables:
557 c-file-style: "gnu"
558 End:
559*/