Commit | Line | Data |
---|---|---|
f353687c LC |
1 | #ifndef SCM_FOREIGN_H |
2 | #define SCM_FOREIGN_H | |
3 | ||
52de2ab4 | 4 | /* Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc. |
e2c2a699 AW |
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 | ||
f353687c | 22 | #include "libguile/__scm.h" |
e2c2a699 | 23 | |
5b46a8c2 LC |
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 | |
52de2ab4 | 26 | `scm_tc7_pointer' type code. |
52fd9639 | 27 | |
52fd9639 | 28 | The basic idea is that we can help the programmer to avoid cutting herself, |
d4149a51 LC |
29 | but we won't take away her knives. */ |
30 | ||
31 | enum scm_t_foreign_type | |
e2c2a699 | 32 | { |
d4149a51 LC |
33 | SCM_FOREIGN_TYPE_VOID, |
34 | SCM_FOREIGN_TYPE_FLOAT, | |
e2c2a699 AW |
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, | |
52fd9639 | 44 | SCM_FOREIGN_TYPE_LAST = SCM_FOREIGN_TYPE_INT64 |
d4149a51 | 45 | }; |
e2c2a699 | 46 | |
d4149a51 | 47 | typedef enum scm_t_foreign_type scm_t_foreign_type; |
e2c2a699 | 48 | |
5b46a8c2 | 49 | typedef void (*scm_t_pointer_finalizer) (void *); |
e2c2a699 | 50 | |
5b46a8c2 LC |
51 | #define SCM_POINTER_P(x) \ |
52 | (!SCM_IMP (x) && SCM_TYP7(x) == scm_tc7_pointer) | |
53 | #define SCM_VALIDATE_POINTER(pos, x) \ | |
54 | SCM_MAKE_VALIDATE (pos, x, POINTER_P) | |
55 | #define SCM_POINTER_VALUE(x) \ | |
d4149a51 | 56 | ((void *) SCM_CELL_WORD_1 (x)) |
d4149a51 | 57 | |
1d00abb0 | 58 | SCM_API void *scm_to_pointer (SCM pointer); |
5b46a8c2 | 59 | SCM_API SCM scm_from_pointer (void *, scm_t_pointer_finalizer); |
52fd9639 | 60 | |
9a396cbd AW |
61 | SCM_API SCM scm_alignof (SCM type); |
62 | SCM_API SCM scm_sizeof (SCM type); | |
5b46a8c2 LC |
63 | SCM_API SCM scm_pointer_address (SCM pointer); |
64 | SCM_API SCM scm_pointer_to_bytevector (SCM pointer, SCM type, | |
20aafae2 | 65 | SCM offset, SCM len); |
5b46a8c2 | 66 | SCM_API SCM scm_set_pointer_finalizer_x (SCM pointer, SCM finalizer); |
22697acb | 67 | SCM_API SCM scm_bytevector_to_pointer (SCM bv, SCM offset); |
e2c2a699 | 68 | |
6e097560 | 69 | SCM_INTERNAL SCM scm_pointer_p (SCM obj); |
d4149a51 | 70 | SCM_INTERNAL SCM scm_make_pointer (SCM address, SCM finalizer); |
5b46a8c2 | 71 | SCM_INTERNAL void scm_i_pointer_print (SCM pointer, SCM port, |
e2c2a699 | 72 | scm_print_state *pstate); |
d8b04f04 | 73 | |
fa2a89a6 | 74 | SCM_INTERNAL SCM scm_dereference_pointer (SCM pointer); |
c6b08d21 AW |
75 | SCM_INTERNAL SCM scm_string_to_pointer (SCM string, SCM encoding); |
76 | SCM_INTERNAL SCM scm_pointer_to_string (SCM pointer, SCM length, SCM encoding); | |
fa2a89a6 | 77 | |
d8b04f04 AW |
78 | \f |
79 | ||
80 | /* Foreign functions */ | |
81 | ||
82 | /* The goal is to make it so that calling a foreign function doesn't cause any | |
83 | heap allocation. That means we need native Scheme formats for all kinds of | |
84 | arguments. | |
85 | ||
86 | For "value" types like s64 or f32, we just use native Scheme value types. | |
87 | (Note that in both these cases, allocation is possible / likely, as the | |
88 | value might need to be boxed, but perhaps we won't worry about that. Hmm.) | |
89 | ||
90 | For everything else, we use foreign pointers. This includes arrays, pointer | |
91 | arguments and return vals, struct args and return vals, and out and in/out | |
92 | arguments. | |
93 | */ | |
94 | ||
2ee07358 LC |
95 | SCM_API SCM scm_pointer_to_procedure (SCM return_type, SCM func_ptr, |
96 | SCM arg_types); | |
33186356 LC |
97 | SCM_API SCM scm_procedure_to_pointer (SCM return_type, SCM func_ptr, |
98 | SCM arg_types); | |
165a8643 | 99 | SCM_INTERNAL SCM scm_i_foreign_call (SCM foreign, const SCM *argv); |
d8b04f04 AW |
100 | |
101 | \f | |
102 | ||
ab4779ff | 103 | SCM_INTERNAL void scm_register_foreign (void); |
e2c2a699 AW |
104 | |
105 | ||
106 | #endif /* SCM_FOREIGN_H */ |