From fb3b95977f067df650d5cd52b90d3376900892e6 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Mon, 27 Sep 2021 11:16:21 -0500 Subject: [PATCH] Add a function to tie two scopes together --- src/clj/coffi/mem.clj | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/clj/coffi/mem.clj b/src/clj/coffi/mem.clj index 483aff8..2c5ab29 100644 --- a/src/clj/coffi/mem.clj +++ b/src/clj/coffi/mem.clj @@ -98,6 +98,11 @@ ([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 a `scope` to ensure it will not be released until the `body` completes. @@ -105,10 +110,9 @@ an arbitrary passed scope, it is best practice to wrap code that interacts with it wrapped in this." [scope & body] - `(let [scope# ~scope - handle# (.acquire ^ResourceScope scope#)] - (try ~@body - (finally (.release ^ResourceScope scope# handle#))))) + `(with-open [scope# (stack-scope)] + (keep-alive scope# ~scope) + ~@body)) (defn address-of "Gets the address of a given segment.