summaryrefslogtreecommitdiff
path: root/libguile/foreign.h
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2010-07-26 17:44:43 +0200
committerLudovic Courtès <ludo@gnu.org>2010-07-26 19:38:52 +0200
commitd4149a510e4a87915b625255f4de3301510d810c (patch)
tree8145892f76b6eda3ec86f0be752e391913b9a656 /libguile/foreign.h
parent1af772303bf4eafb632a95bf4015a7736275e9e7 (diff)
downloadguile-d4149a510e4a87915b625255f4de3301510d810c.tar.gz
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.
Diffstat (limited to 'libguile/foreign.h')
-rw-r--r--libguile/foreign.h60
1 files changed, 15 insertions, 45 deletions
diff --git a/libguile/foreign.h b/libguile/foreign.h
index a162d5d76..4712d761a 100644
--- a/libguile/foreign.h
+++ b/libguile/foreign.h
@@ -26,20 +26,13 @@
scm_tc7_foreign typecode and type of the aliased (pointed-to) value in its
lower 16 bits.
- There are numeric types, like uint32 and float, and there is a "generic
- pointer" type, void. Void pointers also have a length associated with them,
- in the high bits of the first word of the SCM object, but since they really
- are pointers out into the wild wooly world of C, perhaps we don't actually
- know how much memory they take up. In that, most general case, the "len"
- will be stored as 0.
-
The basic idea is that we can help the programmer to avoid cutting herself,
- but we won't take away her knives.
-*/
-typedef enum
+ but we won't take away her knives. */
+
+enum scm_t_foreign_type
{
- SCM_FOREIGN_TYPE_VOID, /* a pointer out into the wilderness */
- SCM_FOREIGN_TYPE_FLOAT,
+ SCM_FOREIGN_TYPE_VOID,
+ SCM_FOREIGN_TYPE_FLOAT,
SCM_FOREIGN_TYPE_DOUBLE,
SCM_FOREIGN_TYPE_UINT8,
SCM_FOREIGN_TYPE_INT8,
@@ -50,55 +43,32 @@ typedef enum
SCM_FOREIGN_TYPE_UINT64,
SCM_FOREIGN_TYPE_INT64,
SCM_FOREIGN_TYPE_LAST = SCM_FOREIGN_TYPE_INT64
- } scm_t_foreign_type;
+ };
+typedef enum scm_t_foreign_type scm_t_foreign_type;
typedef void (*scm_t_foreign_finalizer) (void *);
#define SCM_FOREIGN_P(x) \
(!SCM_IMP (x) && SCM_TYP7(x) == scm_tc7_foreign)
-#define SCM_VALIDATE_FOREIGN(pos, x) \
+#define SCM_VALIDATE_FOREIGN(pos, x) \
SCM_MAKE_VALIDATE (pos, x, FOREIGN_P)
-#define SCM_FOREIGN_TYPE(x) \
- ((scm_t_foreign_type)((SCM_CELL_WORD_0 (x) >> 8)&0xff))
-#define SCM_FOREIGN_POINTER(x, ctype) \
- ((ctype*)SCM_CELL_WORD_1 (x))
-#define SCM_FOREIGN_VALUE_REF(x, ctype) \
- (*SCM_FOREIGN_POINTER (x, ctype))
-#define SCM_FOREIGN_VALUE_SET(x, ctype, val) \
- (*SCM_FOREIGN_POINTER (x, ctype) = (val))
-#define SCM_FOREIGN_HAS_FINALIZER(x) \
+#define SCM_FOREIGN_POINTER(x) \
+ ((void *) SCM_CELL_WORD_1 (x))
+#define SCM_FOREIGN_HAS_FINALIZER(x) \
((SCM_CELL_WORD_0 (x) >> 16) & 0x1)
-#define SCM_FOREIGN_LEN(x) \
- ((size_t)(SCM_CELL_WORD_0 (x) >> 17))
-
-#define SCM_FOREIGN_TYPED_P(x, type) \
- (SCM_FOREIGN_P (x) && SCM_FOREIGN_TYPE (x) == SCM_FOREIGN_TYPE_##type)
-#define SCM_VALIDATE_FOREIGN_TYPED(pos, x, type) \
- do { \
- SCM_ASSERT_TYPE (SCM_FOREIGN_TYPED_P (x, type), x, pos, FUNC_NAME, \
- "FOREIGN_"#type"_P"); \
- } while (0)
-
-#define SCM_FOREIGN_VALUE_P(x) \
- (SCM_FOREIGN_P (x) && SCM_FOREIGN_TYPE (x) != SCM_FOREIGN_TYPE_VOID)
-#define SCM_VALIDATE_FOREIGN_VALUE(pos, x) \
- SCM_MAKE_VALIDATE (pos, x, FOREIGN_VALUE_P)
-
-SCM_API SCM scm_take_foreign_pointer (scm_t_foreign_type type, void *ptr,
- size_t len,
- scm_t_foreign_finalizer finalizer);
+
+SCM_API SCM scm_take_foreign_pointer (void *, scm_t_foreign_finalizer);
SCM_API SCM scm_alignof (SCM type);
SCM_API SCM scm_sizeof (SCM type);
-SCM_API SCM scm_foreign_type (SCM foreign);
-SCM_API SCM scm_foreign_ref (SCM foreign);
-SCM_API SCM scm_foreign_set_x (SCM foreign, SCM val);
+SCM_API SCM scm_foreign_address (SCM foreign);
SCM_API SCM scm_foreign_to_bytevector (SCM foreign, SCM type,
SCM offset, SCM len);
SCM_API SCM scm_foreign_set_finalizer_x (SCM foreign, SCM finalizer);
SCM_API SCM scm_bytevector_to_foreign (SCM bv, SCM offset, SCM len);
+SCM_INTERNAL SCM scm_make_pointer (SCM address, SCM finalizer);
SCM_INTERNAL void scm_i_foreign_print (SCM foreign, SCM port,
scm_print_state *pstate);