From 3dd9571d24cf29cf6f256a18187083a907eb5752 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Wed, 15 Sep 2021 12:43:08 -0500 Subject: [PATCH] Add functions for manipulating addresses --- src/coffi/ffi.clj | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/coffi/ffi.clj b/src/coffi/ffi.clj index 8438d0d..125b841 100644 --- a/src/coffi/ffi.clj +++ b/src/coffi/ffi.clj @@ -198,6 +198,37 @@ [] (ResourceScope/newSharedScope)) +(defn address-of + "Gets the address of a given segment. + + This value can be used as an argument to functions which take a pointer." + [segment] + (.address ^MemorySegment segment)) + +(defn slice-global + "Gets a slice of the global address space. + + Because this fetches from the global segment, it has no associated scope, and + therefore the reference created here cannot prevent the value from being + freed. Be careful to ensure that you are not retaining an object incorrectly." + [address size] + (.asSlice (MemorySegment/globalNativeSegment) + ^MemoryAddress address ^long size)) + +(defn slice + "Get a slice over the `segment` with the given `offset`." + ([segment offset] + (.asSlice ^MemorySegment segment ^long offset)) + ([segment offset size] + (.asSlice ^MemorySegment segment ^long offset ^long size))) + +(defn slice-at + "Get a slice over the `segment` starting at the `address`." + ([segment address] + (.asSlice ^MemorySegment segment ^MemoryAddress address)) + ([segment address size] + (.asSlice ^MemorySegment segment ^MemoryAddress address ^long size))) + (comment ;;; Prospective syntax for ffi