Remove duplicate c-layout implementation
Signed-off-by: Kristin Rutenkolk <kristin.rutenkolk@hhu.de>
This commit is contained in:
parent
8ea121723c
commit
33e1a95ba9
1 changed files with 5 additions and 26 deletions
|
|
@ -3,29 +3,8 @@
|
||||||
(:require
|
(:require
|
||||||
[coffi.mem :as mem]))
|
[coffi.mem :as mem]))
|
||||||
|
|
||||||
(defn with-c-layout
|
(def with-c-layout
|
||||||
"Forces a struct specification to C layout rules.
|
@#'mem/with-c-layout)
|
||||||
|
(alter-meta! #'with-c-layout #(merge (-> (meta #'mem/with-c-layout)
|
||||||
This will add padding fields between fields to match C alignment
|
(dissoc :private))
|
||||||
requirements."
|
%))
|
||||||
[struct-spec]
|
|
||||||
(let [aligned-fields
|
|
||||||
(loop [offset 0
|
|
||||||
aligned-fields []
|
|
||||||
fields (nth struct-spec 1)]
|
|
||||||
(if (seq fields)
|
|
||||||
(let [[[_ type :as field] & fields] fields
|
|
||||||
size (mem/size-of type)
|
|
||||||
align (mem/align-of type)
|
|
||||||
r (rem offset align)]
|
|
||||||
(recur (cond-> (+ offset size)
|
|
||||||
(pos? r) (+ (- align r)))
|
|
||||||
(cond-> aligned-fields
|
|
||||||
(pos? r) (conj [::padding [::mem/padding (- align r)]])
|
|
||||||
:always (conj field))
|
|
||||||
fields))
|
|
||||||
(let [strongest-alignment (reduce max (map (comp mem/align-of second) (nth struct-spec 1)))
|
|
||||||
r (rem offset strongest-alignment)]
|
|
||||||
(cond-> aligned-fields
|
|
||||||
(pos? r) (conj [::padding [::mem/padding (- strongest-alignment r)]])))))]
|
|
||||||
(assoc struct-spec 1 aligned-fields)))
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue