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