Include <config.h> in all C files; use `#ifdef HAVE_CONFIG_H' rather than `#if'.
[bpt/guile.git] / libguile / smob.c
CommitLineData
2b829bbb 1/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
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.
0f2d19dd 7 *
73be1d9e
MV
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.
0f2d19dd 12 *
73be1d9e
MV
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
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd 19\f
dbb605f5 20#ifdef HAVE_CONFIG_H
f9fe039d
RB
21# include <config.h>
22#endif
0f2d19dd
JB
23
24#include <stdio.h>
e6e2e95a
MD
25#include <errno.h>
26
a0599745 27#include "libguile/_scm.h"
20e6290e 28
4e047c3e 29#include "libguile/async.h"
a0599745 30#include "libguile/objects.h"
9511876f 31#include "libguile/goops.h"
a0599745 32#include "libguile/ports.h"
d7ec6b9f 33
0f2d19dd
JB
34#ifdef HAVE_MALLOC_H
35#include <malloc.h>
36#endif
37
a0599745 38#include "libguile/smob.h"
9dd5943c 39
0f2d19dd
JB
40\f
41
42/* scm_smobs scm_numsmob
7a7f7c53 43 * implement a fixed sized array of smob records.
0f2d19dd
JB
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).
46 */
7a7f7c53
DH
47
48#define MAX_SMOB_COUNT 256
c014a02e 49long scm_numsmob;
7a7f7c53 50scm_smob_descriptor scm_smobs[MAX_SMOB_COUNT];
0f2d19dd 51
37fc18ae
MV
52/* Lower 16 bit of data must be zero.
53*/
54void
55scm_i_set_smob_flags (SCM x, scm_t_bits data)
56{
57 SCM_SET_CELL_WORD_0 (x, (SCM_CELL_WORD_0 (x) & 0xFFFF) | data);
58}
59
197b0573
MV
60void
61scm_assert_smob_type (scm_t_bits tag, SCM val)
62{
63 if (!SCM_SMOB_PREDICATE (tag, val))
64 scm_wrong_type_arg_msg (NULL, 0, val, scm_smobs[SCM_TC2SMOBNUM(tag)].name);
65}
66
9dd5943c
MD
67/* {Mark}
68 */
69
70/* This function is vestigial. It used to be the mark function's
71 responsibility to set the mark bit on the smob or port, but now the
72 generic marking routine in gc.c takes care of that, and a zero
73 pointer for a mark function means "don't bother". So you never
74 need scm_mark0.
75
76 However, we leave it here because it's harmless to call it, and
77 people out there have smob code that uses it, and there's no reason
78 to make their links fail. */
79
80SCM
e81d98ec 81scm_mark0 (SCM ptr SCM_UNUSED)
9dd5943c
MD
82{
83 return SCM_BOOL_F;
84}
85
86SCM
22a52da1
DH
87/* Dirk::FIXME: The name markcdr is misleading, since the term cdr should only
88 be used for real pairs. */
6e8d25a6 89scm_markcdr (SCM ptr)
9dd5943c 90{
22a52da1 91 return SCM_CELL_OBJECT_1 (ptr);
9dd5943c
MD
92}
93
94/* {Free}
95 */
96
1be6b49c 97size_t
e81d98ec 98scm_free0 (SCM ptr SCM_UNUSED)
9dd5943c
MD
99{
100 return 0;
101}
102
1be6b49c 103size_t
9dd5943c
MD
104scm_smob_free (SCM obj)
105{
4c9419ac
MV
106 long n = SCM_SMOBNUM (obj);
107 if (scm_smobs[n].size > 0)
108 scm_gc_free ((void *) SCM_CELL_WORD_1 (obj),
109 scm_smobs[n].size, SCM_SMOBNAME (n));
110 return 0;
9dd5943c
MD
111}
112
113/* {Print}
114 */
115
116int
e81d98ec 117scm_smob_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
9dd5943c 118{
c014a02e 119 long n = SCM_SMOBNUM (exp);
9dd5943c 120 scm_puts ("#<", port);
2c16a78a 121 scm_puts (SCM_SMOBNAME (n) ? SCM_SMOBNAME (n) : "smob", port);
9dd5943c 122 scm_putc (' ', port);
7a7f7c53 123 if (scm_smobs[n].size)
0345e278 124 scm_uintprint (SCM_CELL_WORD_1 (exp), 16, port);
7a7f7c53 125 else
0345e278 126 scm_uintprint (SCM_UNPACK (exp), 16, port);
9dd5943c
MD
127 scm_putc ('>', port);
128 return 1;
129}
1cc91f1b 130
0717dfd8
KN
131/* {Apply}
132 */
133
cb1c46c5
KN
134#define SCM_SMOB_APPLY0(SMOB) \
135 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB)
34d19ef6 136#define SCM_SMOB_APPLY1(SMOB, A1) \
cb1c46c5 137 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1)
34d19ef6 138#define SCM_SMOB_APPLY2(SMOB, A1, A2) \
cb1c46c5 139 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2)
34d19ef6 140#define SCM_SMOB_APPLY3(SMOB, A1, A2, A3) \
cb1c46c5
KN
141 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2, A3)
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
cb1c46c5
KN
185static SCM
186scm_smob_apply_1_020 (SCM smob, SCM a1)
187{
188 return SCM_SMOB_APPLY2 (smob, a1, SCM_UNDEFINED);
189}
190
191static SCM
192scm_smob_apply_1_030 (SCM smob, SCM a1)
193{
194 return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_UNDEFINED);
195}
196
197static SCM
198scm_smob_apply_1_001 (SCM smob, SCM a1)
199{
1afff620 200 return SCM_SMOB_APPLY1 (smob, scm_list_1 (a1));
cb1c46c5
KN
201}
202
203static SCM
204scm_smob_apply_1_011 (SCM smob, SCM a1)
205{
206 return SCM_SMOB_APPLY2 (smob, a1, SCM_EOL);
207}
208
209static SCM
210scm_smob_apply_1_021 (SCM smob, SCM a1)
211{
212 return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_EOL);
213}
214
215static SCM
e81d98ec 216scm_smob_apply_1_error (SCM smob, SCM a1 SCM_UNUSED)
cb1c46c5
KN
217{
218 scm_wrong_num_args (smob);
219}
220
cb1c46c5
KN
221static SCM
222scm_smob_apply_2_030 (SCM smob, SCM a1, SCM a2)
223{
224 return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_UNDEFINED);
225}
226
227static SCM
228scm_smob_apply_2_001 (SCM smob, SCM a1, SCM a2)
229{
1afff620 230 return SCM_SMOB_APPLY1 (smob, scm_list_2 (a1, a2));
7c58e21b 231}
cb1c46c5
KN
232
233static SCM
234scm_smob_apply_2_011 (SCM smob, SCM a1, SCM a2)
235{
1afff620 236 return SCM_SMOB_APPLY2 (smob, a1, scm_list_1 (a2));
cb1c46c5
KN
237}
238
239static SCM
240scm_smob_apply_2_021 (SCM smob, SCM a1, SCM a2)
241{
242 return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_EOL);
243}
244
245static SCM
e81d98ec 246scm_smob_apply_2_error (SCM smob, SCM a1 SCM_UNUSED, SCM a2 SCM_UNUSED)
cb1c46c5
KN
247{
248 scm_wrong_num_args (smob);
249}
250
251static SCM
252scm_smob_apply_3_030 (SCM smob, SCM a1, SCM a2, SCM rst)
253{
d2e53ed6 254 if (!scm_is_null (SCM_CDR (rst)))
cb1c46c5
KN
255 scm_wrong_num_args (smob);
256 return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_CAR (rst));
257}
258
259static SCM
260scm_smob_apply_3_001 (SCM smob, SCM a1, SCM a2, SCM rst)
261{
262 return SCM_SMOB_APPLY1 (smob, scm_cons2 (a1, a2, rst));
0717dfd8
KN
263}
264
cb1c46c5
KN
265static SCM
266scm_smob_apply_3_011 (SCM smob, SCM a1, SCM a2, SCM rst)
267{
268 return SCM_SMOB_APPLY2 (smob, a1, scm_cons (a2, rst));
269}
270
271static SCM
272scm_smob_apply_3_021 (SCM smob, SCM a1, SCM a2, SCM rst)
273{
274 return SCM_SMOB_APPLY3 (smob, a1, a2, rst);
275}
276
277static SCM
e81d98ec
DH
278scm_smob_apply_3_error (SCM smob,
279 SCM a1 SCM_UNUSED,
280 SCM a2 SCM_UNUSED,
281 SCM rst SCM_UNUSED)
cb1c46c5
KN
282{
283 scm_wrong_num_args (smob);
284}
285
286\f
7a7f7c53 287
92c2555f 288scm_t_bits
da0e6c2b 289scm_make_smob_type (char const *name, size_t size)
7a7f7c53 290#define FUNC_NAME "scm_make_smob_type"
0f2d19dd 291{
c014a02e 292 long new_smob;
7a7f7c53 293
9de87eea 294 SCM_CRITICAL_SECTION_START;
7a7f7c53
DH
295 new_smob = scm_numsmob;
296 if (scm_numsmob != MAX_SMOB_COUNT)
297 ++scm_numsmob;
9de87eea 298 SCM_CRITICAL_SECTION_END;
7a7f7c53
DH
299
300 if (new_smob == MAX_SMOB_COUNT)
301 scm_misc_error (FUNC_NAME, "maximum number of smobs exceeded", SCM_EOL);
302
303 scm_smobs[new_smob].name = name;
304 if (size != 0)
2500356c 305 {
7a7f7c53
DH
306 scm_smobs[new_smob].size = size;
307 scm_smobs[new_smob].free = scm_smob_free;
2500356c 308 }
7a7f7c53 309
d7ec6b9f
MD
310 /* Make a class object if Goops is present. */
311 if (scm_smob_class)
74b6d6e4 312 scm_smob_class[new_smob] = scm_make_extended_class (name, 0);
7a7f7c53
DH
313
314 return scm_tc7_smob + new_smob * 256;
0f2d19dd 315}
7a7f7c53
DH
316#undef FUNC_NAME
317
0f2d19dd 318
9dd5943c 319void
92c2555f 320scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM))
9dd5943c
MD
321{
322 scm_smobs[SCM_TC2SMOBNUM (tc)].mark = mark;
323}
324
325void
92c2555f 326scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM))
9dd5943c
MD
327{
328 scm_smobs[SCM_TC2SMOBNUM (tc)].free = free;
329}
330
331void
92c2555f 332scm_set_smob_print (scm_t_bits tc, int (*print) (SCM, SCM, scm_print_state*))
9dd5943c
MD
333{
334 scm_smobs[SCM_TC2SMOBNUM (tc)].print = print;
335}
336
337void
92c2555f 338scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM))
9dd5943c
MD
339{
340 scm_smobs[SCM_TC2SMOBNUM (tc)].equalp = equalp;
341}
342
0717dfd8 343void
92c2555f 344scm_set_smob_apply (scm_t_bits tc, SCM (*apply) (),
7c58e21b 345 unsigned int req, unsigned int opt, unsigned int rst)
0717dfd8 346{
cb1c46c5
KN
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);
352
7c58e21b 353 if (rst > 1 || req + opt + rst > 3)
cb1c46c5
KN
354 {
355 puts ("Unsupported smob application type");
356 abort ();
357 }
358
359 switch (type)
360 {
361 case SCM_GSUBR_MAKTYPE (0, 0, 0):
7c58e21b 362 apply_0 = apply; break;
cb1c46c5
KN
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;
375 default:
376 apply_0 = scm_smob_apply_0_error; break;
377 }
378
379 switch (type)
380 {
381 case SCM_GSUBR_MAKTYPE (1, 0, 0):
382 case SCM_GSUBR_MAKTYPE (0, 1, 0):
7c58e21b 383 apply_1 = apply; break;
cb1c46c5
KN
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;
398 default:
399 apply_1 = scm_smob_apply_1_error; break;
400 }
401
402 switch (type)
403 {
404 case SCM_GSUBR_MAKTYPE (2, 0, 0):
405 case SCM_GSUBR_MAKTYPE (1, 1, 0):
406 case SCM_GSUBR_MAKTYPE (0, 2, 0):
7c58e21b 407 apply_2 = apply; break;
cb1c46c5
KN
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;
421 default:
422 apply_2 = scm_smob_apply_2_error; break;
423 }
424
425 switch (type)
426 {
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;
441 default:
442 apply_3 = scm_smob_apply_3_error; break;
443 }
444
03416a99 445 scm_smobs[SCM_TC2SMOBNUM (tc)].apply = apply;
cb1c46c5
KN
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;
68b06924 450 scm_smobs[SCM_TC2SMOBNUM (tc)].gsubr_type = type;
74b6d6e4
MD
451
452 if (scm_smob_class)
453 scm_i_inherit_applicable (scm_smob_class[SCM_TC2SMOBNUM (tc)]);
0717dfd8
KN
454}
455
9dd5943c 456SCM
92c2555f 457scm_make_smob (scm_t_bits tc)
9dd5943c 458{
c014a02e 459 long n = SCM_TC2SMOBNUM (tc);
1be6b49c 460 size_t size = scm_smobs[n].size;
16d4699b 461 scm_t_bits data = (size > 0
4c9419ac 462 ? (scm_t_bits) scm_gc_malloc (size, SCM_SMOBNAME (n))
16d4699b 463 : 0);
228a24ef 464 return scm_cell (tc, data);
9dd5943c
MD
465}
466
ceef3208 467\f
534c55a9 468/* {Initialization for the type of free cells}
0f2d19dd
JB
469 */
470
ceef3208 471static int
e81d98ec 472free_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
ceef3208
JB
473{
474 char buf[100];
e841c3e0
KN
475 sprintf (buf, "#<freed cell %p; GC missed a reference>",
476 (void *) SCM_UNPACK (exp));
ceef3208 477 scm_puts (buf, port);
1e71eafb
HWN
478
479#if (SCM_DEBUG_CELL_ACCESSES == 1)
480 if (scm_debug_cell_accesses_p)
481 abort();
482#endif
483
ceef3208
JB
484
485 return 1;
486}
487
0f2d19dd
JB
488void
489scm_smob_prehistory ()
0f2d19dd 490{
c014a02e 491 long i;
92c2555f 492 scm_t_bits tc;
e841c3e0 493
0f2d19dd 494 scm_numsmob = 0;
7a7f7c53
DH
495 for (i = 0; i < MAX_SMOB_COUNT; ++i)
496 {
497 scm_smobs[i].name = 0;
498 scm_smobs[i].size = 0;
499 scm_smobs[i].mark = 0;
500 scm_smobs[i].free = 0;
501 scm_smobs[i].print = scm_smob_print;
502 scm_smobs[i].equalp = 0;
503 scm_smobs[i].apply = 0;
504 scm_smobs[i].apply_0 = 0;
505 scm_smobs[i].apply_1 = 0;
506 scm_smobs[i].apply_2 = 0;
507 scm_smobs[i].apply_3 = 0;
508 scm_smobs[i].gsubr_type = 0;
509 }
9dd5943c 510
534c55a9 511 /* WARNING: This scm_make_smob_type call must be done first. */
e841c3e0
KN
512 tc = scm_make_smob_type ("free", 0);
513 scm_set_smob_print (tc, free_print);
0f2d19dd 514}
89e00824
ML
515
516/*
517 Local Variables:
518 c-file-style: "gnu"
519 End:
520*/