Add gtk-doc support
[gnome.gobject-introspection] / girepository / girffi.c
1 /* GObject introspection: Helper functions for ffi integration
2  *
3  * Copyright (C) 2008 Red Hat, Inc
4  * Copyright (C) 2005 Matthias Clasen
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
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
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.
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
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 #include <config.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/mman.h>
25 #include <unistd.h>
26 #include "girffi.h"
27 #include "girepository.h"
28
29 ffi_type *
30 g_ir_ffi_get_ffi_type (GITypeTag tag)
31 {
32   switch (tag)
33     {
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;
54     case GI_TYPE_TAG_INT:
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;
63 #else
64 #  error "Unexpected size for size_t: not 4 or 8"
65 #endif
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;
74 #else
75 #  error "Unexpected size for size_t: not 4 or 8"
76 #endif
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;
82 #else
83 #  error "Unexpected size for time_t: not 4 or 8"
84 #endif
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;
100     }
101
102   g_assert_not_reached ();
103
104   return NULL;
105 }
106
107 /**
108  * g_callable_info_get_ffi_arg_types:
109  * @callable_info: a callable info from a typelib
110  *
111  * Return value: an array of ffi_type*. The array itself
112  * should be freed using g_free() after use.
113  */
114 ffi_type **
115 g_callable_info_get_ffi_arg_types (GICallableInfo *callable_info)
116 {
117     ffi_type **arg_types;
118     gint n_args, i;
119     
120     g_return_val_if_fail (callable_info != NULL, NULL);
121
122     n_args = g_callable_info_get_n_args (callable_info);
123     
124     arg_types = (ffi_type **) g_new0 (ffi_type *, n_args + 1);
125     
126     for (i = 0; i < n_args; ++i)
127       {
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);
134       }
135     arg_types[n_args] = NULL;
136
137     return arg_types;
138 }
139
140 /**
141  * g_callable_info_get_ffi_return_type:
142  * @callable_info: a callable info from a typelib
143  *
144  * Fetches the ffi_type for a corresponding return value of
145  * a #GICallableInfo
146  * Return value: the ffi_type for the return value
147  */
148 ffi_type *
149 g_callable_info_get_ffi_return_type (GICallableInfo *callable_info)
150 {
151   GITypeInfo *return_type;
152   GITypeTag type_tag;
153
154   g_return_val_if_fail (callable_info != NULL, NULL);
155
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);
159 }
160
161 /**
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
167  *
168  * Prepares a callback for ffi invocation.
169  *
170  * Note: this function requires the heap to be executable, which
171  *       might not function properly on systems with SELinux enabled.
172  *
173  * Return value: the ffi_closure or NULL on error.
174  * The return value should be freed by calling g_callable_info_prepare_closure().
175  */
176 ffi_closure *
177 g_callable_info_prepare_closure (GICallableInfo       *callable_info,
178                                  ffi_cif              *cif,
179                                  GIFFIClosureCallback  callback,
180                                  gpointer              user_data)
181 {
182   ffi_closure *closure;
183   ffi_status status;
184     
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);
188     
189   closure = mmap (NULL, sizeof (ffi_closure),
190                   PROT_EXEC | PROT_READ | PROT_WRITE,
191                   MAP_ANON | MAP_PRIVATE, -1, sysconf (_SC_PAGE_SIZE));
192   if (!closure)
193     {
194       g_warning("mmap failed: %s\n", strerror(errno));
195       return NULL;
196     }
197
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)
203     {
204       g_warning("ffi_prep_cif failed: %d\n", status);
205       munmap(closure, sizeof (closure));
206       return NULL;
207     }
208
209   status = ffi_prep_closure (closure, cif, callback, user_data);
210   if (status != FFI_OK)
211     {
212       g_warning ("ffi_prep_closure failed: %d\n", status);
213       munmap(closure, sizeof (closure));
214       return NULL;
215     }
216
217   if (mprotect(closure, sizeof (closure), PROT_READ | PROT_EXEC) == -1)
218     {
219       g_warning ("ffi_prep_closure failed: %s\n", strerror(errno));
220       munmap(closure, sizeof (closure));
221       return NULL;
222     }
223   
224   return closure;
225 }
226
227 /**
228  * g_callable_info_free_closure:
229  * @callable_info: a callable info from a typelib
230  * @closure: ffi closure
231  *
232  * Frees a ffi_closure returned from g_callable_info_prepare_closure()
233  */
234 void
235 g_callable_info_free_closure (GICallableInfo *callable_info,
236                               ffi_closure    *closure)
237 {
238   munmap(closure, sizeof (closure));
239 }