6 /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2004, 2006, 2009,
7 * 2010, 2011, 2012 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"
32 /* This is the internal representation of a smob type */
34 typedef struct scm_smob_descriptor
40 int (*print
) (SCM exp
, SCM port
, scm_print_state
*pstate
);
41 SCM (*equalp
) (SCM
, SCM
);
44 } scm_smob_descriptor
;
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)
53 #define SCM_SMOB_PREDICATE(tag, obj) SCM_HAS_TYP16 (obj, tag)
54 #define SCM_SMOB_DESCRIPTOR(x) (scm_smobs[SCM_SMOBNUM (x)])
55 #define SCM_SMOB_APPLICABLE_P(x) (SCM_SMOB_DESCRIPTOR (x).apply)
57 /* Maximum number of SMOB types. */
58 #define SCM_I_MAX_SMOB_TYPE_COUNT 256
60 SCM_API
long scm_numsmob
;
61 SCM_API scm_smob_descriptor scm_smobs
[];
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
);
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
);
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
)
79 scm_t_bits smobnum
= SCM_TC2SMOBNUM (tc
);
81 if (SCM_UNLIKELY (scm_smobs
[smobnum
].mark
|| scm_smobs
[smobnum
].free
))
82 return scm_i_new_smob (tc
, data
);
84 return scm_cell (tc
, data
);
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
)
91 scm_t_bits smobnum
= SCM_TC2SMOBNUM (tc
);
93 if (SCM_UNLIKELY (scm_smobs
[smobnum
].mark
|| scm_smobs
[smobnum
].free
))
94 return scm_i_new_double_smob (tc
, data1
, data2
, data3
);
96 return scm_double_cell (tc
, data1
, data2
, data3
);
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))
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)
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))
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)))
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)))
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)))
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)))
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)))
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)))
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)))
157 #define SCM_SMOB_OBJECT_LOC(x) (SCM_SMOB_OBJECT_1_LOC (x)))
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))
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
);
170 SCM_API
int scm_smob_print (SCM exp
, SCM port
, scm_print_state
*pstate
);
172 /* The following set of functions is the standard way to create new
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'.
180 SCM_API scm_t_bits
scm_make_smob_type (char const *name
, size_t size
);
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
,
193 SCM_API
void scm_assert_smob_type (scm_t_bits tag
, SCM val
);
195 /* Function for creating smobs */
197 SCM_API SCM
scm_make_smob (scm_t_bits tc
);
199 SCM_API
void scm_smob_prehistory (void);
201 #endif /* SCM_SMOB_H */