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
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
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.
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 02110-1301 USA
27 #include "libguile/_scm.h"
29 #include "libguile/async.h"
30 #include "libguile/objects.h"
31 #include "libguile/goops.h"
32 #include "libguile/ports.h"
38 #include "libguile/smob.h"
42 /* scm_smobs scm_numsmob
43 * implement a fixed sized array of smob records.
44 * Indexes into this table are used when generating type
45 * tags for smobjects (if you know a tag you can get an index and conversely).
48 #define MAX_SMOB_COUNT SCM_I_MAX_SMOB_TYPE_COUNT
51 scm_smob_descriptor scm_smobs
[MAX_SMOB_COUNT
];
53 /* Lower 16 bit of data must be zero.
56 scm_i_set_smob_flags (SCM x
, scm_t_bits data
)
58 SCM_SET_CELL_WORD_0 (x
, (SCM_CELL_WORD_0 (x
) & 0xFFFF) | data
);
62 scm_assert_smob_type (scm_t_bits tag
, SCM val
)
64 if (!SCM_SMOB_PREDICATE (tag
, val
))
65 scm_wrong_type_arg_msg (NULL
, 0, val
, scm_smobs
[SCM_TC2SMOBNUM(tag
)].name
);
71 /* This function is vestigial. It used to be the mark function's
72 responsibility to set the mark bit on the smob or port, but now the
73 generic marking routine in gc.c takes care of that, and a zero
74 pointer for a mark function means "don't bother". So you never
77 However, we leave it here because it's harmless to call it, and
78 people out there have smob code that uses it, and there's no reason
79 to make their links fail. */
82 scm_mark0 (SCM ptr SCM_UNUSED
)
88 /* Dirk::FIXME: The name markcdr is misleading, since the term cdr should only
89 be used for real pairs. */
92 return SCM_CELL_OBJECT_1 (ptr
);
99 scm_free0 (SCM ptr SCM_UNUSED
)
105 scm_smob_free (SCM obj
)
107 long n
= SCM_SMOBNUM (obj
);
108 if (scm_smobs
[n
].size
> 0)
109 scm_gc_free ((void *) SCM_CELL_WORD_1 (obj
),
110 scm_smobs
[n
].size
, SCM_SMOBNAME (n
));
118 scm_smob_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
120 long n
= SCM_SMOBNUM (exp
);
121 scm_puts ("#<", port
);
122 scm_puts (SCM_SMOBNAME (n
) ? SCM_SMOBNAME (n
) : "smob", port
);
123 scm_putc (' ', port
);
124 if (scm_smobs
[n
].size
)
125 scm_uintprint (SCM_CELL_WORD_1 (exp
), 16, port
);
127 scm_uintprint (SCM_UNPACK (exp
), 16, port
);
128 scm_putc ('>', port
);
135 #define SCM_SMOB_APPLY0(SMOB) \
136 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB)
137 #define SCM_SMOB_APPLY1(SMOB, A1) \
138 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1)
139 #define SCM_SMOB_APPLY2(SMOB, A1, A2) \
140 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2)
141 #define SCM_SMOB_APPLY3(SMOB, A1, A2, A3) \
142 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2, A3)
145 scm_smob_apply_0_010 (SCM smob
)
147 return SCM_SMOB_APPLY1 (smob
, SCM_UNDEFINED
);
151 scm_smob_apply_0_020 (SCM smob
)
153 return SCM_SMOB_APPLY2 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
);
157 scm_smob_apply_0_030 (SCM smob
)
159 return SCM_SMOB_APPLY3 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
, SCM_UNDEFINED
);
163 scm_smob_apply_0_001 (SCM smob
)
165 return SCM_SMOB_APPLY1 (smob
, SCM_EOL
);
169 scm_smob_apply_0_011 (SCM smob
)
171 return SCM_SMOB_APPLY2 (smob
, SCM_UNDEFINED
, SCM_EOL
);
175 scm_smob_apply_0_021 (SCM smob
)
177 return SCM_SMOB_APPLY3 (smob
, SCM_UNDEFINED
, SCM_UNDEFINED
, SCM_EOL
);
181 scm_smob_apply_0_error (SCM smob
)
183 scm_wrong_num_args (smob
);
187 scm_smob_apply_1_020 (SCM smob
, SCM a1
)
189 return SCM_SMOB_APPLY2 (smob
, a1
, SCM_UNDEFINED
);
193 scm_smob_apply_1_030 (SCM smob
, SCM a1
)
195 return SCM_SMOB_APPLY3 (smob
, a1
, SCM_UNDEFINED
, SCM_UNDEFINED
);
199 scm_smob_apply_1_001 (SCM smob
, SCM a1
)
201 return SCM_SMOB_APPLY1 (smob
, scm_list_1 (a1
));
205 scm_smob_apply_1_011 (SCM smob
, SCM a1
)
207 return SCM_SMOB_APPLY2 (smob
, a1
, SCM_EOL
);
211 scm_smob_apply_1_021 (SCM smob
, SCM a1
)
213 return SCM_SMOB_APPLY3 (smob
, a1
, SCM_UNDEFINED
, SCM_EOL
);
217 scm_smob_apply_1_error (SCM smob
, SCM a1 SCM_UNUSED
)
219 scm_wrong_num_args (smob
);
223 scm_smob_apply_2_030 (SCM smob
, SCM a1
, SCM a2
)
225 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_UNDEFINED
);
229 scm_smob_apply_2_001 (SCM smob
, SCM a1
, SCM a2
)
231 return SCM_SMOB_APPLY1 (smob
, scm_list_2 (a1
, a2
));
235 scm_smob_apply_2_011 (SCM smob
, SCM a1
, SCM a2
)
237 return SCM_SMOB_APPLY2 (smob
, a1
, scm_list_1 (a2
));
241 scm_smob_apply_2_021 (SCM smob
, SCM a1
, SCM a2
)
243 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_EOL
);
247 scm_smob_apply_2_error (SCM smob
, SCM a1 SCM_UNUSED
, SCM a2 SCM_UNUSED
)
249 scm_wrong_num_args (smob
);
253 scm_smob_apply_3_030 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
255 if (!scm_is_null (SCM_CDR (rst
)))
256 scm_wrong_num_args (smob
);
257 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, SCM_CAR (rst
));
261 scm_smob_apply_3_001 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
263 return SCM_SMOB_APPLY1 (smob
, scm_cons2 (a1
, a2
, rst
));
267 scm_smob_apply_3_011 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
269 return SCM_SMOB_APPLY2 (smob
, a1
, scm_cons (a2
, rst
));
273 scm_smob_apply_3_021 (SCM smob
, SCM a1
, SCM a2
, SCM rst
)
275 return SCM_SMOB_APPLY3 (smob
, a1
, a2
, rst
);
279 scm_smob_apply_3_error (SCM smob
,
284 scm_wrong_num_args (smob
);
290 scm_make_smob_type (char const *name
, size_t size
)
291 #define FUNC_NAME "scm_make_smob_type"
295 SCM_CRITICAL_SECTION_START
;
296 new_smob
= scm_numsmob
;
297 if (scm_numsmob
!= MAX_SMOB_COUNT
)
299 SCM_CRITICAL_SECTION_END
;
301 if (new_smob
== MAX_SMOB_COUNT
)
302 scm_misc_error (FUNC_NAME
, "maximum number of smobs exceeded", SCM_EOL
);
304 scm_smobs
[new_smob
].name
= name
;
307 scm_smobs
[new_smob
].size
= size
;
308 scm_smobs
[new_smob
].free
= scm_smob_free
;
311 /* Make a class object if Goops is present. */
312 if (SCM_UNPACK (scm_smob_class
[0]) != 0)
313 scm_smob_class
[new_smob
] = scm_make_extended_class (name
, 0);
315 return scm_tc7_smob
+ new_smob
* 256;
321 scm_set_smob_mark (scm_t_bits tc
, SCM (*mark
) (SCM
))
323 scm_smobs
[SCM_TC2SMOBNUM (tc
)].mark
= mark
;
327 scm_set_smob_free (scm_t_bits tc
, size_t (*free
) (SCM
))
329 scm_smobs
[SCM_TC2SMOBNUM (tc
)].free
= free
;
333 scm_set_smob_print (scm_t_bits tc
, int (*print
) (SCM
, SCM
, scm_print_state
*))
335 scm_smobs
[SCM_TC2SMOBNUM (tc
)].print
= print
;
339 scm_set_smob_equalp (scm_t_bits tc
, SCM (*equalp
) (SCM
, SCM
))
341 scm_smobs
[SCM_TC2SMOBNUM (tc
)].equalp
= equalp
;
345 scm_set_smob_apply (scm_t_bits tc
, SCM (*apply
) (),
346 unsigned int req
, unsigned int opt
, unsigned int rst
)
348 SCM (*apply_0
) (SCM
);
349 SCM (*apply_1
) (SCM
, SCM
);
350 SCM (*apply_2
) (SCM
, SCM
, SCM
);
351 SCM (*apply_3
) (SCM
, SCM
, SCM
, SCM
);
352 int type
= SCM_GSUBR_MAKTYPE (req
, opt
, rst
);
354 if (rst
> 1 || req
+ opt
+ rst
> 3)
356 puts ("Unsupported smob application type");
362 case SCM_GSUBR_MAKTYPE (0, 0, 0):
363 apply_0
= apply
; break;
364 case SCM_GSUBR_MAKTYPE (0, 1, 0):
365 apply_0
= scm_smob_apply_0_010
; break;
366 case SCM_GSUBR_MAKTYPE (0, 2, 0):
367 apply_0
= scm_smob_apply_0_020
; break;
368 case SCM_GSUBR_MAKTYPE (0, 3, 0):
369 apply_0
= scm_smob_apply_0_030
; break;
370 case SCM_GSUBR_MAKTYPE (0, 0, 1):
371 apply_0
= scm_smob_apply_0_001
; break;
372 case SCM_GSUBR_MAKTYPE (0, 1, 1):
373 apply_0
= scm_smob_apply_0_011
; break;
374 case SCM_GSUBR_MAKTYPE (0, 2, 1):
375 apply_0
= scm_smob_apply_0_021
; break;
377 apply_0
= scm_smob_apply_0_error
; break;
382 case SCM_GSUBR_MAKTYPE (1, 0, 0):
383 case SCM_GSUBR_MAKTYPE (0, 1, 0):
384 apply_1
= apply
; break;
385 case SCM_GSUBR_MAKTYPE (1, 1, 0):
386 case SCM_GSUBR_MAKTYPE (0, 2, 0):
387 apply_1
= scm_smob_apply_1_020
; break;
388 case SCM_GSUBR_MAKTYPE (1, 2, 0):
389 case SCM_GSUBR_MAKTYPE (0, 3, 0):
390 apply_1
= scm_smob_apply_1_030
; break;
391 case SCM_GSUBR_MAKTYPE (0, 0, 1):
392 apply_1
= scm_smob_apply_1_001
; break;
393 case SCM_GSUBR_MAKTYPE (1, 0, 1):
394 case SCM_GSUBR_MAKTYPE (0, 1, 1):
395 apply_1
= scm_smob_apply_1_011
; break;
396 case SCM_GSUBR_MAKTYPE (1, 1, 1):
397 case SCM_GSUBR_MAKTYPE (0, 2, 1):
398 apply_1
= scm_smob_apply_1_021
; break;
400 apply_1
= scm_smob_apply_1_error
; break;
405 case SCM_GSUBR_MAKTYPE (2, 0, 0):
406 case SCM_GSUBR_MAKTYPE (1, 1, 0):
407 case SCM_GSUBR_MAKTYPE (0, 2, 0):
408 apply_2
= apply
; break;
409 case SCM_GSUBR_MAKTYPE (2, 1, 0):
410 case SCM_GSUBR_MAKTYPE (1, 2, 0):
411 case SCM_GSUBR_MAKTYPE (0, 3, 0):
412 apply_2
= scm_smob_apply_2_030
; break;
413 case SCM_GSUBR_MAKTYPE (0, 0, 1):
414 apply_2
= scm_smob_apply_2_001
; break;
415 case SCM_GSUBR_MAKTYPE (1, 0, 1):
416 case SCM_GSUBR_MAKTYPE (0, 1, 1):
417 apply_2
= scm_smob_apply_2_011
; break;
418 case SCM_GSUBR_MAKTYPE (2, 0, 1):
419 case SCM_GSUBR_MAKTYPE (1, 1, 1):
420 case SCM_GSUBR_MAKTYPE (0, 2, 1):
421 apply_2
= scm_smob_apply_2_021
; break;
423 apply_2
= scm_smob_apply_2_error
; break;
428 case SCM_GSUBR_MAKTYPE (3, 0, 0):
429 case SCM_GSUBR_MAKTYPE (2, 1, 0):
430 case SCM_GSUBR_MAKTYPE (1, 2, 0):
431 case SCM_GSUBR_MAKTYPE (0, 3, 0):
432 apply_3
= scm_smob_apply_3_030
; break;
433 case SCM_GSUBR_MAKTYPE (0, 0, 1):
434 apply_3
= scm_smob_apply_3_001
; break;
435 case SCM_GSUBR_MAKTYPE (1, 0, 1):
436 case SCM_GSUBR_MAKTYPE (0, 1, 1):
437 apply_3
= scm_smob_apply_3_011
; break;
438 case SCM_GSUBR_MAKTYPE (2, 0, 1):
439 case SCM_GSUBR_MAKTYPE (1, 1, 1):
440 case SCM_GSUBR_MAKTYPE (0, 2, 1):
441 apply_3
= scm_smob_apply_3_021
; break;
443 apply_3
= scm_smob_apply_3_error
; break;
446 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply
= apply
;
447 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_0
= apply_0
;
448 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_1
= apply_1
;
449 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_2
= apply_2
;
450 scm_smobs
[SCM_TC2SMOBNUM (tc
)].apply_3
= apply_3
;
451 scm_smobs
[SCM_TC2SMOBNUM (tc
)].gsubr_type
= type
;
453 if (SCM_UNPACK (scm_smob_class
[0]) != 0)
454 scm_i_inherit_applicable (scm_smob_class
[SCM_TC2SMOBNUM (tc
)]);
458 scm_make_smob (scm_t_bits tc
)
460 long n
= SCM_TC2SMOBNUM (tc
);
461 size_t size
= scm_smobs
[n
].size
;
462 scm_t_bits data
= (size
> 0
463 ? (scm_t_bits
) scm_gc_malloc (size
, SCM_SMOBNAME (n
))
465 return scm_cell (tc
, data
);
469 /* {Initialization for the type of free cells}
473 free_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
476 sprintf (buf
, "#<freed cell %p; GC missed a reference>",
477 (void *) SCM_UNPACK (exp
));
478 scm_puts (buf
, port
);
480 #if (SCM_DEBUG_CELL_ACCESSES == 1)
481 if (scm_debug_cell_accesses_p
)
490 scm_smob_prehistory ()
496 for (i
= 0; i
< MAX_SMOB_COUNT
; ++i
)
498 scm_smobs
[i
].name
= 0;
499 scm_smobs
[i
].size
= 0;
500 scm_smobs
[i
].mark
= 0;
501 scm_smobs
[i
].free
= 0;
502 scm_smobs
[i
].print
= scm_smob_print
;
503 scm_smobs
[i
].equalp
= 0;
504 scm_smobs
[i
].apply
= 0;
505 scm_smobs
[i
].apply_0
= 0;
506 scm_smobs
[i
].apply_1
= 0;
507 scm_smobs
[i
].apply_2
= 0;
508 scm_smobs
[i
].apply_3
= 0;
509 scm_smobs
[i
].gsubr_type
= 0;
512 /* WARNING: This scm_make_smob_type call must be done first. */
513 tc
= scm_make_smob_type ("free", 0);
514 scm_set_smob_print (tc
, free_print
);