*** empty log message ***
[bpt/guile.git] / libguile / smob.c
CommitLineData
74b6d6e4 1/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
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)
6 * any later version.
7 *
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.
12 *
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
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.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
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.
37 *
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84 41
1bbd0b84 42
0f2d19dd 43\f
f9fe039d
RB
44#if HAVE_CONFIG_H
45# include <config.h>
46#endif
0f2d19dd
JB
47
48#include <stdio.h>
e6e2e95a
MD
49#include <errno.h>
50
a0599745 51#include "libguile/_scm.h"
20e6290e 52
a0599745
MD
53#include "libguile/objects.h"
54#include "libguile/ports.h"
d7ec6b9f 55
0f2d19dd
JB
56#ifdef HAVE_MALLOC_H
57#include <malloc.h>
58#endif
59
a0599745 60#include "libguile/smob.h"
9dd5943c 61
0f2d19dd
JB
62\f
63
64/* scm_smobs scm_numsmob
7a7f7c53 65 * implement a fixed sized array of smob records.
0f2d19dd
JB
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).
68 */
7a7f7c53
DH
69
70#define MAX_SMOB_COUNT 256
c014a02e 71long scm_numsmob;
7a7f7c53 72scm_smob_descriptor scm_smobs[MAX_SMOB_COUNT];
0f2d19dd 73
9dd5943c
MD
74/* {Mark}
75 */
76
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
81 need scm_mark0.
82
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. */
86
87SCM
e81d98ec 88scm_mark0 (SCM ptr SCM_UNUSED)
9dd5943c
MD
89{
90 return SCM_BOOL_F;
91}
92
93SCM
22a52da1
DH
94/* Dirk::FIXME: The name markcdr is misleading, since the term cdr should only
95 be used for real pairs. */
6e8d25a6 96scm_markcdr (SCM ptr)
9dd5943c 97{
22a52da1 98 return SCM_CELL_OBJECT_1 (ptr);
9dd5943c
MD
99}
100
101/* {Free}
102 */
103
1be6b49c 104size_t
e81d98ec 105scm_free0 (SCM ptr SCM_UNUSED)
9dd5943c
MD
106{
107 return 0;
108}
109
1be6b49c 110size_t
9dd5943c
MD
111scm_smob_free (SCM obj)
112{
4c9419ac
MV
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));
117 return 0;
9dd5943c
MD
118}
119
120/* {Print}
121 */
122
123int
e81d98ec 124scm_smob_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
9dd5943c 125{
c014a02e 126 long n = SCM_SMOBNUM (exp);
9dd5943c 127 scm_puts ("#<", port);
2c16a78a 128 scm_puts (SCM_SMOBNAME (n) ? SCM_SMOBNAME (n) : "smob", port);
9dd5943c 129 scm_putc (' ', port);
7a7f7c53
DH
130 if (scm_smobs[n].size)
131 scm_intprint (SCM_CELL_WORD_1 (exp), 16, port);
132 else
133 scm_intprint (SCM_UNPACK (exp), 16, port);
9dd5943c
MD
134 scm_putc ('>', port);
135 return 1;
136}
1cc91f1b 137
0717dfd8
KN
138/* {Apply}
139 */
140
cb1c46c5
KN
141#define SCM_SMOB_APPLY0(SMOB) \
142 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB)
34d19ef6 143#define SCM_SMOB_APPLY1(SMOB, A1) \
cb1c46c5 144 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1)
34d19ef6 145#define SCM_SMOB_APPLY2(SMOB, A1, A2) \
cb1c46c5 146 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2)
34d19ef6 147#define SCM_SMOB_APPLY3(SMOB, A1, A2, A3) \
cb1c46c5
KN
148 SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2, A3)
149
cb1c46c5
KN
150static SCM
151scm_smob_apply_0_010 (SCM smob)
0717dfd8 152{
cb1c46c5 153 return SCM_SMOB_APPLY1 (smob, SCM_UNDEFINED);
0717dfd8
KN
154}
155
cb1c46c5
KN
156static SCM
157scm_smob_apply_0_020 (SCM smob)
0717dfd8 158{
cb1c46c5 159 return SCM_SMOB_APPLY2 (smob, SCM_UNDEFINED, SCM_UNDEFINED);
0717dfd8
KN
160}
161
cb1c46c5
KN
162static SCM
163scm_smob_apply_0_030 (SCM smob)
0717dfd8 164{
cb1c46c5 165 return SCM_SMOB_APPLY3 (smob, SCM_UNDEFINED, SCM_UNDEFINED, SCM_UNDEFINED);
0717dfd8
KN
166}
167
cb1c46c5
KN
168static SCM
169scm_smob_apply_0_001 (SCM smob)
0717dfd8 170{
cb1c46c5
KN
171 return SCM_SMOB_APPLY1 (smob, SCM_EOL);
172}
173
174static SCM
175scm_smob_apply_0_011 (SCM smob)
176{
177 return SCM_SMOB_APPLY2 (smob, SCM_UNDEFINED, SCM_EOL);
178}
179
180static SCM
181scm_smob_apply_0_021 (SCM smob)
182{
183 return SCM_SMOB_APPLY3 (smob, SCM_UNDEFINED, SCM_UNDEFINED, SCM_EOL);
184}
185
186static SCM
187scm_smob_apply_0_error (SCM smob)
188{
189 scm_wrong_num_args (smob);
190}
191
cb1c46c5
KN
192static SCM
193scm_smob_apply_1_020 (SCM smob, SCM a1)
194{
195 return SCM_SMOB_APPLY2 (smob, a1, SCM_UNDEFINED);
196}
197
198static SCM
199scm_smob_apply_1_030 (SCM smob, SCM a1)
200{
201 return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_UNDEFINED);
202}
203
204static SCM
205scm_smob_apply_1_001 (SCM smob, SCM a1)
206{
1afff620 207 return SCM_SMOB_APPLY1 (smob, scm_list_1 (a1));
cb1c46c5
KN
208}
209
210static SCM
211scm_smob_apply_1_011 (SCM smob, SCM a1)
212{
213 return SCM_SMOB_APPLY2 (smob, a1, SCM_EOL);
214}
215
216static SCM
217scm_smob_apply_1_021 (SCM smob, SCM a1)
218{
219 return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_EOL);
220}
221
222static SCM
e81d98ec 223scm_smob_apply_1_error (SCM smob, SCM a1 SCM_UNUSED)
cb1c46c5
KN
224{
225 scm_wrong_num_args (smob);
226}
227
cb1c46c5
KN
228static SCM
229scm_smob_apply_2_030 (SCM smob, SCM a1, SCM a2)
230{
231 return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_UNDEFINED);
232}
233
234static SCM
235scm_smob_apply_2_001 (SCM smob, SCM a1, SCM a2)
236{
1afff620 237 return SCM_SMOB_APPLY1 (smob, scm_list_2 (a1, a2));
7c58e21b 238}
cb1c46c5
KN
239
240static SCM
241scm_smob_apply_2_011 (SCM smob, SCM a1, SCM a2)
242{
1afff620 243 return SCM_SMOB_APPLY2 (smob, a1, scm_list_1 (a2));
cb1c46c5
KN
244}
245
246static SCM
247scm_smob_apply_2_021 (SCM smob, SCM a1, SCM a2)
248{
249 return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_EOL);
250}
251
252static SCM
e81d98ec 253scm_smob_apply_2_error (SCM smob, SCM a1 SCM_UNUSED, SCM a2 SCM_UNUSED)
cb1c46c5
KN
254{
255 scm_wrong_num_args (smob);
256}
257
258static SCM
259scm_smob_apply_3_030 (SCM smob, SCM a1, SCM a2, SCM rst)
260{
261 if (!SCM_NULLP (SCM_CDR (rst)))
262 scm_wrong_num_args (smob);
263 return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_CAR (rst));
264}
265
266static SCM
267scm_smob_apply_3_001 (SCM smob, SCM a1, SCM a2, SCM rst)
268{
269 return SCM_SMOB_APPLY1 (smob, scm_cons2 (a1, a2, rst));
0717dfd8
KN
270}
271
cb1c46c5
KN
272static SCM
273scm_smob_apply_3_011 (SCM smob, SCM a1, SCM a2, SCM rst)
274{
275 return SCM_SMOB_APPLY2 (smob, a1, scm_cons (a2, rst));
276}
277
278static SCM
279scm_smob_apply_3_021 (SCM smob, SCM a1, SCM a2, SCM rst)
280{
281 return SCM_SMOB_APPLY3 (smob, a1, a2, rst);
282}
283
284static SCM
e81d98ec
DH
285scm_smob_apply_3_error (SCM smob,
286 SCM a1 SCM_UNUSED,
287 SCM a2 SCM_UNUSED,
288 SCM rst SCM_UNUSED)
cb1c46c5
KN
289{
290 scm_wrong_num_args (smob);
291}
292
293\f
7a7f7c53 294
92c2555f 295scm_t_bits
1be6b49c 296scm_make_smob_type (char *name, size_t size)
7a7f7c53 297#define FUNC_NAME "scm_make_smob_type"
0f2d19dd 298{
c014a02e 299 long new_smob;
7a7f7c53
DH
300
301 SCM_ENTER_A_SECTION; /* scm_numsmob */
302 new_smob = scm_numsmob;
303 if (scm_numsmob != MAX_SMOB_COUNT)
304 ++scm_numsmob;
305 SCM_EXIT_A_SECTION;
306
307 if (new_smob == MAX_SMOB_COUNT)
308 scm_misc_error (FUNC_NAME, "maximum number of smobs exceeded", SCM_EOL);
309
310 scm_smobs[new_smob].name = name;
311 if (size != 0)
2500356c 312 {
7a7f7c53
DH
313 scm_smobs[new_smob].size = size;
314 scm_smobs[new_smob].free = scm_smob_free;
2500356c 315 }
7a7f7c53 316
d7ec6b9f
MD
317 /* Make a class object if Goops is present. */
318 if (scm_smob_class)
74b6d6e4 319 scm_smob_class[new_smob] = scm_make_extended_class (name, 0);
7a7f7c53
DH
320
321 return scm_tc7_smob + new_smob * 256;
0f2d19dd 322}
7a7f7c53
DH
323#undef FUNC_NAME
324
0f2d19dd 325
9dd5943c 326void
92c2555f 327scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM))
9dd5943c
MD
328{
329 scm_smobs[SCM_TC2SMOBNUM (tc)].mark = mark;
330}
331
332void
92c2555f 333scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM))
9dd5943c
MD
334{
335 scm_smobs[SCM_TC2SMOBNUM (tc)].free = free;
336}
337
338void
92c2555f 339scm_set_smob_print (scm_t_bits tc, int (*print) (SCM, SCM, scm_print_state*))
9dd5943c
MD
340{
341 scm_smobs[SCM_TC2SMOBNUM (tc)].print = print;
342}
343
344void
92c2555f 345scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM))
9dd5943c
MD
346{
347 scm_smobs[SCM_TC2SMOBNUM (tc)].equalp = equalp;
348}
349
0717dfd8 350void
92c2555f 351scm_set_smob_apply (scm_t_bits tc, SCM (*apply) (),
7c58e21b 352 unsigned int req, unsigned int opt, unsigned int rst)
0717dfd8 353{
cb1c46c5
KN
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);
359
7c58e21b 360 if (rst > 1 || req + opt + rst > 3)
cb1c46c5
KN
361 {
362 puts ("Unsupported smob application type");
363 abort ();
364 }
365
366 switch (type)
367 {
368 case SCM_GSUBR_MAKTYPE (0, 0, 0):
7c58e21b 369 apply_0 = apply; break;
cb1c46c5
KN
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;
382 default:
383 apply_0 = scm_smob_apply_0_error; break;
384 }
385
386 switch (type)
387 {
388 case SCM_GSUBR_MAKTYPE (1, 0, 0):
389 case SCM_GSUBR_MAKTYPE (0, 1, 0):
7c58e21b 390 apply_1 = apply; break;
cb1c46c5
KN
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;
405 default:
406 apply_1 = scm_smob_apply_1_error; break;
407 }
408
409 switch (type)
410 {
411 case SCM_GSUBR_MAKTYPE (2, 0, 0):
412 case SCM_GSUBR_MAKTYPE (1, 1, 0):
413 case SCM_GSUBR_MAKTYPE (0, 2, 0):
7c58e21b 414 apply_2 = apply; break;
cb1c46c5
KN
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;
428 default:
429 apply_2 = scm_smob_apply_2_error; break;
430 }
431
432 switch (type)
433 {
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;
448 default:
449 apply_3 = scm_smob_apply_3_error; break;
450 }
451
03416a99 452 scm_smobs[SCM_TC2SMOBNUM (tc)].apply = apply;
cb1c46c5
KN
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;
68b06924 457 scm_smobs[SCM_TC2SMOBNUM (tc)].gsubr_type = type;
74b6d6e4
MD
458
459 if (scm_smob_class)
460 scm_i_inherit_applicable (scm_smob_class[SCM_TC2SMOBNUM (tc)]);
0717dfd8
KN
461}
462
9dd5943c 463SCM
92c2555f 464scm_make_smob (scm_t_bits tc)
9dd5943c 465{
c014a02e 466 long n = SCM_TC2SMOBNUM (tc);
1be6b49c 467 size_t size = scm_smobs[n].size;
16d4699b 468 scm_t_bits data = (size > 0
4c9419ac 469 ? (scm_t_bits) scm_gc_malloc (size, SCM_SMOBNAME (n))
16d4699b 470 : 0);
228a24ef 471 return scm_cell (tc, data);
9dd5943c
MD
472}
473
ceef3208 474\f
0f2d19dd
JB
475/* {Initialization for i/o types, float, bignum, the type of free cells}
476 */
477
ceef3208 478static int
e81d98ec 479free_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
ceef3208
JB
480{
481 char buf[100];
e841c3e0
KN
482 sprintf (buf, "#<freed cell %p; GC missed a reference>",
483 (void *) SCM_UNPACK (exp));
ceef3208 484 scm_puts (buf, port);
1e71eafb
HWN
485
486#if (SCM_DEBUG_CELL_ACCESSES == 1)
487 if (scm_debug_cell_accesses_p)
488 abort();
489#endif
490
ceef3208
JB
491
492 return 1;
493}
494
0f2d19dd
JB
495void
496scm_smob_prehistory ()
0f2d19dd 497{
c014a02e 498 long i;
92c2555f 499 scm_t_bits tc;
e841c3e0 500
0f2d19dd 501 scm_numsmob = 0;
7a7f7c53
DH
502 for (i = 0; i < MAX_SMOB_COUNT; ++i)
503 {
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;
516 }
9dd5943c
MD
517
518 /* WARNING: These scm_make_smob_type calls must be done in this order */
e841c3e0
KN
519 tc = scm_make_smob_type ("free", 0);
520 scm_set_smob_print (tc, free_print);
23a62151 521
7a7f7c53 522 tc = scm_make_smob_type ("big", 0); /* freed in gc */
e841c3e0
KN
523 scm_set_smob_print (tc, scm_bigprint);
524 scm_set_smob_equalp (tc, scm_bigequal);
23a62151 525
e841c3e0
KN
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);
16d35552 529
7a7f7c53 530 tc = scm_make_smob_type ("complex", 0); /* freed in gc */
e841c3e0
KN
531 scm_set_smob_print (tc, scm_print_complex);
532 scm_set_smob_equalp (tc, scm_complex_equalp);
0f2d19dd 533}
89e00824
ML
534
535/*
536 Local Variables:
537 c-file-style: "gnu"
538 End:
539*/