diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2017-05-10 22:12:53 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2017-05-10 22:12:53 -0700 |
commit | 39413e2d3568c3d18976d9b1c55af55af7905e4e (patch) | |
tree | 0dc61f0dce8aba927d4065b8eebf3f6f84718e2c | |
parent | a8d124d7626602c6ff44c63757d3c56e6392fc80 (diff) | |
download | txr-39413e2d3568c3d18976d9b1c55af55af7905e4e.tar.gz txr-39413e2d3568c3d18976d9b1c55af55af7905e4e.tar.bz2 txr-39413e2d3568c3d18976d9b1c55af55af7905e4e.zip |
ffi: split varray behavior into separate virtuals.
* ffi.c (struct txr_ffi_type): is_varray flag removed.
We no longer need to test this and adjust behavior.
(ffi_array_in, ffi_array_out): Don't calculate nelem
differently based on flag; just pass down tft->nelem. Code
cleaned up.
(ffi_varray_put, ffi_varray_in): New functions.
(ffi_type_compile): Use ffi_varray_put and ffi_varray_in for
variable array type instead of setting flag. Also, specify the
out function as null; it never made sense to have one, since
there is no get. Finally, set the size of the type to zero,
since it has no pass by value semantics.
-rw-r--r-- | ffi.c | 47 |
1 files changed, 26 insertions, 21 deletions
@@ -94,7 +94,6 @@ struct txr_ffi_type { cnum size, align; cnum nelem; unsigned null_term : 1; - unsigned is_varray : 1; unsigned char_conv : 1; unsigned wchar_conv : 1; unsigned bchar_conv : 1; @@ -1074,22 +1073,18 @@ static val ffi_array_in_common(struct txr_ffi_type *tft, int copy, static val ffi_array_in(struct txr_ffi_type *tft, int copy, mem_t *src, val vec, val self) { - cnum nelem = if3(tft->is_varray, c_num(length(vec)), tft->nelem); - if (tft->char_conv) { - val str = ffi_char_array_get(tft, src, nelem); - vec = if3(vec, replace(vec, str, zero, t), str); + val str = ffi_char_array_get(tft, src, tft->nelem); + return if3(vec, replace(vec, str, zero, t), str); } else if (tft->wchar_conv) { - val str = ffi_wchar_array_get(tft, src, nelem); - vec = if3(vec, replace(vec, str, zero, t), str); + val str = ffi_wchar_array_get(tft, src, tft->nelem); + return if3(vec, replace(vec, str, zero, t), str); } else if (tft->bchar_conv) { - val str = ffi_bchar_array_get(tft, src, nelem); - vec = if3(vec, replace(vec, str, zero, t), str); + val str = ffi_bchar_array_get(tft, src, tft->nelem); + return if3(vec, replace(vec, str, zero, t), str); } else { - vec = ffi_array_in_common(tft, copy, src, vec, self, nelem); + return ffi_array_in_common(tft, copy, src, vec, self, tft->nelem); } - - return vec; } static void ffi_array_put_common(struct txr_ffi_type *tft, val vec, mem_t *dst, @@ -1117,9 +1112,7 @@ static void ffi_array_put_common(struct txr_ffi_type *tft, val vec, mem_t *dst, static void ffi_array_put(struct txr_ffi_type *tft, val vec, mem_t *dst, val self) { - int nt = tft->null_term; - cnum nelem = if3(tft->is_varray, c_num(length(vec)) + nt, tft->nelem); - ffi_array_put_common(tft, vec, dst, self, nelem); + ffi_array_put_common(tft, vec, dst, self, tft->nelem); } static void ffi_array_out_common(struct txr_ffi_type *tft, int copy, val vec, @@ -1151,9 +1144,7 @@ static void ffi_array_out_common(struct txr_ffi_type *tft, int copy, val vec, static void ffi_array_out(struct txr_ffi_type *tft, int copy, val vec, mem_t *dst, val self) { - int nt = tft->null_term; - cnum nelem = if3(tft->is_varray, c_num(length(vec)) + nt, tft->nelem); - ffi_array_out_common(tft, copy, vec, dst, self, nelem); + ffi_array_out_common(tft, copy, vec, dst, self, tft->nelem); } static val ffi_array_get_common(struct txr_ffi_type *tft, mem_t *src, val self, @@ -1190,6 +1181,20 @@ static val ffi_array_get(struct txr_ffi_type *tft, mem_t *src, val self) return ffi_array_get_common(tft, src, self, nelem); } +static void ffi_varray_put(struct txr_ffi_type *tft, val vec, mem_t *dst, + val self) +{ + cnum nelem = c_num(length(vec)) + tft->null_term; + ffi_array_put_common(tft, vec, dst, self, nelem); +} + +static val ffi_varray_in(struct txr_ffi_type *tft, int copy, mem_t *src, + val vec, val self) +{ + cnum nelem = c_num(length(vec)) + tft->null_term; + return ffi_array_in_common(tft, copy, src, vec, self, nelem); +} + static val make_ffi_type_builtin(val syntax, val lisp_type, cnum size, ffi_type *ft, void (*put)(struct txr_ffi_type *, @@ -1398,15 +1403,15 @@ val ffi_type_compile(val syntax) val eltype_syntax = cadr(syntax); val eltype = ffi_type_compile(eltype_syntax); val type = make_ffi_type_pointer(syntax, vec_s, sizeof (mem_t *), - ffi_array_put, ffi_void_get, - ffi_array_in, ffi_array_out, + ffi_varray_put, ffi_void_get, + ffi_varray_in, 0, eltype); struct txr_ffi_type *tft = ffi_type_struct(type); if (sym == zarray_s) tft->null_term = 1; - tft->is_varray = 1; tft->alloc = ffi_varray_alloc; tft->free = free; + tft->size = 0; return type; } else if (length(syntax) == three) { val dim = cadr(syntax); |