* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
+
+#include <sys/types.h>
+#include <sys/mman.h>
+
#include <config.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
#include "girffi.h"
+#include "girffi-private.h"
+#include "girepository.h"
ffi_type *
-g_ir_ffi_get_ffi_type (GITypeTag tag)
+g_ir_ffi_get_ffi_type (GITypeTag tag,
+ gboolean is_pointer)
{
switch (tag)
{
- case GI_TYPE_TAG_VOID:
- return &ffi_type_void;
case GI_TYPE_TAG_BOOLEAN:
return &ffi_type_uint;
case GI_TYPE_TAG_INT8:
return &ffi_type_sint64;
case GI_TYPE_TAG_UINT64:
return &ffi_type_uint64;
+ case GI_TYPE_TAG_SHORT:
+ return &ffi_type_sshort;
+ case GI_TYPE_TAG_USHORT:
+ return &ffi_type_ushort;
case GI_TYPE_TAG_INT:
return &ffi_type_sint;
case GI_TYPE_TAG_UINT:
case GI_TYPE_TAG_GHASH:
case GI_TYPE_TAG_ERROR:
return &ffi_type_pointer;
+ case GI_TYPE_TAG_VOID:
+ if (is_pointer)
+ return &ffi_type_pointer;
+ else
+ return &ffi_type_void;
}
g_assert_not_reached ();
return NULL;
+}
+
+/**
+ * g_type_info_get_ffi_type:
+ * @info: A #GITypeInfo
+ *
+ * Returns: A #ffi_type corresponding to the platform default C ABI for @info.
+ */
+ffi_type *
+g_type_info_get_ffi_type (GITypeInfo *info)
+{
+ return g_ir_ffi_get_ffi_type (g_type_info_get_tag (info), g_type_info_is_pointer (info));
+}
+
+/**
+ * g_callable_info_get_ffi_arg_types:
+ * @callable_info: a callable info from a typelib
+ *
+ * Return value: an array of ffi_type*. The array itself
+ * should be freed using g_free() after use.
+ */
+static ffi_type **
+g_callable_info_get_ffi_arg_types (GICallableInfo *callable_info)
+{
+ ffi_type **arg_types;
+ gint n_args, i;
+
+ g_return_val_if_fail (callable_info != NULL, NULL);
+
+ n_args = g_callable_info_get_n_args (callable_info);
+
+ arg_types = (ffi_type **) g_new0 (ffi_type *, n_args + 1);
+
+ for (i = 0; i < n_args; ++i)
+ {
+ GIArgInfo *arg_info = g_callable_info_get_arg (callable_info, i);
+ GITypeInfo *arg_type = g_arg_info_get_type (arg_info);
+ arg_types[i] = g_type_info_get_ffi_type (arg_type);
+ g_base_info_unref ((GIBaseInfo *)arg_info);
+ g_base_info_unref ((GIBaseInfo *)arg_type);
+ }
+ arg_types[n_args] = NULL;
+
+ return arg_types;
+}
+
+/**
+ * g_callable_info_get_ffi_return_type:
+ * @callable_info: a callable info from a typelib
+ *
+ * Fetches the ffi_type for a corresponding return value of
+ * a #GICallableInfo
+ * Return value: the ffi_type for the return value
+ */
+static ffi_type *
+g_callable_info_get_ffi_return_type (GICallableInfo *callable_info)
+{
+ GITypeInfo *return_type;
+ GITypeTag type_tag;
+ ffi_type *return_ffi_type;
+
+ g_return_val_if_fail (callable_info != NULL, NULL);
+
+ return_type = g_callable_info_get_return_type (callable_info);
+ type_tag = g_type_info_get_tag (return_type);
+ return_ffi_type = g_type_info_get_ffi_type (return_type);
+ g_base_info_unref((GIBaseInfo*)return_type);
+
+ return return_ffi_type;
+}
+
+/**
+ * g_function_info_prep_invoker:
+ * @info: A #GIFunctionInfo
+ * @invoker: Output invoker structure
+ * @error: A #GError
+ *
+ * Initialize the caller-allocated @invoker structure with a cache
+ * of information needed to invoke the C function corresponding to
+ * @info with the platform's default ABI.
+ *
+ * A primary intent of this function is that a dynamic structure allocated
+ * by a language binding could contain a #GIFunctionInvoker structure
+ * inside the binding's function mapping.
+ */
+gboolean
+g_function_info_prep_invoker (GIFunctionInfo *info,
+ GIFunctionInvoker *invoker,
+ GError **error)
+{
+ const char *symbol;
+ ffi_type *rtype;
+ ffi_type **atypes;
+ GITypeInfo *tinfo;
+ GIArgInfo *ainfo;
+ gboolean is_method;
+ gboolean throws;
+ gint n_args, n_invoke_args, i;
+
+ g_return_val_if_fail (info != NULL, FALSE);
+ g_return_val_if_fail (invoker != NULL, FALSE);
+
+ symbol = g_function_info_get_symbol ((GIFunctionInfo*) info);
+
+ if (!g_typelib_symbol (g_base_info_get_typelib((GIBaseInfo *) info),
+ symbol, &(invoker->native_address)))
+ {
+ g_set_error (error,
+ G_INVOKE_ERROR,
+ G_INVOKE_ERROR_SYMBOL_NOT_FOUND,
+ "Could not locate %s: %s", symbol, g_module_error ());
+
+ return FALSE;
+ }
+
+ is_method = (g_function_info_get_flags (info) & GI_FUNCTION_IS_METHOD) != 0
+ && (g_function_info_get_flags (info) & GI_FUNCTION_IS_CONSTRUCTOR) == 0;
+ throws = g_function_info_get_flags (info) & GI_FUNCTION_THROWS;
+
+ tinfo = g_callable_info_get_return_type ((GICallableInfo *)info);
+ rtype = g_type_info_get_ffi_type (tinfo);
+ g_base_info_unref ((GIBaseInfo *)tinfo);
+
+ n_args = g_callable_info_get_n_args ((GICallableInfo *)info);
+ if (is_method)
+ n_invoke_args = n_args+1;
+ else
+ n_invoke_args = n_args;
+
+ if (throws)
+ /* Add an argument for the GError */
+ n_invoke_args ++;
+
+ /* TODO: avoid malloc here? */
+ atypes = g_malloc0 (sizeof (ffi_type*) * n_invoke_args);
+
+ if (is_method)
+ {
+ atypes[0] = &ffi_type_pointer;
+ }
+ for (i = 0; i < n_args; i++)
+ {
+ int offset = (is_method ? 1 : 0);
+ ainfo = g_callable_info_get_arg ((GICallableInfo *)info, i);
+ switch (g_arg_info_get_direction (ainfo))
+ {
+ case GI_DIRECTION_IN:
+ tinfo = g_arg_info_get_type (ainfo);
+ atypes[i+offset] = g_type_info_get_ffi_type (tinfo);
+ g_base_info_unref ((GIBaseInfo *)tinfo);
+ break;
+ case GI_DIRECTION_OUT:
+ case GI_DIRECTION_INOUT:
+ atypes[i+offset] = &ffi_type_pointer;
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+ g_base_info_unref ((GIBaseInfo *)ainfo);
+ }
+
+ if (throws)
+ {
+ atypes[n_invoke_args - 1] = &ffi_type_pointer;
+ }
+
+ return ffi_prep_cif (&(invoker->cif), FFI_DEFAULT_ABI, n_invoke_args, rtype, atypes) == FFI_OK;
+}
+
+/**
+ * g_function_info_invoker_destroy:
+ * @invoker: A #GIFunctionInvoker
+ *
+ * Release all resources allocated for the internals of @invoker; callers
+ * are responsible for freeing any resources allocated for the structure
+ * itself however.
+ */
+void
+g_function_invoker_destroy (GIFunctionInvoker *invoker)
+{
+ g_free (invoker->cif.arg_types);
+}
+
+/**
+ * g_callable_info_prepare_closure:
+ * @callable_info: a callable info from a typelib
+ * @cif: a ffi_cif structure
+ * @callback: the ffi callback
+ * @user_data: data to be passed into the callback
+ *
+ * Prepares a callback for ffi invocation.
+ *
+ * Note: this function requires the heap to be executable, which
+ * might not function properly on systems with SELinux enabled.
+ *
+ * Return value: the ffi_closure or NULL on error.
+ * The return value should be freed by calling g_callable_info_prepare_closure().
+ */
+ffi_closure *
+g_callable_info_prepare_closure (GICallableInfo *callable_info,
+ ffi_cif *cif,
+ GIFFIClosureCallback callback,
+ gpointer user_data)
+{
+ ffi_closure *closure;
+ ffi_status status;
+
+ g_return_val_if_fail (callable_info != NULL, FALSE);
+ g_return_val_if_fail (cif != NULL, FALSE);
+ g_return_val_if_fail (callback != NULL, FALSE);
+
+ closure = mmap (NULL, sizeof (ffi_closure),
+ PROT_EXEC | PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_PRIVATE, -1, sysconf (_SC_PAGE_SIZE));
+ if (!closure)
+ {
+ g_warning("mmap failed: %s\n", strerror(errno));
+ return NULL;
+ }
+
+ status = ffi_prep_cif (cif, FFI_DEFAULT_ABI,
+ g_callable_info_get_n_args (callable_info),
+ g_callable_info_get_ffi_return_type (callable_info),
+ g_callable_info_get_ffi_arg_types (callable_info));
+ if (status != FFI_OK)
+ {
+ g_warning("ffi_prep_cif failed: %d\n", status);
+ munmap(closure, sizeof (closure));
+ return NULL;
+ }
+
+ status = ffi_prep_closure (closure, cif, callback, user_data);
+ if (status != FFI_OK)
+ {
+ g_warning ("ffi_prep_closure failed: %d\n", status);
+ munmap(closure, sizeof (closure));
+ return NULL;
+ }
+
+ if (mprotect(closure, sizeof (closure), PROT_READ | PROT_EXEC) == -1)
+ {
+ g_warning ("ffi_prep_closure failed: %s\n", strerror(errno));
+ munmap(closure, sizeof (closure));
+ return NULL;
+ }
+
+ return closure;
+}
+
+/**
+ * g_callable_info_free_closure:
+ * @callable_info: a callable info from a typelib
+ * @closure: ffi closure
+ *
+ * Frees a ffi_closure returned from g_callable_info_prepare_closure()
+ */
+void
+g_callable_info_free_closure (GICallableInfo *callable_info,
+ ffi_closure *closure)
+{
+ munmap(closure, sizeof (closure));
}