1 /* Copyright (C) 1995, 1996, 1998, 1999 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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
60 /* scm_smobs scm_numsmob
61 * implement a dynamicly resized array of smob records.
62 * Indexes into this table are used when generating type
63 * tags for smobjects (if you know a tag you can get an index and conversely).
66 scm_smob_descriptor
*scm_smobs
;
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. */
103 scm_smob_free (SCM obj
)
105 scm_must_free ((char *) SCM_CDR (obj
));
106 return scm_smobs
[SCM_SMOBNUM (obj
)].size
;
113 scm_smob_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
115 int n
= SCM_SMOBNUM (exp
);
116 scm_puts ("#<", port
);
117 scm_puts (SCM_SMOBNAME (n
) ? SCM_SMOBNAME (n
) : "smob", port
);
118 scm_putc (' ', port
);
119 scm_intprint (scm_smobs
[n
].size
? SCM_CDR (exp
) : exp
, 16, port
);
120 scm_putc ('>', port
);
125 scm_make_smob_type (char *name
, scm_sizet size
)
128 if (255 <= scm_numsmob
)
131 SCM_SYSCALL (tmp
= (char *) realloc ((char *) scm_smobs
,
133 * sizeof (scm_smob_descriptor
)));
136 scm_smobs
= (scm_smob_descriptor
*) tmp
;
137 scm_smobs
[scm_numsmob
].name
= name
;
138 scm_smobs
[scm_numsmob
].size
= size
;
139 scm_smobs
[scm_numsmob
].mark
= 0;
140 scm_smobs
[scm_numsmob
].free
= (size
== 0 ? scm_free0
: scm_smob_free
);
141 scm_smobs
[scm_numsmob
].print
= scm_smob_print
;
142 scm_smobs
[scm_numsmob
].equalp
= 0;
147 smoberr
:scm_wta (SCM_MAKINUM ((long) scm_numsmob
),
148 (char *) SCM_NALLOC
, "scm_make_smob_type");
149 /* Make a class object if Goops is present. */
151 scm_smob_class
[scm_numsmob
- 1]
152 = scm_make_extended_class (SCM_SMOBNAME (scm_numsmob
- 1));
153 return scm_tc7_smob
+ (scm_numsmob
- 1) * 256;
157 scm_make_smob_type_mfpe (char *name
, scm_sizet size
,
159 scm_sizet (*free
) (SCM
),
160 int (*print
) (SCM
, SCM
, scm_print_state
*),
161 SCM (*equalp
) (SCM
, SCM
))
163 long answer
= scm_make_smob_type (name
, size
);
164 scm_set_smob_mfpe (answer
, mark
, free
, print
, equalp
);
169 scm_set_smob_mark (long tc
, SCM (*mark
) (SCM
))
171 scm_smobs
[SCM_TC2SMOBNUM (tc
)].mark
= mark
;
175 scm_set_smob_free (long tc
, scm_sizet (*free
) (SCM
))
177 scm_smobs
[SCM_TC2SMOBNUM (tc
)].free
= free
;
181 scm_set_smob_print (long tc
, int (*print
) (SCM
, SCM
, scm_print_state
*))
183 scm_smobs
[SCM_TC2SMOBNUM (tc
)].print
= print
;
187 scm_set_smob_equalp (long tc
, SCM (*equalp
) (SCM
, SCM
))
189 scm_smobs
[SCM_TC2SMOBNUM (tc
)].equalp
= equalp
;
193 scm_set_smob_mfpe (long tc
,
195 scm_sizet (*free
) (SCM
),
196 int (*print
) (SCM
, SCM
, scm_print_state
*),
197 SCM (*equalp
) (SCM
, SCM
))
199 if (mark
) scm_set_smob_mark (tc
, mark
);
200 if (free
) scm_set_smob_free (tc
, free
);
201 if (print
) scm_set_smob_print (tc
, print
);
202 if (equalp
) scm_set_smob_equalp (tc
, equalp
);
205 /* Deprecated function - use scm_make_smob_type, or scm_make_smob_type_mfpe
208 scm_newsmob (const scm_smobfuns
*smob
)
210 long tc
= scm_make_smob_type (0, 0);
211 scm_set_smob_mark (tc
, smob
->mark
);
212 scm_set_smob_free (tc
, smob
->free
);
213 scm_set_smob_print (tc
, smob
->print
);
214 scm_set_smob_equalp (tc
, smob
->equalp
);
220 scm_make_smob (long tc
)
222 int n
= SCM_TC2SMOBNUM (tc
);
223 scm_sizet size
= scm_smobs
[n
].size
;
229 SCM_ASSERT (scm_smobs
[n
].mark
== 0,
231 "forbidden operation for smobs with GC data, use SCM_NEWSMOB",
234 SCM_SET_SMOB_DATA (z
, scm_must_malloc (size
, SCM_SMOBNAME (n
)));
241 /* {Initialization for i/o types, float, bignum, the type of free cells}
247 scm_print_state
*pstate
)
251 sprintf (buf
, "#<freed cell %p; GC missed a reference>", (void *) exp
);
252 scm_puts (buf
, port
);
259 scm_smob_prehistory ()
262 scm_smobs
= ((scm_smob_descriptor
*)
263 malloc (7 * sizeof (scm_smob_descriptor
)));
265 /* WARNING: These scm_make_smob_type calls must be done in this order */
266 scm_make_smob_type_mfpe ("free", 0,
267 NULL
, NULL
, freeprint
, NULL
);
269 scm_make_smob_type_mfpe ("flo", 0, /* freed in gc */
270 NULL
, NULL
, scm_floprint
, scm_floequal
);
272 scm_make_smob_type_mfpe ("bigpos", 0, /* freed in gc */
273 NULL
, NULL
, scm_bigprint
, scm_bigequal
);
275 scm_make_smob_type_mfpe ("bigneg", 0,
276 NULL
, NULL
, scm_bigprint
, scm_bigequal
);
278 scm_make_smob_type("allocated", 0);