Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / foreign.h
1 #ifndef SCM_FOREIGN_H
2 #define SCM_FOREIGN_H
3
4 /* Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 */
21
22 #include "libguile/__scm.h"
23
24 /* A "foreign pointer" is a wrapped C pointer. It is represented by a
25 cell whose second word is a pointer. The first word has the
26 `scm_tc7_pointer' type code.
27
28 The basic idea is that we can help the programmer to avoid cutting herself,
29 but we won't take away her knives. */
30
31 enum scm_t_foreign_type
32 {
33 SCM_FOREIGN_TYPE_VOID,
34 SCM_FOREIGN_TYPE_FLOAT,
35 SCM_FOREIGN_TYPE_DOUBLE,
36 SCM_FOREIGN_TYPE_UINT8,
37 SCM_FOREIGN_TYPE_INT8,
38 SCM_FOREIGN_TYPE_UINT16,
39 SCM_FOREIGN_TYPE_INT16,
40 SCM_FOREIGN_TYPE_UINT32,
41 SCM_FOREIGN_TYPE_INT32,
42 SCM_FOREIGN_TYPE_UINT64,
43 SCM_FOREIGN_TYPE_INT64,
44 SCM_FOREIGN_TYPE_LAST = SCM_FOREIGN_TYPE_INT64
45 };
46
47 typedef enum scm_t_foreign_type scm_t_foreign_type;
48
49 typedef void (*scm_t_pointer_finalizer) (void *);
50
51 #define SCM_POINTER_P(x) (SCM_HAS_TYP7 (x, scm_tc7_pointer))
52 #define SCM_VALIDATE_POINTER(pos, x) \
53 SCM_MAKE_VALIDATE (pos, x, POINTER_P)
54 #define SCM_POINTER_VALUE(x) \
55 ((void *) SCM_CELL_WORD_1 (x))
56
57 SCM_API void *scm_to_pointer (SCM pointer);
58 SCM_API SCM scm_from_pointer (void *, scm_t_pointer_finalizer);
59
60 SCM_API SCM scm_alignof (SCM type);
61 SCM_API SCM scm_sizeof (SCM type);
62 SCM_API SCM scm_pointer_address (SCM pointer);
63 SCM_API SCM scm_pointer_to_bytevector (SCM pointer, SCM type,
64 SCM offset, SCM len);
65 SCM_API SCM scm_set_pointer_finalizer_x (SCM pointer, SCM finalizer);
66 SCM_API SCM scm_bytevector_to_pointer (SCM bv, SCM offset);
67
68 SCM_INTERNAL SCM scm_pointer_p (SCM obj);
69 SCM_INTERNAL SCM scm_make_pointer (SCM address, SCM finalizer);
70 SCM_INTERNAL void scm_i_pointer_print (SCM pointer, SCM port,
71 scm_print_state *pstate);
72
73 SCM_INTERNAL SCM scm_dereference_pointer (SCM pointer);
74 SCM_INTERNAL SCM scm_string_to_pointer (SCM string, SCM encoding);
75 SCM_INTERNAL SCM scm_pointer_to_string (SCM pointer, SCM length, SCM encoding);
76
77 \f
78
79 /* Foreign functions */
80
81 /* The goal is to make it so that calling a foreign function doesn't cause any
82 heap allocation. That means we need native Scheme formats for all kinds of
83 arguments.
84
85 For "value" types like s64 or f32, we just use native Scheme value types.
86 (Note that in both these cases, allocation is possible / likely, as the
87 value might need to be boxed, but perhaps we won't worry about that. Hmm.)
88
89 For everything else, we use foreign pointers. This includes arrays, pointer
90 arguments and return vals, struct args and return vals, and out and in/out
91 arguments.
92 */
93
94 SCM_API SCM scm_pointer_to_procedure (SCM return_type, SCM func_ptr,
95 SCM arg_types);
96 SCM_API SCM scm_procedure_to_pointer (SCM return_type, SCM func_ptr,
97 SCM arg_types);
98 SCM_INTERNAL SCM scm_i_foreign_call (SCM foreign, const SCM *argv);
99
100 \f
101
102 SCM_INTERNAL void scm_register_foreign (void);
103
104
105 #endif /* SCM_FOREIGN_H */