1 /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc.
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)
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.
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
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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.
40 * If you do not wish that, delete this exception notice. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
50 #include "libguile/_scm.h"
52 #include "libguile/objects.h"
53 #include "libguile/ports.h"
59 #include "libguile/smob.h"
63 /* scm_smobs scm_numsmob
64 * implement a fixed sized array of smob records.
65 * Indexes into this table are used when generating type
66 * tags for smobjects (if you know a tag you can get an index and conversely).
69 #define MAX_SMOB_COUNT 256
70 scm_bits_t scm_numsmob
;
71 scm_smob_descriptor scm_smobs
[MAX_SMOB_COUNT
];
76 /* This function is vestigial. It used to be the mark function's
77 responsibility to set the mark bit on the smob or port, but now the
78 generic marking routine in gc.c takes care of that, and a zero
79 pointer for a mark function means "don't bother". So you never
82 However, we leave it here because it's harmless to call it, and
83 people out there have smob code that uses it, and there's no reason
84 to make their links fail. */
93 /* Dirk::FIXME: The name markcdr is misleading, since the term cdr should only
94 be used for real pairs. */
97 return SCM_CELL_OBJECT_1 (ptr
);
110 scm_smob_free (SCM obj
)
112 scm_must_free ((char *) SCM_CELL_WORD_1 (obj
));
113 return scm_smobs
[SCM_SMOBNUM (obj
)].size
;
120 scm_smob_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
122 size_t n
= SCM_SMOBNUM (exp
);
123 scm_puts ("#<", port
);
124 scm_puts (SCM_SMOBNAME (n
) ? SCM_SMOBNAME (n
) : "smob", port
);
125 scm_putc (' ', port
);
126 if (scm_smobs
[n
].size
)
127 scm_intprint (SCM_CELL_WORD_1 (exp
), 16, port
);
129 scm_intprint (SCM_UNPACK (exp
), 16, port
);
130 scm_putc ('>', port
);
137 #define SCM_SMOB_APPLY0(SMOB) \
138 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB)
139 #define SCM_SMOB_APPLY1(SMOB,A1) \
140 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1)
141 #define SCM_SMOB_APPLY2(SMOB,A1,A2) \
142 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2)
143 #define SCM_SMOB_APPLY3(SMOB,A1,A2,A3) \
144 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2, A3)
147 scm_smob_apply_0_010 (SCM smob
)
149 return SCM_SMOB_APPLY1 (smob
, SCM_UNDEFINED
);
153 scm_smob_apply_0_020 (SCM smob
)
155 return SCM_SMOB_APPLY2 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
);
159 scm_smob_apply_0_030 (SCM smob
)
161 return SCM_SMOB_APPLY3 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
, SCM_UNDEFINED
);
165 scm_smob_apply_0_001 (SCM smob
)
167 return SCM_SMOB_APPLY1 (smob
, SCM_EOL
);
171 scm_smob_apply_0_011 (SCM smob
)
173 return SCM_SMOB_APPLY2 (smob
, SCM_UNDEFINED
, SCM_EOL
);
177 scm_smob_apply_0_021 (SCM smob
)
179 return SCM_SMOB_APPLY3 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
, SCM_EOL
);
183 scm_smob_apply_0_error (SCM smob
)
185 scm_wrong_num_args (smob
);
189 scm_smob_apply_1_020 (SCM smob
, SCM a1
)
191 return SCM_SMOB_APPLY2 (smob
, a1
, SCM_UNDEFINED
);
195 scm_smob_apply_1_030 (SCM smob
, SCM a1
)
197 return SCM_SMOB_APPLY3 (smob
, a1
, SCM_UNDEFINED
, SCM_UNDEFINED
);
201 scm_smob_apply_1_001 (SCM smob
, SCM a1
)
203 return SCM_SMOB_APPLY1 (smob
, SCM_LIST1 (a1
));
207 scm_smob_apply_1_011 (SCM smob
, SCM a1
)
209 return SCM_SMOB_APPLY2 (smob
, a1
, SCM_EOL
);
213 scm_smob_apply_1_021 (SCM smob
, SCM a1
)
215 return SCM_SMOB_APPLY3 (smob
, a1
, SCM_UNDEFINED
, SCM_EOL
);
219 scm_smob_apply_1_error (SCM smob
, SCM a1
)
221 scm_wrong_num_args (smob
);
225 scm_smob_apply_2_030 (SCM smob
, SCM a1
, SCM a2
)
227 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_UNDEFINED
);
231 scm_smob_apply_2_001 (SCM smob
, SCM a1
, SCM a2
)
233 return SCM_SMOB_APPLY1 (smob
, SCM_LIST2 (a1
, a2
));
237 scm_smob_apply_2_011 (SCM smob
, SCM a1
, SCM a2
)
239 return SCM_SMOB_APPLY2 (smob
, a1
, SCM_LIST1 (a2
));
243 scm_smob_apply_2_021 (SCM smob
, SCM a1
, SCM a2
)
245 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_EOL
);
249 scm_smob_apply_2_error (SCM smob
, SCM a1
, SCM a2
)
251 scm_wrong_num_args (smob
);
255 scm_smob_apply_3_030 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
257 if (!SCM_NULLP (SCM_CDR (rst
)))
258 scm_wrong_num_args (smob
);
259 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_CAR (rst
));
263 scm_smob_apply_3_001 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
265 return SCM_SMOB_APPLY1 (smob
, scm_cons2 (a1
, a2
, rst
));
269 scm_smob_apply_3_011 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
271 return SCM_SMOB_APPLY2 (smob
, a1
, scm_cons (a2
, rst
));
275 scm_smob_apply_3_021 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
277 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, rst
);
281 scm_smob_apply_3_error (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
283 scm_wrong_num_args (smob
);
289 scm_make_smob_type (char *name
, size_t size
)
290 #define FUNC_NAME "scm_make_smob_type"
294 SCM_ENTER_A_SECTION
; /* scm_numsmob */
295 new_smob
= scm_numsmob
;
296 if (scm_numsmob
!= MAX_SMOB_COUNT
)
300 if (new_smob
== MAX_SMOB_COUNT
)
301 scm_misc_error (FUNC_NAME
, "maximum number of smobs exceeded", SCM_EOL
);
303 scm_smobs
[new_smob
].name
= name
;
306 scm_smobs
[new_smob
].size
= size
;
307 scm_smobs
[new_smob
].free
= scm_smob_free
;
310 /* Make a class object if Goops is present. */
312 scm_smob_class
[new_smob
] = scm_make_extended_class (name
);
314 return scm_tc7_smob
+ new_smob
* 256;
320 scm_set_smob_mark (scm_bits_t tc
, SCM (*mark
) (SCM
))
322 scm_smobs
[SCM_TC2SMOBNUM (tc
)].mark
= mark
;
326 scm_set_smob_free (scm_bits_t tc
, size_t (*free
) (SCM
))
328 scm_smobs
[SCM_TC2SMOBNUM (tc
)].free
= free
;
332 scm_set_smob_print (scm_bits_t tc
, int (*print
) (SCM
, SCM
, scm_print_state
*))
334 scm_smobs
[SCM_TC2SMOBNUM (tc
)].print
= print
;
338 scm_set_smob_equalp (scm_bits_t tc
, SCM (*equalp
) (SCM
, SCM
))
340 scm_smobs
[SCM_TC2SMOBNUM (tc
)].equalp
= equalp
;
344 scm_set_smob_apply (scm_bits_t tc
, SCM (*apply
) (),
345 unsigned int req
, unsigned int opt
, unsigned int rst
)
347 SCM (*apply_0
) (SCM
);
348 SCM (*apply_1
) (SCM
, SCM
);
349 SCM (*apply_2
) (SCM
, SCM
, SCM
);
350 SCM (*apply_3
) (SCM
, SCM
, SCM
, SCM
);
351 int type
= SCM_GSUBR_MAKTYPE (req
, opt
, rst
);
353 if (rst
> 1 || req
+ opt
+ rst
> 3)
355 puts ("Unsupported smob application type");
361 case SCM_GSUBR_MAKTYPE (0, 0, 0):
362 apply_0
= apply
; break;
363 case SCM_GSUBR_MAKTYPE (0, 1, 0):
364 apply_0
= scm_smob_apply_0_010
; break;
365 case SCM_GSUBR_MAKTYPE (0, 2, 0):
366 apply_0
= scm_smob_apply_0_020
; break;
367 case SCM_GSUBR_MAKTYPE (0, 3, 0):
368 apply_0
= scm_smob_apply_0_030
; break;
369 case SCM_GSUBR_MAKTYPE (0, 0, 1):
370 apply_0
= scm_smob_apply_0_001
; break;
371 case SCM_GSUBR_MAKTYPE (0, 1, 1):
372 apply_0
= scm_smob_apply_0_011
; break;
373 case SCM_GSUBR_MAKTYPE (0, 2, 1):
374 apply_0
= scm_smob_apply_0_021
; break;
376 apply_0
= scm_smob_apply_0_error
; break;
381 case SCM_GSUBR_MAKTYPE (1, 0, 0):
382 case SCM_GSUBR_MAKTYPE (0, 1, 0):
383 apply_1
= apply
; break;
384 case SCM_GSUBR_MAKTYPE (1, 1, 0):
385 case SCM_GSUBR_MAKTYPE (0, 2, 0):
386 apply_1
= scm_smob_apply_1_020
; break;
387 case SCM_GSUBR_MAKTYPE (1, 2, 0):
388 case SCM_GSUBR_MAKTYPE (0, 3, 0):
389 apply_1
= scm_smob_apply_1_030
; break;
390 case SCM_GSUBR_MAKTYPE (0, 0, 1):
391 apply_1
= scm_smob_apply_1_001
; break;
392 case SCM_GSUBR_MAKTYPE (1, 0, 1):
393 case SCM_GSUBR_MAKTYPE (0, 1, 1):
394 apply_1
= scm_smob_apply_1_011
; break;
395 case SCM_GSUBR_MAKTYPE (1, 1, 1):
396 case SCM_GSUBR_MAKTYPE (0, 2, 1):
397 apply_1
= scm_smob_apply_1_021
; break;
399 apply_1
= scm_smob_apply_1_error
; break;
404 case SCM_GSUBR_MAKTYPE (2, 0, 0):
405 case SCM_GSUBR_MAKTYPE (1, 1, 0):
406 case SCM_GSUBR_MAKTYPE (0, 2, 0):
407 apply_2
= apply
; break;
408 case SCM_GSUBR_MAKTYPE (2, 1, 0):
409 case SCM_GSUBR_MAKTYPE (1, 2, 0):
410 case SCM_GSUBR_MAKTYPE (0, 3, 0):
411 apply_2
= scm_smob_apply_2_030
; break;
412 case SCM_GSUBR_MAKTYPE (0, 0, 1):
413 apply_2
= scm_smob_apply_2_001
; break;
414 case SCM_GSUBR_MAKTYPE (1, 0, 1):
415 case SCM_GSUBR_MAKTYPE (0, 1, 1):
416 apply_2
= scm_smob_apply_2_011
; break;
417 case SCM_GSUBR_MAKTYPE (2, 0, 1):
418 case SCM_GSUBR_MAKTYPE (1, 1, 1):
419 case SCM_GSUBR_MAKTYPE (0, 2, 1):
420 apply_2
= scm_smob_apply_2_021
; break;
422 apply_2
= scm_smob_apply_2_error
; break;
427 case SCM_GSUBR_MAKTYPE (3, 0, 0):
428 case SCM_GSUBR_MAKTYPE (2, 1, 0):
429 case SCM_GSUBR_MAKTYPE (1, 2, 0):
430 case SCM_GSUBR_MAKTYPE (0, 3, 0):
431 apply_3
= scm_smob_apply_3_030
; break;
432 case SCM_GSUBR_MAKTYPE (0, 0, 1):
433 apply_3
= scm_smob_apply_3_001
; break;
434 case SCM_GSUBR_MAKTYPE (1, 0, 1):
435 case SCM_GSUBR_MAKTYPE (0, 1, 1):
436 apply_3
= scm_smob_apply_3_011
; break;
437 case SCM_GSUBR_MAKTYPE (2, 0, 1):
438 case SCM_GSUBR_MAKTYPE (1, 1, 1):
439 case SCM_GSUBR_MAKTYPE (0, 2, 1):
440 apply_3
= scm_smob_apply_3_021
; break;
442 apply_3
= scm_smob_apply_3_error
; break;
445 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply
= apply
;
446 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_0
= apply_0
;
447 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_1
= apply_1
;
448 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_2
= apply_2
;
449 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_3
= apply_3
;
450 scm_smobs
[SCM_TC2SMOBNUM (tc
)].gsubr_type
= type
;
454 scm_make_smob (scm_bits_t tc
)
456 size_t n
= SCM_TC2SMOBNUM (tc
);
457 size_t size
= scm_smobs
[n
].size
;
463 if (scm_smobs
[n
].mark
!= 0)
467 "forbidden operation for smobs with GC data, use SCM_NEWSMOB\n");
471 SCM_SET_SMOB_DATA (z
, scm_must_malloc (size
, SCM_SMOBNAME (n
)));
473 SCM_SET_CELL_TYPE (z
, tc
);
478 /* {Deprecated stuff}
481 #if (SCM_DEBUG_DEPRECATED == 0)
484 scm_make_smob_type_mfpe (char *name
, size_t size
,
486 size_t (*free
) (SCM
),
487 int (*print
) (SCM
, SCM
, scm_print_state
*),
488 SCM (*equalp
) (SCM
, SCM
))
490 scm_bits_t answer
= scm_make_smob_type (name
, size
);
491 scm_set_smob_mfpe (answer
, mark
, free
, print
, equalp
);
496 scm_set_smob_mfpe (long tc
,
498 size_t (*free
) (SCM
),
499 int (*print
) (SCM
, SCM
, scm_print_state
*),
500 SCM (*equalp
) (SCM
, SCM
))
502 if (mark
) scm_set_smob_mark (tc
, mark
);
503 if (free
) scm_set_smob_free (tc
, free
);
504 if (print
) scm_set_smob_print (tc
, print
);
505 if (equalp
) scm_set_smob_equalp (tc
, equalp
);
508 #endif /* SCM_DEBUG_DEPRECATED == 0 */
511 /* {Initialization for i/o types, float, bignum, the type of free cells}
515 free_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
519 sprintf (buf
, "#<freed cell %p; GC missed a reference>",
520 (void *) SCM_UNPACK (exp
));
521 scm_puts (buf
, port
);
527 scm_smob_prehistory ()
533 for (i
= 0; i
< MAX_SMOB_COUNT
; ++i
)
535 scm_smobs
[i
].name
= 0;
536 scm_smobs
[i
].size
= 0;
537 scm_smobs
[i
].mark
= 0;
538 scm_smobs
[i
].free
= 0;
539 scm_smobs
[i
].print
= scm_smob_print
;
540 scm_smobs
[i
].equalp
= 0;
541 scm_smobs
[i
].apply
= 0;
542 scm_smobs
[i
].apply_0
= 0;
543 scm_smobs
[i
].apply_1
= 0;
544 scm_smobs
[i
].apply_2
= 0;
545 scm_smobs
[i
].apply_3
= 0;
546 scm_smobs
[i
].gsubr_type
= 0;
549 /* WARNING: These scm_make_smob_type calls must be done in this order */
550 tc
= scm_make_smob_type ("free", 0);
551 scm_set_smob_print (tc
, free_print
);
553 tc
= scm_make_smob_type ("big", 0); /* freed in gc */
554 scm_set_smob_print (tc
, scm_bigprint
);
555 scm_set_smob_equalp (tc
, scm_bigequal
);
557 tc
= scm_make_smob_type ("real", 0); /* freed in gc */
558 scm_set_smob_print (tc
, scm_print_real
);
559 scm_set_smob_equalp (tc
, scm_real_equalp
);
561 tc
= scm_make_smob_type ("complex", 0); /* freed in gc */
562 scm_set_smob_print (tc
, scm_print_complex
);
563 scm_set_smob_equalp (tc
, scm_complex_equalp
);