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, |
1bbf7f75 | 7 | * 2010, 2011, 2012, 2015 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; |
80be163f | 43 | SCM apply_trampoline; |
9dd5943c MD |
44 | } scm_smob_descriptor; |
45 | ||
378f2625 | 46 | |
27583e74 AW |
47 | #define SCM_SMOB_TYPE_MASK 0xffff |
48 | #define SCM_SMOB_TYPE_BITS(tc) (tc) | |
49 | #define SCM_TC2SMOBNUM(x) (0x0ff & ((x) >> 8)) | |
50 | #define SCM_SMOBNUM(x) (SCM_TC2SMOBNUM (SCM_CELL_TYPE (x))) | |
51 | /* SCM_SMOBNAME can be 0 if name is missing */ | |
52 | #define SCM_SMOBNAME(smobnum) (scm_smobs[smobnum].name) | |
dc7da0be | 53 | #define SCM_SMOB_PREDICATE(tag, obj) SCM_HAS_TYP16 (obj, tag) |
27583e74 AW |
54 | #define SCM_SMOB_DESCRIPTOR(x) (scm_smobs[SCM_SMOBNUM (x)]) |
55 | #define SCM_SMOB_APPLICABLE_P(x) (SCM_SMOB_DESCRIPTOR (x).apply) | |
56 | ||
57 | /* Maximum number of SMOB types. */ | |
58 | #define SCM_I_MAX_SMOB_TYPE_COUNT 256 | |
59 | ||
60 | SCM_API long scm_numsmob; | |
61 | SCM_API scm_smob_descriptor scm_smobs[]; | |
62 | ||
63 | ||
0f2d19dd | 64 | \f |
87621a14 | 65 | |
27583e74 AW |
66 | SCM_API SCM scm_i_new_smob (scm_t_bits tc, scm_t_bits); |
67 | SCM_API SCM scm_i_new_double_smob (scm_t_bits tc, scm_t_bits, | |
68 | scm_t_bits, scm_t_bits); | |
69 | ||
70 | ||
71 | SCM_INLINE SCM scm_new_smob (scm_t_bits tc, scm_t_bits); | |
72 | SCM_INLINE SCM scm_new_double_smob (scm_t_bits tc, scm_t_bits, | |
73 | scm_t_bits, scm_t_bits); | |
74 | ||
75 | #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES | |
76 | SCM_INLINE_IMPLEMENTATION SCM | |
77 | scm_new_smob (scm_t_bits tc, scm_t_bits data) | |
78 | { | |
79 | scm_t_bits smobnum = SCM_TC2SMOBNUM (tc); | |
80 | ||
81 | if (SCM_UNLIKELY (scm_smobs[smobnum].mark || scm_smobs[smobnum].free)) | |
82 | return scm_i_new_smob (tc, data); | |
83 | else | |
84 | return scm_cell (tc, data); | |
85 | } | |
86 | ||
87 | SCM_INLINE_IMPLEMENTATION SCM | |
88 | scm_new_double_smob (scm_t_bits tc, scm_t_bits data1, | |
89 | scm_t_bits data2, scm_t_bits data3) | |
90 | { | |
91 | scm_t_bits smobnum = SCM_TC2SMOBNUM (tc); | |
92 | ||
93 | if (SCM_UNLIKELY (scm_smobs[smobnum].mark || scm_smobs[smobnum].free)) | |
94 | return scm_i_new_double_smob (tc, data1, data2, data3); | |
95 | else | |
96 | return scm_double_cell (tc, data1, data2, data3); | |
97 | } | |
98 | #endif | |
99 | ||
100 | #define SCM_NEWSMOB(z, tc, data) \ | |
101 | z = scm_new_smob ((tc), (scm_t_bits)(data)) | |
102 | #define SCM_RETURN_NEWSMOB(tc, data) \ | |
103 | return scm_new_smob ((tc), (scm_t_bits)(data)) | |
104 | ||
105 | #define SCM_NEWSMOB2(z, tc, data1, data2) \ | |
106 | z = scm_new_double_smob ((tc), (scm_t_bits)(data1), \ | |
107 | (scm_t_bits)(data2), 0) | |
108 | #define SCM_RETURN_NEWSMOB2(tc, data1, data2) \ | |
109 | return scm_new_double_smob ((tc), (scm_t_bits)(data1), \ | |
110 | (scm_t_bits)(data2), 0) | |
111 | ||
112 | #define SCM_NEWSMOB3(z, tc, data1, data2, data3) \ | |
113 | z = scm_new_double_smob ((tc), (scm_t_bits)(data1), \ | |
114 | (scm_t_bits)(data2), (scm_t_bits)(data3)) | |
115 | #define SCM_RETURN_NEWSMOB3(tc, data1, data2, data3) \ | |
116 | return scm_new_double_smob ((tc), (scm_t_bits)(data1), \ | |
117 | (scm_t_bits)(data2), (scm_t_bits)(data3)) | |
118 | ||
119 | \f | |
9dd5943c | 120 | |
56164dc4 AW |
121 | #define SCM_SMOB_DATA_N(x, n) (SCM_CELL_WORD ((x), (n))) |
122 | #define SCM_SET_SMOB_DATA_N(x, n, data) (SCM_SET_CELL_WORD ((x), (n), (data))) | |
123 | ||
124 | #define SCM_SMOB_DATA_0(x) (SCM_SMOB_DATA_N ((x), 0)) | |
125 | #define SCM_SMOB_DATA_1(x) (SCM_SMOB_DATA_N ((x), 1)) | |
126 | #define SCM_SMOB_DATA_2(x) (SCM_SMOB_DATA_N ((x), 2)) | |
127 | #define SCM_SMOB_DATA_3(x) (SCM_SMOB_DATA_N ((x), 3)) | |
128 | #define SCM_SET_SMOB_DATA_0(x, data) (SCM_SET_SMOB_DATA_N ((x), 0, (data))) | |
129 | #define SCM_SET_SMOB_DATA_1(x, data) (SCM_SET_SMOB_DATA_N ((x), 1, (data))) | |
130 | #define SCM_SET_SMOB_DATA_2(x, data) (SCM_SET_SMOB_DATA_N ((x), 2, (data))) | |
131 | #define SCM_SET_SMOB_DATA_3(x, data) (SCM_SET_SMOB_DATA_N ((x), 3, (data))) | |
132 | ||
133 | #define SCM_SMOB_FLAGS(x) (SCM_SMOB_DATA_0 (x) >> 16) | |
134 | #define SCM_SMOB_DATA(x) (SCM_SMOB_DATA_1 (x)) | |
135 | #define SCM_SET_SMOB_FLAGS(x, data) (SCM_SET_SMOB_DATA_0 ((x), (SCM_CELL_TYPE (x)&0xffff)|((data)<<16))) | |
136 | #define SCM_SET_SMOB_DATA(x, data) (SCM_SET_SMOB_DATA_1 ((x), (data))) | |
137 | ||
138 | #define SCM_SMOB_OBJECT_N(x,n) (SCM_CELL_OBJECT ((x), (n))) | |
139 | #define SCM_SET_SMOB_OBJECT_N(x,n,obj) (SCM_SET_CELL_OBJECT ((x), (n), (obj))) | |
140 | #define SCM_SMOB_OBJECT_N_LOC(x,n) (SCM_CELL_OBJECT_LOC ((x), (n))) | |
141 | ||
142 | /*#define SCM_SMOB_OBJECT_0(x) (SCM_SMOB_OBJECT_N ((x), 0))*/ | |
143 | #define SCM_SMOB_OBJECT_1(x) (SCM_SMOB_OBJECT_N ((x), 1)) | |
144 | #define SCM_SMOB_OBJECT_2(x) (SCM_SMOB_OBJECT_N ((x), 2)) | |
145 | #define SCM_SMOB_OBJECT_3(x) (SCM_SMOB_OBJECT_N ((x), 3)) | |
146 | /*#define SCM_SET_SMOB_OBJECT_0(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 0, (obj)))*/ | |
147 | #define SCM_SET_SMOB_OBJECT_1(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 1, (obj))) | |
148 | #define SCM_SET_SMOB_OBJECT_2(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 2, (obj))) | |
149 | #define SCM_SET_SMOB_OBJECT_3(x,obj) (SCM_SET_SMOB_OBJECT_N ((x), 3, (obj))) | |
8442211e DK |
150 | #define SCM_SMOB_OBJECT_0_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 0)) |
151 | #define SCM_SMOB_OBJECT_1_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 1)) | |
152 | #define SCM_SMOB_OBJECT_2_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 2)) | |
153 | #define SCM_SMOB_OBJECT_3_LOC(x) (SCM_SMOB_OBJECT_N_LOC ((x), 3)) | |
56164dc4 AW |
154 | |
155 | #define SCM_SMOB_OBJECT(x) (SCM_SMOB_OBJECT_1 (x)) | |
156 | #define SCM_SET_SMOB_OBJECT(x,obj) (SCM_SET_SMOB_OBJECT_1 ((x), (obj))) | |
8442211e | 157 | #define SCM_SMOB_OBJECT_LOC(x) (SCM_SMOB_OBJECT_1_LOC (x)) |
56164dc4 | 158 | |
37fc18ae | 159 | |
75c3ed28 AW |
160 | #define SCM_SMOB_APPLY_0(x) (scm_call_0 (x)) |
161 | #define SCM_SMOB_APPLY_1(x, a1) (scm_call_1 (x, a1)) | |
162 | #define SCM_SMOB_APPLY_2(x, a1, a2) (scm_call_2 (x, a1, a2)) | |
163 | #define SCM_SMOB_APPLY_3(x, a1, a2, rst) (scm_call_3 (x, a1, a2, a3)) | |
0f2d19dd | 164 | |
0f2d19dd | 165 | \f |
0f2d19dd | 166 | |
33b001fd MV |
167 | SCM_API SCM scm_mark0 (SCM ptr); |
168 | SCM_API SCM scm_markcdr (SCM ptr); | |
169 | SCM_API size_t scm_free0 (SCM ptr); | |
33b001fd | 170 | SCM_API int scm_smob_print (SCM exp, SCM port, scm_print_state *pstate); |
23a62151 | 171 | |
c72baaaa MD |
172 | /* The following set of functions is the standard way to create new |
173 | * SMOB types. | |
174 | * | |
175 | * Create a type tag using `scm_make_smob_type', accept default values | |
176 | * for mark, free, print and/or equalp functions, or set your own | |
177 | * values using `scm_set_smob_xxx'. | |
178 | */ | |
23a62151 | 179 | |
df5af69a | 180 | SCM_API scm_t_bits scm_make_smob_type (char const *name, size_t size); |
23a62151 | 181 | |
33b001fd MV |
182 | SCM_API void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM)); |
183 | SCM_API void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM)); | |
184 | SCM_API void scm_set_smob_print (scm_t_bits tc, | |
185 | int (*print) (SCM, SCM, scm_print_state*)); | |
186 | SCM_API void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM)); | |
187 | SCM_API void scm_set_smob_apply (scm_t_bits tc, | |
be90d0b6 | 188 | scm_t_subr apply, |
33b001fd MV |
189 | unsigned int req, |
190 | unsigned int opt, | |
191 | unsigned int rst); | |
c72baaaa | 192 | |
1bbf7f75 AW |
193 | SCM_API SCM scm_smob_type_class (scm_t_bits tc); |
194 | ||
197b0573 MV |
195 | SCM_API void scm_assert_smob_type (scm_t_bits tag, SCM val); |
196 | ||
7c58e21b | 197 | /* Function for creating smobs */ |
c72baaaa | 198 | |
33b001fd | 199 | SCM_API SCM scm_make_smob (scm_t_bits tc); |
197b0573 | 200 | |
33b001fd | 201 | SCM_API void scm_smob_prehistory (void); |
7c58e21b | 202 | |
7a7f7c53 | 203 | #endif /* SCM_SMOB_H */ |
89e00824 ML |
204 | |
205 | /* | |
206 | Local Variables: | |
207 | c-file-style: "gnu" | |
208 | End: | |
209 | */ |