1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006, 2009 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * 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.
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
28 #include "libguile/_scm.h"
30 #include "libguile/async.h"
31 #include "libguile/objects.h"
32 #include "libguile/goops.h"
33 #include "libguile/ports.h"
39 #include "libguile/smob.h"
43 /* scm_smobs scm_numsmob
44 * implement a fixed sized array of smob records.
45 * Indexes into this table are used when generating type
46 * tags for smobjects (if you know a tag you can get an index and conversely).
49 #define MAX_SMOB_COUNT SCM_I_MAX_SMOB_TYPE_COUNT
52 scm_smob_descriptor scm_smobs
[MAX_SMOB_COUNT
];
54 /* Lower 16 bit of data must be zero.
57 scm_i_set_smob_flags (SCM x
, scm_t_bits data
)
59 SCM_SET_CELL_WORD_0 (x
, (SCM_CELL_WORD_0 (x
) & 0xFFFF) | data
);
63 scm_assert_smob_type (scm_t_bits tag
, SCM val
)
65 if (!SCM_SMOB_PREDICATE (tag
, val
))
66 scm_wrong_type_arg_msg (NULL
, 0, val
, scm_smobs
[SCM_TC2SMOBNUM(tag
)].name
);
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
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. */
83 scm_mark0 (SCM ptr SCM_UNUSED
)
89 /* Dirk::FIXME: The name markcdr is misleading, since the term cdr should only
90 be used for real pairs. */
93 return SCM_CELL_OBJECT_1 (ptr
);
100 scm_free0 (SCM ptr SCM_UNUSED
)
106 scm_smob_free (SCM obj
)
108 long n
= SCM_SMOBNUM (obj
);
109 if (scm_smobs
[n
].size
> 0)
110 scm_gc_free ((void *) SCM_CELL_WORD_1 (obj
),
111 scm_smobs
[n
].size
, SCM_SMOBNAME (n
));
119 scm_smob_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
121 long n
= SCM_SMOBNUM (exp
);
122 scm_puts ("#<", port
);
123 scm_puts (SCM_SMOBNAME (n
) ? SCM_SMOBNAME (n
) : "smob", port
);
124 scm_putc (' ', port
);
125 if (scm_smobs
[n
].size
)
126 scm_uintprint (SCM_CELL_WORD_1 (exp
), 16, port
);
128 scm_uintprint (SCM_UNPACK (exp
), 16, port
);
129 scm_putc ('>', port
);
136 #define SCM_SMOB_APPLY0(SMOB) \
137 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB)
138 #define SCM_SMOB_APPLY1(SMOB, A1) \
139 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1)
140 #define SCM_SMOB_APPLY2(SMOB, A1, A2) \
141 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2)
142 #define SCM_SMOB_APPLY3(SMOB, A1, A2, A3) \
143 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2, A3)
146 scm_smob_apply_0_010 (SCM smob
)
148 return SCM_SMOB_APPLY1 (smob
, SCM_UNDEFINED
);
152 scm_smob_apply_0_020 (SCM smob
)
154 return SCM_SMOB_APPLY2 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
);
158 scm_smob_apply_0_030 (SCM smob
)
160 return SCM_SMOB_APPLY3 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
, SCM_UNDEFINED
);
164 scm_smob_apply_0_001 (SCM smob
)
166 return SCM_SMOB_APPLY1 (smob
, SCM_EOL
);
170 scm_smob_apply_0_011 (SCM smob
)
172 return SCM_SMOB_APPLY2 (smob
, SCM_UNDEFINED
, SCM_EOL
);
176 scm_smob_apply_0_021 (SCM smob
)
178 return SCM_SMOB_APPLY3 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
, SCM_EOL
);
182 scm_smob_apply_0_error (SCM smob
)
184 scm_wrong_num_args (smob
);
188 scm_smob_apply_1_020 (SCM smob
, SCM a1
)
190 return SCM_SMOB_APPLY2 (smob
, a1
, SCM_UNDEFINED
);
194 scm_smob_apply_1_030 (SCM smob
, SCM a1
)
196 return SCM_SMOB_APPLY3 (smob
, a1
, SCM_UNDEFINED
, SCM_UNDEFINED
);
200 scm_smob_apply_1_001 (SCM smob
, SCM a1
)
202 return SCM_SMOB_APPLY1 (smob
, scm_list_1 (a1
));
206 scm_smob_apply_1_011 (SCM smob
, SCM a1
)
208 return SCM_SMOB_APPLY2 (smob
, a1
, SCM_EOL
);
212 scm_smob_apply_1_021 (SCM smob
, SCM a1
)
214 return SCM_SMOB_APPLY3 (smob
, a1
, SCM_UNDEFINED
, SCM_EOL
);
218 scm_smob_apply_1_error (SCM smob
, SCM a1 SCM_UNUSED
)
220 scm_wrong_num_args (smob
);
224 scm_smob_apply_2_030 (SCM smob
, SCM a1
, SCM a2
)
226 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_UNDEFINED
);
230 scm_smob_apply_2_001 (SCM smob
, SCM a1
, SCM a2
)
232 return SCM_SMOB_APPLY1 (smob
, scm_list_2 (a1
, a2
));
236 scm_smob_apply_2_011 (SCM smob
, SCM a1
, SCM a2
)
238 return SCM_SMOB_APPLY2 (smob
, a1
, scm_list_1 (a2
));
242 scm_smob_apply_2_021 (SCM smob
, SCM a1
, SCM a2
)
244 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_EOL
);
248 scm_smob_apply_2_error (SCM smob
, SCM a1 SCM_UNUSED
, SCM a2 SCM_UNUSED
)
250 scm_wrong_num_args (smob
);
254 scm_smob_apply_3_030 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
256 if (!scm_is_null (SCM_CDR (rst
)))
257 scm_wrong_num_args (smob
);
258 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_CAR (rst
));
262 scm_smob_apply_3_001 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
264 return SCM_SMOB_APPLY1 (smob
, scm_cons2 (a1
, a2
, rst
));
268 scm_smob_apply_3_011 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
270 return SCM_SMOB_APPLY2 (smob
, a1
, scm_cons (a2
, rst
));
274 scm_smob_apply_3_021 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
276 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, rst
);
280 scm_smob_apply_3_error (SCM smob
,
285 scm_wrong_num_args (smob
);
291 scm_make_smob_type (char const *name
, size_t size
)
292 #define FUNC_NAME "scm_make_smob_type"
296 SCM_CRITICAL_SECTION_START
;
297 new_smob
= scm_numsmob
;
298 if (scm_numsmob
!= MAX_SMOB_COUNT
)
300 SCM_CRITICAL_SECTION_END
;
302 if (new_smob
== MAX_SMOB_COUNT
)
303 scm_misc_error (FUNC_NAME
, "maximum number of smobs exceeded", SCM_EOL
);
305 scm_smobs
[new_smob
].name
= name
;
308 scm_smobs
[new_smob
].size
= size
;
309 scm_smobs
[new_smob
].free
= scm_smob_free
;
312 /* Make a class object if Goops is present. */
313 if (SCM_UNPACK (scm_smob_class
[0]) != 0)
314 scm_smob_class
[new_smob
] = scm_make_extended_class (name
, 0);
316 return scm_tc7_smob
+ new_smob
* 256;
322 scm_set_smob_mark (scm_t_bits tc
, SCM (*mark
) (SCM
))
324 scm_smobs
[SCM_TC2SMOBNUM (tc
)].mark
= mark
;
328 scm_set_smob_free (scm_t_bits tc
, size_t (*free
) (SCM
))
330 scm_smobs
[SCM_TC2SMOBNUM (tc
)].free
= free
;
334 scm_set_smob_print (scm_t_bits tc
, int (*print
) (SCM
, SCM
, scm_print_state
*))
336 scm_smobs
[SCM_TC2SMOBNUM (tc
)].print
= print
;
340 scm_set_smob_equalp (scm_t_bits tc
, SCM (*equalp
) (SCM
, SCM
))
342 scm_smobs
[SCM_TC2SMOBNUM (tc
)].equalp
= equalp
;
346 scm_set_smob_apply (scm_t_bits tc
, SCM (*apply
) (),
347 unsigned int req
, unsigned int opt
, unsigned int rst
)
349 SCM (*apply_0
) (SCM
);
350 SCM (*apply_1
) (SCM
, SCM
);
351 SCM (*apply_2
) (SCM
, SCM
, SCM
);
352 SCM (*apply_3
) (SCM
, SCM
, SCM
, SCM
);
353 int type
= SCM_GSUBR_MAKTYPE (req
, opt
, rst
);
355 if (rst
> 1 || req
+ opt
+ rst
> 3)
357 puts ("Unsupported smob application type");
363 case SCM_GSUBR_MAKTYPE (0, 0, 0):
364 apply_0
= apply
; break;
365 case SCM_GSUBR_MAKTYPE (0, 1, 0):
366 apply_0
= scm_smob_apply_0_010
; break;
367 case SCM_GSUBR_MAKTYPE (0, 2, 0):
368 apply_0
= scm_smob_apply_0_020
; break;
369 case SCM_GSUBR_MAKTYPE (0, 3, 0):
370 apply_0
= scm_smob_apply_0_030
; break;
371 case SCM_GSUBR_MAKTYPE (0, 0, 1):
372 apply_0
= scm_smob_apply_0_001
; break;
373 case SCM_GSUBR_MAKTYPE (0, 1, 1):
374 apply_0
= scm_smob_apply_0_011
; break;
375 case SCM_GSUBR_MAKTYPE (0, 2, 1):
376 apply_0
= scm_smob_apply_0_021
; break;
378 apply_0
= scm_smob_apply_0_error
; break;
383 case SCM_GSUBR_MAKTYPE (1, 0, 0):
384 case SCM_GSUBR_MAKTYPE (0, 1, 0):
385 apply_1
= apply
; break;
386 case SCM_GSUBR_MAKTYPE (1, 1, 0):
387 case SCM_GSUBR_MAKTYPE (0, 2, 0):
388 apply_1
= scm_smob_apply_1_020
; break;
389 case SCM_GSUBR_MAKTYPE (1, 2, 0):
390 case SCM_GSUBR_MAKTYPE (0, 3, 0):
391 apply_1
= scm_smob_apply_1_030
; break;
392 case SCM_GSUBR_MAKTYPE (0, 0, 1):
393 apply_1
= scm_smob_apply_1_001
; break;
394 case SCM_GSUBR_MAKTYPE (1, 0, 1):
395 case SCM_GSUBR_MAKTYPE (0, 1, 1):
396 apply_1
= scm_smob_apply_1_011
; break;
397 case SCM_GSUBR_MAKTYPE (1, 1, 1):
398 case SCM_GSUBR_MAKTYPE (0, 2, 1):
399 apply_1
= scm_smob_apply_1_021
; break;
401 apply_1
= scm_smob_apply_1_error
; break;
406 case SCM_GSUBR_MAKTYPE (2, 0, 0):
407 case SCM_GSUBR_MAKTYPE (1, 1, 0):
408 case SCM_GSUBR_MAKTYPE (0, 2, 0):
409 apply_2
= apply
; break;
410 case SCM_GSUBR_MAKTYPE (2, 1, 0):
411 case SCM_GSUBR_MAKTYPE (1, 2, 0):
412 case SCM_GSUBR_MAKTYPE (0, 3, 0):
413 apply_2
= scm_smob_apply_2_030
; break;
414 case SCM_GSUBR_MAKTYPE (0, 0, 1):
415 apply_2
= scm_smob_apply_2_001
; break;
416 case SCM_GSUBR_MAKTYPE (1, 0, 1):
417 case SCM_GSUBR_MAKTYPE (0, 1, 1):
418 apply_2
= scm_smob_apply_2_011
; break;
419 case SCM_GSUBR_MAKTYPE (2, 0, 1):
420 case SCM_GSUBR_MAKTYPE (1, 1, 1):
421 case SCM_GSUBR_MAKTYPE (0, 2, 1):
422 apply_2
= scm_smob_apply_2_021
; break;
424 apply_2
= scm_smob_apply_2_error
; break;
429 case SCM_GSUBR_MAKTYPE (3, 0, 0):
430 case SCM_GSUBR_MAKTYPE (2, 1, 0):
431 case SCM_GSUBR_MAKTYPE (1, 2, 0):
432 case SCM_GSUBR_MAKTYPE (0, 3, 0):
433 apply_3
= scm_smob_apply_3_030
; break;
434 case SCM_GSUBR_MAKTYPE (0, 0, 1):
435 apply_3
= scm_smob_apply_3_001
; break;
436 case SCM_GSUBR_MAKTYPE (1, 0, 1):
437 case SCM_GSUBR_MAKTYPE (0, 1, 1):
438 apply_3
= scm_smob_apply_3_011
; break;
439 case SCM_GSUBR_MAKTYPE (2, 0, 1):
440 case SCM_GSUBR_MAKTYPE (1, 1, 1):
441 case SCM_GSUBR_MAKTYPE (0, 2, 1):
442 apply_3
= scm_smob_apply_3_021
; break;
444 apply_3
= scm_smob_apply_3_error
; break;
447 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply
= apply
;
448 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_0
= apply_0
;
449 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_1
= apply_1
;
450 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_2
= apply_2
;
451 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_3
= apply_3
;
452 scm_smobs
[SCM_TC2SMOBNUM (tc
)].gsubr_type
= type
;
454 if (SCM_UNPACK (scm_smob_class
[0]) != 0)
455 scm_i_inherit_applicable (scm_smob_class
[SCM_TC2SMOBNUM (tc
)]);
459 scm_make_smob (scm_t_bits tc
)
461 long n
= SCM_TC2SMOBNUM (tc
);
462 size_t size
= scm_smobs
[n
].size
;
463 scm_t_bits data
= (size
> 0
464 ? (scm_t_bits
) scm_gc_malloc (size
, SCM_SMOBNAME (n
))
466 return scm_cell (tc
, data
);
470 /* {Initialization for the type of free cells}
474 free_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
477 sprintf (buf
, "#<freed cell %p; GC missed a reference>",
478 (void *) SCM_UNPACK (exp
));
479 scm_puts (buf
, port
);
481 #if (SCM_DEBUG_CELL_ACCESSES == 1)
482 if (scm_debug_cell_accesses_p
)
491 scm_smob_prehistory ()
497 for (i
= 0; i
< MAX_SMOB_COUNT
; ++i
)
499 scm_smobs
[i
].name
= 0;
500 scm_smobs
[i
].size
= 0;
501 scm_smobs
[i
].mark
= 0;
502 scm_smobs
[i
].free
= 0;
503 scm_smobs
[i
].print
= scm_smob_print
;
504 scm_smobs
[i
].equalp
= 0;
505 scm_smobs
[i
].apply
= 0;
506 scm_smobs
[i
].apply_0
= 0;
507 scm_smobs
[i
].apply_1
= 0;
508 scm_smobs
[i
].apply_2
= 0;
509 scm_smobs
[i
].apply_3
= 0;
510 scm_smobs
[i
].gsubr_type
= 0;
513 /* WARNING: This scm_make_smob_type call must be done first. */
514 tc
= scm_make_smob_type ("free", 0);
515 scm_set_smob_print (tc
, free_print
);