1 /* GObject introspection: Helper functions for ffi integration
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2005 Matthias Clasen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
27 #include "girepository.h"
30 g_ir_ffi_get_ffi_type (GITypeTag tag)
34 case GI_TYPE_TAG_VOID:
35 return &ffi_type_void;
36 case GI_TYPE_TAG_BOOLEAN:
37 return &ffi_type_uint;
38 case GI_TYPE_TAG_INT8:
39 return &ffi_type_sint8;
40 case GI_TYPE_TAG_UINT8:
41 return &ffi_type_uint8;
42 case GI_TYPE_TAG_INT16:
43 return &ffi_type_sint16;
44 case GI_TYPE_TAG_UINT16:
45 return &ffi_type_uint16;
46 case GI_TYPE_TAG_INT32:
47 return &ffi_type_sint32;
48 case GI_TYPE_TAG_UINT32:
49 return &ffi_type_uint32;
50 case GI_TYPE_TAG_INT64:
51 return &ffi_type_sint64;
52 case GI_TYPE_TAG_UINT64:
53 return &ffi_type_uint64;
55 return &ffi_type_sint;
56 case GI_TYPE_TAG_UINT:
57 return &ffi_type_uint;
58 case GI_TYPE_TAG_SSIZE:
59 #if GLIB_SIZEOF_SIZE_T == 4
60 return &ffi_type_sint32;
61 #elif GLIB_SIZEOF_SIZE_T == 8
62 return &ffi_type_sint64;
64 # error "Unexpected size for size_t: not 4 or 8"
66 case GI_TYPE_TAG_LONG:
67 return &ffi_type_slong;
68 case GI_TYPE_TAG_SIZE:
69 case GI_TYPE_TAG_GTYPE:
70 #if GLIB_SIZEOF_SIZE_T == 4
71 return &ffi_type_uint32;
72 #elif GLIB_SIZEOF_SIZE_T == 8
73 return &ffi_type_uint64;
75 # error "Unexpected size for size_t: not 4 or 8"
77 case GI_TYPE_TAG_TIME_T:
78 #if SIZEOF_TIME_T == 4
79 return &ffi_type_sint32;
80 #elif SIZEOF_TIME_T == 8
81 return &ffi_type_sint64;
83 # error "Unexpected size for time_t: not 4 or 8"
85 case GI_TYPE_TAG_ULONG:
86 return &ffi_type_ulong;
87 case GI_TYPE_TAG_FLOAT:
88 return &ffi_type_float;
89 case GI_TYPE_TAG_DOUBLE:
90 return &ffi_type_double;
91 case GI_TYPE_TAG_UTF8:
92 case GI_TYPE_TAG_FILENAME:
93 case GI_TYPE_TAG_ARRAY:
94 case GI_TYPE_TAG_INTERFACE:
95 case GI_TYPE_TAG_GLIST:
96 case GI_TYPE_TAG_GSLIST:
97 case GI_TYPE_TAG_GHASH:
98 case GI_TYPE_TAG_ERROR:
99 return &ffi_type_pointer;
102 g_assert_not_reached ();
108 * g_callable_info_get_ffi_arg_types:
109 * @callable_info: a callable info from a typelib
111 * Return value: an array of ffi_type*. The array itself
112 * should be freed using g_free() after use.
115 g_callable_info_get_ffi_arg_types (GICallableInfo *callable_info)
117 ffi_type **arg_types;
120 g_return_val_if_fail (callable_info != NULL, NULL);
122 n_args = g_callable_info_get_n_args (callable_info);
124 arg_types = (ffi_type **) g_new0 (ffi_type *, n_args + 1);
126 for (i = 0; i < n_args; ++i)
128 GIArgInfo *arg_info = g_callable_info_get_arg (callable_info, i);
129 GITypeInfo *arg_type = g_arg_info_get_type (arg_info);
130 GITypeTag type_tag = g_type_info_get_tag (arg_type);
131 arg_types[i] = g_ir_ffi_get_ffi_type (type_tag);
132 g_base_info_unref ((GIBaseInfo *)arg_info);
133 g_base_info_unref ((GIBaseInfo *)arg_type);
135 arg_types[n_args] = NULL;
141 * g_callable_info_get_ffi_return_type:
142 * @callable_info: a callable info from a typelib
144 * Fetches the ffi_type for a corresponding return value of
146 * Return value: the ffi_type for the return value
149 g_callable_info_get_ffi_return_type (GICallableInfo *callable_info)
151 GITypeInfo *return_type;
154 g_return_val_if_fail (callable_info != NULL, NULL);
156 return_type = g_callable_info_get_return_type (callable_info);
157 type_tag = g_type_info_get_tag (return_type);
158 return g_ir_ffi_get_ffi_type (type_tag);
162 * g_callable_info_prepare_closure:
163 * @callable_info: a callable info from a typelib
164 * @cif: a ffi_cif structure
165 * @callback: the ffi callback
166 * @user_data: data to be passed into the callback
168 * Prepares a callback for ffi invocation.
170 * Note: this function requires the heap to be executable, which
171 * might not function properly on systems with SELinux enabled.
173 * Return value: the ffi_closure or NULL on error.
174 * The return value should be freed by calling g_callable_info_prepare_closure().
177 g_callable_info_prepare_closure (GICallableInfo *callable_info,
179 GIFFIClosureCallback callback,
182 ffi_closure *closure;
185 g_return_val_if_fail (callable_info != NULL, FALSE);
186 g_return_val_if_fail (cif != NULL, FALSE);
187 g_return_val_if_fail (callback != NULL, FALSE);
189 closure = mmap (NULL, sizeof (ffi_closure),
190 PROT_EXEC | PROT_READ | PROT_WRITE,
191 MAP_ANON | MAP_PRIVATE, -1, sysconf (_SC_PAGE_SIZE));
194 g_warning("mmap failed: %s\n", strerror(errno));
198 status = ffi_prep_cif (cif, FFI_DEFAULT_ABI,
199 g_callable_info_get_n_args (callable_info),
200 g_callable_info_get_ffi_return_type (callable_info),
201 g_callable_info_get_ffi_arg_types (callable_info));
202 if (status != FFI_OK)
204 g_warning("ffi_prep_cif failed: %d\n", status);
205 munmap(closure, sizeof (closure));
209 status = ffi_prep_closure (closure, cif, callback, user_data);
210 if (status != FFI_OK)
212 g_warning ("ffi_prep_closure failed: %d\n", status);
213 munmap(closure, sizeof (closure));
217 if (mprotect(closure, sizeof (closure), PROT_READ | PROT_EXEC) == -1)
219 g_warning ("ffi_prep_closure failed: %s\n", strerror(errno));
220 munmap(closure, sizeof (closure));
228 * g_callable_info_free_closure:
229 * @callable_info: a callable info from a typelib
230 * @closure: ffi closure
232 * Frees a ffi_closure returned from g_callable_info_prepare_closure()
235 g_callable_info_free_closure (GICallableInfo *callable_info,
236 ffi_closure *closure)
238 munmap(closure, sizeof (closure));