summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-05-21 05:20:03 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-05-21 05:20:03 -0700
commit8465d27df3fef6089ea122914a0c6b0cf7b82e85 (patch)
tree73da2bc6f0be7e726db7c7080776c4c80aef9395
parent8f9697ed8f42681ab0df16ab38859d61ea05412e (diff)
downloadtxr-8465d27df3fef6089ea122914a0c6b0cf7b82e85.tar.gz
txr-8465d27df3fef6089ea122914a0c6b0cf7b82e85.tar.bz2
txr-8465d27df3fef6089ea122914a0c6b0cf7b82e85.zip
ffi: flex structs: minor refactor.
* ffi.c (ffi_flex_struct_in): Function renamed to ffi_flex_array_len, because its responsibility is determining the length of a flexible array that is not null terminated. We don't pass in the structure's type's descriptor any more, but the member descriptor. (ffi_struct_in, ffi_struct_get): Follow rename and changed parameter conventions. * tests/017/flexstruct.tl: Added test case with nested flexible structure.
-rw-r--r--ffi.c7
-rw-r--r--tests/017/flexstruct.tl9
2 files changed, 12 insertions, 4 deletions
diff --git a/ffi.c b/ffi.c
index 18382cf6..404af578 100644
--- a/ffi.c
+++ b/ffi.c
@@ -2443,9 +2443,8 @@ static void ffi_ptr_in_release(struct txr_ffi_type *tft, val obj,
*loc = 0;
}
-static val ffi_flex_struct_in(struct txr_ffi_type *tft, val strct, val self)
+static val ffi_flex_array_len(struct smemb *lastm, val strct, val self)
{
- struct smemb *lastm = &tft->memb[tft->nelem - 1];
struct txr_ffi_type *lmtft = lastm->mtft;
(void) self;
@@ -2499,7 +2498,7 @@ static val ffi_struct_in(struct txr_ffi_type *tft, int copy, mem_t *src,
ucnum offs = memb[i].offs;
if (slsym) {
if (flexp && copy && i == nmemb - 1)
- ffi_flex_struct_in(tft, strct, self);
+ ffi_flex_array_len(&memb[i], strct, self);
if (mtft->in != 0) {
val slval = slot(strct, slsym);
slotset(strct, slsym, mtft->in(mtft, copy, src + offs, slval, self));
@@ -2566,7 +2565,7 @@ static val ffi_struct_get(struct txr_ffi_type *tft, mem_t *src, val self)
ucnum offs = memb[i].offs;
if (slsym) {
if (flexp && i == nmemb - 1) {
- val slval = ffi_flex_struct_in(tft, strct, self);
+ val slval = ffi_flex_array_len(&memb[i], strct, self);
if (mtft->in != 0)
slotset(strct, slsym, mtft->in(mtft, 1, src + offs, slval, self));
} else {
diff --git a/tests/017/flexstruct.tl b/tests/017/flexstruct.tl
index c8feedef..8f91096e 100644
--- a/tests/017/flexstruct.tl
+++ b/tests/017/flexstruct.tl
@@ -62,3 +62,12 @@
(mtest
(ffi-get #b'0300010002000300' (ffi fs3)) #S(fs3 a 3 b #(1 2 3)))
+
+(typedef fs4 (struct fs4
+ (c int8)
+ (s fs0)))
+
+(mtest
+ (sizeof fs4) 2
+ (ffi-put #S(fs4 c 93 s #S(fs0 a 4 b "ABCD")) (ffi fs4)) #b'5d0441424344'
+ (ffi-get #b'5d0441424344' (ffi fs4)) #S(fs4 c 93 s #S(fs0 a 4 b "ABCD")))