diff --git a/CHANGELOG.md b/CHANGELOG.md index 2694a21..cfea49d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. This change - New function to allow getting the backing memory segment of a `coffi.ffi.StaticVariable`, to replace the `Addressable` implementation lost in the migration to JDK 18 ### Fixed +- Bug where padding in structs may be increased when fields have alignments less than their size - Bug where pointer alignment was incorrectly defined ## [0.5.357] - 2022-07-07 diff --git a/src/clj/coffi/layout.clj b/src/clj/coffi/layout.clj index 047bd62..15a2db6 100644 --- a/src/clj/coffi/layout.clj +++ b/src/clj/coffi/layout.clj @@ -16,11 +16,12 @@ (if (seq fields) (let [[[_ type :as field] & fields] fields size (mem/size-of type) - r (rem offset (mem/align-of type))] + align (mem/align-of type) + r (rem offset align)] (recur (cond-> (+ offset size) - (pos? r) (+ (- size r))) + (pos? r) (+ (- align r))) (cond-> aligned-fields - (pos? r) (conj [::padding [::mem/padding (- size r)]]) + (pos? r) (conj [::padding [::mem/padding (- align r)]]) :always (conj field)) fields)) (let [strongest-alignment (mem/align-of struct-spec)