* Removed some unused identifiers and commented code.
[bpt/guile.git] / libguile / smob.c
CommitLineData
16d35552 1/* Copyright (C) 1995, 1996, 1998, 1999, 2000 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
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
47#include <stdio.h>
a0599745 48#include "libguile/_scm.h"
20e6290e 49
a0599745
MD
50#include "libguile/objects.h"
51#include "libguile/ports.h"
d7ec6b9f 52
0f2d19dd
JB
53#ifdef HAVE_MALLOC_H
54#include <malloc.h>
55#endif
56
a0599745 57#include "libguile/smob.h"
9dd5943c 58
0f2d19dd
JB
59\f
60
61/* scm_smobs scm_numsmob
62 * implement a dynamicly resized array of smob records.
63 * Indexes into this table are used when generating type
64 * tags for smobjects (if you know a tag you can get an index and conversely).
65 */
4e6e2119 66int scm_numsmob;
9dd5943c 67scm_smob_descriptor *scm_smobs;
0f2d19dd 68
9dd5943c
MD
69/* {Mark}
70 */
71
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
76 need scm_mark0.
77
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. */
81
82SCM
6e8d25a6 83scm_mark0 (SCM ptr)
9dd5943c
MD
84{
85 return SCM_BOOL_F;
86}
87
88SCM
6e8d25a6 89scm_markcdr (SCM ptr)
9dd5943c
MD
90{
91 return SCM_CDR (ptr);
92}
93
94/* {Free}
95 */
96
97scm_sizet
6e8d25a6 98scm_free0 (SCM ptr)
9dd5943c
MD
99{
100 return 0;
101}
102
103scm_sizet
104scm_smob_free (SCM obj)
105{
54778cd3 106 scm_must_free ((char *) SCM_CELL_WORD_1 (obj));
9dd5943c
MD
107 return scm_smobs[SCM_SMOBNUM (obj)].size;
108}
109
110/* {Print}
111 */
112
113int
114scm_smob_print (SCM exp, SCM port, scm_print_state *pstate)
115{
116 int n = SCM_SMOBNUM (exp);
117 scm_puts ("#<", port);
2c16a78a 118 scm_puts (SCM_SMOBNAME (n) ? SCM_SMOBNAME (n) : "smob", port);
9dd5943c 119 scm_putc (' ', port);
f1267706 120 scm_intprint (SCM_UNPACK (scm_smobs[n].size ? SCM_CDR (exp) : exp), 16, port);
9dd5943c
MD
121 scm_putc ('>', port);
122 return 1;
123}
1cc91f1b 124
0f2d19dd 125long
9dd5943c 126scm_make_smob_type (char *name, scm_sizet size)
0f2d19dd
JB
127{
128 char *tmp;
129 if (255 <= scm_numsmob)
130 goto smoberr;
131 SCM_DEFER_INTS;
9dd5943c
MD
132 SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_smobs,
133 (1 + scm_numsmob)
134 * sizeof (scm_smob_descriptor)));
0f2d19dd
JB
135 if (tmp)
136 {
9dd5943c
MD
137 scm_smobs = (scm_smob_descriptor *) tmp;
138 scm_smobs[scm_numsmob].name = name;
139 scm_smobs[scm_numsmob].size = size;
140 scm_smobs[scm_numsmob].mark = 0;
141 scm_smobs[scm_numsmob].free = (size == 0 ? scm_free0 : scm_smob_free);
142 scm_smobs[scm_numsmob].print = scm_smob_print;
143 scm_smobs[scm_numsmob].equalp = 0;
0f2d19dd
JB
144 scm_numsmob++;
145 }
146 SCM_ALLOW_INTS;
147 if (!tmp)
9dd5943c
MD
148 smoberr:scm_wta (SCM_MAKINUM ((long) scm_numsmob),
149 (char *) SCM_NALLOC, "scm_make_smob_type");
d7ec6b9f
MD
150 /* Make a class object if Goops is present. */
151 if (scm_smob_class)
152 scm_smob_class[scm_numsmob - 1]
153 = scm_make_extended_class (SCM_SMOBNAME (scm_numsmob - 1));
0f2d19dd
JB
154 return scm_tc7_smob + (scm_numsmob - 1) * 256;
155}
156
23a62151
MD
157long
158scm_make_smob_type_mfpe (char *name, scm_sizet size,
159 SCM (*mark) (SCM),
160 scm_sizet (*free) (SCM),
161 int (*print) (SCM, SCM, scm_print_state *),
162 SCM (*equalp) (SCM, SCM))
163{
164 long answer = scm_make_smob_type (name, size);
165 scm_set_smob_mfpe (answer, mark, free, print, equalp);
166 return answer;
167}
168
9dd5943c
MD
169void
170scm_set_smob_mark (long tc, SCM (*mark) (SCM))
171{
172 scm_smobs[SCM_TC2SMOBNUM (tc)].mark = mark;
173}
174
175void
176scm_set_smob_free (long tc, scm_sizet (*free) (SCM))
177{
178 scm_smobs[SCM_TC2SMOBNUM (tc)].free = free;
179}
180
181void
182scm_set_smob_print (long tc, int (*print) (SCM, SCM, scm_print_state*))
183{
184 scm_smobs[SCM_TC2SMOBNUM (tc)].print = print;
185}
186
187void
188scm_set_smob_equalp (long tc, SCM (*equalp) (SCM, SCM))
189{
190 scm_smobs[SCM_TC2SMOBNUM (tc)].equalp = equalp;
191}
192
23a62151
MD
193void
194scm_set_smob_mfpe (long tc,
195 SCM (*mark) (SCM),
196 scm_sizet (*free) (SCM),
197 int (*print) (SCM, SCM, scm_print_state *),
198 SCM (*equalp) (SCM, SCM))
199{
200 if (mark) scm_set_smob_mark (tc, mark);
201 if (free) scm_set_smob_free (tc, free);
202 if (print) scm_set_smob_print (tc, print);
203 if (equalp) scm_set_smob_equalp (tc, equalp);
204}
205
f5f2dcff 206
9dd5943c
MD
207SCM
208scm_make_smob (long tc)
209{
210 int n = SCM_TC2SMOBNUM (tc);
211 scm_sizet size = scm_smobs[n].size;
212 SCM z;
213 SCM_NEWCELL (z);
214 if (size != 0)
215 {
216#if 0
217 SCM_ASSERT (scm_smobs[n].mark == 0,
218 0,
219 "forbidden operation for smobs with GC data, use SCM_NEWSMOB",
220 SCM_SMOBNAME (n));
221#endif
222 SCM_SET_SMOB_DATA (z, scm_must_malloc (size, SCM_SMOBNAME (n)));
223 }
54778cd3 224 SCM_SET_CELL_TYPE (z, tc);
9dd5943c
MD
225 return z;
226}
227
ceef3208 228\f
0f2d19dd
JB
229/* {Initialization for i/o types, float, bignum, the type of free cells}
230 */
231
ceef3208
JB
232static int
233freeprint (SCM exp,
234 SCM port,
235 scm_print_state *pstate)
236{
237 char buf[100];
238
54778cd3 239 sprintf (buf, "#<freed cell %p; GC missed a reference>", (void *) SCM_UNPACK (exp));
ceef3208
JB
240 scm_puts (buf, port);
241
242 return 1;
243}
244
245
0f2d19dd
JB
246void
247scm_smob_prehistory ()
0f2d19dd
JB
248{
249 scm_numsmob = 0;
9dd5943c
MD
250 scm_smobs = ((scm_smob_descriptor *)
251 malloc (7 * sizeof (scm_smob_descriptor)));
252
253 /* WARNING: These scm_make_smob_type calls must be done in this order */
23a62151
MD
254 scm_make_smob_type_mfpe ("free", 0,
255 NULL, NULL, freeprint, NULL);
256
16d35552 257 scm_make_smob_type_mfpe ("big", 0, /* freed in gc */
23a62151
MD
258 NULL, NULL, scm_bigprint, scm_bigequal);
259
16d35552
MD
260 scm_make_smob_type_mfpe ("real", 0, /* freed in gc */
261 NULL, NULL, scm_print_real, scm_real_equalp);
262
263 scm_make_smob_type_mfpe ("complex", 0, /* freed in gc */
264 NULL, NULL, scm_print_complex, scm_complex_equalp);
1bbd0b84 265
16d35552 266 scm_make_smob_type ("allocated", 0);
0f2d19dd 267}
89e00824
ML
268
269/*
270 Local Variables:
271 c-file-style: "gnu"
272 End:
273*/