FFI: Honor alignment constraints in `parse-c-struct'.
authorLudovic Courtès <ludo@gnu.org>
Mon, 8 Nov 2010 22:08:48 +0000 (23:08 +0100)
committerLudovic Courtès <ludo@gnu.org>
Thu, 11 Nov 2010 15:41:15 +0000 (16:41 +0100)
* module/system/foreign.scm (parse-c-struct): Honor alignment
  constraints for TYPE.

* test-suite/tests/foreign.test ("structs")["alignment constraints
  honored"]: New test.

module/system/foreign.scm
test-suite/tests/foreign.test

index 84d1a03..2990521 100644 (file)
 
 (define (parse-c-struct foreign types)
   (let ((size (fold (lambda (type total)
-                      (+ (sizeof type) total))
+                      (+ (sizeof type)
+                         (align total (alignof type))))
                     0
                     types)))
     (read-c-struct (pointer->bytevector foreign size) 0 types)))
index db92eca..05846ed 100644 (file)
   (pass-if "parse-c-struct"
     (let ((layout (list int64 uint8))
           (data   (list -300 43)))
+      (equal? (parse-c-struct (make-c-struct layout data)
+                              layout)
+              data)))
+
+  (pass-if "alignment constraints honored"
+    (let ((layout (list int8 double))
+          (data   (list -7 3.14)))
       (equal? (parse-c-struct (make-c-struct layout data)
                               layout)
               data))))