Add type hints to most utility functions

This commit is contained in:
Joshua Suskalo 2022-01-18 13:38:32 -06:00
parent b029a41f6a
commit e8b3c8e2b2

View file

@ -88,14 +88,14 @@
"Allocates `size` bytes. "Allocates `size` bytes.
If a `scope` is provided, the allocation will be reclaimed when it is closed." If a `scope` is provided, the allocation will be reclaimed when it is closed."
([size] (alloc size (connected-scope))) (^MemorySegment [size] (alloc size (connected-scope)))
([size scope] (MemorySegment/allocateNative (long size) ^ResourceScope scope))) (^MemorySegment [size scope] (MemorySegment/allocateNative (long size) ^ResourceScope scope)))
(defn alloc-with (defn alloc-with
"Allocates `size` bytes using the `allocator`." "Allocates `size` bytes using the `allocator`."
([allocator size] (^MemorySegment [allocator size]
(.allocate ^SegmentAllocator allocator (long size))) (.allocate ^SegmentAllocator allocator (long size)))
([allocator size alignment] (^MemorySegment [allocator size alignment]
(.allocate ^SegmentAllocator allocator (long size) (long alignment)))) (.allocate ^SegmentAllocator allocator (long size) (long alignment))))
(defmacro with-acquired (defmacro with-acquired
@ -123,7 +123,7 @@
"Gets the address of a given segment. "Gets the address of a given segment.
This value can be used as an argument to functions which take a pointer." This value can be used as an argument to functions which take a pointer."
[addressable] ^MemoryAddress [addressable]
(.address ^Addressable addressable)) (.address ^Addressable addressable))
(defn null? (defn null?
@ -144,27 +144,27 @@
Because this fetches from the global segment, it has no associated scope, and Because this fetches from the global segment, it has no associated scope, and
therefore the reference created here cannot prevent the value from being therefore the reference created here cannot prevent the value from being
freed. Be careful to ensure that you are not retaining an object incorrectly." freed. Be careful to ensure that you are not retaining an object incorrectly."
[address size] ^MemorySegment [address size]
(.asSlice (MemorySegment/globalNativeSegment) (.asSlice (MemorySegment/globalNativeSegment)
^MemoryAddress address (long size))) ^MemoryAddress address (long size)))
(defn slice (defn slice
"Get a slice over the `segment` with the given `offset`." "Get a slice over the `segment` with the given `offset`."
([segment offset] (^MemorySegment [segment offset]
(.asSlice ^MemorySegment segment (long offset))) (.asSlice ^MemorySegment segment (long offset)))
([segment offset size] (^MemorySegment [segment offset size]
(.asSlice ^MemorySegment segment (long offset) (long size)))) (.asSlice ^MemorySegment segment (long offset) (long size))))
(defn slice-into (defn slice-into
"Get a slice into the `segment` starting at the `address`." "Get a slice into the `segment` starting at the `address`."
([address segment] (^MemorySegment [address segment]
(.asSlice ^MemorySegment segment ^MemoryAddress address)) (.asSlice ^MemorySegment segment ^MemoryAddress address))
([address segment size] (^MemorySegment [address segment size]
(.asSlice ^MemorySegment segment ^MemoryAddress address (long size)))) (.asSlice ^MemorySegment segment ^MemoryAddress address (long size))))
(defn with-offset (defn with-offset
"Get a new address `offset` from the old `address`." "Get a new address `offset` from the old `address`."
[address offset] ^MemoryAddress [address offset]
(.addOffset ^MemoryAddress address (long offset))) (.addOffset ^MemoryAddress address (long offset)))
(defn as-segment (defn as-segment
@ -175,34 +175,37 @@
cleanup in a way that doesn't require modifying the code at the point of cleanup in a way that doesn't require modifying the code at the point of
freeing, and allows shared or garbage collected resources to be freed freeing, and allows shared or garbage collected resources to be freed
correctly." correctly."
([address size scope] (^MemorySegment [^MemoryAddress address size scope]
(.asSegment ^MemoryAddress address size scope)) (.asSegment address (long size) scope))
([address size scope cleanup] (^MemorySegment [^MemoryAddress address size ^ResourceScope scope cleanup]
(.asSegment ^MemoryAddress address size cleanup scope))) (.asSegment address (long size) cleanup scope)))
(defn add-close-action! (defn add-close-action!
"Adds a 0-arity function to be run when the `scope` closes." "Adds a 0-arity function to be run when the `scope` closes."
[scope action] [^ResourceScope scope ^Runnable action]
(.addCloseAction ^ResourceScope scope action)) (.addCloseAction scope action)
nil)
(defn copy-segment (defn copy-segment
"Copies the content to `dest` from `src`" "Copies the content to `dest` from `src`.
[dest src]
Returns `dest`."
^MemorySegment [^MemorySegment dest ^MemorySegment src]
(with-acquired (map segment-scope [src dest]) (with-acquired (map segment-scope [src dest])
(.copyFrom ^MemorySegment dest ^MemorySegment src))) (.copyFrom dest src)
dest))
(defn clone-segment (defn clone-segment
"Clones the content of `segment` into a new segment of the same size." "Clones the content of `segment` into a new segment of the same size."
([segment] (clone-segment segment (connected-scope))) (^MemorySegment [segment] (clone-segment segment (connected-scope)))
([segment scope] (^MemorySegment [^MemorySegment segment scope]
(with-acquired [(segment-scope segment) scope] (with-acquired [(segment-scope segment) scope]
(doto ^MemorySegment (alloc (.byteSize ^MemorySegment segment) scope) (copy-segment ^MemorySegment (alloc (.byteSize segment) scope) segment))))
(copy-segment segment)))))
(defn slice-segments (defn slice-segments
"Constructs a lazy seq of `size`-length memory segments, sliced from `segment`." "Constructs a lazy seq of `size`-length memory segments, sliced from `segment`."
[segment size] [^MemorySegment segment size]
(let [num-segments (quot (.byteSize ^MemorySegment segment) size)] (let [num-segments (quot (.byteSize segment) size)]
(map #(slice segment (* % size) size) (map #(slice segment (* % size) size)
(range num-segments)))) (range num-segments))))