summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ffi.c9
-rw-r--r--ffi.h1
-rw-r--r--txr.141
3 files changed, 51 insertions, 0 deletions
diff --git a/ffi.c b/ffi.c
index 0f0129dc..72d3ae35 100644
--- a/ffi.c
+++ b/ffi.c
@@ -2615,6 +2615,14 @@ val carray_buf(val buf, val type)
return make_carray(type, data, nelem, buf);
}
+val carray_cptr(val cptr, val type, val len)
+{
+ mem_t *data = cptr_get(cptr);
+ cnum nelem = c_num(default_arg(len, negone));
+ (void) ffi_type_struct(type);
+ return make_carray(type, data, nelem, 0);
+}
+
val vec_carray(val carray, val null_term_p)
{
val nt_p = default_null_arg(null_term_p);
@@ -2744,6 +2752,7 @@ void ffi_init(void)
reg_fun(intern(lit("carray-vec"), user_package), func_n3o(carray_vec, 2));
reg_fun(intern(lit("carray-blank"), user_package), func_n2(carray_blank));
reg_fun(intern(lit("carray-buf"), user_package), func_n2(carray_buf));
+ reg_fun(intern(lit("carray-cptr"), user_package), func_n3o(carray_cptr, 2));
reg_fun(intern(lit("vec-carray"), user_package), func_n2o(vec_carray, 1));
reg_fun(intern(lit("list-carray"), user_package), func_n2o(list_carray, 1));
reg_fun(intern(lit("carray-ref"), user_package), func_n2(carray_ref));
diff --git a/ffi.h b/ffi.h
index 31031e3f..59a3ec30 100644
--- a/ffi.h
+++ b/ffi.h
@@ -74,6 +74,7 @@ mem_t *carray_get(val carray, val type, val self);
val carray_vec(val vec, val type, val null_term_p);
val carray_blank(val nelem, val type);
val carray_buf(val buf, val type);
+val carray_cptr(val cptr, val type, val len);
val vec_carray(val carray, val null_term_p);
val list_carray(val carray, val null_term_p);
val carray_ref(val carray, val idx);
diff --git a/txr.1 b/txr.1
index 9b5956fd..4de55690 100644
--- a/txr.1
+++ b/txr.1
@@ -54968,6 +54968,47 @@ type, the calculated number of elements is rounded down. The trailing portion
of the buffer corresponding to the division remainder, being insufficient
to constitute a whole array element, is excluded from the array view.
+.coNP Function @ carray-cptr
+.synb
+.mets (carray-cptr < cptr < type <> [ length ])
+.syne
+.desc
+The
+.code carray-cptr
+function creates a
+.code carray
+object based on a pointer derived from a
+.code cptr
+object.
+
+The
+.meta cptr
+argument must be of type
+.codn cptr .
+The object's
+.code cptr
+type tag is ignored.
+
+The
+.meta type
+argument must specify a compiled FFI type, which will become
+the element type of the returned
+.codn carray .
+
+If
+.meta length
+is specified as
+.codn nil ,
+or not specified,
+then the returned
+.code carray
+object will be of unknown length. Otherwise,
+.meta length
+must be a non-negative integer which will be taken as the
+length of the array.
+
+Note: this conversion is inherently unsafe.
+
.coNP Function @ length-carray
.synb
.mets (length-carray << carray )