From a6a5a200aff519d458d5582fdab776e30af40efc Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 19 May 2017 22:11:50 -0700 Subject: ffi: new function, carray-cptr. * ffi.c (carray_cptr): New function. (ffi_init): Registered intrinsic. * ffi.h (carray_cptr): Declared. * txr.1: Documented. --- ffi.c | 9 +++++++++ ffi.h | 1 + txr.1 | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) 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 ) -- cgit v1.2.3