* libguile/numbers.h (scm_to_intptr_t, scm_from_intptr_t)
(scm_to_uintptr_t, scm_from_uintptr_t): New defines.
* libguile/foreign.c: Remove definitions here; adapt callers to use new
names with _t suffixes.
* libguile/foreign.c (OBJCODE_HEADER, META_HEADER, META): Change these
into higher-order macros.
(GEN_CODE): New higher-order macro based on 'CODE'.
(M_STATIC, M_DYNAMIC): New macros.
(CODE): Reimplement using 'GEN_CODE' and 'M_STATIC'.
(make_objcode_trampoline): New static function.
(large_objcode_trampolines, large_objcode_trampolines_mutex): New
static variables.
(get_objcode_trampoline): New static function.
(cif_to_procedure): Use 'get_objcode_trampoline'.
* test-suite/standalone/test-ffi-lib.c (test_ffi_sum_many):
New function.
* test-suite/standalone/test-ffi: Add test.
Fixes <http://bugs.gnu.org/11468>.
* libguile/ports.c (scm_conversion_strategy): Remove.
(default_conversion_strategy_var, sym_error, sym_substitute,
sym_escape): New variables.
(scm_i_get_conversion_strategy, scm_i_set_conversion_strategy_x):
Remove.
(scm_i_default_port_conversion_handler,
scm_i_set_default_port_conversion_handler): New functions.
(scm_port_conversion_strategy): Use
`scm_i_default_port_conversion_handler' when PORT is #f.
(scm_set_port_conversion_strategy_x): Use SYM_ERROR, SYM_SUBSTITUTE,
and SYM_ESCAPE. Use `scm_i_set_default_port_conversion_handler' when
PORT is #f.
(scm_init_ports): Initialize DEFAULT_CONVERSION_STRATEGY_VAR.
* libguile/ports.h: Update declarations accordingly.
* libguile/foreign.c: Change
`scm_i_get_conversion_strategy (SCM_BOOL_F)' to
`scm_i_default_port_conversion_handler ()'.
* libguile/strings.c: Likewise.
* test-suite/tests/ports.test ("%default-port-conversion-strategy"): New
test prefix.
* test-suite/tests/foreign.test ("pointer<->string")["%default-port-conversion-strategy
is error", "%default-port-conversion-strategy is soft"]: New tests.
* test-suite/test-suite/lib.scm (exception:encoding-error): Allow the
regexp to match `scm_to_stringn' error messages.
* doc/ref/api-io.texi (Ports): Document `%default-port-conversion-strategy'.
* libguile/foreign.c (scm_sizeof): Make sure the overall size is a
multiple of the alignment of the structure.
* test-suite/tests/foreign.test: Test size of { double, int8 }.
This is a followup to 690a0112e5 ("Remove
the "has finalizer?" bit from pointer objects.")
* libguile/foreign.c (scm_set_pointer_finalizer_x): Leave the type cell
unchanged. Before, `equal?' would break on pointers on which
`set-pointer-finalizer!' had been called.
* test-suite/tests/foreign.test ("make-pointer")["equal? modulo
finalizer (set-pointer-finalizer!)"]: New test.
* libguile/foreign.c: Use `alignof_type' instead of `alignof'; the
latter was removed from Gnulib's <alignof.h> in
408e170e3ae81f73fb65686c5834693d89a96594 (Nov. 1 2011).
* libguile/vm-i-scheme.c: Likewise.
* libguile/foreign.c (scm_procedure_to_pointer): Keep a weak reference
to PROC.
* test-suite/tests/foreign.test ("procedure->pointer")["procedure is
retained"]: New test.
* libguile/foreign.c (scm_procedure_to_pointer): Keep a weak reference
to CIF so that it is not reclaimed before POINTER. Before that it
could be reclaimed and typically reused to store the CIF of another
procedure with the same arity, leading to obscure wrong-type-arg
errors.
* libguile/ports.c (scm_putc, scm_puts):
* libguile/ports.h (scm_putc_unlocked, scm_puts_unlocked): Separate into
_unlocked and locked variants. Change all callers to use the
_unlocked versions.
* libguile/tags.h (SCM_HEAP_OBJECT_BASE): New macro. Given a SCM,
returns a pointer to the start of its memory area on the heap.
* libguile/bytevectors.c:
* libguile/fluids.c:
* libguile/foreign.c:
* libguile/gc.h:
* libguile/guardians.c:
* libguile/numbers.h:
* libguile/ports.c:
* libguile/smob.c:
* libguile/struct.c:
* libguile/weak-set.c:
* libguile/weak-table.c:
* libguile/weak-vector.c: Use it.
* libguile/tags.h (SCM_UNPACK_POINTER, SCM_PACK_POINTER): New macros.
The old SCM2PTR and PTR2SCM were defined in such a way that
round-tripping through a pointer could lose precision, even in the
case in which you weren't interested in actually dereferencing the
pointer, it was simply that you needed to plumb a SCM through APIs
that take pointers. These new macros are more like SCM_PACK and
SCM_UNPACK, but for pointer types. The bit representation of the
pointer should be the same as the scm_t_bits representation.
* libguile/gc.h (PTR2SCM, SCM2PTR): Remove support for (old) UNICOS
pointers. We are going to try tagging the SCM object itself in the
future, and I don't think that keeping this support is worth its
cost. It probably doesn't work anyway.
* libguile/backtrace.c:
* libguile/bytevectors.c:
* libguile/continuations.c:
* libguile/fluids.c:
* libguile/foreign.c:
* libguile/gc.h:
* libguile/guardians.c:
* libguile/hashtab.c:
* libguile/load.c:
* libguile/numbers.c:
* libguile/ports.c:
* libguile/smob.c:
* libguile/strings.c:
* libguile/symbols.c:
* libguile/vm.c:
* libguile/weak-set.c:
* libguile/weak-table.c:
* libguile/weak-vector.c: Update many sites to use the new macros.
The weak table API isn't public yet. It could be after some review.
But we can go ahead and use it now internally.
* libguile/foreign.c:
* libguile/goops.c:
* libguile/objprop.c:
* libguile/procprop.c:
* libguile/smob.c:
* libguile/srcprop.c: Update weak table users to new API. No locking
needed!
* libguile/foreign.c (pack): Add `return_value_p' parameter. Update
callers.
When RETURN_VALUE_P is true, assume LOC points to an `ffi_arg', and
cast its results to the relevant type. This fixes packing of integer
return values smaller than `int' on SPARC64 and PowerPC64. Reported
by Nelson H. F. Beebe <beebe@math.utah.edu>.
* libguile/bytevectors.h (SCM_BYTEVECTOR_HEADER_SIZE): Bump, giving
bytevectors another word: a parent pointer. Will allow for
sub-bytevectors and efficient mmap bindings.
* libguile/bytevectors.c (make_bytevector):
(make_bytevector_from_buffer): Init parent to #f.
(scm_c_take_bytevector, scm_c_take_typed_bytevector): Another
argument, the parent, which gets set in the bytevector.
* libguile/foreign.c (scm_pointer_to_bytevector): Use the parent field
instead of registering a weak reference from bytevector to foreign
pointer.
* libguile/objcodes.c (scm_objcode_to_bytecode): Use the parent field to
avoid copying the objcode.
* libguile/srfi-4.c (DEFINE_SRFI_4_C_FUNCS):
* libguile/strings.c (scm_from_stringn):
* libguile/vm.c (really_make_boot_program):
* libguile/r6rs-ports.c (scm_get_bytevector_some)
(scm_get_bytevector_all, bytevector_output_port_procedure): Set the
parent to #f.
* test-suite/tests/foreign.test ("pointer<->string"): Add test cases.
* libguile/foreign.c (scm_string_to_pointer, scm_pointer_to_string): Add
optional encoding, and in the pointer->string case, length arguments.
* libguile/foreign.h: Update prototypes of internal functions.
Shouldn't affect ABI as they are internal.
* doc/ref/api-foreign.texi (Void Pointers and Byte Access): Update
docs.
* libguile/foreign.c (scm_pointer_to_scm, scm_scm_to_pointer): New
functions, useful to pass and receive SCM values to and from foreign
functions.
* module/system/foreign.scm: Export the new functions.
* doc/ref/api-foreign.texi (Foreign Variables): Add docs.
* test-suite/tests/foreign.test ("pointer<->scm"): Tests.
* libguile/foreign.c (unpack): Make sure X is a pointer before using
`SCM_POINTER_VALUE'.
* test-suite/tests/foreign.test ("pointer->procedure"): New test prefix.
* libguile/foreign.h (SCM_POINTER_HAS_FINALIZER): Remove.
* libguile/foreign.c (scm_from_pointer): Store nothing more than
`scm_tc7_pointer' in the type slot.
* libguile/objcodes.h (SCM_OBJCODE_TYPE_MMAP)
(SCM_OBJCODE_TYPE_BYTEVECTOR, SCM_OBJCODE_TYPE_SLICE)
(SCM_OBJCODE_TYPE_STATIC): Enumerate objcode types instead of
expressing them as flags.
(SCM_OBJCODE_TYPE): Type is held in bits 8-15.
(SCM_OBJCODE_FLAGS): Flags are now shifted by 16 bits, not 8.
(SCM_MAKE_OBJCODE_TAG): New helper.
* libguile/continuations.c (STATIC_OBJCODE_TAG):
* libguile/control.c (STATIC_OBJCODE_TAG):
* libguile/foreign.c (STATIC_OBJCODE_TAG):
* libguile/gsubr.c (STATIC_OBJCODE_TAG):
* libguile/smob.c (STATIC_OBJCODE_TAG):
* libguile/objcodes.c (make_objcode_by_mmap, scm_c_make_objcode_slice)
(scm_bytecode_to_objcode): : Use SCM_MAKE_OBJCODE_TAG.
* libguile/gc-malloc.c: Add a note that the gc-malloc does not clear the
memory block, so users need to make sure it is initialized.
* libguile/bitvectors.c (scm_c_make_bitvector):
* libguile/bytevectors.c (scm_make_bytevector):
* libguile/strings.c (scm_c_make_string): If no initializer is given,
initialize the bytes to 0. Prevents information leakage if an app uses
make-string et al without initializers.
* libguile/foreign.c (make_cif): Initialize this too, to prevent leakage
in the struct holes. Paranoia...
* libguile/foreign.c (make_cif): New procedure, with code formerly in
`scm_make_foreign_function'.
(scm_make_foreign_function): Use it.
(invoke_closure, scm_procedure_to_pointer)[FFI_CLOSURES]: New
functions.
* libguile/foreign.h (scm_procedure_to_pointer): New declaration.
* module/system/foreign.scm: Export `procedure->pointer' when available.
* test-suite/standalone/test-ffi (f-callback-1, f-callback-2): New
procedures and related tests.
* test-suite/standalone/test-ffi-lib.c (test_ffi_callback_1,
test_ffi_callback_2): New functions.
* test-suite/tests/foreign.test ("procedure->pointer"): New test prefix.
* doc/ref/api-foreign.texi (Dynamic FFI): Document `procedure->pointer'.