#include "libguile/_scm.h"
#include "libguile/bytevectors.h"
#include "libguile/instructions.h"
+#include "libguile/threads.h"
#include "libguile/foreign.h"
\f
static SCM pointer_weak_refs = SCM_BOOL_F;
+
static void
register_weak_reference (SCM from, SCM to)
{
- scm_hashq_set_x (pointer_weak_refs, from, to);
+ scm_weak_table_putq_x (pointer_weak_refs, from, to);
}
static void
pointer_finalizer_trampoline (GC_PTR ptr, GC_PTR data)
{
scm_t_pointer_finalizer finalizer = data;
- finalizer (SCM_POINTER_VALUE (PTR2SCM (ptr)));
+ finalizer (SCM_POINTER_VALUE (SCM_PACK_POINTER (ptr)));
}
+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
+
SCM_DEFINE (scm_make_pointer, "make-pointer", 1, 1, 0,
(SCM address, SCM finalizer),
"Return a foreign pointer object pointing to @var{address}. "
/* Register a finalizer for the newly created instance. */
GC_finalization_proc prev_finalizer;
GC_PTR prev_finalizer_data;
- GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret),
+ GC_REGISTER_FINALIZER_NO_ORDER (SCM_HEAP_OBJECT_BASE (ret),
pointer_finalizer_trampoline,
finalizer,
&prev_finalizer,
}
#undef FUNC_NAME
+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);
+ if (SCM_HEAP_OBJECT_P (ret))
+ register_weak_reference (ret, scm);
+
+ return ret;
+}
+#undef FUNC_NAME
+
SCM_DEFINE (scm_pointer_to_bytevector, "pointer->bytevector", 2, 2, 0,
(SCM pointer, SCM len, SCM offset, SCM uvec_type),
"Return a bytevector aliasing the @var{len} bytes pointed\n"
blen = scm_to_size_t (len);
- ret = scm_c_take_typed_bytevector (ptr + boffset, blen, btype);
- register_weak_reference (ret, pointer);
+ ret = scm_c_take_typed_bytevector (ptr + boffset, blen, btype, pointer);
+
return ret;
}
#undef FUNC_NAME
SCM_SET_CELL_WORD_0 (pointer, SCM_CELL_WORD_0 (pointer) | (1 << 16UL));
- GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (pointer),
+ GC_REGISTER_FINALIZER_NO_ORDER (SCM_HEAP_OBJECT_BASE (pointer),
pointer_finalizer_trampoline,
c_finalizer,
&prev_finalizer,
void
scm_i_pointer_print (SCM pointer, SCM port, scm_print_state *pstate)
{
- scm_puts ("#<pointer 0x", port);
+ scm_puts_unlocked ("#<pointer 0x", port);
scm_uintprint (scm_to_uintptr (scm_pointer_address (pointer)), 16, port);
- scm_putc ('>', port);
+ scm_putc_unlocked ('>', port);
}
\f
}
#undef FUNC_NAME
-SCM_DEFINE (scm_string_to_pointer, "string->pointer", 1, 0, 0,
- (SCM string),
+SCM_DEFINE (scm_string_to_pointer, "string->pointer", 1, 1, 0,
+ (SCM string, SCM encoding),
"Return a foreign pointer to a nul-terminated copy of\n"
- "@var{string} in the current locale encoding. The C\n"
- "string is freed when the returned foreign pointer\n"
- "becomes unreachable.\n\n"
- "This is the Scheme equivalent of @code{scm_to_locale_string}.")
+ "@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}.")
#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. */
- return scm_from_pointer (scm_to_locale_string (string), free);
+ 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,
+ scm_i_get_conversion_strategy (SCM_BOOL_F)),
+ free);
+
+ scm_dynwind_end ();
+
+ return ret;
+ }
}
#undef FUNC_NAME
-SCM_DEFINE (scm_pointer_to_string, "pointer->string", 1, 0, 0,
- (SCM pointer),
- "Return the string representing the C nul-terminated string\n"
- "pointed to by @var{pointer}. The C string is assumed to be\n"
- "in the current locale encoding.\n\n"
- "This is the Scheme equivalent of @code{scm_from_locale_string}.")
+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}.")
#define FUNC_NAME s_scm_pointer_to_string
{
+ size_t len;
+
SCM_VALIDATE_POINTER (1, pointer);
- return scm_from_locale_string (SCM_POINTER_VALUE (pointer));
+ 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,
+ scm_i_get_conversion_strategy (SCM_BOOL_F));
+
+ scm_dynwind_end ();
+
+ return ret;
+ }
}
#undef FUNC_NAME
switch (SCM_I_INUM (type))
{
case SCM_FOREIGN_TYPE_FLOAT:
- return scm_from_size_t (alignof (float));
+ return scm_from_size_t (alignof_type (float));
case SCM_FOREIGN_TYPE_DOUBLE:
- return scm_from_size_t (alignof (double));
+ return scm_from_size_t (alignof_type (double));
case SCM_FOREIGN_TYPE_UINT8:
- return scm_from_size_t (alignof (scm_t_uint8));
+ return scm_from_size_t (alignof_type (scm_t_uint8));
case SCM_FOREIGN_TYPE_INT8:
- return scm_from_size_t (alignof (scm_t_int8));
+ return scm_from_size_t (alignof_type (scm_t_int8));
case SCM_FOREIGN_TYPE_UINT16:
- return scm_from_size_t (alignof (scm_t_uint16));
+ return scm_from_size_t (alignof_type (scm_t_uint16));
case SCM_FOREIGN_TYPE_INT16:
- return scm_from_size_t (alignof (scm_t_int16));
+ return scm_from_size_t (alignof_type (scm_t_int16));
case SCM_FOREIGN_TYPE_UINT32:
- return scm_from_size_t (alignof (scm_t_uint32));
+ return scm_from_size_t (alignof_type (scm_t_uint32));
case SCM_FOREIGN_TYPE_INT32:
- return scm_from_size_t (alignof (scm_t_int32));
+ return scm_from_size_t (alignof_type (scm_t_int32));
case SCM_FOREIGN_TYPE_UINT64:
- return scm_from_size_t (alignof (scm_t_uint64));
+ return scm_from_size_t (alignof_type (scm_t_uint64));
case SCM_FOREIGN_TYPE_INT64:
- return scm_from_size_t (alignof (scm_t_int64));
+ return scm_from_size_t (alignof_type (scm_t_int64));
default:
scm_wrong_type_arg (FUNC_NAME, 1, type);
}
}
else if (scm_is_eq (type, sym_asterisk))
/* a pointer */
- return scm_from_size_t (alignof (void*));
+ return scm_from_size_t (alignof_type (void*));
else if (scm_is_pair (type))
- /* a struct, yo */
- return scm_alignof (scm_car (type));
+ {
+ /* 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);
+ }
else
scm_wrong_type_arg (FUNC_NAME, 1, type);
}
/* then ffi_type pointers: one for each arg, one for each struct
element, and one for each struct (for null-termination) */
- cif_len = (ROUND_UP (cif_len, alignof(void*))
+ cif_len = (ROUND_UP (cif_len, alignof_type (void *))
+ (nargs + n_structs + n_struct_elts)*sizeof(void*));
/* then the ffi_type structs themselves, one per arg and struct element, and
one for the return val */
- cif_len = (ROUND_UP (cif_len, alignof(ffi_type))
+ cif_len = (ROUND_UP (cif_len, alignof_type (ffi_type))
+ (nargs + n_struct_elts + 1)*sizeof(ffi_type));
mem = scm_gc_malloc_pointerless (cif_len, "foreign");
cif = (ffi_cif *) mem;
/* reuse cif_len to walk through the mem */
- cif_len = ROUND_UP (sizeof (ffi_cif), alignof(void*));
+ cif_len = ROUND_UP (sizeof (ffi_cif), alignof_type (void *));
type_ptrs = (ffi_type**)(mem + cif_len);
cif_len = ROUND_UP (cif_len
+ (nargs + n_structs + n_struct_elts)*sizeof(void*),
- alignof(ffi_type));
+ alignof_type (ffi_type));
types = (ffi_type*)(mem + cif_len);
/* whew. now knit the pointers together. */
/* Set *LOC to the foreign representation of X with TYPE. */
static void
-unpack (const ffi_type *type, void *loc, SCM x)
+unpack (const ffi_type *type, void *loc, SCM x, int return_value_p)
+#define FUNC_NAME "scm_i_foreign_call"
{
switch (type->type)
{
case FFI_TYPE_DOUBLE:
*(double *) loc = scm_to_double (x);
break;
+
+ /* For integer return values smaller than `int', libffi expects the
+ result in an `ffi_arg'-long buffer. */
+
case FFI_TYPE_UINT8:
- *(scm_t_uint8 *) loc = scm_to_uint8 (x);
+ if (return_value_p)
+ *(ffi_arg *) loc = scm_to_uint8 (x);
+ else
+ *(scm_t_uint8 *) loc = scm_to_uint8 (x);
break;
case FFI_TYPE_SINT8:
- *(scm_t_int8 *) loc = scm_to_int8 (x);
+ if (return_value_p)
+ *(ffi_arg *) loc = scm_to_int8 (x);
+ else
+ *(scm_t_int8 *) loc = scm_to_int8 (x);
break;
case FFI_TYPE_UINT16:
- *(scm_t_uint16 *) loc = scm_to_uint16 (x);
+ if (return_value_p)
+ *(ffi_arg *) loc = scm_to_uint16 (x);
+ else
+ *(scm_t_uint16 *) loc = scm_to_uint16 (x);
break;
case FFI_TYPE_SINT16:
- *(scm_t_int16 *) loc = scm_to_int16 (x);
+ if (return_value_p)
+ *(ffi_arg *) loc = scm_to_int16 (x);
+ else
+ *(scm_t_int16 *) loc = scm_to_int16 (x);
break;
case FFI_TYPE_UINT32:
- *(scm_t_uint32 *) loc = scm_to_uint32 (x);
+ if (return_value_p)
+ *(ffi_arg *) loc = scm_to_uint32 (x);
+ else
+ *(scm_t_uint32 *) loc = scm_to_uint32 (x);
break;
case FFI_TYPE_SINT32:
- *(scm_t_int32 *) loc = scm_to_int32 (x);
+ if (return_value_p)
+ *(ffi_arg *) loc = scm_to_int32 (x);
+ else
+ *(scm_t_int32 *) loc = scm_to_int32 (x);
break;
case FFI_TYPE_UINT64:
*(scm_t_uint64 *) loc = scm_to_uint64 (x);
*(scm_t_int64 *) loc = scm_to_int64 (x);
break;
case FFI_TYPE_STRUCT:
+ SCM_VALIDATE_POINTER (1, x);
memcpy (loc, SCM_POINTER_VALUE (x), type->size);
break;
case FFI_TYPE_POINTER:
+ SCM_VALIDATE_POINTER (1, x);
*(void **) loc = SCM_POINTER_VALUE (x);
break;
+ case FFI_TYPE_VOID:
+ /* Do nothing. */
+ break;
default:
abort ();
}
}
+#undef FUNC_NAME
-/* Return a Scheme representation of the foreign value at LOC of type TYPE. */
+/* 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. */
static SCM
-pack (const ffi_type * type, const void *loc)
+pack (const ffi_type * type, const void *loc, int return_value_p)
{
switch (type->type)
{
return scm_from_double (*(float *) loc);
case FFI_TYPE_DOUBLE:
return scm_from_double (*(double *) loc);
+
+ /* 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. */
+
case FFI_TYPE_UINT8:
- return scm_from_uint8 (*(scm_t_uint8 *) loc);
+ if (return_value_p)
+ return scm_from_uint8 ((scm_t_uint8) *(ffi_arg *) loc);
+ else
+ return scm_from_uint8 (* (scm_t_uint8 *) loc);
case FFI_TYPE_SINT8:
- return scm_from_int8 (*(scm_t_int8 *) loc);
+ if (return_value_p)
+ return scm_from_int8 ((scm_t_int8) *(ffi_arg *) loc);
+ else
+ return scm_from_int8 (* (scm_t_int8 *) loc);
case FFI_TYPE_UINT16:
- return scm_from_uint16 (*(scm_t_uint16 *) loc);
+ if (return_value_p)
+ return scm_from_uint16 ((scm_t_uint16) *(ffi_arg *) loc);
+ else
+ return scm_from_uint16 (* (scm_t_uint16 *) loc);
case FFI_TYPE_SINT16:
- return scm_from_int16 (*(scm_t_int16 *) loc);
+ if (return_value_p)
+ return scm_from_int16 ((scm_t_int16) *(ffi_arg *) loc);
+ else
+ return scm_from_int16 (* (scm_t_int16 *) loc);
case FFI_TYPE_UINT32:
- return scm_from_uint32 (*(scm_t_uint32 *) loc);
+ if (return_value_p)
+ return scm_from_uint32 ((scm_t_uint32) *(ffi_arg *) loc);
+ else
+ return scm_from_uint32 (* (scm_t_uint32 *) loc);
case FFI_TYPE_SINT32:
- return scm_from_int32 (*(scm_t_int32 *) loc);
+ if (return_value_p)
+ return scm_from_int32 ((scm_t_int32) *(ffi_arg *) loc);
+ else
+ return scm_from_int32 (* (scm_t_int32 *) loc);
case FFI_TYPE_UINT64:
return scm_from_uint64 (*(scm_t_uint64 *) loc);
case FFI_TYPE_SINT64:
return scm_from_int64 (*(scm_t_int64 *) loc);
+
case FFI_TYPE_STRUCT:
{
void *mem = scm_gc_malloc_pointerless (type->size, "foreign");
args[i] = (void *) ROUND_UP ((scm_t_uintptr) data + off,
cif->arg_types[i]->alignment);
assert ((scm_t_uintptr) args[i] % cif->arg_types[i]->alignment == 0);
- unpack (cif->arg_types[i], args[i], argv[i]);
+ unpack (cif->arg_types[i], args[i], argv[i], 0);
}
/* Prepare space for the return value. On some platforms, such as
/* off we go! */
ffi_call (cif, func, rvalue, args);
- return pack (cif->rtype, rvalue);
+ return pack (cif->rtype, rvalue, 1);
}
\f
size_t i;
SCM proc, *argv, result;
- proc = PTR2SCM (data);
+ proc = SCM_PACK_POINTER (data);
argv = alloca (cif->nargs * sizeof (*argv));
/* Pack ARGS to SCM values, setting ARGV pointers. */
for (i = 0; i < cif->nargs; i++)
- argv[i] = pack (cif->arg_types[i], args[i]);
+ argv[i] = pack (cif->arg_types[i], args[i], 0);
result = scm_call_n (proc, argv, cif->nargs);
- unpack (cif->rtype, ret, result);
+ unpack (cif->rtype, ret, result, 1);
}
SCM_DEFINE (scm_procedure_to_pointer, "procedure->pointer", 3, 0, 0,
"type should match @var{return-type} and @var{arg-types}.\n")
#define FUNC_NAME s_scm_procedure_to_pointer
{
- SCM pointer;
+ SCM cif_pointer, pointer;
ffi_cif *cif;
ffi_status err;
void *closure, *executable;
closure = ffi_closure_alloc (sizeof (ffi_closure), &executable);
err = ffi_prep_closure_loc ((ffi_closure *) closure, cif,
- invoke_closure, SCM2PTR (proc),
+ invoke_closure, SCM_UNPACK_POINTER (proc),
executable);
if (err != FFI_OK)
{
SCM_MISC_ERROR ("`ffi_prep_closure_loc' failed", SCM_EOL);
}
+ /* 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);
+
if (closure == executable)
- pointer = scm_from_pointer (executable, ffi_closure_free);
+ {
+ pointer = scm_from_pointer (executable, ffi_closure_free);
+ register_weak_reference (pointer,
+ scm_list_2 (proc, cif_pointer));
+ }
else
{
/* CLOSURE needs to be freed eventually. However, since
pointer = scm_from_pointer (executable, NULL);
friend = scm_from_pointer (closure, ffi_closure_free);
- register_weak_reference (pointer, friend);
+ register_weak_reference (pointer,
+ scm_list_3 (proc, cif_pointer, friend));
}
return pointer;
"scm_init_foreign",
(scm_t_extension_init_func)scm_init_foreign,
NULL);
- pointer_weak_refs = scm_make_weak_key_hash_table (SCM_UNDEFINED);
+ pointer_weak_refs = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
}
/*