From bd7216a06e6698a8583407d9b29ce0d6fe4ac9cc Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Wed, 2 Oct 2024 16:22:29 -0400 Subject: [PATCH] Add `null` for implementing serdes --- CHANGELOG.md | 3 +++ src/clj/coffi/ffi.clj | 2 +- src/clj/coffi/mem.clj | 14 +++++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0732987..0a2accb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). ## [UNRELEASED] +### Added +- New `coffi.mem/null` var for implementing custom types + ### Fixed - Usage of deprecated `(Class/STATIC_FIELD)` access pattern diff --git a/src/clj/coffi/ffi.clj b/src/clj/coffi/ffi.clj index 9263c45..1ec9a4b 100644 --- a/src/clj/coffi/ffi.clj +++ b/src/clj/coffi/ffi.clj @@ -292,7 +292,7 @@ ;; cast null pointers to something understood by panama (#{::mem/pointer} type) - `(or ~sym MemorySegment/NULL) + `(or ~sym mem/null) (mem/primitive-type type) `(mem/serialize* ~sym ~type-sym ~arena) diff --git a/src/clj/coffi/mem.clj b/src/clj/coffi/mem.clj index fcf3ecb..99bd00e 100644 --- a/src/clj/coffi/mem.clj +++ b/src/clj/coffi/mem.clj @@ -112,10 +112,18 @@ ^long [addressable] (.address ^MemorySegment addressable)) +(def ^MemorySegment null + "The NULL pointer object. + + While this object is safe to pass to functions which serialize to a pointer, + it's generally encouraged to simply pass `nil`. This value primarily exists to + make it easier to write custom types with a primitive pointer representation." + MemorySegment/NULL) + (defn null? "Checks if a memory address is null." [addr] - (or (.equals MemorySegment/NULL addr) (not addr))) + (or (.equals null addr) (not addr))) (defn address? "Checks if an object is a memory address. @@ -872,7 +880,7 @@ (serialize-into obj (second type) segment arena) (address-of segment)) obj) - MemorySegment/NULL)) + null)) (defmethod serialize* ::void [_obj _type _arena] @@ -1132,7 +1140,7 @@ [obj _type ^Arena arena] (if obj (.allocateFrom arena ^String obj) - MemorySegment/NULL)) + null)) (defmethod deserialize* ::c-string [addr _type]