From b960c01bb3940d110905d3be38de435bbdfd5587 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Tue, 18 Jan 2022 13:28:00 -0600 Subject: [PATCH] Add support for layouts being passed to size-of and align-of --- src/clj/coffi/mem.clj | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/clj/coffi/mem.clj b/src/clj/coffi/mem.clj index c43694a..ad66d26 100644 --- a/src/clj/coffi/mem.clj +++ b/src/clj/coffi/mem.clj @@ -378,18 +378,22 @@ If a type serializes to a primitive it returns return a Java primitive type. Otherwise, it returns [[MemorySegment]]." - [type] + ^Class [type] (java-prim-layout (or (primitive-type type) type) MemorySegment)) (defn size-of "The size in bytes of the given `type`." - [type] - (.byteSize ^MemoryLayout (c-layout type))) + ^long [type] + (let [t (cond-> type + (not (instance? MemoryLayout type)) c-layout)] + (.byteSize ^MemoryLayout t))) (defn align-of "The alignment in bytes of the given `type`." - [type] - (.byteAlignment ^MemoryLayout (c-layout type))) + ^long [type] + (let [t (cond-> type + (not (instance? MemoryLayout type)) c-layout)] + (.byteAlignment ^MemoryLayout t))) (defn alloc-instance "Allocates a memory segment for the given `type`."