6 /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2004, 2006, 2009,
7 * 2010, 2011 Free Software Foundation, Inc.
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.
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.
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
27 #include "libguile/__scm.h"
28 #include "libguile/print.h"
30 #include "libguile/bdw-gc.h"
34 /* This is the internal representation of a smob type */
36 typedef struct scm_smob_descriptor
42 int (*print
) (SCM exp
, SCM port
, scm_print_state
*pstate
);
43 SCM (*equalp
) (SCM
, SCM
);
45 SCM apply_trampoline_objcode
;
46 } scm_smob_descriptor
;
50 SCM_API SCM
scm_i_new_smob_with_mark_proc (scm_t_bits tc
,
51 scm_t_bits
, scm_t_bits
, scm_t_bits
);
55 #define SCM_NEWSMOB(z, tc, data) \
58 register scm_t_bits _smobnum = SCM_TC2SMOBNUM (tc); \
59 z = (scm_smobs[_smobnum].mark \
60 ? scm_i_new_smob_with_mark_proc ((tc), (scm_t_bits)(data), \
62 : scm_cell (tc, (scm_t_bits)(data))); \
63 if (scm_smobs[_smobnum].free) \
65 GC_finalization_proc _prev_finalizer; \
66 GC_PTR _prev_finalizer_data; \
68 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (z), scm_i_finalize_smob, \
71 &_prev_finalizer_data); \
76 #define SCM_RETURN_NEWSMOB(tc, data) \
77 do { SCM __SCM_smob_answer; \
78 SCM_NEWSMOB (__SCM_smob_answer, (tc), (data)); \
79 return __SCM_smob_answer; \
82 #define SCM_NEWSMOB2(z, tc, data1, data2) \
83 SCM_NEWSMOB3 (z, tc, data1, data2, 0)
85 #define SCM_RETURN_NEWSMOB2(tc, data1, data2) \
86 do { SCM __SCM_smob_answer; \
87 SCM_NEWSMOB2 (__SCM_smob_answer, (tc), (data1), (data2)); \
88 return __SCM_smob_answer; \
91 #define SCM_NEWSMOB3(z, tc, data1, data2, data3) \
94 register scm_t_bits _smobnum = SCM_TC2SMOBNUM (tc); \
95 z = (scm_smobs[_smobnum].mark \
96 ? scm_i_new_smob_with_mark_proc (tc, (scm_t_bits)(data1), \
97 (scm_t_bits)(data2), \
98 (scm_t_bits)(data3)) \
99 : scm_double_cell ((tc), (scm_t_bits)(data1), \
100 (scm_t_bits)(data2), \
101 (scm_t_bits)(data3))); \
102 if (scm_smobs[_smobnum].free) \
104 GC_finalization_proc _prev_finalizer; \
105 GC_PTR _prev_finalizer_data; \
107 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (z), scm_i_finalize_smob, \
110 &_prev_finalizer_data); \
115 #define SCM_RETURN_NEWSMOB3(tc, data1, data2, data3) \
116 do { SCM __SCM_smob_answer; \
117 SCM_NEWSMOB3 (__SCM_smob_answer, (tc), (data1), (data2), (data3)); \
118 return __SCM_smob_answer; \
122 #define SCM_SMOB_DATA_N(x, n) (SCM_CELL_WORD ((x), (n)))
123 #define SCM_SET_SMOB_DATA_N(x, n, data) (SCM_SET_CELL_WORD ((x), (n), (data)))
125 #define SCM_SMOB_DATA_0(x) (SCM_SMOB_DATA_N ((x), 0))
126 #define SCM_SMOB_DATA_1(x) (SCM_SMOB_DATA_N ((x), 1))
127 #define SCM_SMOB_DATA_2(x) (SCM_SMOB_DATA_N ((x), 2))
128 #define SCM_SMOB_DATA_3(x) (SCM_SMOB_DATA_N ((x), 3))
129 #define SCM_SET_SMOB_DATA_0(x, data) (SCM_SET_SMOB_DATA_N ((x), 0, (data)))
130 #define SCM_SET_SMOB_DATA_1(x, data) (SCM_SET_SMOB_DATA_N ((x), 1, (data)))
131 #define SCM_SET_SMOB_DATA_2(x, data) (SCM_SET_SMOB_DATA_N ((x), 2, (data)))
132 #define SCM_SET_SMOB_DATA_3(x, data) (SCM_SET_SMOB_DATA_N ((x), 3, (data)))
134 #define SCM_SMOB_FLAGS(x) (SCM_SMOB_DATA_0 (x) >> 16)
135 #define SCM_SMOB_DATA(x) (SCM_SMOB_DATA_1 (x))
136 #define SCM_SET_SMOB_FLAGS(x, data) (SCM_SET_SMOB_DATA_0 ((x), (SCM_CELL_TYPE (x)&0xffff)|((data)<<16)))
137 #define SCM_SET_SMOB_DATA(x, data) (SCM_SET_SMOB_DATA_1 ((x), (data)))
139 #define SCM_SMOB_OBJECT_N(x,n) (SCM_CELL_OBJECT ((x), (n)))
140 #define SCM_SET_SMOB_OBJECT_N(x,n,obj) (SCM_SET_CELL_OBJECT ((x), (n), (obj)))
141 #define SCM_SMOB_OBJECT_N_LOC(x,n) (SCM_CELL_OBJECT_LOC ((x), (n)))
143 /*#define SCM_SMOB_OBJECT_0(x) (SCM_SMOB_OBJECT_N ((x), 0))*/
144 #define SCM_SMOB_OBJECT_1(x) (SCM_SMOB_OBJECT_N ((x), 1))
145 #define SCM_SMOB_OBJECT_2(x) (SCM_SMOB_OBJECT_N ((x), 2))
146 #define SCM_SMOB_OBJECT_3(x) (SCM_SMOB_OBJECT_N ((x), 3))
147 /*#define SCM_SET_SMOB_OBJECT_0(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 0, (obj)))*/
148 #define SCM_SET_SMOB_OBJECT_1(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 1, (obj)))
149 #define SCM_SET_SMOB_OBJECT_2(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 2, (obj)))
150 #define SCM_SET_SMOB_OBJECT_3(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 3, (obj)))
151 #define SCM_SMOB_OBJECT_0_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 0)))
152 #define SCM_SMOB_OBJECT_1_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 1)))
153 #define SCM_SMOB_OBJECT_2_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 2)))
154 #define SCM_SMOB_OBJECT_3_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 3)))
156 #define SCM_SMOB_OBJECT(x) (SCM_SMOB_OBJECT_1 (x))
157 #define SCM_SET_SMOB_OBJECT(x,obj) (SCM_SET_SMOB_OBJECT_1 ((x), (obj)))
158 #define SCM_SMOB_OBJECT_LOC(x) (SCM_SMOB_OBJECT_1_LOC (x)))
161 #define SCM_SMOB_TYPE_MASK 0xffff
162 #define SCM_SMOB_TYPE_BITS(tc) (tc)
163 #define SCM_TC2SMOBNUM(x) (0x0ff & ((x) >> 8))
164 #define SCM_SMOBNUM(x) (SCM_TC2SMOBNUM (SCM_CELL_TYPE (x)))
165 /* SCM_SMOBNAME can be 0 if name is missing */
166 #define SCM_SMOBNAME(smobnum) (scm_smobs[smobnum].name)
167 #define SCM_SMOB_PREDICATE(tag, obj) SCM_TYP16_PREDICATE (tag, obj)
168 #define SCM_SMOB_DESCRIPTOR(x) (scm_smobs[SCM_SMOBNUM (x)])
169 #define SCM_SMOB_APPLICABLE_P(x) (SCM_SMOB_DESCRIPTOR (x).apply)
170 #define SCM_SMOB_APPLY_0(x) (scm_call_0 (x))
171 #define SCM_SMOB_APPLY_1(x, a1) (scm_call_1 (x, a1))
172 #define SCM_SMOB_APPLY_2(x, a1, a2) (scm_call_2 (x, a1, a2))
173 #define SCM_SMOB_APPLY_3(x, a1, a2, rst) (scm_call_3 (x, a1, a2, a3))
175 /* Maximum number of SMOB types. */
176 #define SCM_I_MAX_SMOB_TYPE_COUNT 256
178 SCM_API
long scm_numsmob
;
179 SCM_API scm_smob_descriptor scm_smobs
[];
181 SCM_API
void scm_i_finalize_smob (GC_PTR obj
, GC_PTR data
);
185 SCM_API SCM
scm_mark0 (SCM ptr
);
186 SCM_API SCM
scm_markcdr (SCM ptr
);
187 SCM_API
size_t scm_free0 (SCM ptr
);
188 SCM_API
int scm_smob_print (SCM exp
, SCM port
, scm_print_state
*pstate
);
190 /* The following set of functions is the standard way to create new
193 * Create a type tag using `scm_make_smob_type', accept default values
194 * for mark, free, print and/or equalp functions, or set your own
195 * values using `scm_set_smob_xxx'.
198 SCM_API scm_t_bits
scm_make_smob_type (char const *name
, size_t size
);
200 SCM_API
void scm_set_smob_mark (scm_t_bits tc
, SCM (*mark
) (SCM
));
201 SCM_API
void scm_set_smob_free (scm_t_bits tc
, size_t (*free
) (SCM
));
202 SCM_API
void scm_set_smob_print (scm_t_bits tc
,
203 int (*print
) (SCM
, SCM
, scm_print_state
*));
204 SCM_API
void scm_set_smob_equalp (scm_t_bits tc
, SCM (*equalp
) (SCM
, SCM
));
205 SCM_API
void scm_set_smob_apply (scm_t_bits tc
,
211 SCM_API
void scm_assert_smob_type (scm_t_bits tag
, SCM val
);
213 /* Function for creating smobs */
215 SCM_API SCM
scm_make_smob (scm_t_bits tc
);
217 SCM_INTERNAL SCM
scm_i_smob_apply_trampoline (SCM smob
);
219 SCM_API
void scm_smob_prehistory (void);
221 #endif /* SCM_SMOB_H */