Fix FFI struct sizing to account for trailing padding.
authorKen Raeburn <raeburn@raeburn.org>
Mon, 21 May 2012 04:30:45 +0000 (00:30 -0400)
committerKen Raeburn <raeburn@raeburn.org>
Mon, 21 May 2012 05:13:13 +0000 (01:13 -0400)
* 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 }.

libguile/foreign.c
test-suite/tests/foreign.test

index 00e9c75..8329131 100644 (file)
@@ -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);
index 6eafe95..47686ee 100644 (file)
     (= (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)