1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003 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. */
51 #include "libguile/_scm.h"
53 #include "libguile/objects.h"
54 #include "libguile/ports.h"
60 #include "libguile/smob.h"
64 /* scm_smobs scm_numsmob
65 * implement a fixed sized array of smob records.
66 * Indexes into this table are used when generating type
67 * tags for smobjects (if you know a tag you can get an index and conversely).
70 #define MAX_SMOB_COUNT 256
72 scm_smob_descriptor scm_smobs
[MAX_SMOB_COUNT
];
77 /* This function is vestigial. It used to be the mark function's
78 responsibility to set the mark bit on the smob or port, but now the
79 generic marking routine in gc.c takes care of that, and a zero
80 pointer for a mark function means "don't bother". So you never
83 However, we leave it here because it's harmless to call it, and
84 people out there have smob code that uses it, and there's no reason
85 to make their links fail. */
88 scm_mark0 (SCM ptr SCM_UNUSED
)
94 /* Dirk::FIXME: The name markcdr is misleading, since the term cdr should only
95 be used for real pairs. */
98 return SCM_CELL_OBJECT_1 (ptr
);
105 scm_free0 (SCM ptr SCM_UNUSED
)
111 scm_smob_free (SCM obj
)
113 long n
= SCM_SMOBNUM (obj
);
114 if (scm_smobs
[n
].size
> 0)
115 scm_gc_free ((void *) SCM_CELL_WORD_1 (obj
),
116 scm_smobs
[n
].size
, SCM_SMOBNAME (n
));
124 scm_smob_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
126 long n
= SCM_SMOBNUM (exp
);
127 scm_puts ("#<", port
);
128 scm_puts (SCM_SMOBNAME (n
) ? SCM_SMOBNAME (n
) : "smob", port
);
129 scm_putc (' ', port
);
130 if (scm_smobs
[n
].size
)
131 scm_intprint (SCM_CELL_WORD_1 (exp
), 16, port
);
133 scm_intprint (SCM_UNPACK (exp
), 16, port
);
134 scm_putc ('>', port
);
141 #define SCM_SMOB_APPLY0(SMOB) \
142 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB)
143 #define SCM_SMOB_APPLY1(SMOB, A1) \
144 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1)
145 #define SCM_SMOB_APPLY2(SMOB, A1, A2) \
146 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2)
147 #define SCM_SMOB_APPLY3(SMOB, A1, A2, A3) \
148 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2, A3)
151 scm_smob_apply_0_010 (SCM smob
)
153 return SCM_SMOB_APPLY1 (smob
, SCM_UNDEFINED
);
157 scm_smob_apply_0_020 (SCM smob
)
159 return SCM_SMOB_APPLY2 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
);
163 scm_smob_apply_0_030 (SCM smob
)
165 return SCM_SMOB_APPLY3 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
, SCM_UNDEFINED
);
169 scm_smob_apply_0_001 (SCM smob
)
171 return SCM_SMOB_APPLY1 (smob
, SCM_EOL
);
175 scm_smob_apply_0_011 (SCM smob
)
177 return SCM_SMOB_APPLY2 (smob
, SCM_UNDEFINED
, SCM_EOL
);
181 scm_smob_apply_0_021 (SCM smob
)
183 return SCM_SMOB_APPLY3 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
, SCM_EOL
);
187 scm_smob_apply_0_error (SCM smob
)
189 scm_wrong_num_args (smob
);
193 scm_smob_apply_1_020 (SCM smob
, SCM a1
)
195 return SCM_SMOB_APPLY2 (smob
, a1
, SCM_UNDEFINED
);
199 scm_smob_apply_1_030 (SCM smob
, SCM a1
)
201 return SCM_SMOB_APPLY3 (smob
, a1
, SCM_UNDEFINED
, SCM_UNDEFINED
);
205 scm_smob_apply_1_001 (SCM smob
, SCM a1
)
207 return SCM_SMOB_APPLY1 (smob
, scm_list_1 (a1
));
211 scm_smob_apply_1_011 (SCM smob
, SCM a1
)
213 return SCM_SMOB_APPLY2 (smob
, a1
, SCM_EOL
);
217 scm_smob_apply_1_021 (SCM smob
, SCM a1
)
219 return SCM_SMOB_APPLY3 (smob
, a1
, SCM_UNDEFINED
, SCM_EOL
);
223 scm_smob_apply_1_error (SCM smob
, SCM a1 SCM_UNUSED
)
225 scm_wrong_num_args (smob
);
229 scm_smob_apply_2_030 (SCM smob
, SCM a1
, SCM a2
)
231 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_UNDEFINED
);
235 scm_smob_apply_2_001 (SCM smob
, SCM a1
, SCM a2
)
237 return SCM_SMOB_APPLY1 (smob
, scm_list_2 (a1
, a2
));
241 scm_smob_apply_2_011 (SCM smob
, SCM a1
, SCM a2
)
243 return SCM_SMOB_APPLY2 (smob
, a1
, scm_list_1 (a2
));
247 scm_smob_apply_2_021 (SCM smob
, SCM a1
, SCM a2
)
249 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_EOL
);
253 scm_smob_apply_2_error (SCM smob
, SCM a1 SCM_UNUSED
, SCM a2 SCM_UNUSED
)
255 scm_wrong_num_args (smob
);
259 scm_smob_apply_3_030 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
261 if (!SCM_NULLP (SCM_CDR (rst
)))
262 scm_wrong_num_args (smob
);
263 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_CAR (rst
));
267 scm_smob_apply_3_001 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
269 return SCM_SMOB_APPLY1 (smob
, scm_cons2 (a1
, a2
, rst
));
273 scm_smob_apply_3_011 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
275 return SCM_SMOB_APPLY2 (smob
, a1
, scm_cons (a2
, rst
));
279 scm_smob_apply_3_021 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
281 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, rst
);
285 scm_smob_apply_3_error (SCM smob
,
290 scm_wrong_num_args (smob
);
296 scm_make_smob_type (char *name
, size_t size
)
297 #define FUNC_NAME "scm_make_smob_type"
301 SCM_ENTER_A_SECTION
; /* scm_numsmob */
302 new_smob
= scm_numsmob
;
303 if (scm_numsmob
!= MAX_SMOB_COUNT
)
307 if (new_smob
== MAX_SMOB_COUNT
)
308 scm_misc_error (FUNC_NAME
, "maximum number of smobs exceeded", SCM_EOL
);
310 scm_smobs
[new_smob
].name
= name
;
313 scm_smobs
[new_smob
].size
= size
;
314 scm_smobs
[new_smob
].free
= scm_smob_free
;
317 /* Make a class object if Goops is present. */
319 scm_smob_class
[new_smob
] = scm_make_extended_class (name
, 0);
321 return scm_tc7_smob
+ new_smob
* 256;
327 scm_set_smob_mark (scm_t_bits tc
, SCM (*mark
) (SCM
))
329 scm_smobs
[SCM_TC2SMOBNUM (tc
)].mark
= mark
;
333 scm_set_smob_free (scm_t_bits tc
, size_t (*free
) (SCM
))
335 scm_smobs
[SCM_TC2SMOBNUM (tc
)].free
= free
;
339 scm_set_smob_print (scm_t_bits tc
, int (*print
) (SCM
, SCM
, scm_print_state
*))
341 scm_smobs
[SCM_TC2SMOBNUM (tc
)].print
= print
;
345 scm_set_smob_equalp (scm_t_bits tc
, SCM (*equalp
) (SCM
, SCM
))
347 scm_smobs
[SCM_TC2SMOBNUM (tc
)].equalp
= equalp
;
351 scm_set_smob_apply (scm_t_bits tc
, SCM (*apply
) (),
352 unsigned int req
, unsigned int opt
, unsigned int rst
)
354 SCM (*apply_0
) (SCM
);
355 SCM (*apply_1
) (SCM
, SCM
);
356 SCM (*apply_2
) (SCM
, SCM
, SCM
);
357 SCM (*apply_3
) (SCM
, SCM
, SCM
, SCM
);
358 int type
= SCM_GSUBR_MAKTYPE (req
, opt
, rst
);
360 if (rst
> 1 || req
+ opt
+ rst
> 3)
362 puts ("Unsupported smob application type");
368 case SCM_GSUBR_MAKTYPE (0, 0, 0):
369 apply_0
= apply
; break;
370 case SCM_GSUBR_MAKTYPE (0, 1, 0):
371 apply_0
= scm_smob_apply_0_010
; break;
372 case SCM_GSUBR_MAKTYPE (0, 2, 0):
373 apply_0
= scm_smob_apply_0_020
; break;
374 case SCM_GSUBR_MAKTYPE (0, 3, 0):
375 apply_0
= scm_smob_apply_0_030
; break;
376 case SCM_GSUBR_MAKTYPE (0, 0, 1):
377 apply_0
= scm_smob_apply_0_001
; break;
378 case SCM_GSUBR_MAKTYPE (0, 1, 1):
379 apply_0
= scm_smob_apply_0_011
; break;
380 case SCM_GSUBR_MAKTYPE (0, 2, 1):
381 apply_0
= scm_smob_apply_0_021
; break;
383 apply_0
= scm_smob_apply_0_error
; break;
388 case SCM_GSUBR_MAKTYPE (1, 0, 0):
389 case SCM_GSUBR_MAKTYPE (0, 1, 0):
390 apply_1
= apply
; break;
391 case SCM_GSUBR_MAKTYPE (1, 1, 0):
392 case SCM_GSUBR_MAKTYPE (0, 2, 0):
393 apply_1
= scm_smob_apply_1_020
; break;
394 case SCM_GSUBR_MAKTYPE (1, 2, 0):
395 case SCM_GSUBR_MAKTYPE (0, 3, 0):
396 apply_1
= scm_smob_apply_1_030
; break;
397 case SCM_GSUBR_MAKTYPE (0, 0, 1):
398 apply_1
= scm_smob_apply_1_001
; break;
399 case SCM_GSUBR_MAKTYPE (1, 0, 1):
400 case SCM_GSUBR_MAKTYPE (0, 1, 1):
401 apply_1
= scm_smob_apply_1_011
; break;
402 case SCM_GSUBR_MAKTYPE (1, 1, 1):
403 case SCM_GSUBR_MAKTYPE (0, 2, 1):
404 apply_1
= scm_smob_apply_1_021
; break;
406 apply_1
= scm_smob_apply_1_error
; break;
411 case SCM_GSUBR_MAKTYPE (2, 0, 0):
412 case SCM_GSUBR_MAKTYPE (1, 1, 0):
413 case SCM_GSUBR_MAKTYPE (0, 2, 0):
414 apply_2
= apply
; break;
415 case SCM_GSUBR_MAKTYPE (2, 1, 0):
416 case SCM_GSUBR_MAKTYPE (1, 2, 0):
417 case SCM_GSUBR_MAKTYPE (0, 3, 0):
418 apply_2
= scm_smob_apply_2_030
; break;
419 case SCM_GSUBR_MAKTYPE (0, 0, 1):
420 apply_2
= scm_smob_apply_2_001
; break;
421 case SCM_GSUBR_MAKTYPE (1, 0, 1):
422 case SCM_GSUBR_MAKTYPE (0, 1, 1):
423 apply_2
= scm_smob_apply_2_011
; break;
424 case SCM_GSUBR_MAKTYPE (2, 0, 1):
425 case SCM_GSUBR_MAKTYPE (1, 1, 1):
426 case SCM_GSUBR_MAKTYPE (0, 2, 1):
427 apply_2
= scm_smob_apply_2_021
; break;
429 apply_2
= scm_smob_apply_2_error
; break;
434 case SCM_GSUBR_MAKTYPE (3, 0, 0):
435 case SCM_GSUBR_MAKTYPE (2, 1, 0):
436 case SCM_GSUBR_MAKTYPE (1, 2, 0):
437 case SCM_GSUBR_MAKTYPE (0, 3, 0):
438 apply_3
= scm_smob_apply_3_030
; break;
439 case SCM_GSUBR_MAKTYPE (0, 0, 1):
440 apply_3
= scm_smob_apply_3_001
; break;
441 case SCM_GSUBR_MAKTYPE (1, 0, 1):
442 case SCM_GSUBR_MAKTYPE (0, 1, 1):
443 apply_3
= scm_smob_apply_3_011
; break;
444 case SCM_GSUBR_MAKTYPE (2, 0, 1):
445 case SCM_GSUBR_MAKTYPE (1, 1, 1):
446 case SCM_GSUBR_MAKTYPE (0, 2, 1):
447 apply_3
= scm_smob_apply_3_021
; break;
449 apply_3
= scm_smob_apply_3_error
; break;
452 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply
= apply
;
453 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_0
= apply_0
;
454 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_1
= apply_1
;
455 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_2
= apply_2
;
456 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_3
= apply_3
;
457 scm_smobs
[SCM_TC2SMOBNUM (tc
)].gsubr_type
= type
;
460 scm_i_inherit_applicable (scm_smob_class
[SCM_TC2SMOBNUM (tc
)]);
464 scm_make_smob (scm_t_bits tc
)
466 long n
= SCM_TC2SMOBNUM (tc
);
467 size_t size
= scm_smobs
[n
].size
;
468 scm_t_bits data
= (size
> 0
469 ? (scm_t_bits
) scm_gc_malloc (size
, SCM_SMOBNAME (n
))
471 return scm_cell (tc
, data
);
475 /* {Initialization for i/o types, float, bignum, the type of free cells}
479 free_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
482 sprintf (buf
, "#<freed cell %p; GC missed a reference>",
483 (void *) SCM_UNPACK (exp
));
484 scm_puts (buf
, port
);
486 #if (SCM_DEBUG_CELL_ACCESSES == 1)
487 if (scm_debug_cell_accesses_p
)
496 scm_smob_prehistory ()
502 for (i
= 0; i
< MAX_SMOB_COUNT
; ++i
)
504 scm_smobs
[i
].name
= 0;
505 scm_smobs
[i
].size
= 0;
506 scm_smobs
[i
].mark
= 0;
507 scm_smobs
[i
].free
= 0;
508 scm_smobs
[i
].print
= scm_smob_print
;
509 scm_smobs
[i
].equalp
= 0;
510 scm_smobs
[i
].apply
= 0;
511 scm_smobs
[i
].apply_0
= 0;
512 scm_smobs
[i
].apply_1
= 0;
513 scm_smobs
[i
].apply_2
= 0;
514 scm_smobs
[i
].apply_3
= 0;
515 scm_smobs
[i
].gsubr_type
= 0;
518 /* WARNING: These scm_make_smob_type calls must be done in this order */
519 tc
= scm_make_smob_type ("free", 0);
520 scm_set_smob_print (tc
, free_print
);
522 tc
= scm_make_smob_type ("big", 0); /* freed in gc */
523 scm_set_smob_print (tc
, scm_bigprint
);
524 scm_set_smob_equalp (tc
, scm_bigequal
);
526 tc
= scm_make_smob_type ("real", 0); /* freed in gc */
527 scm_set_smob_print (tc
, scm_print_real
);
528 scm_set_smob_equalp (tc
, scm_real_equalp
);
530 tc
= scm_make_smob_type ("complex", 0); /* freed in gc */
531 scm_set_smob_print (tc
, scm_print_complex
);
532 scm_set_smob_equalp (tc
, scm_complex_equalp
);