Add a function to tie two scopes together

This commit is contained in:
Joshua Suskalo 2021-09-27 11:16:21 -05:00
parent 5f96439432
commit fb3b95977f

View file

@ -98,6 +98,11 @@
([allocator size alignment] ([allocator size alignment]
(.allocate ^SegmentAllocator allocator (long size) (long 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 (defmacro with-acquired
"Acquires a `scope` to ensure it will not be released until the `body` completes. "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 an arbitrary passed scope, it is best practice to wrap code that interacts
with it wrapped in this." with it wrapped in this."
[scope & body] [scope & body]
`(let [scope# ~scope `(with-open [scope# (stack-scope)]
handle# (.acquire ^ResourceScope scope#)] (keep-alive scope# ~scope)
(try ~@body ~@body))
(finally (.release ^ResourceScope scope# handle#)))))
(defn address-of (defn address-of
"Gets the address of a given segment. "Gets the address of a given segment.