Commit | Line | Data |
---|---|---|
0f2d19dd JB |
1 | /* classes: h_files */ |
2 | ||
7a7f7c53 DH |
3 | #ifndef SCM_SMOB_H |
4 | #define SCM_SMOB_H | |
dee01b01 | 5 | |
be90d0b6 | 6 | /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2004, 2006, 2009, |
c46fee43 | 7 | * 2010, 2011, 2012 Free Software Foundation, Inc. |
dee01b01 | 8 | * |
73be1d9e | 9 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
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. | |
dee01b01 | 13 | * |
53befeb7 NJ |
14 | * This library is distributed in the hope that it will be useful, but |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | * Lesser General Public License for more details. | |
dee01b01 | 18 | * |
73be1d9e MV |
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 | |
53befeb7 NJ |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 | * 02110-1301 USA | |
73be1d9e | 23 | */ |
dee01b01 | 24 | |
0f2d19dd | 25 | \f |
dee01b01 | 26 | |
b4309c3c | 27 | #include "libguile/__scm.h" |
b08323d1 | 28 | #include "libguile/print.h" |
0f2d19dd | 29 | |
10fb3386 | 30 | |
0f2d19dd | 31 | \f |
9dd5943c MD |
32 | /* This is the internal representation of a smob type */ |
33 | ||
34 | typedef struct scm_smob_descriptor | |
35 | { | |
da0e6c2b | 36 | char const *name; |
1be6b49c | 37 | size_t size; |
7866a09b | 38 | SCM (*mark) (SCM); |
1be6b49c | 39 | size_t (*free) (SCM); |
7866a09b GB |
40 | int (*print) (SCM exp, SCM port, scm_print_state *pstate); |
41 | SCM (*equalp) (SCM, SCM); | |
be90d0b6 | 42 | scm_t_subr apply; |
c05805a4 | 43 | /* In 2.2 this field is renamed to "apply_trampoline". */ |
75c3ed28 | 44 | SCM apply_trampoline_objcode; |
9dd5943c MD |
45 | } scm_smob_descriptor; |
46 | ||
378f2625 | 47 | |
c46fee43 AW |
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 | ||
0f2d19dd | 65 | \f |
87621a14 | 66 | |
c46fee43 AW |
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. */ | |
0f6dd250 | 79 | SCM_API void scm_i_finalize_smob (void *ptr, void *data); |
c46fee43 AW |
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 | |
9dd5943c | 129 | |
56164dc4 AW |
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 | ||
37fc18ae | 168 | |
75c3ed28 AW |
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)) | |
0f2d19dd | 173 | |
0f2d19dd | 174 | \f |
0f2d19dd | 175 | |
33b001fd MV |
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); | |
33b001fd | 179 | SCM_API int scm_smob_print (SCM exp, SCM port, scm_print_state *pstate); |
23a62151 | 180 | |
c72baaaa MD |
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 | */ | |
23a62151 | 188 | |
df5af69a | 189 | SCM_API scm_t_bits scm_make_smob_type (char const *name, size_t size); |
23a62151 | 190 | |
33b001fd MV |
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, | |
be90d0b6 | 197 | scm_t_subr apply, |
33b001fd MV |
198 | unsigned int req, |
199 | unsigned int opt, | |
200 | unsigned int rst); | |
c72baaaa | 201 | |
197b0573 MV |
202 | SCM_API void scm_assert_smob_type (scm_t_bits tag, SCM val); |
203 | ||
7c58e21b | 204 | /* Function for creating smobs */ |
c72baaaa | 205 | |
33b001fd | 206 | SCM_API SCM scm_make_smob (scm_t_bits tc); |
197b0573 | 207 | |
33b001fd | 208 | SCM_API void scm_smob_prehistory (void); |
7c58e21b | 209 | |
7a7f7c53 | 210 | #endif /* SCM_SMOB_H */ |
89e00824 ML |
211 | |
212 | /* | |
213 | Local Variables: | |
214 | c-file-style: "gnu" | |
215 | End: | |
216 | */ |