From 7686d9d9ca4188747a225a35472832c50c19ed39 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Wed, 29 Sep 2021 11:00:21 -0500 Subject: [PATCH 1/5] Update changelog for unreleased section --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0df9aaa..ccc7bac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Change Log 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] + ## [0.1.176] - 2021-09-29 ### Fixed - Usage of `defcfn` without a docstring produced an invalid `def` form @@ -15,5 +17,6 @@ All notable changes to this project will be documented in this file. This change - Support for serializing and deserializing arbitrary Clojure functions - Support for serializing and deserializing arbitrary Clojure data structures +[Unreleased]: https://github.com/IGJoshua/coffi/compare/v0.1.176...HEAD [0.1.176]: https://github.com/IGJoshua/coffi/compare/v0.1.169...v0.1.176 [0.1.169]: https://github.com/IGJoshua/coffi/compare/16f56bc31d69142ec4d2fb61b15b069d78b127ca...v0.1.169 From 5ab2bae1fa325bd8692652fbf3b6d78a6954e342 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Thu, 30 Sep 2021 15:40:55 -0500 Subject: [PATCH 2/5] Add a function's address to the metadata of deserialized functions --- CHANGELOG.md | 2 ++ src/clj/coffi/ffi.clj | 16 +++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53a927d..8f363c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ 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 +- An `::ffi/address` key to deserialized function pointers' metadata ## [0.1.184] - 2021-09-30 ### Fixed diff --git a/src/clj/coffi/ffi.clj b/src/clj/coffi/ffi.clj index c6558a0..fed0b1b 100644 --- a/src/clj/coffi/ffi.clj +++ b/src/clj/coffi/ffi.clj @@ -368,13 +368,15 @@ (defmethod mem/deserialize* ::fn [addr [_fn arg-types ret-type & {:keys [raw-fn?]}]] (when-not (mem/null? addr) - (-> addr - (downcall-handle - (method-type arg-types ret-type) - (function-descriptor arg-types ret-type)) - (downcall-fn arg-types ret-type) - (cond-> - (not raw-fn?) (make-serde-wrapper arg-types ret-type))))) + (vary-meta + (-> addr + (downcall-handle + (method-type arg-types ret-type) + (function-descriptor arg-types ret-type)) + (downcall-fn arg-types ret-type) + (cond-> + (not raw-fn?) (make-serde-wrapper arg-types ret-type))) + assoc ::address addr))) ;;; Static memory access From daa1949f3209d3a90d95cc6b7758a80e726c210e Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Thu, 30 Sep 2021 15:45:10 -0500 Subject: [PATCH 3/5] Add address metadata key to defcfn as well --- CHANGELOG.md | 2 +- src/clj/coffi/ffi.clj | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f363c2..007bd61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ All notable changes to this project will be documented in this file. This change ## [Unreleased] ### Added -- An `::ffi/address` key to deserialized function pointers' metadata +- An `::ffi/address` key to wrapper functions' metadata ## [0.1.184] - 2021-09-30 ### Fixed diff --git a/src/clj/coffi/ffi.clj b/src/clj/coffi/ffi.clj index fed0b1b..6fa1e32 100644 --- a/src/clj/coffi/ffi.clj +++ b/src/clj/coffi/ffi.clj @@ -545,6 +545,7 @@ (let [args (s/conform ::defcfn-args args) args-types (gensym "args-types") ret-type (gensym "ret-type") + address (gensym "symbol") invoke (gensym "invoke") native-sym (gensym "native") [arity fn-tail] (-> args :wrapper :fn-tail) @@ -558,7 +559,8 @@ nil))] `(let [~args-types ~(:native-arglist args) ~ret-type ~(:return-type args) - ~invoke (make-downcall ~(name (:symbol args)) ~args-types ~ret-type) + ~address (find-symbol ~(name (:symbol args))) + ~invoke (make-downcall ~address ~args-types ~ret-type) ~(or (-> args :wrapper :native-fn) native-sym) ~(if (and (every? #(= % (mem/primitive-type %)) (:native-arglist args)) @@ -581,7 +583,8 @@ (list (mapv (comp symbol name) (:native-arglist args))))))) - (:attr-map args))) + (assoc (:attr-map args) + ::address address))) ~@(when-let [doc (:doc args)] (list doc)) fun#)))) From 9d65f47a96efbb80a54b1799e00ff4864d5ace6d Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Thu, 30 Sep 2021 19:54:48 -0500 Subject: [PATCH 4/5] Remove reference to method that isn't in panama anymore --- CHANGELOG.md | 3 +++ src/clj/coffi/mem.clj | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 007bd61..28e0bfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. This change ### Added - An `::ffi/address` key to wrapper functions' metadata +### Fixed +- Usage of a method no longer in Panama that breaks `with-acquired` + ## [0.1.184] - 2021-09-30 ### Fixed - Deserializing nullpointers as functions threw an exception diff --git a/src/clj/coffi/mem.clj b/src/clj/coffi/mem.clj index 5f284c2..69118f6 100644 --- a/src/clj/coffi/mem.clj +++ b/src/clj/coffi/mem.clj @@ -29,6 +29,7 @@ MemoryLayout MemorySegment ResourceScope + ResourceScope$Handle SegmentAllocator))) (defn stack-scope @@ -96,11 +97,6 @@ ([allocator size alignment] (.allocate ^SegmentAllocator allocator (long size) (long alignment)))) -(defn keep-alive - "Ensures that the scope `target` is not released before `source`." - [source target] - (.keepAlive ^ResourceScope source ^ResourceScope target)) - (defmacro with-acquired "Acquires one or more `scopes` until the `body` completes. @@ -109,9 +105,15 @@ with it wrapped in this." {:style/indent 1} [scopes & body] - `(with-open [scope# (stack-scope)] - (run! (partial keep-alive scope#) ~scopes) - ~@body)) + `(let [scopes# (vec ~scopes) + handles# (mapv #(.acquire ^ResourceScope %) scopes#)] + (try ~@body + (finally + (doseq [idx# (range (count scopes#)) + :let [scope# (nth scopes# idx#) + handle# (nth handles# idx#)]] + (.release ^ResourceScope scope# + ^ResourceScope$Handle handle#)))))) (s/fdef with-acquired :args (s/cat :scopes any? :body (s/* any?))) From 1eb9edb194ee5fcf6a450e38a1438d0e8ba49aef Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Thu, 30 Sep 2021 20:04:34 -0500 Subject: [PATCH 5/5] Update changelog and readme for new release --- CHANGELOG.md | 4 ++-- README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28e0bfb..9d249e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Change Log 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] +## [0.1.192] - 2021-09-30 ### Added - An `::ffi/address` key to wrapper functions' metadata @@ -28,7 +28,7 @@ All notable changes to this project will be documented in this file. This change - Support for serializing and deserializing arbitrary Clojure functions - Support for serializing and deserializing arbitrary Clojure data structures -[Unreleased]: https://github.com/IGJoshua/coffi/compare/v0.1.184...HEAD +[0.1.192]: https://github.com/IGJoshua/coffi/compare/v0.1.184...v0.1.192 [0.1.184]: https://github.com/IGJoshua/coffi/compare/v0.1.176...v0.1.184 [0.1.176]: https://github.com/IGJoshua/coffi/compare/v0.1.169...v0.1.176 [0.1.169]: https://github.com/IGJoshua/coffi/compare/16f56bc31d69142ec4d2fb61b15b069d78b127ca...v0.1.169 diff --git a/README.md b/README.md index 5bcbb00..c5ac49a 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ This library is available on Clojars. Add one of the following entries to the `:deps` key of your `deps.edn`: ```clojure -org.suskalo/coffi {:mvn/version "0.1.184"} -io.github.IGJoshua/coffi {:git/tag "v0.1.184" :git/sha "ea53cfb"} +org.suskalo/coffi {:mvn/version "0.1.192"} +io.github.IGJoshua/coffi {:git/tag "v0.1.192" :git/sha "9d65f47"} ``` If you use this library as a git dependency, you will need to prepare the