Remove duplicate c-layout implementation

Signed-off-by: Kristin Rutenkolk <kristin.rutenkolk@hhu.de>
This commit is contained in:
Joshua Suskalo 2025-01-01 20:12:04 -05:00 committed by Kristin Rutenkolk
parent 8ea121723c
commit 33e1a95ba9

View file

@ -3,29 +3,8 @@
(:require
[coffi.mem :as mem]))
(defn with-c-layout
"Forces a struct specification to C layout rules.
This will add padding fields between fields to match C alignment
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)))
(def with-c-layout
@#'mem/with-c-layout)
(alter-meta! #'with-c-layout #(merge (-> (meta #'mem/with-c-layout)
(dissoc :private))
%))