1 /* Copyright (C) 2010 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "libguile/_scm.h"
25 #include "libguile/foreign.h"
30 foreign_finalizer_trampoline (GC_PTR ptr
, GC_PTR data
)
32 scm_t_foreign_finalizer finalizer
= data
;
33 finalizer (SCM_FOREIGN_POINTER (PTR2SCM (ptr
), void));
37 scm_take_foreign_pointer (scm_t_foreign_type type
, void *ptr
, size_t len
,
38 scm_t_foreign_finalizer finalizer
)
43 word0
= (scm_t_bits
)(scm_tc7_foreign
| (type
<<8)
44 | (finalizer
? (1<<16) : 0) | (len
<<17));
45 if (SCM_UNLIKELY ((word0
>> 16) != len
))
46 scm_out_of_range ("scm_take_foreign_pointer", scm_from_size_t (len
));
48 ret
= PTR2SCM (scm_gc_malloc_pointerless (sizeof (scm_t_bits
) * 2,
50 SCM_SET_CELL_WORD_0 (ret
, word0
);
51 SCM_SET_CELL_WORD_1 (ret
, (scm_t_bits
)ptr
);
55 /* Register a finalizer for the newly created instance. */
56 GC_finalization_proc prev_finalizer
;
57 GC_PTR prev_finalizer_data
;
58 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret
),
59 foreign_finalizer_trampoline
,
62 &prev_finalizer_data
);
69 keepalive (GC_PTR obj
, GC_PTR data
)
73 SCM_DEFINE (scm_foreign_ref
, "foreign-ref", 1, 3, 0,
74 (SCM foreign
, SCM type
, SCM offset
, SCM len
),
75 "Reference the foreign value wrapped by @var{foreign}.\n\n"
76 "The value will be referenced according to its type.\n"
77 "If and only if the type of the foreign value is @code{void},\n"
78 "this function accepts optional @var{type} and @var{offset}\n"
79 "arguments, indicating that the pointer wrapped by\n"
80 "@var{foreign} should be incremented by @var{offset} bytes,\n"
81 "and treated as a pointer to a value of the given @var{type}.\n"
82 "@var{offset} defaults to 0.\n\n"
83 "If @var{type} itself is @code{void}, @var{len} will be used\n"
84 "to specify the size of the resulting @code{void} pointer.")
85 #define FUNC_NAME s_scm_foreign_ref
87 scm_t_foreign_type ftype
;
90 SCM_VALIDATE_FOREIGN (1, foreign
);
91 ptr
= SCM_FOREIGN_POINTER (foreign
, scm_t_uint8
);
93 ftype
= SCM_FOREIGN_TYPE (foreign
);
94 if (ftype
== SCM_FOREIGN_TYPE_VOID
)
96 if (SCM_UNBNDP (type
))
97 scm_error_num_args_subr (FUNC_NAME
);
98 ftype
= scm_to_unsigned_integer (type
, 0, SCM_FOREIGN_TYPE_LAST
);
99 if (!SCM_UNBNDP (offset
))
100 ptr
+= scm_to_ssize_t (offset
);
104 if (!SCM_UNBNDP (type
))
105 scm_error_num_args_subr (FUNC_NAME
);
108 /* FIXME: is there a window in which we can see ptr but not foreign? */
111 case SCM_FOREIGN_TYPE_FLOAT
:
112 return scm_from_double (*(float*)ptr
);
113 case SCM_FOREIGN_TYPE_DOUBLE
:
114 return scm_from_double (*(double*)ptr
);
115 case SCM_FOREIGN_TYPE_UINT8
:
116 return scm_from_uint8 (*(scm_t_uint8
*)ptr
);
117 case SCM_FOREIGN_TYPE_INT8
:
118 return scm_from_int8 (*(scm_t_int8
*)ptr
);
119 case SCM_FOREIGN_TYPE_UINT16
:
120 return scm_from_uint16 (*(scm_t_uint16
*)ptr
);
121 case SCM_FOREIGN_TYPE_INT16
:
122 return scm_from_int16 (*(scm_t_int16
*)ptr
);
123 case SCM_FOREIGN_TYPE_UINT32
:
124 return scm_from_uint32 (*(scm_t_uint32
*)ptr
);
125 case SCM_FOREIGN_TYPE_INT32
:
126 return scm_from_int32 (*(scm_t_int32
*)ptr
);
127 case SCM_FOREIGN_TYPE_UINT64
:
128 return scm_from_uint64 (*(scm_t_uint64
*)ptr
);
129 case SCM_FOREIGN_TYPE_INT64
:
130 return scm_from_int64 (*(scm_t_int64
*)ptr
);
131 case SCM_FOREIGN_TYPE_VOID
:
132 /* seems we're making a new pointer, woo */
134 GC_finalization_proc prev_finalizer
;
135 GC_PTR prev_finalizer_data
;
136 SCM ret
= scm_take_foreign_pointer
137 (ftype
, ptr
, SCM_UNBNDP (len
) ? 0 : scm_to_size_t (len
), NULL
);
138 /* while the kid is alive, keep the parent alive */
139 if (SCM_FOREIGN_HAS_FINALIZER (foreign
))
140 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret
), keepalive
, foreign
,
141 &prev_finalizer
, &prev_finalizer_data
);
150 SCM_DEFINE (scm_foreign_set_x
, "foreign-set!", 2, 2, 0,
151 (SCM foreign
, SCM val
, SCM type
, SCM offset
),
152 "Set the foreign value wrapped by @var{foreign}.\n\n"
153 "The value will be set according to its type.\n"
154 "If and only if the type of the foreign value is @code{void},\n"
155 "this function accepts optional @var{type} and @var{offset}\n"
156 "arguments, indicating that the pointer wrapped by\n"
157 "@var{foreign} should be incremented by @var{offset} bytes,\n"
158 "and treated as a pointer to a value of the given @var{type}.\n"
159 "@var{offset} defaults to 0.")
160 #define FUNC_NAME s_scm_foreign_set_x
162 scm_t_foreign_type ftype
;
165 SCM_VALIDATE_FOREIGN (1, foreign
);
166 ptr
= SCM_FOREIGN_POINTER (foreign
, scm_t_uint8
);
168 ftype
= SCM_FOREIGN_TYPE (foreign
);
169 if (ftype
== SCM_FOREIGN_TYPE_VOID
)
171 if (SCM_UNBNDP (type
))
172 scm_error_num_args_subr (FUNC_NAME
);
173 ftype
= scm_to_unsigned_integer (type
, 0, SCM_FOREIGN_TYPE_LAST
);
174 if (!SCM_UNBNDP (offset
))
175 ptr
+= scm_to_ssize_t (offset
);
179 if (!SCM_UNBNDP (type
))
180 scm_error_num_args_subr (FUNC_NAME
);
183 /* FIXME: is there a window in which we can see ptr but not foreign? */
186 case SCM_FOREIGN_TYPE_FLOAT
:
187 *(float*)ptr
= scm_to_double (val
);
189 case SCM_FOREIGN_TYPE_DOUBLE
:
190 *(double*)ptr
= scm_to_double (val
);
192 case SCM_FOREIGN_TYPE_UINT8
:
193 *(scm_t_uint8
*)ptr
= scm_to_uint8 (val
);
195 case SCM_FOREIGN_TYPE_INT8
:
196 *(scm_t_int8
*)ptr
= scm_to_int8 (val
);
198 case SCM_FOREIGN_TYPE_UINT16
:
199 *(scm_t_uint16
*)ptr
= scm_to_uint16 (val
);
201 case SCM_FOREIGN_TYPE_INT16
:
202 *(scm_t_int16
*)ptr
= scm_to_int16 (val
);
204 case SCM_FOREIGN_TYPE_UINT32
:
205 *(scm_t_uint32
*)ptr
= scm_to_uint32 (val
);
207 case SCM_FOREIGN_TYPE_INT32
:
208 *(scm_t_int32
*)ptr
= scm_to_int32 (val
);
210 case SCM_FOREIGN_TYPE_UINT64
:
211 *(scm_t_uint64
*)ptr
= scm_to_uint64 (val
);
213 case SCM_FOREIGN_TYPE_INT64
:
214 *(scm_t_int64
*)ptr
= scm_to_int64 (val
);
216 case SCM_FOREIGN_TYPE_VOID
:
217 SCM_VALIDATE_FOREIGN (2, val
);
218 if (SCM_FOREIGN_HAS_FINALIZER (val
))
219 /* setting a pointer inside one foreign value to the pointer of another?
220 that is asking for trouble */
221 scm_wrong_type_arg_msg (FUNC_NAME
, 2, val
,
222 "foreign value without finalizer");
223 *(void**)ptr
= SCM_FOREIGN_POINTER (val
, void*);
229 return SCM_UNSPECIFIED
;
234 scm_i_foreign_print (SCM foreign
, SCM port
, scm_print_state
*pstate
)
236 scm_puts ("#<foreign ", port
);
237 switch (SCM_FOREIGN_TYPE (foreign
))
239 case SCM_FOREIGN_TYPE_FLOAT
:
240 scm_puts ("float ", port
);
241 scm_display (scm_foreign_ref (foreign
, SCM_UNDEFINED
, SCM_UNDEFINED
,
245 case SCM_FOREIGN_TYPE_DOUBLE
:
246 scm_puts ("double ", port
);
247 scm_display (scm_foreign_ref (foreign
, SCM_UNDEFINED
, SCM_UNDEFINED
,
251 case SCM_FOREIGN_TYPE_UINT8
:
252 scm_puts ("uint8 ", port
);
253 scm_display (scm_foreign_ref (foreign
, SCM_UNDEFINED
, SCM_UNDEFINED
,
257 case SCM_FOREIGN_TYPE_INT8
:
258 scm_puts ("int8 ", port
);
259 scm_display (scm_foreign_ref (foreign
, SCM_UNDEFINED
, SCM_UNDEFINED
,
263 case SCM_FOREIGN_TYPE_UINT16
:
264 scm_puts ("uint16 ", port
);
265 scm_display (scm_foreign_ref (foreign
, SCM_UNDEFINED
, SCM_UNDEFINED
,
269 case SCM_FOREIGN_TYPE_INT16
:
270 scm_puts ("int16 ", port
);
271 scm_display (scm_foreign_ref (foreign
, SCM_UNDEFINED
, SCM_UNDEFINED
,
275 case SCM_FOREIGN_TYPE_UINT32
:
276 scm_puts ("uint32 ", port
);
277 scm_display (scm_foreign_ref (foreign
, SCM_UNDEFINED
, SCM_UNDEFINED
,
281 case SCM_FOREIGN_TYPE_INT32
:
282 scm_puts ("int32 ", port
);
283 scm_display (scm_foreign_ref (foreign
, SCM_UNDEFINED
, SCM_UNDEFINED
,
287 case SCM_FOREIGN_TYPE_UINT64
:
288 scm_puts ("uint64 ", port
);
289 scm_display (scm_foreign_ref (foreign
, SCM_UNDEFINED
, SCM_UNDEFINED
,
293 case SCM_FOREIGN_TYPE_INT64
:
294 scm_puts ("int64 ", port
);
295 scm_display (scm_foreign_ref (foreign
, SCM_UNDEFINED
, SCM_UNDEFINED
,
299 case SCM_FOREIGN_TYPE_VOID
:
300 scm_puts ("pointer 0x", port
);
301 scm_uintprint ((scm_t_bits
)SCM_FOREIGN_POINTER (foreign
, void), 16, port
);
306 scm_putc ('>', port
);
312 scm_init_foreign (void)
314 #ifndef SCM_MAGIC_SNARFER
315 #include "libguile/foreign.x"