make applicable smob calls cheaper, and fix a memory leak
[bpt/guile.git] / libguile / smob.h
1 /* classes: h_files */
2
3 #ifndef SCM_SMOB_H
4 #define SCM_SMOB_H
5
6 /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2004, 2006, 2009,
7 * 2010, 2011, 2012 Free Software Foundation, Inc.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 */
24
25 \f
26
27 #include "libguile/__scm.h"
28 #include "libguile/print.h"
29
30
31 \f
32 /* This is the internal representation of a smob type */
33
34 typedef struct scm_smob_descriptor
35 {
36 char const *name;
37 size_t size;
38 SCM (*mark) (SCM);
39 size_t (*free) (SCM);
40 int (*print) (SCM exp, SCM port, scm_print_state *pstate);
41 SCM (*equalp) (SCM, SCM);
42 scm_t_subr apply;
43 /* In 2.2 this field is renamed to "apply_trampoline". */
44 SCM apply_trampoline_objcode;
45 } scm_smob_descriptor;
46
47
48 #define SCM_SMOB_TYPE_MASK 0xffff
49 #define SCM_SMOB_TYPE_BITS(tc) (tc)
50 #define SCM_TC2SMOBNUM(x) (0x0ff & ((x) >> 8))
51 #define SCM_SMOBNUM(x) (SCM_TC2SMOBNUM (SCM_CELL_TYPE (x)))
52 /* SCM_SMOBNAME can be 0 if name is missing */
53 #define SCM_SMOBNAME(smobnum) (scm_smobs[smobnum].name)
54 #define SCM_SMOB_PREDICATE(tag, obj) SCM_TYP16_PREDICATE (tag, obj)
55 #define SCM_SMOB_DESCRIPTOR(x) (scm_smobs[SCM_SMOBNUM (x)])
56 #define SCM_SMOB_APPLICABLE_P(x) (SCM_SMOB_DESCRIPTOR (x).apply)
57
58 /* Maximum number of SMOB types. */
59 #define SCM_I_MAX_SMOB_TYPE_COUNT 256
60
61 SCM_API long scm_numsmob;
62 SCM_API scm_smob_descriptor scm_smobs[];
63
64
65 \f
66
67 SCM_API SCM scm_i_new_smob (scm_t_bits tc, scm_t_bits);
68 SCM_API SCM scm_i_new_double_smob (scm_t_bits tc, scm_t_bits,
69 scm_t_bits, scm_t_bits);
70
71
72 SCM_INLINE SCM scm_new_smob (scm_t_bits tc, scm_t_bits);
73 SCM_INLINE SCM scm_new_double_smob (scm_t_bits tc, scm_t_bits,
74 scm_t_bits, scm_t_bits);
75
76 /* These two are internal details of the previous implementation of
77 SCM_NEWSMOB and are no longer used. They are still here to preserve
78 ABI stability in the 2.0 series. */
79 SCM_API void scm_i_finalize_smob (GC_PTR ptr, GC_PTR data);
80 SCM_API SCM scm_i_new_smob_with_mark_proc (scm_t_bits tc, scm_t_bits,
81 scm_t_bits, scm_t_bits);
82
83
84 #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
85 SCM_INLINE_IMPLEMENTATION SCM
86 scm_new_smob (scm_t_bits tc, scm_t_bits data)
87 {
88 scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
89
90 if (SCM_UNLIKELY (scm_smobs[smobnum].mark || scm_smobs[smobnum].free))
91 return scm_i_new_smob (tc, data);
92 else
93 return scm_cell (tc, data);
94 }
95
96 SCM_INLINE_IMPLEMENTATION SCM
97 scm_new_double_smob (scm_t_bits tc, scm_t_bits data1,
98 scm_t_bits data2, scm_t_bits data3)
99 {
100 scm_t_bits smobnum = SCM_TC2SMOBNUM (tc);
101
102 if (SCM_UNLIKELY (scm_smobs[smobnum].mark || scm_smobs[smobnum].free))
103 return scm_i_new_double_smob (tc, data1, data2, data3);
104 else
105 return scm_double_cell (tc, data1, data2, data3);
106 }
107 #endif
108
109 #define SCM_NEWSMOB(z, tc, data) \
110 z = scm_new_smob ((tc), (scm_t_bits)(data))
111 #define SCM_RETURN_NEWSMOB(tc, data) \
112 return scm_new_smob ((tc), (scm_t_bits)(data))
113
114 #define SCM_NEWSMOB2(z, tc, data1, data2) \
115 z = scm_new_double_smob ((tc), (scm_t_bits)(data1), \
116 (scm_t_bits)(data2), 0)
117 #define SCM_RETURN_NEWSMOB2(tc, data1, data2) \
118 return scm_new_double_smob ((tc), (scm_t_bits)(data1), \
119 (scm_t_bits)(data2), 0)
120
121 #define SCM_NEWSMOB3(z, tc, data1, data2, data3) \
122 z = scm_new_double_smob ((tc), (scm_t_bits)(data1), \
123 (scm_t_bits)(data2), (scm_t_bits)(data3))
124 #define SCM_RETURN_NEWSMOB3(tc, data1, data2, data3) \
125 return scm_new_double_smob ((tc), (scm_t_bits)(data1), \
126 (scm_t_bits)(data2), (scm_t_bits)(data3))
127
128 \f
129
130 #define SCM_SMOB_DATA_N(x, n) (SCM_CELL_WORD ((x), (n)))
131 #define SCM_SET_SMOB_DATA_N(x, n, data) (SCM_SET_CELL_WORD ((x), (n), (data)))
132
133 #define SCM_SMOB_DATA_0(x) (SCM_SMOB_DATA_N ((x), 0))
134 #define SCM_SMOB_DATA_1(x) (SCM_SMOB_DATA_N ((x), 1))
135 #define SCM_SMOB_DATA_2(x) (SCM_SMOB_DATA_N ((x), 2))
136 #define SCM_SMOB_DATA_3(x) (SCM_SMOB_DATA_N ((x), 3))
137 #define SCM_SET_SMOB_DATA_0(x, data) (SCM_SET_SMOB_DATA_N ((x), 0, (data)))
138 #define SCM_SET_SMOB_DATA_1(x, data) (SCM_SET_SMOB_DATA_N ((x), 1, (data)))
139 #define SCM_SET_SMOB_DATA_2(x, data) (SCM_SET_SMOB_DATA_N ((x), 2, (data)))
140 #define SCM_SET_SMOB_DATA_3(x, data) (SCM_SET_SMOB_DATA_N ((x), 3, (data)))
141
142 #define SCM_SMOB_FLAGS(x) (SCM_SMOB_DATA_0 (x) >> 16)
143 #define SCM_SMOB_DATA(x) (SCM_SMOB_DATA_1 (x))
144 #define SCM_SET_SMOB_FLAGS(x, data) (SCM_SET_SMOB_DATA_0 ((x), (SCM_CELL_TYPE (x)&0xffff)|((data)<<16)))
145 #define SCM_SET_SMOB_DATA(x, data) (SCM_SET_SMOB_DATA_1 ((x), (data)))
146
147 #define SCM_SMOB_OBJECT_N(x,n) (SCM_CELL_OBJECT ((x), (n)))
148 #define SCM_SET_SMOB_OBJECT_N(x,n,obj) (SCM_SET_CELL_OBJECT ((x), (n), (obj)))
149 #define SCM_SMOB_OBJECT_N_LOC(x,n) (SCM_CELL_OBJECT_LOC ((x), (n)))
150
151 /*#define SCM_SMOB_OBJECT_0(x) (SCM_SMOB_OBJECT_N ((x), 0))*/
152 #define SCM_SMOB_OBJECT_1(x) (SCM_SMOB_OBJECT_N ((x), 1))
153 #define SCM_SMOB_OBJECT_2(x) (SCM_SMOB_OBJECT_N ((x), 2))
154 #define SCM_SMOB_OBJECT_3(x) (SCM_SMOB_OBJECT_N ((x), 3))
155 /*#define SCM_SET_SMOB_OBJECT_0(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 0, (obj)))*/
156 #define SCM_SET_SMOB_OBJECT_1(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 1, (obj)))
157 #define SCM_SET_SMOB_OBJECT_2(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 2, (obj)))
158 #define SCM_SET_SMOB_OBJECT_3(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 3, (obj)))
159 #define SCM_SMOB_OBJECT_0_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 0)))
160 #define SCM_SMOB_OBJECT_1_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 1)))
161 #define SCM_SMOB_OBJECT_2_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 2)))
162 #define SCM_SMOB_OBJECT_3_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 3)))
163
164 #define SCM_SMOB_OBJECT(x) (SCM_SMOB_OBJECT_1 (x))
165 #define SCM_SET_SMOB_OBJECT(x,obj) (SCM_SET_SMOB_OBJECT_1 ((x), (obj)))
166 #define SCM_SMOB_OBJECT_LOC(x) (SCM_SMOB_OBJECT_1_LOC (x)))
167
168
169 #define SCM_SMOB_APPLY_0(x) (scm_call_0 (x))
170 #define SCM_SMOB_APPLY_1(x, a1) (scm_call_1 (x, a1))
171 #define SCM_SMOB_APPLY_2(x, a1, a2) (scm_call_2 (x, a1, a2))
172 #define SCM_SMOB_APPLY_3(x, a1, a2, rst) (scm_call_3 (x, a1, a2, a3))
173
174 \f
175
176 SCM_API SCM scm_mark0 (SCM ptr);
177 SCM_API SCM scm_markcdr (SCM ptr);
178 SCM_API size_t scm_free0 (SCM ptr);
179 SCM_API int scm_smob_print (SCM exp, SCM port, scm_print_state *pstate);
180
181 /* The following set of functions is the standard way to create new
182 * SMOB types.
183 *
184 * Create a type tag using `scm_make_smob_type', accept default values
185 * for mark, free, print and/or equalp functions, or set your own
186 * values using `scm_set_smob_xxx'.
187 */
188
189 SCM_API scm_t_bits scm_make_smob_type (char const *name, size_t size);
190
191 SCM_API void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM));
192 SCM_API void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM));
193 SCM_API void scm_set_smob_print (scm_t_bits tc,
194 int (*print) (SCM, SCM, scm_print_state*));
195 SCM_API void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
196 SCM_API void scm_set_smob_apply (scm_t_bits tc,
197 scm_t_subr apply,
198 unsigned int req,
199 unsigned int opt,
200 unsigned int rst);
201
202 SCM_API void scm_assert_smob_type (scm_t_bits tag, SCM val);
203
204 /* Function for creating smobs */
205
206 SCM_API SCM scm_make_smob (scm_t_bits tc);
207
208 SCM_API void scm_smob_prehistory (void);
209
210 #endif /* SCM_SMOB_H */
211
212 /*
213 Local Variables:
214 c-file-style: "gnu"
215 End:
216 */