2018-06-20 19:17:06 +02:00
|
|
|
|
/* Copyright 2010-2016,2018
|
2018-06-20 20:01:49 +02:00
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
|
|
|
|
This file is part of Guile.
|
|
|
|
|
|
|
|
|
|
|
|
Guile is free software: you can redistribute it and/or modify it
|
|
|
|
|
|
under the terms of the GNU Lesser General Public License as published
|
|
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
|
|
Guile is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
License along with Guile. If not, see
|
|
|
|
|
|
<https://www.gnu.org/licenses/>. */
|
2010-01-04 12:11:33 +01:00
|
|
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
|
|
# include <config.h>
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2010-01-25 18:15:35 +01:00
|
|
|
|
#include <alignof.h>
|
2018-06-20 18:31:24 +02:00
|
|
|
|
#include <alloca.h>
|
2010-05-30 22:54:39 +02:00
|
|
|
|
#include <assert.h>
|
2016-01-05 16:30:41 -05:00
|
|
|
|
#include <errno.h>
|
2018-06-20 18:31:24 +02:00
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <ffi.h>
|
2010-05-30 22:54:39 +02:00
|
|
|
|
|
2018-06-20 17:19:31 +02:00
|
|
|
|
#include "boolean.h"
|
|
|
|
|
|
#include "bytevectors.h"
|
|
|
|
|
|
#include "dynwind.h"
|
|
|
|
|
|
#include "eq.h"
|
|
|
|
|
|
#include "eval.h"
|
|
|
|
|
|
#include "extensions.h"
|
|
|
|
|
|
#include "finalizers.h"
|
2018-06-20 18:31:24 +02:00
|
|
|
|
#include "gsubr.h"
|
|
|
|
|
|
#include "instructions.h"
|
2018-06-26 09:22:07 +02:00
|
|
|
|
#include "intrinsics.h"
|
2018-06-20 17:19:31 +02:00
|
|
|
|
#include "keywords.h"
|
|
|
|
|
|
#include "list.h"
|
|
|
|
|
|
#include "modules.h"
|
2018-06-20 18:31:24 +02:00
|
|
|
|
#include "numbers.h"
|
2018-06-20 17:19:31 +02:00
|
|
|
|
#include "pairs.h"
|
|
|
|
|
|
#include "ports.h"
|
|
|
|
|
|
#include "stacks.h"
|
|
|
|
|
|
#include "symbols.h"
|
|
|
|
|
|
#include "threads.h"
|
|
|
|
|
|
#include "weak-table.h"
|
2018-10-07 15:15:02 +02:00
|
|
|
|
#include "version.h"
|
2010-01-04 12:11:33 +01:00
|
|
|
|
|
2018-06-20 18:31:24 +02:00
|
|
|
|
#include "foreign.h"
|
|
|
|
|
|
|
2010-01-04 12:11:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
2018-06-17 19:10:52 +02:00
|
|
|
|
/* Return the first integer greater than or equal to LEN such that
|
|
|
|
|
|
LEN % ALIGN == 0. Return LEN if ALIGN is zero. */
|
|
|
|
|
|
#define ROUND_UP(len, align) \
|
|
|
|
|
|
((align) ? (((len) - 1UL) | ((align) - 1UL)) + 1UL : (len))
|
|
|
|
|
|
|
2010-01-18 12:16:13 +01:00
|
|
|
|
SCM_SYMBOL (sym_void, "void");
|
|
|
|
|
|
SCM_SYMBOL (sym_float, "float");
|
|
|
|
|
|
SCM_SYMBOL (sym_double, "double");
|
|
|
|
|
|
SCM_SYMBOL (sym_uint8, "uint8");
|
|
|
|
|
|
SCM_SYMBOL (sym_int8, "int8");
|
|
|
|
|
|
SCM_SYMBOL (sym_uint16, "uint16");
|
|
|
|
|
|
SCM_SYMBOL (sym_int16, "int16");
|
|
|
|
|
|
SCM_SYMBOL (sym_uint32, "uint32");
|
|
|
|
|
|
SCM_SYMBOL (sym_int32, "int32");
|
|
|
|
|
|
SCM_SYMBOL (sym_uint64, "uint64");
|
|
|
|
|
|
SCM_SYMBOL (sym_int64, "int64");
|
2010-11-11 16:26:50 +01:00
|
|
|
|
SCM_SYMBOL (sym_short, "short");
|
Provide `int', `long', `size_t', etc. in `(system foreign)'.
* libguile/foreign.c (sym_int, sym_long, sym_unsigned_int,
sym_unsigned_long, sym_size_t): New variables.
(scm_init_foreign): Define them at the Scheme level.
* module/system/foreign.scm (int, unsigned-int, long,
unsigned-long, size_t): New exported bindings.
2010-03-17 00:45:57 +01:00
|
|
|
|
SCM_SYMBOL (sym_int, "int");
|
|
|
|
|
|
SCM_SYMBOL (sym_long, "long");
|
2010-11-11 16:26:50 +01:00
|
|
|
|
SCM_SYMBOL (sym_unsigned_short, "unsigned-short");
|
Provide `int', `long', `size_t', etc. in `(system foreign)'.
* libguile/foreign.c (sym_int, sym_long, sym_unsigned_int,
sym_unsigned_long, sym_size_t): New variables.
(scm_init_foreign): Define them at the Scheme level.
* module/system/foreign.scm (int, unsigned-int, long,
unsigned-long, size_t): New exported bindings.
2010-03-17 00:45:57 +01:00
|
|
|
|
SCM_SYMBOL (sym_unsigned_int, "unsigned-int");
|
|
|
|
|
|
SCM_SYMBOL (sym_unsigned_long, "unsigned-long");
|
|
|
|
|
|
SCM_SYMBOL (sym_size_t, "size_t");
|
2013-02-02 04:04:49 -05:00
|
|
|
|
SCM_SYMBOL (sym_ssize_t, "ssize_t");
|
|
|
|
|
|
SCM_SYMBOL (sym_ptrdiff_t, "ptrdiff_t");
|
2017-11-22 16:32:39 +01:00
|
|
|
|
SCM_SYMBOL (sym_intptr_t, "intptr_t");
|
|
|
|
|
|
SCM_SYMBOL (sym_uintptr_t, "uintptr_t");
|
2010-01-18 12:16:13 +01:00
|
|
|
|
|
2010-01-27 22:12:58 +01:00
|
|
|
|
/* that's for pointers, you know. */
|
|
|
|
|
|
SCM_SYMBOL (sym_asterisk, "*");
|
|
|
|
|
|
|
2010-03-18 00:27:38 +01:00
|
|
|
|
SCM_SYMBOL (sym_null, "%null-pointer");
|
2010-04-09 00:30:10 +02:00
|
|
|
|
SCM_SYMBOL (sym_null_pointer_error, "null-pointer-error");
|
2010-03-18 00:27:38 +01:00
|
|
|
|
|
|
|
|
|
|
/* The cell representing the null pointer. */
|
2010-05-20 01:00:37 +02:00
|
|
|
|
static SCM null_pointer;
|
2010-01-27 22:12:58 +01:00
|
|
|
|
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
|
2010-04-09 00:30:10 +02:00
|
|
|
|
/* Raise a null pointer dereference error. */
|
|
|
|
|
|
static void
|
|
|
|
|
|
null_pointer_error (const char *func_name)
|
|
|
|
|
|
{
|
|
|
|
|
|
scm_error (sym_null_pointer_error, func_name,
|
|
|
|
|
|
"null pointer dereference", SCM_EOL, SCM_EOL);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-05 16:30:41 -05:00
|
|
|
|
static SCM cif_to_procedure (SCM cif, SCM func_ptr, int with_errno);
|
2010-01-25 18:15:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
static SCM pointer_weak_refs = SCM_BOOL_F;
|
2011-02-10 21:06:55 +01:00
|
|
|
|
|
2010-01-18 14:36:23 +01:00
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
|
register_weak_reference (SCM from, SCM to)
|
|
|
|
|
|
{
|
2011-10-23 23:38:51 +02:00
|
|
|
|
scm_weak_table_putq_x (pointer_weak_refs, from, to);
|
2010-01-18 14:36:23 +01:00
|
|
|
|
}
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
|
2010-01-04 12:11:33 +01:00
|
|
|
|
static void
|
2012-07-01 17:32:03 +02:00
|
|
|
|
pointer_finalizer_trampoline (void *ptr, void *data)
|
2010-01-04 12:11:33 +01:00
|
|
|
|
{
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
scm_t_pointer_finalizer finalizer = data;
|
2011-10-24 17:58:22 +02:00
|
|
|
|
finalizer (SCM_POINTER_VALUE (SCM_PACK_POINTER (ptr)));
|
2010-01-04 12:11:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-01-30 22:47:35 +01:00
|
|
|
|
SCM_DEFINE (scm_pointer_p, "pointer?", 1, 0, 0,
|
|
|
|
|
|
(SCM obj),
|
|
|
|
|
|
"Return @code{#t} if @var{obj} is a pointer object, "
|
|
|
|
|
|
"@code{#f} otherwise.\n")
|
|
|
|
|
|
#define FUNC_NAME s_scm_pointer_p
|
|
|
|
|
|
{
|
|
|
|
|
|
return scm_from_bool (SCM_POINTER_P (obj));
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
SCM_DEFINE (scm_make_pointer, "make-pointer", 1, 1, 0,
|
|
|
|
|
|
(SCM address, SCM finalizer),
|
|
|
|
|
|
"Return a foreign pointer object pointing to @var{address}. "
|
|
|
|
|
|
"If @var{finalizer} is passed, it should be a pointer to a "
|
|
|
|
|
|
"one-argument C function that will be called when the pointer "
|
|
|
|
|
|
"object becomes unreachable.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_make_pointer
|
|
|
|
|
|
{
|
|
|
|
|
|
void *c_finalizer;
|
2018-06-21 08:39:03 +02:00
|
|
|
|
uintptr_t c_address;
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
|
2013-05-23 14:18:47 +02:00
|
|
|
|
c_address = scm_to_uintptr_t (address);
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
if (SCM_UNBNDP (finalizer))
|
|
|
|
|
|
c_finalizer = NULL;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
SCM_VALIDATE_POINTER (2, finalizer);
|
|
|
|
|
|
c_finalizer = SCM_POINTER_VALUE (finalizer);
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2010-07-27 18:44:45 +02:00
|
|
|
|
return scm_from_pointer ((void *) c_address, c_finalizer);
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2012-02-02 17:23:35 -05:00
|
|
|
|
void *
|
|
|
|
|
|
scm_to_pointer (SCM pointer)
|
|
|
|
|
|
#define FUNC_NAME "scm_to_pointer"
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM_VALIDATE_POINTER (1, pointer);
|
|
|
|
|
|
return SCM_POINTER_VALUE (pointer);
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2010-01-04 12:11:33 +01:00
|
|
|
|
SCM
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
scm_from_pointer (void *ptr, scm_t_pointer_finalizer finalizer)
|
2010-01-04 12:11:33 +01:00
|
|
|
|
{
|
2010-01-07 10:40:38 +01:00
|
|
|
|
SCM ret;
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
|
2010-07-27 18:44:45 +02:00
|
|
|
|
if (ptr == NULL && finalizer == NULL)
|
|
|
|
|
|
ret = null_pointer;
|
|
|
|
|
|
else
|
2010-01-04 12:11:33 +01:00
|
|
|
|
{
|
2011-01-30 22:05:21 +01:00
|
|
|
|
ret = scm_cell (scm_tc7_pointer, (scm_t_bits) ptr);
|
2010-07-27 18:44:45 +02:00
|
|
|
|
|
|
|
|
|
|
if (finalizer)
|
2012-02-19 12:23:29 +01:00
|
|
|
|
scm_i_set_finalizer (SCM2PTR (ret), pointer_finalizer_trampoline,
|
|
|
|
|
|
finalizer);
|
2010-01-04 12:11:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2010-01-07 10:40:38 +01:00
|
|
|
|
return ret;
|
2010-01-04 12:11:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
SCM_DEFINE (scm_pointer_address, "pointer-address", 1, 0, 0,
|
|
|
|
|
|
(SCM pointer),
|
|
|
|
|
|
"Return the numerical value of @var{pointer}.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_pointer_address
|
2010-01-04 12:11:33 +01:00
|
|
|
|
{
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
SCM_VALIDATE_POINTER (1, pointer);
|
2010-04-09 00:30:10 +02:00
|
|
|
|
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_uintptr_t ((uintptr_t) SCM_POINTER_VALUE (pointer));
|
2010-01-04 12:11:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2011-03-17 10:39:02 +01:00
|
|
|
|
SCM_DEFINE (scm_pointer_to_scm, "pointer->scm", 1, 0, 0,
|
|
|
|
|
|
(SCM pointer),
|
|
|
|
|
|
"Unsafely cast @var{pointer} to a Scheme object.\n"
|
|
|
|
|
|
"Cross your fingers!")
|
|
|
|
|
|
#define FUNC_NAME s_scm_pointer_to_scm
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM_VALIDATE_POINTER (1, pointer);
|
|
|
|
|
|
|
|
|
|
|
|
return SCM_PACK ((scm_t_bits) SCM_POINTER_VALUE (pointer));
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_scm_to_pointer, "scm->pointer", 1, 0, 0,
|
|
|
|
|
|
(SCM scm),
|
|
|
|
|
|
"Return a foreign pointer object with the @code{object-address}\n"
|
|
|
|
|
|
"of @var{scm}.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_scm_to_pointer
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM ret;
|
|
|
|
|
|
|
|
|
|
|
|
ret = scm_from_pointer ((void*) SCM_UNPACK (scm), NULL);
|
2011-10-24 18:13:51 +02:00
|
|
|
|
if (SCM_HEAP_OBJECT_P (ret))
|
2011-03-17 10:39:02 +01:00
|
|
|
|
register_weak_reference (ret, scm);
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
SCM_DEFINE (scm_pointer_to_bytevector, "pointer->bytevector", 2, 2, 0,
|
|
|
|
|
|
(SCM pointer, SCM len, SCM offset, SCM uvec_type),
|
2010-07-28 12:01:54 +02:00
|
|
|
|
"Return a bytevector aliasing the @var{len} bytes pointed\n"
|
|
|
|
|
|
"to by @var{pointer}.\n\n"
|
2010-01-18 14:36:23 +01:00
|
|
|
|
"The user may specify an alternate default interpretation for\n"
|
|
|
|
|
|
"the memory by passing the @var{uvec_type} argument, to indicate\n"
|
|
|
|
|
|
"that the memory is an array of elements of that type.\n"
|
|
|
|
|
|
"@var{uvec_type} should be something that\n"
|
|
|
|
|
|
"@code{uniform-vector-element-type} would return, like @code{f32}\n"
|
|
|
|
|
|
"or @code{s16}.\n\n"
|
2010-07-28 12:01:54 +02:00
|
|
|
|
"When @var{offset} is passed, it specifies the offset in bytes\n"
|
|
|
|
|
|
"relative to @var{pointer} of the memory region aliased by the\n"
|
|
|
|
|
|
"returned bytevector.")
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
#define FUNC_NAME s_scm_pointer_to_bytevector
|
2010-01-18 14:36:23 +01:00
|
|
|
|
{
|
|
|
|
|
|
SCM ret;
|
2018-06-21 08:39:03 +02:00
|
|
|
|
int8_t *ptr;
|
2010-01-18 14:36:23 +01:00
|
|
|
|
size_t boffset, blen;
|
|
|
|
|
|
scm_t_array_element_type btype;
|
|
|
|
|
|
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
SCM_VALIDATE_POINTER (1, pointer);
|
|
|
|
|
|
ptr = SCM_POINTER_VALUE (pointer);
|
2010-03-18 00:27:38 +01:00
|
|
|
|
|
|
|
|
|
|
if (SCM_UNLIKELY (ptr == NULL))
|
2010-04-09 00:30:10 +02:00
|
|
|
|
null_pointer_error (FUNC_NAME);
|
2010-03-18 00:27:38 +01:00
|
|
|
|
|
2010-01-18 14:36:23 +01:00
|
|
|
|
if (SCM_UNBNDP (uvec_type))
|
|
|
|
|
|
btype = SCM_ARRAY_ELEMENT_TYPE_VU8;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
int i;
|
|
|
|
|
|
for (i = 0; i <= SCM_ARRAY_ELEMENT_TYPE_LAST; i++)
|
|
|
|
|
|
if (scm_is_eq (uvec_type, scm_i_array_element_types[i]))
|
|
|
|
|
|
break;
|
|
|
|
|
|
switch (i)
|
|
|
|
|
|
{
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_VU8:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_U8:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_S8:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_U16:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_S16:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_U32:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_S32:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_U64:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_S64:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_F32:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_F64:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_C32:
|
|
|
|
|
|
case SCM_ARRAY_ELEMENT_TYPE_C64:
|
|
|
|
|
|
btype = i;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, uvec_type,
|
|
|
|
|
|
"uniform vector type");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
|
2010-01-18 14:36:23 +01:00
|
|
|
|
if (SCM_UNBNDP (offset))
|
|
|
|
|
|
boffset = 0;
|
|
|
|
|
|
else
|
|
|
|
|
|
boffset = scm_to_size_t (offset);
|
|
|
|
|
|
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
blen = scm_to_size_t (len);
|
2010-01-18 14:36:23 +01:00
|
|
|
|
|
2012-01-14 22:22:22 +01:00
|
|
|
|
ret = scm_c_take_typed_bytevector ((signed char *) ptr + boffset,
|
2012-01-30 18:25:07 +01:00
|
|
|
|
blen, btype, pointer);
|
2011-05-07 14:57:15 +02:00
|
|
|
|
|
2010-01-18 14:36:23 +01:00
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2010-07-27 18:46:23 +02:00
|
|
|
|
SCM_DEFINE (scm_bytevector_to_pointer, "bytevector->pointer", 1, 1, 0,
|
|
|
|
|
|
(SCM bv, SCM offset),
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
"Return a pointer pointer aliasing the memory pointed to by\n"
|
2010-07-27 18:46:23 +02:00
|
|
|
|
"@var{bv} or @var{offset} bytes after @var{bv} when @var{offset}\n"
|
|
|
|
|
|
"is passed.")
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
#define FUNC_NAME s_scm_bytevector_to_pointer
|
2010-01-18 14:36:23 +01:00
|
|
|
|
{
|
|
|
|
|
|
SCM ret;
|
2012-01-14 22:22:22 +01:00
|
|
|
|
signed char *ptr;
|
2010-07-27 18:46:23 +02:00
|
|
|
|
size_t boffset;
|
2010-01-18 14:36:23 +01:00
|
|
|
|
|
|
|
|
|
|
SCM_VALIDATE_BYTEVECTOR (1, bv);
|
|
|
|
|
|
ptr = SCM_BYTEVECTOR_CONTENTS (bv);
|
2010-07-27 18:46:23 +02:00
|
|
|
|
|
2010-01-18 14:36:23 +01:00
|
|
|
|
if (SCM_UNBNDP (offset))
|
|
|
|
|
|
boffset = 0;
|
|
|
|
|
|
else
|
|
|
|
|
|
boffset = scm_to_unsigned_integer (offset, 0,
|
|
|
|
|
|
SCM_BYTEVECTOR_LENGTH (bv) - 1);
|
|
|
|
|
|
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
ret = scm_from_pointer (ptr + boffset, NULL);
|
2010-01-18 14:36:23 +01:00
|
|
|
|
register_weak_reference (ret, bv);
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
SCM_DEFINE (scm_set_pointer_finalizer_x, "set-pointer-finalizer!", 2, 0, 0,
|
|
|
|
|
|
(SCM pointer, SCM finalizer),
|
2010-01-27 22:12:58 +01:00
|
|
|
|
"Arrange for the C procedure wrapped by @var{finalizer} to be\n"
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
"called on the pointer wrapped by @var{pointer} when @var{pointer}\n"
|
2010-01-27 22:12:58 +01:00
|
|
|
|
"becomes unreachable. Note: the C procedure should not call into\n"
|
|
|
|
|
|
"Scheme. If you need a Scheme finalizer, use guardians.")
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
#define FUNC_NAME s_scm_set_pointer_finalizer_x
|
2010-01-27 22:12:58 +01:00
|
|
|
|
{
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
SCM_VALIDATE_POINTER (1, pointer);
|
|
|
|
|
|
SCM_VALIDATE_POINTER (2, finalizer);
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
|
2012-02-19 12:23:29 +01:00
|
|
|
|
scm_i_add_finalizer (SCM2PTR (pointer), pointer_finalizer_trampoline,
|
|
|
|
|
|
SCM_POINTER_VALUE (finalizer));
|
2010-01-27 22:12:58 +01:00
|
|
|
|
|
|
|
|
|
|
return SCM_UNSPECIFIED;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2010-01-04 12:11:33 +01:00
|
|
|
|
void
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
scm_i_pointer_print (SCM pointer, SCM port, scm_print_state *pstate)
|
2010-01-04 12:11:33 +01:00
|
|
|
|
{
|
2016-04-26 23:07:28 +02:00
|
|
|
|
scm_puts ("#<pointer 0x", port);
|
2013-05-23 14:18:47 +02:00
|
|
|
|
scm_uintprint (scm_to_uintptr_t (scm_pointer_address (pointer)), 16, port);
|
2016-04-26 23:01:14 +02:00
|
|
|
|
scm_putc ('>', port);
|
2010-01-04 12:11:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2010-08-15 16:42:33 +02:00
|
|
|
|
|
|
|
|
|
|
/* Non-primitive helpers functions. These procedures could be
|
|
|
|
|
|
implemented in terms of the primitives above but would be inefficient
|
|
|
|
|
|
(heap allocation overhead, Scheme/C round trips, etc.) */
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_dereference_pointer, "dereference-pointer", 1, 0, 0,
|
|
|
|
|
|
(SCM pointer),
|
|
|
|
|
|
"Assuming @var{pointer} points to a memory region that\n"
|
|
|
|
|
|
"holds a pointer, return this pointer.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_dereference_pointer
|
|
|
|
|
|
{
|
2013-08-09 18:23:56 -04:00
|
|
|
|
void **ptr;
|
|
|
|
|
|
|
2010-08-15 16:42:33 +02:00
|
|
|
|
SCM_VALIDATE_POINTER (1, pointer);
|
|
|
|
|
|
|
2013-08-09 18:23:56 -04:00
|
|
|
|
ptr = SCM_POINTER_VALUE (pointer);
|
|
|
|
|
|
if (SCM_UNLIKELY (ptr == NULL))
|
|
|
|
|
|
null_pointer_error (FUNC_NAME);
|
|
|
|
|
|
|
|
|
|
|
|
return scm_from_pointer (*ptr, NULL);
|
2010-08-15 16:42:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2011-04-01 13:31:26 +02:00
|
|
|
|
SCM_DEFINE (scm_string_to_pointer, "string->pointer", 1, 1, 0,
|
|
|
|
|
|
(SCM string, SCM encoding),
|
2010-08-15 16:42:33 +02:00
|
|
|
|
"Return a foreign pointer to a nul-terminated copy of\n"
|
2011-04-01 13:31:26 +02:00
|
|
|
|
"@var{string} in the given @var{encoding}, defaulting to\n"
|
|
|
|
|
|
"the current locale encoding. The C string is freed when\n"
|
|
|
|
|
|
"the returned foreign pointer becomes unreachable.\n\n"
|
|
|
|
|
|
"This is the Scheme equivalent of @code{scm_to_stringn}.")
|
2010-08-15 16:42:33 +02:00
|
|
|
|
#define FUNC_NAME s_scm_string_to_pointer
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM_VALIDATE_STRING (1, string);
|
|
|
|
|
|
|
|
|
|
|
|
/* XXX: Finalizers slow down libgc; they could be avoided if
|
|
|
|
|
|
`scm_to_string' & co. were able to use libgc-allocated memory. */
|
|
|
|
|
|
|
2011-04-01 13:31:26 +02:00
|
|
|
|
if (SCM_UNBNDP (encoding))
|
|
|
|
|
|
return scm_from_pointer (scm_to_locale_string (string), free);
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
char *enc;
|
|
|
|
|
|
SCM ret;
|
|
|
|
|
|
|
|
|
|
|
|
SCM_VALIDATE_STRING (2, encoding);
|
|
|
|
|
|
|
|
|
|
|
|
enc = scm_to_locale_string (encoding);
|
|
|
|
|
|
scm_dynwind_begin (0);
|
|
|
|
|
|
scm_dynwind_free (enc);
|
|
|
|
|
|
|
|
|
|
|
|
ret = scm_from_pointer
|
|
|
|
|
|
(scm_to_stringn (string, NULL, enc,
|
Port conversion strategies internally are symbols
* libguile/ports.h (scm_t_port): Represent the conversion strategy as a
symbol, to make things easier for Scheme. Rename to
"conversion_strategy".
(scm_c_make_port_with_encoding): Change to take encoding and
conversion_strategy arguments as symbols.
(scm_i_string_failed_conversion_handler): New internal helper, to turn
a symbol to a scm_t_string_failed_conversion_handler.
(scm_i_default_port_encoding): Return the default port encoding as a
symbol.
(scm_i_default_port_conversion_strategy)
(scm_i_set_default_port_conversion_strategy): Rename from
scm_i_default_port_conversion_handler et al. Take and return Scheme
symbols.
* libguile/foreign.c (scm_string_to_pointer, scm_pointer_to_string): Use
scm_i_default_string_failed_conversion_handler instead of
scm_i_default_port_conversion_handler.
* libguile/print.c (PORT_CONVERSION_HANDLER): Update definition.
(print_normal_symbol): Use PORT_CONVERSION_HANDLER.
* libguile/r6rs-ports.c (make_bytevector_input_port):
(make_custom_binary_input_port, make_bytevector_output_port): Adapt to
changes in scm_c_make_port_with_encoding.
* libguile/strings.h:
* libguile/strings.c (scm_i_default_string_failed_conversion_handler):
New helper.
(scm_from_locale_stringn, scm_from_port_stringn):
(scm_to_locale_stringn, scm_to_port_stringn): Adapt to interface
changes.
* libguile/strports.c (scm_mkstrport): Adapt to
scm_c_make_port_with_encoding change.
* libguile/ports.c (scm_c_make_port): Adapt to
scm_c_make_port_with_encoding change.
(ascii_toupper, encoding_matches, canonicalize_encoding): Move down in
the file.
(peek_codepoint, get_codepoint, scm_ungetc): Adapt to port conversion
strategy change. Remove duplicate case in get_codepoint.
(scm_init_ports): Move symbol initializations to the same place.
2016-05-04 10:31:21 +02:00
|
|
|
|
scm_i_default_string_failed_conversion_handler ()),
|
2011-04-01 13:31:26 +02:00
|
|
|
|
free);
|
|
|
|
|
|
|
|
|
|
|
|
scm_dynwind_end ();
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
2010-08-15 16:42:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2011-04-01 13:31:26 +02:00
|
|
|
|
SCM_DEFINE (scm_pointer_to_string, "pointer->string", 1, 2, 0,
|
|
|
|
|
|
(SCM pointer, SCM length, SCM encoding),
|
|
|
|
|
|
"Return the string representing the C string pointed to by\n"
|
|
|
|
|
|
"@var{pointer}. If @var{length} is omitted or @code{-1}, the\n"
|
|
|
|
|
|
"string is assumed to be nul-terminated. Otherwise\n"
|
|
|
|
|
|
"@var{length} is the number of bytes in memory pointed to by\n"
|
|
|
|
|
|
"@var{pointer}. The C string is assumed to be in the given\n"
|
|
|
|
|
|
"@var{encoding}, defaulting to the current locale encoding.\n\n"
|
|
|
|
|
|
"This is the Scheme equivalent of @code{scm_from_stringn}.")
|
2010-08-15 16:42:33 +02:00
|
|
|
|
#define FUNC_NAME s_scm_pointer_to_string
|
|
|
|
|
|
{
|
2011-04-01 13:31:26 +02:00
|
|
|
|
size_t len;
|
|
|
|
|
|
|
2010-08-15 16:42:33 +02:00
|
|
|
|
SCM_VALIDATE_POINTER (1, pointer);
|
|
|
|
|
|
|
2011-04-01 13:31:26 +02:00
|
|
|
|
if (SCM_UNBNDP (length)
|
|
|
|
|
|
|| scm_is_true (scm_eqv_p (length, scm_from_int (-1))))
|
|
|
|
|
|
len = (size_t)-1;
|
|
|
|
|
|
else
|
|
|
|
|
|
len = scm_to_size_t (length);
|
|
|
|
|
|
|
|
|
|
|
|
if (SCM_UNBNDP (encoding))
|
|
|
|
|
|
return scm_from_locale_stringn (SCM_POINTER_VALUE (pointer), len);
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
char *enc;
|
|
|
|
|
|
SCM ret;
|
|
|
|
|
|
|
|
|
|
|
|
SCM_VALIDATE_STRING (3, encoding);
|
|
|
|
|
|
|
|
|
|
|
|
enc = scm_to_locale_string (encoding);
|
|
|
|
|
|
scm_dynwind_begin (0);
|
|
|
|
|
|
scm_dynwind_free (enc);
|
|
|
|
|
|
|
|
|
|
|
|
ret = scm_from_stringn (SCM_POINTER_VALUE (pointer), len, enc,
|
Port conversion strategies internally are symbols
* libguile/ports.h (scm_t_port): Represent the conversion strategy as a
symbol, to make things easier for Scheme. Rename to
"conversion_strategy".
(scm_c_make_port_with_encoding): Change to take encoding and
conversion_strategy arguments as symbols.
(scm_i_string_failed_conversion_handler): New internal helper, to turn
a symbol to a scm_t_string_failed_conversion_handler.
(scm_i_default_port_encoding): Return the default port encoding as a
symbol.
(scm_i_default_port_conversion_strategy)
(scm_i_set_default_port_conversion_strategy): Rename from
scm_i_default_port_conversion_handler et al. Take and return Scheme
symbols.
* libguile/foreign.c (scm_string_to_pointer, scm_pointer_to_string): Use
scm_i_default_string_failed_conversion_handler instead of
scm_i_default_port_conversion_handler.
* libguile/print.c (PORT_CONVERSION_HANDLER): Update definition.
(print_normal_symbol): Use PORT_CONVERSION_HANDLER.
* libguile/r6rs-ports.c (make_bytevector_input_port):
(make_custom_binary_input_port, make_bytevector_output_port): Adapt to
changes in scm_c_make_port_with_encoding.
* libguile/strings.h:
* libguile/strings.c (scm_i_default_string_failed_conversion_handler):
New helper.
(scm_from_locale_stringn, scm_from_port_stringn):
(scm_to_locale_stringn, scm_to_port_stringn): Adapt to interface
changes.
* libguile/strports.c (scm_mkstrport): Adapt to
scm_c_make_port_with_encoding change.
* libguile/ports.c (scm_c_make_port): Adapt to
scm_c_make_port_with_encoding change.
(ascii_toupper, encoding_matches, canonicalize_encoding): Move down in
the file.
(peek_codepoint, get_codepoint, scm_ungetc): Adapt to port conversion
strategy change. Remove duplicate case in get_codepoint.
(scm_init_ports): Move symbol initializations to the same place.
2016-05-04 10:31:21 +02:00
|
|
|
|
scm_i_default_string_failed_conversion_handler ());
|
2011-04-01 13:31:26 +02:00
|
|
|
|
|
|
|
|
|
|
scm_dynwind_end ();
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
2010-08-15 16:42:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2010-01-25 18:15:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2010-04-06 23:17:10 +02:00
|
|
|
|
SCM_DEFINE (scm_alignof, "alignof", 1, 0, 0, (SCM type),
|
|
|
|
|
|
"Return the alignment of @var{type}, in bytes.\n\n"
|
|
|
|
|
|
"@var{type} should be a valid C type, like @code{int}.\n"
|
|
|
|
|
|
"Alternately @var{type} may be the symbol @code{*}, in which\n"
|
|
|
|
|
|
"case the alignment of a pointer is returned. @var{type} may\n"
|
|
|
|
|
|
"also be a list of types, in which case the alignment of a\n"
|
|
|
|
|
|
"@code{struct} with ABI-conventional packing is returned.")
|
2010-01-26 22:16:35 +01:00
|
|
|
|
#define FUNC_NAME s_scm_alignof
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SCM_I_INUMP (type))
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (SCM_I_INUM (type))
|
|
|
|
|
|
{
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_FLOAT:
|
2011-12-14 23:44:30 +01:00
|
|
|
|
return scm_from_size_t (alignof_type (float));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_DOUBLE:
|
2011-12-14 23:44:30 +01:00
|
|
|
|
return scm_from_size_t (alignof_type (double));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_UINT8:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (alignof_type (uint8_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_INT8:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (alignof_type (int8_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_UINT16:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (alignof_type (uint16_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_INT16:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (alignof_type (int16_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_UINT32:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (alignof_type (uint32_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_INT32:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (alignof_type (int32_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_UINT64:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (alignof_type (uint64_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_INT64:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (alignof_type (int64_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
default:
|
|
|
|
|
|
scm_wrong_type_arg (FUNC_NAME, 1, type);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2010-01-27 22:12:58 +01:00
|
|
|
|
else if (scm_is_eq (type, sym_asterisk))
|
|
|
|
|
|
/* a pointer */
|
2011-12-14 23:44:30 +01:00
|
|
|
|
return scm_from_size_t (alignof_type (void*));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
else if (scm_is_pair (type))
|
2011-03-10 22:24:23 +01:00
|
|
|
|
{
|
|
|
|
|
|
/* TYPE is a structure. Section 3-3 of the i386, x86_64, PowerPC,
|
|
|
|
|
|
and SPARC P.S. of the System V ABI all say: "Aggregates
|
|
|
|
|
|
(structures and arrays) and unions assume the alignment of
|
|
|
|
|
|
their most strictly aligned component." */
|
|
|
|
|
|
size_t max;
|
|
|
|
|
|
|
|
|
|
|
|
for (max = 0; scm_is_pair (type); type = SCM_CDR (type))
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t align;
|
|
|
|
|
|
|
|
|
|
|
|
align = scm_to_size_t (scm_alignof (SCM_CAR (type)));
|
|
|
|
|
|
if (align > max)
|
|
|
|
|
|
max = align;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return scm_from_size_t (max);
|
|
|
|
|
|
}
|
2010-01-26 22:16:35 +01:00
|
|
|
|
else
|
|
|
|
|
|
scm_wrong_type_arg (FUNC_NAME, 1, type);
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2010-04-06 23:17:10 +02:00
|
|
|
|
SCM_DEFINE (scm_sizeof, "sizeof", 1, 0, 0, (SCM type),
|
|
|
|
|
|
"Return the size of @var{type}, in bytes.\n\n"
|
|
|
|
|
|
"@var{type} should be a valid C type, like @code{int}.\n"
|
|
|
|
|
|
"Alternately @var{type} may be the symbol @code{*}, in which\n"
|
|
|
|
|
|
"case the size of a pointer is returned. @var{type} may also\n"
|
|
|
|
|
|
"be a list of types, in which case the size of a @code{struct}\n"
|
|
|
|
|
|
"with ABI-conventional packing is returned.")
|
2010-01-26 22:16:35 +01:00
|
|
|
|
#define FUNC_NAME s_scm_sizeof
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SCM_I_INUMP (type))
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (SCM_I_INUM (type))
|
|
|
|
|
|
{
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_FLOAT:
|
|
|
|
|
|
return scm_from_size_t (sizeof (float));
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_DOUBLE:
|
|
|
|
|
|
return scm_from_size_t (sizeof (double));
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_UINT8:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (sizeof (uint8_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_INT8:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (sizeof (int8_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_UINT16:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (sizeof (uint16_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_INT16:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (sizeof (int16_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_UINT32:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (sizeof (uint32_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_INT32:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (sizeof (int32_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_UINT64:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (sizeof (uint64_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
case SCM_FOREIGN_TYPE_INT64:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_size_t (sizeof (int64_t));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
default:
|
|
|
|
|
|
scm_wrong_type_arg (FUNC_NAME, 1, type);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2010-01-27 22:12:58 +01:00
|
|
|
|
else if (scm_is_eq (type, sym_asterisk))
|
|
|
|
|
|
/* a pointer */
|
|
|
|
|
|
return scm_from_size_t (sizeof (void*));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
else if (scm_is_pair (type))
|
|
|
|
|
|
{
|
|
|
|
|
|
/* a struct */
|
|
|
|
|
|
size_t off = 0;
|
2012-05-21 00:30:45 -04:00
|
|
|
|
size_t align = scm_to_size_t (scm_alignof(type));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
while (scm_is_pair (type))
|
|
|
|
|
|
{
|
|
|
|
|
|
off = ROUND_UP (off, scm_to_size_t (scm_alignof (scm_car (type))));
|
|
|
|
|
|
off += scm_to_size_t (scm_sizeof (scm_car (type)));
|
|
|
|
|
|
type = scm_cdr (type);
|
|
|
|
|
|
}
|
2012-05-21 00:30:45 -04:00
|
|
|
|
return scm_from_size_t (ROUND_UP(off, align));
|
2010-01-26 22:16:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
scm_wrong_type_arg (FUNC_NAME, 1, type);
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-25 18:15:35 +01:00
|
|
|
|
/* return 1 on success, 0 on failure */
|
|
|
|
|
|
static int
|
|
|
|
|
|
parse_ffi_type (SCM type, int return_p, long *n_structs, long *n_struct_elts)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SCM_I_INUMP (type))
|
|
|
|
|
|
{
|
|
|
|
|
|
if ((SCM_I_INUM (type) < 0 )
|
|
|
|
|
|
|| (SCM_I_INUM (type) > SCM_FOREIGN_TYPE_LAST))
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
else if (SCM_I_INUM (type) == SCM_FOREIGN_TYPE_VOID && !return_p)
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
else
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
2010-01-27 22:12:58 +01:00
|
|
|
|
else if (scm_is_eq (type, sym_asterisk))
|
|
|
|
|
|
/* a pointer */
|
|
|
|
|
|
return 1;
|
2010-01-25 18:15:35 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
long len;
|
|
|
|
|
|
|
|
|
|
|
|
len = scm_ilength (type);
|
|
|
|
|
|
if (len < 1)
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
while (len--)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!parse_ffi_type (scm_car (type), 0, n_structs, n_struct_elts))
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
(*n_struct_elts)++;
|
|
|
|
|
|
type = scm_cdr (type);
|
|
|
|
|
|
}
|
|
|
|
|
|
(*n_structs)++;
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
|
fill_ffi_type (SCM type, ffi_type *ftype, ffi_type ***type_ptrs,
|
|
|
|
|
|
ffi_type **types)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SCM_I_INUMP (type))
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (SCM_I_INUM (type))
|
|
|
|
|
|
{
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_FLOAT:
|
|
|
|
|
|
*ftype = ffi_type_float;
|
|
|
|
|
|
return;
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_DOUBLE:
|
|
|
|
|
|
*ftype = ffi_type_double;
|
|
|
|
|
|
return;
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_UINT8:
|
|
|
|
|
|
*ftype = ffi_type_uint8;
|
|
|
|
|
|
return;
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_INT8:
|
|
|
|
|
|
*ftype = ffi_type_sint8;
|
|
|
|
|
|
return;
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_UINT16:
|
|
|
|
|
|
*ftype = ffi_type_uint16;
|
|
|
|
|
|
return;
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_INT16:
|
|
|
|
|
|
*ftype = ffi_type_sint16;
|
|
|
|
|
|
return;
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_UINT32:
|
|
|
|
|
|
*ftype = ffi_type_uint32;
|
|
|
|
|
|
return;
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_INT32:
|
|
|
|
|
|
*ftype = ffi_type_sint32;
|
|
|
|
|
|
return;
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_UINT64:
|
|
|
|
|
|
*ftype = ffi_type_uint64;
|
|
|
|
|
|
return;
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_INT64:
|
|
|
|
|
|
*ftype = ffi_type_sint64;
|
|
|
|
|
|
return;
|
|
|
|
|
|
case SCM_FOREIGN_TYPE_VOID:
|
|
|
|
|
|
*ftype = ffi_type_void;
|
|
|
|
|
|
return;
|
|
|
|
|
|
default:
|
2010-09-06 22:24:44 +02:00
|
|
|
|
scm_wrong_type_arg_msg ("pointer->procedure", 0, type,
|
2010-01-26 22:55:13 +01:00
|
|
|
|
"foreign type");
|
2010-01-25 18:15:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2010-01-27 22:12:58 +01:00
|
|
|
|
else if (scm_is_eq (type, sym_asterisk))
|
|
|
|
|
|
/* a pointer */
|
|
|
|
|
|
{
|
|
|
|
|
|
*ftype = ffi_type_pointer;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2010-01-25 18:15:35 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
long i, len;
|
|
|
|
|
|
|
|
|
|
|
|
len = scm_ilength (type);
|
|
|
|
|
|
|
|
|
|
|
|
ftype->size = 0;
|
|
|
|
|
|
ftype->alignment = 0;
|
|
|
|
|
|
ftype->type = FFI_TYPE_STRUCT;
|
|
|
|
|
|
ftype->elements = *type_ptrs;
|
|
|
|
|
|
*type_ptrs += len + 1;
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
|
{
|
2010-01-26 22:16:35 +01:00
|
|
|
|
ftype->elements[i] = *types;
|
|
|
|
|
|
*types += 1;
|
2010-01-25 18:15:35 +01:00
|
|
|
|
fill_ffi_type (scm_car (type), ftype->elements[i],
|
|
|
|
|
|
type_ptrs, types);
|
|
|
|
|
|
type = scm_cdr (type);
|
|
|
|
|
|
}
|
|
|
|
|
|
ftype->elements[i] = NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2010-09-03 15:12:54 +02:00
|
|
|
|
|
|
|
|
|
|
/* Return a "cif" (call interface) for the given RETURN_TYPE and
|
|
|
|
|
|
ARG_TYPES. */
|
|
|
|
|
|
static ffi_cif *
|
|
|
|
|
|
make_cif (SCM return_type, SCM arg_types, const char *caller)
|
|
|
|
|
|
#define FUNC_NAME caller
|
2010-01-25 18:15:35 +01:00
|
|
|
|
{
|
2010-09-03 15:12:54 +02:00
|
|
|
|
SCM walk;
|
2010-01-25 18:15:35 +01:00
|
|
|
|
long i, nargs, n_structs, n_struct_elts;
|
|
|
|
|
|
size_t cif_len;
|
|
|
|
|
|
char *mem;
|
|
|
|
|
|
ffi_cif *cif;
|
|
|
|
|
|
ffi_type **type_ptrs;
|
|
|
|
|
|
ffi_type *types;
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
|
2010-01-25 18:15:35 +01:00
|
|
|
|
nargs = scm_ilength (arg_types);
|
|
|
|
|
|
SCM_ASSERT (nargs >= 0, arg_types, 3, FUNC_NAME);
|
|
|
|
|
|
/* fixme: assert nargs < 1<<32 */
|
|
|
|
|
|
n_structs = n_struct_elts = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/* For want of talloc, we're going to have to do this in two passes: first we
|
|
|
|
|
|
figure out how much memory is needed for all types, then we allocate the
|
|
|
|
|
|
cif and the types all in one block. */
|
|
|
|
|
|
if (!parse_ffi_type (return_type, 1, &n_structs, &n_struct_elts))
|
|
|
|
|
|
scm_wrong_type_arg (FUNC_NAME, 1, return_type);
|
|
|
|
|
|
for (walk = arg_types; scm_is_pair (walk); walk = scm_cdr (walk))
|
|
|
|
|
|
if (!parse_ffi_type (scm_car (walk), 0, &n_structs, &n_struct_elts))
|
|
|
|
|
|
scm_wrong_type_arg (FUNC_NAME, 3, scm_car (walk));
|
2010-09-03 15:12:54 +02:00
|
|
|
|
|
2010-01-25 18:15:35 +01:00
|
|
|
|
/* the memory: with space for the cif itself */
|
|
|
|
|
|
cif_len = sizeof (ffi_cif);
|
|
|
|
|
|
|
|
|
|
|
|
/* then ffi_type pointers: one for each arg, one for each struct
|
|
|
|
|
|
element, and one for each struct (for null-termination) */
|
2011-12-14 23:44:30 +01:00
|
|
|
|
cif_len = (ROUND_UP (cif_len, alignof_type (void *))
|
2010-09-03 15:12:54 +02:00
|
|
|
|
+ (nargs + n_structs + n_struct_elts)*sizeof(void*));
|
|
|
|
|
|
|
2010-01-25 18:15:35 +01:00
|
|
|
|
/* then the ffi_type structs themselves, one per arg and struct element, and
|
|
|
|
|
|
one for the return val */
|
2011-12-14 23:44:30 +01:00
|
|
|
|
cif_len = (ROUND_UP (cif_len, alignof_type (ffi_type))
|
2010-09-03 15:12:54 +02:00
|
|
|
|
+ (nargs + n_struct_elts + 1)*sizeof(ffi_type));
|
2010-03-16 23:51:21 +01:00
|
|
|
|
|
|
|
|
|
|
mem = scm_gc_malloc_pointerless (cif_len, "foreign");
|
2010-12-04 19:31:20 +01:00
|
|
|
|
/* ensure all the memory is initialized, even the holes */
|
|
|
|
|
|
memset (mem, 0, cif_len);
|
2010-03-16 23:51:21 +01:00
|
|
|
|
cif = (ffi_cif *) mem;
|
|
|
|
|
|
|
2010-01-25 18:15:35 +01:00
|
|
|
|
/* reuse cif_len to walk through the mem */
|
2011-12-14 23:44:30 +01:00
|
|
|
|
cif_len = ROUND_UP (sizeof (ffi_cif), alignof_type (void *));
|
2010-01-25 18:15:35 +01:00
|
|
|
|
type_ptrs = (ffi_type**)(mem + cif_len);
|
|
|
|
|
|
cif_len = ROUND_UP (cif_len
|
2010-09-03 15:12:54 +02:00
|
|
|
|
+ (nargs + n_structs + n_struct_elts)*sizeof(void*),
|
2011-12-14 23:44:30 +01:00
|
|
|
|
alignof_type (ffi_type));
|
2010-01-25 18:15:35 +01:00
|
|
|
|
types = (ffi_type*)(mem + cif_len);
|
2010-09-03 15:12:54 +02:00
|
|
|
|
|
2010-01-25 18:15:35 +01:00
|
|
|
|
/* whew. now knit the pointers together. */
|
|
|
|
|
|
cif->rtype = types++;
|
|
|
|
|
|
fill_ffi_type (return_type, cif->rtype, &type_ptrs, &types);
|
|
|
|
|
|
cif->arg_types = type_ptrs;
|
|
|
|
|
|
type_ptrs += nargs;
|
|
|
|
|
|
for (walk = arg_types, i = 0; scm_is_pair (walk); walk = scm_cdr (walk), i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
cif->arg_types[i] = types++;
|
|
|
|
|
|
fill_ffi_type (scm_car (walk), cif->arg_types[i], &type_ptrs, &types);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* round out the cif, and we're done. */
|
|
|
|
|
|
cif->abi = FFI_DEFAULT_ABI;
|
|
|
|
|
|
cif->nargs = nargs;
|
|
|
|
|
|
cif->bytes = 0;
|
|
|
|
|
|
cif->flags = 0;
|
2010-09-03 15:12:54 +02:00
|
|
|
|
|
2010-01-25 18:15:35 +01:00
|
|
|
|
if (FFI_OK != ffi_prep_cif (cif, FFI_DEFAULT_ABI, cif->nargs, cif->rtype,
|
2010-09-03 15:12:54 +02:00
|
|
|
|
cif->arg_types))
|
|
|
|
|
|
SCM_MISC_ERROR ("ffi_prep_cif failed", SCM_EOL);
|
|
|
|
|
|
|
|
|
|
|
|
return cif;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2016-01-05 16:30:41 -05:00
|
|
|
|
static SCM
|
|
|
|
|
|
pointer_to_procedure (SCM return_type, SCM func_ptr, SCM arg_types,
|
|
|
|
|
|
int with_errno)
|
|
|
|
|
|
#define FUNC_NAME "pointer->procedure"
|
2010-09-03 15:12:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
ffi_cif *cif;
|
|
|
|
|
|
|
|
|
|
|
|
SCM_VALIDATE_POINTER (2, func_ptr);
|
2010-01-25 18:15:35 +01:00
|
|
|
|
|
2010-09-03 15:12:54 +02:00
|
|
|
|
cif = make_cif (return_type, arg_types, FUNC_NAME);
|
|
|
|
|
|
|
2016-01-05 16:30:41 -05:00
|
|
|
|
return cif_to_procedure (scm_from_pointer (cif, NULL), func_ptr,
|
|
|
|
|
|
with_errno);
|
2010-01-25 18:15:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2016-01-05 16:30:41 -05:00
|
|
|
|
SCM
|
|
|
|
|
|
scm_pointer_to_procedure (SCM return_type, SCM func_ptr, SCM arg_types)
|
|
|
|
|
|
{
|
|
|
|
|
|
return pointer_to_procedure (return_type, func_ptr, arg_types, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCM
|
|
|
|
|
|
scm_pointer_to_procedure_with_errno (SCM return_type, SCM func_ptr,
|
|
|
|
|
|
SCM arg_types)
|
|
|
|
|
|
{
|
|
|
|
|
|
return pointer_to_procedure (return_type, func_ptr, arg_types, 1);
|
|
|
|
|
|
}
|
2010-01-25 18:15:35 +01:00
|
|
|
|
|
2016-01-05 16:30:41 -05:00
|
|
|
|
SCM_KEYWORD (k_return_errno, "return-errno?");
|
2010-01-25 18:15:35 +01:00
|
|
|
|
|
2016-01-05 16:30:41 -05:00
|
|
|
|
SCM_INTERNAL SCM scm_i_pointer_to_procedure (SCM, SCM, SCM, SCM);
|
|
|
|
|
|
SCM_DEFINE (scm_i_pointer_to_procedure, "pointer->procedure", 3, 0, 1,
|
|
|
|
|
|
(SCM return_type, SCM func_ptr, SCM arg_types, SCM keyword_args),
|
|
|
|
|
|
"Make a foreign function.\n\n"
|
|
|
|
|
|
"Given the foreign void pointer @var{func_ptr}, its argument and\n"
|
|
|
|
|
|
"return types @var{arg_types} and @var{return_type}, return a\n"
|
|
|
|
|
|
"procedure that will pass arguments to the foreign function\n"
|
|
|
|
|
|
"and return appropriate values.\n\n"
|
|
|
|
|
|
"@var{arg_types} should be a list of foreign types.\n"
|
|
|
|
|
|
"@code{return_type} should be a foreign type.\n"
|
|
|
|
|
|
"If the @code{#:return-errno?} keyword argument is provided and\n"
|
|
|
|
|
|
"its value is true, then the returned procedure will return two\n"
|
|
|
|
|
|
"values, with @code{errno} as the second value.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_i_pointer_to_procedure
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM return_errno = SCM_BOOL_F;
|
2013-10-18 17:41:33 +02:00
|
|
|
|
|
2016-01-05 16:30:41 -05:00
|
|
|
|
scm_c_bind_keyword_arguments (FUNC_NAME, keyword_args, 0,
|
|
|
|
|
|
k_return_errno, &return_errno,
|
|
|
|
|
|
SCM_UNDEFINED);
|
2013-10-18 17:41:33 +02:00
|
|
|
|
|
2016-01-05 16:30:41 -05:00
|
|
|
|
return pointer_to_procedure (return_type, func_ptr, arg_types,
|
|
|
|
|
|
scm_to_bool (return_errno));
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
2010-01-25 18:15:35 +01:00
|
|
|
|
|
2016-01-05 16:30:41 -05:00
|
|
|
|
|
2013-10-18 17:41:33 +02:00
|
|
|
|
|
2018-06-21 08:39:03 +02:00
|
|
|
|
static const uint32_t *
|
2016-01-05 16:30:41 -05:00
|
|
|
|
get_foreign_stub_code (unsigned int nargs, int with_errno)
|
2013-02-26 17:25:51 -05:00
|
|
|
|
{
|
2016-01-05 16:30:41 -05:00
|
|
|
|
size_t i;
|
|
|
|
|
|
size_t code_len = with_errno ? 4 : 5;
|
2018-08-06 13:35:41 +02:00
|
|
|
|
uint32_t *ret, *code;
|
2016-01-05 16:30:41 -05:00
|
|
|
|
|
|
|
|
|
|
if (nargs >= (1 << 24) + 1)
|
|
|
|
|
|
scm_misc_error ("make-foreign-function", "too many arguments: ~a",
|
|
|
|
|
|
scm_list_1 (scm_from_uint (nargs)));
|
|
|
|
|
|
|
2018-08-06 13:35:41 +02:00
|
|
|
|
ret = scm_i_alloc_primitive_code_with_instrumentation (code_len, &code);
|
|
|
|
|
|
|
2016-01-05 16:30:41 -05:00
|
|
|
|
i = 0;
|
|
|
|
|
|
code[i++] = SCM_PACK_OP_24 (assert_nargs_ee, nargs + 1);
|
|
|
|
|
|
code[i++] = SCM_PACK_OP_12_12 (foreign_call, 0, 1);
|
|
|
|
|
|
code[i++] = SCM_PACK_OP_24 (handle_interrupts, 0);
|
|
|
|
|
|
if (!with_errno)
|
2018-07-19 13:56:13 +02:00
|
|
|
|
code[i++] = SCM_PACK_OP_24 (reset_frame, 1);
|
2016-01-05 16:30:41 -05:00
|
|
|
|
code[i++] = SCM_PACK_OP_24 (return_values, 0);
|
|
|
|
|
|
|
2018-08-06 13:35:41 +02:00
|
|
|
|
return ret;
|
2013-02-26 17:25:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static SCM
|
2016-01-05 16:30:41 -05:00
|
|
|
|
cif_to_procedure (SCM cif, SCM func_ptr, int with_errno)
|
2013-02-26 17:25:51 -05:00
|
|
|
|
{
|
|
|
|
|
|
ffi_cif *c_cif;
|
2013-10-18 17:41:33 +02:00
|
|
|
|
SCM ret;
|
|
|
|
|
|
scm_t_bits nfree = 2;
|
|
|
|
|
|
scm_t_bits flags = SCM_F_PROGRAM_IS_FOREIGN;
|
2013-02-26 17:25:51 -05:00
|
|
|
|
|
|
|
|
|
|
c_cif = (ffi_cif *) SCM_POINTER_VALUE (cif);
|
2013-10-18 17:41:33 +02:00
|
|
|
|
|
2013-11-19 18:24:22 +01:00
|
|
|
|
ret = scm_words (scm_tc7_program | (nfree << 16) | flags, nfree + 2);
|
2016-01-05 16:30:41 -05:00
|
|
|
|
SCM_SET_CELL_WORD_1 (ret, get_foreign_stub_code (c_cif->nargs, with_errno));
|
2013-11-19 18:28:19 +01:00
|
|
|
|
SCM_PROGRAM_FREE_VARIABLE_SET (ret, 0, cif);
|
|
|
|
|
|
SCM_PROGRAM_FREE_VARIABLE_SET (ret, 1, func_ptr);
|
2010-01-25 18:15:35 +01:00
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-03-20 17:00:38 +01:00
|
|
|
|
/* Set *LOC to the foreign representation of X with TYPE. */
|
2010-01-25 18:04:45 +01:00
|
|
|
|
static void
|
2011-12-03 12:17:46 +01:00
|
|
|
|
unpack (const ffi_type *type, void *loc, SCM x, int return_value_p)
|
2018-06-26 09:22:07 +02:00
|
|
|
|
#define FUNC_NAME "foreign-call"
|
2010-01-25 18:04:45 +01:00
|
|
|
|
{
|
|
|
|
|
|
switch (type->type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case FFI_TYPE_FLOAT:
|
2010-03-20 17:00:38 +01:00
|
|
|
|
*(float *) loc = scm_to_double (x);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case FFI_TYPE_DOUBLE:
|
2010-03-20 17:00:38 +01:00
|
|
|
|
*(double *) loc = scm_to_double (x);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
2011-12-03 12:17:46 +01:00
|
|
|
|
|
|
|
|
|
|
/* For integer return values smaller than `int', libffi expects the
|
|
|
|
|
|
result in an `ffi_arg'-long buffer. */
|
|
|
|
|
|
|
2010-01-25 18:04:45 +01:00
|
|
|
|
case FFI_TYPE_UINT8:
|
2011-12-03 12:17:46 +01:00
|
|
|
|
if (return_value_p)
|
|
|
|
|
|
*(ffi_arg *) loc = scm_to_uint8 (x);
|
|
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
*(uint8_t *) loc = scm_to_uint8 (x);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case FFI_TYPE_SINT8:
|
2011-12-03 12:17:46 +01:00
|
|
|
|
if (return_value_p)
|
|
|
|
|
|
*(ffi_arg *) loc = scm_to_int8 (x);
|
|
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
*(int8_t *) loc = scm_to_int8 (x);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case FFI_TYPE_UINT16:
|
2011-12-03 12:17:46 +01:00
|
|
|
|
if (return_value_p)
|
|
|
|
|
|
*(ffi_arg *) loc = scm_to_uint16 (x);
|
|
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
*(uint16_t *) loc = scm_to_uint16 (x);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case FFI_TYPE_SINT16:
|
2011-12-03 12:17:46 +01:00
|
|
|
|
if (return_value_p)
|
|
|
|
|
|
*(ffi_arg *) loc = scm_to_int16 (x);
|
|
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
*(int16_t *) loc = scm_to_int16 (x);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case FFI_TYPE_UINT32:
|
2011-12-03 12:17:46 +01:00
|
|
|
|
if (return_value_p)
|
|
|
|
|
|
*(ffi_arg *) loc = scm_to_uint32 (x);
|
|
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
*(uint32_t *) loc = scm_to_uint32 (x);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case FFI_TYPE_SINT32:
|
2011-12-03 12:17:46 +01:00
|
|
|
|
if (return_value_p)
|
|
|
|
|
|
*(ffi_arg *) loc = scm_to_int32 (x);
|
|
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
*(int32_t *) loc = scm_to_int32 (x);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case FFI_TYPE_UINT64:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
*(uint64_t *) loc = scm_to_uint64 (x);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case FFI_TYPE_SINT64:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
*(int64_t *) loc = scm_to_int64 (x);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case FFI_TYPE_STRUCT:
|
2011-02-09 00:08:14 +01:00
|
|
|
|
SCM_VALIDATE_POINTER (1, x);
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
memcpy (loc, SCM_POINTER_VALUE (x), type->size);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case FFI_TYPE_POINTER:
|
2011-02-09 00:08:14 +01:00
|
|
|
|
SCM_VALIDATE_POINTER (1, x);
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
*(void **) loc = SCM_POINTER_VALUE (x);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
break;
|
2011-03-29 23:35:24 +02:00
|
|
|
|
case FFI_TYPE_VOID:
|
|
|
|
|
|
/* Do nothing. */
|
|
|
|
|
|
break;
|
2010-01-25 18:04:45 +01:00
|
|
|
|
default:
|
|
|
|
|
|
abort ();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2011-02-09 00:08:14 +01:00
|
|
|
|
#undef FUNC_NAME
|
2010-01-25 18:04:45 +01:00
|
|
|
|
|
2011-05-08 17:21:59 +02:00
|
|
|
|
/* Return a Scheme representation of the foreign value at LOC of type
|
|
|
|
|
|
TYPE. When RETURN_VALUE_P is true, LOC is assumed to point to a
|
|
|
|
|
|
return value buffer; otherwise LOC is assumed to point to an
|
|
|
|
|
|
argument buffer. */
|
2010-01-25 18:04:45 +01:00
|
|
|
|
static SCM
|
2011-05-08 17:21:59 +02:00
|
|
|
|
pack (const ffi_type * type, const void *loc, int return_value_p)
|
2010-01-25 18:04:45 +01:00
|
|
|
|
{
|
|
|
|
|
|
switch (type->type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case FFI_TYPE_VOID:
|
|
|
|
|
|
return SCM_UNSPECIFIED;
|
|
|
|
|
|
case FFI_TYPE_FLOAT:
|
2010-03-20 17:00:38 +01:00
|
|
|
|
return scm_from_double (*(float *) loc);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
case FFI_TYPE_DOUBLE:
|
2010-03-20 17:00:38 +01:00
|
|
|
|
return scm_from_double (*(double *) loc);
|
2011-05-08 17:21:59 +02:00
|
|
|
|
|
|
|
|
|
|
/* For integer return values smaller than `int', libffi stores the
|
|
|
|
|
|
result in an `ffi_arg'-long buffer, of which only the
|
|
|
|
|
|
significant bits must be kept---hence the pair of casts below.
|
|
|
|
|
|
See <http://thread.gmane.org/gmane.comp.lib.ffi.general/406>
|
|
|
|
|
|
for details. */
|
|
|
|
|
|
|
2010-01-25 18:04:45 +01:00
|
|
|
|
case FFI_TYPE_UINT8:
|
2011-05-08 17:21:59 +02:00
|
|
|
|
if (return_value_p)
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_uint8 ((uint8_t) *(ffi_arg *) loc);
|
2011-05-08 17:21:59 +02:00
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_uint8 (* (uint8_t *) loc);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
case FFI_TYPE_SINT8:
|
2011-05-08 17:21:59 +02:00
|
|
|
|
if (return_value_p)
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_int8 ((int8_t) *(ffi_arg *) loc);
|
2011-05-08 17:21:59 +02:00
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_int8 (* (int8_t *) loc);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
case FFI_TYPE_UINT16:
|
2011-05-08 17:21:59 +02:00
|
|
|
|
if (return_value_p)
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_uint16 ((uint16_t) *(ffi_arg *) loc);
|
2011-05-08 17:21:59 +02:00
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_uint16 (* (uint16_t *) loc);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
case FFI_TYPE_SINT16:
|
2011-05-08 17:21:59 +02:00
|
|
|
|
if (return_value_p)
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_int16 ((int16_t) *(ffi_arg *) loc);
|
2011-05-08 17:21:59 +02:00
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_int16 (* (int16_t *) loc);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
case FFI_TYPE_UINT32:
|
2011-05-08 17:21:59 +02:00
|
|
|
|
if (return_value_p)
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_uint32 ((uint32_t) *(ffi_arg *) loc);
|
2011-05-08 17:21:59 +02:00
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_uint32 (* (uint32_t *) loc);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
case FFI_TYPE_SINT32:
|
2011-05-08 17:21:59 +02:00
|
|
|
|
if (return_value_p)
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_int32 ((int32_t) *(ffi_arg *) loc);
|
2011-05-08 17:21:59 +02:00
|
|
|
|
else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_int32 (* (int32_t *) loc);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
case FFI_TYPE_UINT64:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_uint64 (*(uint64_t *) loc);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
case FFI_TYPE_SINT64:
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_from_int64 (*(int64_t *) loc);
|
2011-05-08 17:21:59 +02:00
|
|
|
|
|
2010-01-25 18:04:45 +01:00
|
|
|
|
case FFI_TYPE_STRUCT:
|
|
|
|
|
|
{
|
2010-03-20 17:00:38 +01:00
|
|
|
|
void *mem = scm_gc_malloc_pointerless (type->size, "foreign");
|
|
|
|
|
|
memcpy (mem, loc, type->size);
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
return scm_from_pointer (mem, NULL);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
case FFI_TYPE_POINTER:
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
return scm_from_pointer (*(void **) loc, NULL);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
default:
|
|
|
|
|
|
abort ();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-03-20 17:00:38 +01:00
|
|
|
|
|
2018-06-17 09:42:19 +02:00
|
|
|
|
#define MAX(A, B) ((A) >= (B) ? (A) : (B))
|
|
|
|
|
|
|
2018-08-11 14:22:32 +02:00
|
|
|
|
SCM
|
|
|
|
|
|
scm_i_foreign_call (SCM cif_scm, SCM pointer_scm, int *errno_ret,
|
|
|
|
|
|
const union scm_vm_stack_element *argv)
|
2010-01-25 18:04:45 +01:00
|
|
|
|
{
|
|
|
|
|
|
/* FOREIGN is the pair that cif_to_procedure set as the 0th element of the
|
|
|
|
|
|
objtable. */
|
|
|
|
|
|
ffi_cif *cif;
|
2010-04-09 00:01:38 +02:00
|
|
|
|
void (*func) (void);
|
2018-06-21 08:39:03 +02:00
|
|
|
|
uint8_t *data;
|
2010-01-25 18:04:45 +01:00
|
|
|
|
void *rvalue;
|
|
|
|
|
|
void **args;
|
|
|
|
|
|
unsigned i;
|
2010-03-20 16:45:14 +01:00
|
|
|
|
size_t arg_size;
|
2018-06-21 08:43:47 +02:00
|
|
|
|
ptrdiff_t off;
|
2010-01-25 18:04:45 +01:00
|
|
|
|
|
2016-01-05 16:30:41 -05:00
|
|
|
|
cif = SCM_POINTER_VALUE (cif_scm);
|
|
|
|
|
|
func = SCM_POINTER_VALUE (pointer_scm);
|
2010-03-20 16:45:14 +01:00
|
|
|
|
|
|
|
|
|
|
/* Argument pointers. */
|
2010-04-09 00:01:38 +02:00
|
|
|
|
args = alloca (sizeof (void *) * cif->nargs);
|
2010-03-20 16:45:14 +01:00
|
|
|
|
|
2010-05-30 22:54:39 +02:00
|
|
|
|
/* Compute the worst-case amount of memory needed to store all the argument
|
|
|
|
|
|
values. Note: as of libffi 3.0.9 `cif->bytes' is undocumented and is zero,
|
|
|
|
|
|
so it can't be used for that purpose. */
|
|
|
|
|
|
for (i = 0, arg_size = 0; i < cif->nargs; i++)
|
|
|
|
|
|
arg_size += cif->arg_types[i]->size + cif->arg_types[i]->alignment - 1;
|
2010-03-20 16:45:14 +01:00
|
|
|
|
|
|
|
|
|
|
/* Space for argument values, followed by return value. */
|
2010-05-30 22:54:39 +02:00
|
|
|
|
data = alloca (arg_size + cif->rtype->size
|
2018-06-17 09:42:19 +02:00
|
|
|
|
+ MAX (sizeof (void *), cif->rtype->alignment));
|
2010-03-20 16:45:14 +01:00
|
|
|
|
|
2010-03-20 17:00:38 +01:00
|
|
|
|
/* Unpack ARGV to native values, setting ARGV pointers. */
|
|
|
|
|
|
for (i = 0, off = 0;
|
|
|
|
|
|
i < cif->nargs;
|
2018-06-21 08:39:03 +02:00
|
|
|
|
off = (uint8_t *) args[i] - data + cif->arg_types[i]->size,
|
2010-05-30 22:54:39 +02:00
|
|
|
|
i++)
|
2010-01-25 18:04:45 +01:00
|
|
|
|
{
|
2010-05-30 22:54:39 +02:00
|
|
|
|
/* Suitably align the storage area for argument I. */
|
2018-06-21 08:39:03 +02:00
|
|
|
|
args[i] = (void *) ROUND_UP ((uintptr_t) data + off,
|
2010-05-30 22:54:39 +02:00
|
|
|
|
cif->arg_types[i]->alignment);
|
2018-06-21 08:39:03 +02:00
|
|
|
|
assert ((uintptr_t) args[i] % cif->arg_types[i]->alignment == 0);
|
2015-10-18 20:25:43 +02:00
|
|
|
|
unpack (cif->arg_types[i], args[i], argv[cif->nargs - i - 1].as_scm, 0);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
}
|
2010-03-20 17:00:38 +01:00
|
|
|
|
|
2010-05-30 22:54:39 +02:00
|
|
|
|
/* Prepare space for the return value. On some platforms, such as
|
|
|
|
|
|
`armv5tel-*-linux-gnueabi', the return value has to be at least
|
|
|
|
|
|
word-aligned, even if its type doesn't have any alignment requirement as is
|
|
|
|
|
|
the case with `char'. */
|
2018-06-21 08:39:03 +02:00
|
|
|
|
rvalue = (void *) ROUND_UP ((uintptr_t) data + off,
|
2018-06-17 09:42:19 +02:00
|
|
|
|
MAX (sizeof (void *), cif->rtype->alignment));
|
2010-01-25 18:04:45 +01:00
|
|
|
|
|
|
|
|
|
|
/* off we go! */
|
2016-01-05 16:30:41 -05:00
|
|
|
|
errno = 0;
|
2010-01-25 18:04:45 +01:00
|
|
|
|
ffi_call (cif, func, rvalue, args);
|
2018-08-11 14:22:32 +02:00
|
|
|
|
*errno_ret = errno;
|
2010-01-25 18:04:45 +01:00
|
|
|
|
|
2011-05-08 17:21:59 +02:00
|
|
|
|
return pack (cif->rtype, rvalue, 1);
|
2010-01-25 18:04:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2010-09-03 15:12:54 +02:00
|
|
|
|
|
|
|
|
|
|
/* Function pointers aka. "callbacks" or "closures". */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef FFI_CLOSURES
|
|
|
|
|
|
|
|
|
|
|
|
/* Trampoline to invoke a libffi closure that wraps a Scheme
|
|
|
|
|
|
procedure. */
|
|
|
|
|
|
static void
|
|
|
|
|
|
invoke_closure (ffi_cif *cif, void *ret, void **args, void *data)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t i;
|
|
|
|
|
|
SCM proc, *argv, result;
|
|
|
|
|
|
|
2011-10-24 17:58:22 +02:00
|
|
|
|
proc = SCM_PACK_POINTER (data);
|
2010-09-03 15:12:54 +02:00
|
|
|
|
|
|
|
|
|
|
argv = alloca (cif->nargs * sizeof (*argv));
|
|
|
|
|
|
|
|
|
|
|
|
/* Pack ARGS to SCM values, setting ARGV pointers. */
|
|
|
|
|
|
for (i = 0; i < cif->nargs; i++)
|
2011-05-08 17:21:59 +02:00
|
|
|
|
argv[i] = pack (cif->arg_types[i], args[i], 0);
|
2010-09-03 15:12:54 +02:00
|
|
|
|
|
|
|
|
|
|
result = scm_call_n (proc, argv, cif->nargs);
|
|
|
|
|
|
|
2011-12-03 12:17:46 +01:00
|
|
|
|
unpack (cif->rtype, ret, result, 1);
|
2010-09-03 15:12:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_procedure_to_pointer, "procedure->pointer", 3, 0, 0,
|
|
|
|
|
|
(SCM return_type, SCM proc, SCM arg_types),
|
2012-01-11 23:33:01 -05:00
|
|
|
|
"Return a pointer to a C function of type @var{return_type}\n"
|
|
|
|
|
|
"taking arguments of types @var{arg_types} (a list) and\n"
|
2010-09-03 15:12:54 +02:00
|
|
|
|
"behaving as a proxy to procedure @var{proc}. Thus\n"
|
|
|
|
|
|
"@var{proc}'s arity, supported argument types, and return\n"
|
2012-01-11 23:33:01 -05:00
|
|
|
|
"type should match @var{return_type} and @var{arg_types}.\n")
|
2010-09-03 15:12:54 +02:00
|
|
|
|
#define FUNC_NAME s_scm_procedure_to_pointer
|
|
|
|
|
|
{
|
2011-11-16 23:53:58 +01:00
|
|
|
|
SCM cif_pointer, pointer;
|
2010-09-03 15:12:54 +02:00
|
|
|
|
ffi_cif *cif;
|
|
|
|
|
|
ffi_status err;
|
|
|
|
|
|
void *closure, *executable;
|
|
|
|
|
|
|
|
|
|
|
|
cif = make_cif (return_type, arg_types, FUNC_NAME);
|
|
|
|
|
|
|
|
|
|
|
|
closure = ffi_closure_alloc (sizeof (ffi_closure), &executable);
|
|
|
|
|
|
err = ffi_prep_closure_loc ((ffi_closure *) closure, cif,
|
2011-10-24 17:58:22 +02:00
|
|
|
|
invoke_closure, SCM_UNPACK_POINTER (proc),
|
2010-09-03 15:12:54 +02:00
|
|
|
|
executable);
|
|
|
|
|
|
if (err != FFI_OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
ffi_closure_free (closure);
|
|
|
|
|
|
SCM_MISC_ERROR ("`ffi_prep_closure_loc' failed", SCM_EOL);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-11-16 23:53:58 +01:00
|
|
|
|
/* CIF points to GC-managed memory and it should remain as long as
|
|
|
|
|
|
POINTER (see below) is live. Wrap it in a Scheme pointer to then
|
|
|
|
|
|
hold a weak reference on it. */
|
|
|
|
|
|
cif_pointer = scm_from_pointer (cif, NULL);
|
|
|
|
|
|
|
2010-09-03 15:12:54 +02:00
|
|
|
|
if (closure == executable)
|
2011-11-16 23:53:58 +01:00
|
|
|
|
{
|
|
|
|
|
|
pointer = scm_from_pointer (executable, ffi_closure_free);
|
2011-11-26 22:27:32 +01:00
|
|
|
|
register_weak_reference (pointer,
|
|
|
|
|
|
scm_list_2 (proc, cif_pointer));
|
2011-11-16 23:53:58 +01:00
|
|
|
|
}
|
2010-09-03 15:12:54 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
/* CLOSURE needs to be freed eventually. However, since
|
|
|
|
|
|
`GC_all_interior_pointers' is disabled, we can't just register
|
|
|
|
|
|
a finalizer for CLOSURE. Instead, we create a pointer object
|
|
|
|
|
|
for CLOSURE, with a finalizer, and register it as a weak
|
|
|
|
|
|
reference of POINTER. */
|
|
|
|
|
|
SCM friend;
|
|
|
|
|
|
|
|
|
|
|
|
pointer = scm_from_pointer (executable, NULL);
|
|
|
|
|
|
friend = scm_from_pointer (closure, ffi_closure_free);
|
|
|
|
|
|
|
2011-11-26 22:27:32 +01:00
|
|
|
|
register_weak_reference (pointer,
|
|
|
|
|
|
scm_list_3 (proc, cif_pointer, friend));
|
2010-09-03 15:12:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return pointer;
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* FFI_CLOSURES */
|
|
|
|
|
|
|
2010-01-04 12:11:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
2010-01-18 12:16:13 +01:00
|
|
|
|
static void
|
2010-01-04 12:11:33 +01:00
|
|
|
|
scm_init_foreign (void)
|
|
|
|
|
|
{
|
|
|
|
|
|
#ifndef SCM_MAGIC_SNARFER
|
2018-06-20 17:19:31 +02:00
|
|
|
|
#include "foreign.x"
|
2010-01-04 12:11:33 +01:00
|
|
|
|
#endif
|
2010-01-18 12:16:13 +01:00
|
|
|
|
scm_define (sym_void, scm_from_uint8 (SCM_FOREIGN_TYPE_VOID));
|
|
|
|
|
|
scm_define (sym_float, scm_from_uint8 (SCM_FOREIGN_TYPE_FLOAT));
|
|
|
|
|
|
scm_define (sym_double, scm_from_uint8 (SCM_FOREIGN_TYPE_DOUBLE));
|
|
|
|
|
|
scm_define (sym_uint8, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT8));
|
|
|
|
|
|
scm_define (sym_int8, scm_from_uint8 (SCM_FOREIGN_TYPE_INT8));
|
|
|
|
|
|
scm_define (sym_uint16, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16));
|
|
|
|
|
|
scm_define (sym_int16, scm_from_uint8 (SCM_FOREIGN_TYPE_INT16));
|
|
|
|
|
|
scm_define (sym_uint32, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32));
|
|
|
|
|
|
scm_define (sym_int32, scm_from_uint8 (SCM_FOREIGN_TYPE_INT32));
|
|
|
|
|
|
scm_define (sym_uint64, scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64));
|
|
|
|
|
|
scm_define (sym_int64, scm_from_uint8 (SCM_FOREIGN_TYPE_INT64));
|
Provide `int', `long', `size_t', etc. in `(system foreign)'.
* libguile/foreign.c (sym_int, sym_long, sym_unsigned_int,
sym_unsigned_long, sym_size_t): New variables.
(scm_init_foreign): Define them at the Scheme level.
* module/system/foreign.scm (int, unsigned-int, long,
unsigned-long, size_t): New exported bindings.
2010-03-17 00:45:57 +01:00
|
|
|
|
|
2010-11-11 16:26:50 +01:00
|
|
|
|
scm_define (sym_short,
|
|
|
|
|
|
#if SIZEOF_SHORT == 8
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
|
|
|
|
|
|
#elif SIZEOF_SHORT == 4
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
|
|
|
|
|
|
#elif SIZEOF_SHORT == 2
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT16)
|
|
|
|
|
|
#else
|
|
|
|
|
|
# error unsupported sizeof (short)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
scm_define (sym_unsigned_short,
|
|
|
|
|
|
#if SIZEOF_SHORT == 8
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
|
|
|
|
|
|
#elif SIZEOF_SHORT == 4
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
|
|
|
|
|
|
#elif SIZEOF_SHORT == 2
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_UINT16)
|
|
|
|
|
|
#else
|
|
|
|
|
|
# error unsupported sizeof (short)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
);
|
|
|
|
|
|
|
Provide `int', `long', `size_t', etc. in `(system foreign)'.
* libguile/foreign.c (sym_int, sym_long, sym_unsigned_int,
sym_unsigned_long, sym_size_t): New variables.
(scm_init_foreign): Define them at the Scheme level.
* module/system/foreign.scm (int, unsigned-int, long,
unsigned-long, size_t): New exported bindings.
2010-03-17 00:45:57 +01:00
|
|
|
|
scm_define (sym_int,
|
|
|
|
|
|
#if SIZEOF_INT == 8
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
|
|
|
|
|
|
#elif SIZEOF_INT == 4
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
|
|
|
|
|
|
#else
|
|
|
|
|
|
# error unsupported sizeof (int)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
scm_define (sym_unsigned_int,
|
|
|
|
|
|
#if SIZEOF_UNSIGNED_INT == 8
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
|
|
|
|
|
|
#elif SIZEOF_UNSIGNED_INT == 4
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
|
|
|
|
|
|
#else
|
|
|
|
|
|
# error unsupported sizeof (unsigned int)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
scm_define (sym_long,
|
|
|
|
|
|
#if SIZEOF_LONG == 8
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
|
|
|
|
|
|
#elif SIZEOF_LONG == 4
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
|
|
|
|
|
|
#else
|
|
|
|
|
|
# error unsupported sizeof (long)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
scm_define (sym_unsigned_long,
|
|
|
|
|
|
#if SIZEOF_UNSIGNED_LONG == 8
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
|
|
|
|
|
|
#elif SIZEOF_UNSIGNED_LONG == 4
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
|
|
|
|
|
|
#else
|
|
|
|
|
|
# error unsupported sizeof (unsigned long)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
scm_define (sym_size_t,
|
|
|
|
|
|
#if SIZEOF_SIZE_T == 8
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
|
|
|
|
|
|
#elif SIZEOF_SIZE_T == 4
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
|
|
|
|
|
|
#else
|
|
|
|
|
|
# error unsupported sizeof (size_t)
|
2013-02-02 04:04:49 -05:00
|
|
|
|
#endif
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
scm_define (sym_ssize_t,
|
|
|
|
|
|
#if SIZEOF_SIZE_T == 8
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
|
|
|
|
|
|
#elif SIZEOF_SIZE_T == 4
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
|
|
|
|
|
|
#else
|
|
|
|
|
|
# error unsupported sizeof (ssize_t)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
scm_define (sym_ptrdiff_t,
|
|
|
|
|
|
#if SCM_SIZEOF_SCM_T_PTRDIFF == 8
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
|
|
|
|
|
|
#elif SCM_SIZEOF_SCM_T_PTRDIFF == 4
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
|
|
|
|
|
|
#else
|
2018-06-21 08:43:47 +02:00
|
|
|
|
# error unsupported sizeof (ptrdiff_t)
|
2017-11-22 16:32:39 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
scm_define (sym_intptr_t,
|
|
|
|
|
|
#if SCM_SIZEOF_INTPTR_T == 8
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT64)
|
|
|
|
|
|
#elif SCM_SIZEOF_INTPTR_T == 4
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_INT32)
|
|
|
|
|
|
#else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
# error unsupported sizeof (intptr_t)
|
2017-11-22 16:32:39 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
scm_define (sym_uintptr_t,
|
|
|
|
|
|
#if SCM_SIZEOF_UINTPTR_T == 8
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_UINT64)
|
|
|
|
|
|
#elif SCM_SIZEOF_UINTPTR_T == 4
|
|
|
|
|
|
scm_from_uint8 (SCM_FOREIGN_TYPE_UINT32)
|
|
|
|
|
|
#else
|
2018-06-21 08:39:03 +02:00
|
|
|
|
# error unsupported sizeof (uintptr_t)
|
Provide `int', `long', `size_t', etc. in `(system foreign)'.
* libguile/foreign.c (sym_int, sym_long, sym_unsigned_int,
sym_unsigned_long, sym_size_t): New variables.
(scm_init_foreign): Define them at the Scheme level.
* module/system/foreign.scm (int, unsigned-int, long,
unsigned-long, size_t): New exported bindings.
2010-03-17 00:45:57 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
);
|
2010-03-18 00:27:38 +01:00
|
|
|
|
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
null_pointer = scm_cell (scm_tc7_pointer, 0);
|
2010-05-20 01:00:37 +02:00
|
|
|
|
scm_define (sym_null, null_pointer);
|
2010-01-18 12:16:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
scm_register_foreign (void)
|
|
|
|
|
|
{
|
2010-03-16 21:18:12 +01:00
|
|
|
|
scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
|
|
|
|
|
|
"scm_init_foreign",
|
2010-01-18 12:16:13 +01:00
|
|
|
|
(scm_t_extension_init_func)scm_init_foreign,
|
|
|
|
|
|
NULL);
|
2011-10-23 23:38:51 +02:00
|
|
|
|
pointer_weak_refs = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
|
2010-01-04 12:11:33 +01:00
|
|
|
|
}
|