Remove incorrect comment in read.c
[bpt/guile.git] / libguile / smob.h
CommitLineData
0f2d19dd
JB
1/* classes: h_files */
2
7a7f7c53
DH
3#ifndef SCM_SMOB_H
4#define SCM_SMOB_H
dee01b01 5
be90d0b6
LC
6/* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2004, 2006, 2009,
7 * 2010, 2011 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
1c44468d 30#include "libguile/bdw-gc.h"
10fb3386
LC
31
32
0f2d19dd 33\f
9dd5943c
MD
34/* This is the internal representation of a smob type */
35
36typedef struct scm_smob_descriptor
37{
da0e6c2b 38 char const *name;
1be6b49c 39 size_t size;
7866a09b 40 SCM (*mark) (SCM);
1be6b49c 41 size_t (*free) (SCM);
7866a09b
GB
42 int (*print) (SCM exp, SCM port, scm_print_state *pstate);
43 SCM (*equalp) (SCM, SCM);
be90d0b6 44 scm_t_subr apply;
75c3ed28 45 SCM apply_trampoline_objcode;
9dd5943c
MD
46} scm_smob_descriptor;
47
378f2625 48
0f2d19dd 49\f
378f2625
LC
50SCM_API SCM scm_i_new_smob_with_mark_proc (scm_t_bits tc,
51 scm_t_bits, scm_t_bits, scm_t_bits);
52
53
0f2d19dd 54
10fb3386
LC
55#define SCM_NEWSMOB(z, tc, data) \
56do \
57 { \
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), \
61 0, 0) \
62 : scm_cell (tc, (scm_t_bits)(data))); \
63 if (scm_smobs[_smobnum].free) \
64 { \
65 GC_finalization_proc _prev_finalizer; \
66 GC_PTR _prev_finalizer_data; \
67 \
68 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (z), scm_i_finalize_smob, \
69 NULL, \
70 &_prev_finalizer, \
71 &_prev_finalizer_data); \
72 } \
73 } \
378f2625 74while (0)
23a62151 75
378f2625
LC
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; \
23a62151
MD
80 } while (0)
81
378f2625
LC
82#define SCM_NEWSMOB2(z, tc, data1, data2) \
83 SCM_NEWSMOB3 (z, tc, data1, data2, 0)
87621a14 84
378f2625
LC
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; \
87621a14
MD
89 } while (0)
90
10fb3386
LC
91#define SCM_NEWSMOB3(z, tc, data1, data2, data3) \
92do \
93 { \
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) \
103 { \
104 GC_finalization_proc _prev_finalizer; \
105 GC_PTR _prev_finalizer_data; \
106 \
107 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (z), scm_i_finalize_smob, \
108 NULL, \
109 &_prev_finalizer, \
110 &_prev_finalizer_data); \
111 } \
112 } \
378f2625 113while (0)
87621a14 114
378f2625
LC
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; \
87621a14
MD
119 } while (0)
120
9dd5943c 121
56164dc4
AW
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)))
124
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)))
133
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)))
138
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)))
142
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)))
155
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)))
159
37fc18ae 160
735bcfe5
AW
161#define SCM_SMOB_TYPE_MASK 0xffff
162#define SCM_SMOB_TYPE_BITS(tc) (tc)
94113249
DH
163#define SCM_TC2SMOBNUM(x) (0x0ff & ((x) >> 8))
164#define SCM_SMOBNUM(x) (SCM_TC2SMOBNUM (SCM_CELL_TYPE (x)))
2c16a78a 165/* SCM_SMOBNAME can be 0 if name is missing */
94113249 166#define SCM_SMOBNAME(smobnum) (scm_smobs[smobnum].name)
e841c3e0 167#define SCM_SMOB_PREDICATE(tag, obj) SCM_TYP16_PREDICATE (tag, obj)
0717dfd8 168#define SCM_SMOB_DESCRIPTOR(x) (scm_smobs[SCM_SMOBNUM (x)])
68b06924 169#define SCM_SMOB_APPLICABLE_P(x) (SCM_SMOB_DESCRIPTOR (x).apply)
75c3ed28
AW
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))
0f2d19dd 174
c891a40e
LC
175/* Maximum number of SMOB types. */
176#define SCM_I_MAX_SMOB_TYPE_COUNT 256
177
33b001fd
MV
178SCM_API long scm_numsmob;
179SCM_API scm_smob_descriptor scm_smobs[];
0f2d19dd 180
10fb3386 181SCM_API void scm_i_finalize_smob (GC_PTR obj, GC_PTR data);
37fc18ae 182
0f2d19dd 183\f
0f2d19dd 184
33b001fd
MV
185SCM_API SCM scm_mark0 (SCM ptr);
186SCM_API SCM scm_markcdr (SCM ptr);
187SCM_API size_t scm_free0 (SCM ptr);
33b001fd 188SCM_API int scm_smob_print (SCM exp, SCM port, scm_print_state *pstate);
23a62151 189
c72baaaa
MD
190/* The following set of functions is the standard way to create new
191 * SMOB types.
192 *
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'.
196 */
23a62151 197
df5af69a 198SCM_API scm_t_bits scm_make_smob_type (char const *name, size_t size);
23a62151 199
33b001fd
MV
200SCM_API void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM));
201SCM_API void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM));
202SCM_API void scm_set_smob_print (scm_t_bits tc,
203 int (*print) (SCM, SCM, scm_print_state*));
204SCM_API void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
205SCM_API void scm_set_smob_apply (scm_t_bits tc,
be90d0b6 206 scm_t_subr apply,
33b001fd
MV
207 unsigned int req,
208 unsigned int opt,
209 unsigned int rst);
c72baaaa 210
197b0573
MV
211SCM_API void scm_assert_smob_type (scm_t_bits tag, SCM val);
212
7c58e21b 213/* Function for creating smobs */
c72baaaa 214
33b001fd 215SCM_API SCM scm_make_smob (scm_t_bits tc);
197b0573 216
75c3ed28 217SCM_INTERNAL SCM scm_i_smob_apply_trampoline (SCM smob);
cc7005bc 218
33b001fd 219SCM_API void scm_smob_prehistory (void);
7c58e21b 220
7a7f7c53 221#endif /* SCM_SMOB_H */
89e00824
ML
222
223/*
224 Local Variables:
225 c-file-style: "gnu"
226 End:
227*/