summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKen Raeburn <raeburn@raeburn.org>2012-05-21 00:30:45 -0400
committerKen Raeburn <raeburn@raeburn.org>2012-05-21 01:13:13 -0400
commitcff1d39b2003470b5dcdab988e279587ae2eed8c (patch)
tree4b9cda9c33e5b87bab1c50c58f5d15f2ea5174a8
parenta722bcaa13acd3185e8e7b84f64cb614a46f3532 (diff)
downloadguile-cff1d39b2003470b5dcdab988e279587ae2eed8c.tar.gz
Fix FFI struct sizing to account for trailing padding.
* libguile/foreign.c (scm_sizeof): Make sure the overall size is a multiple of the alignment of the structure. * test-suite/tests/foreign.test: Test size of { double, int8 }.
-rw-r--r--libguile/foreign.c3
-rw-r--r--test-suite/tests/foreign.test4
2 files changed, 6 insertions, 1 deletions
diff --git a/libguile/foreign.c b/libguile/foreign.c
index 00e9c7544..832913147 100644
--- a/libguile/foreign.c
+++ b/libguile/foreign.c
@@ -536,13 +536,14 @@ SCM_DEFINE (scm_sizeof, "sizeof", 1, 0, 0, (SCM type),
{
/* a struct */
size_t off = 0;
+ size_t align = scm_to_size_t (scm_alignof(type));
while (scm_is_pair (type))
{
off = ROUND_UP (off, scm_to_size_t (scm_alignof (scm_car (type))));
off += scm_to_size_t (scm_sizeof (scm_car (type)));
type = scm_cdr (type);
}
- return scm_from_size_t (off);
+ return scm_from_size_t (ROUND_UP(off, align));
}
else
scm_wrong_type_arg (FUNC_NAME, 1, type);
diff --git a/test-suite/tests/foreign.test b/test-suite/tests/foreign.test
index 6eafe954a..47686eebd 100644
--- a/test-suite/tests/foreign.test
+++ b/test-suite/tests/foreign.test
@@ -303,6 +303,10 @@
(= (sizeof (list int8 double))
(+ (alignof double) (sizeof double))))
+ (pass-if "sizeof { double, int8 }"
+ (= (sizeof (list double int8))
+ (+ (alignof double) (sizeof double))))
+
(pass-if "sizeof { short, int, long, pointer }"
(let ((layout (list short int long '*)))
(>= (sizeof layout)