From 4181ea5e77cbb71bb998742a236048e1682417f4 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 3 Sep 2024 19:02:59 +0200 Subject: [PATCH] [FSharp] Add pin, pinArr, and NativePtr.Address --- .../Utilities/Interop/FSLibExtensions.fs | 2 ++ src/Aardvark.Base.FSharp/Utilities/Native.fs | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/src/Aardvark.Base.FSharp/Utilities/Interop/FSLibExtensions.fs b/src/Aardvark.Base.FSharp/Utilities/Interop/FSLibExtensions.fs index 206b1425..cd4fc62c 100644 --- a/src/Aardvark.Base.FSharp/Utilities/Interop/FSLibExtensions.fs +++ b/src/Aardvark.Base.FSharp/Utilities/Interop/FSLibExtensions.fs @@ -214,6 +214,8 @@ module Prelude = let inline sndv (struct (_, y)) = y type nativeptr<'T when 'T : unmanaged> with + member inline ptr.Address = NativePtr.toNativeInt ptr + member ptr.Value with inline get () = NativePtr.read ptr and inline set (value : 'T) = NativePtr.write ptr value diff --git a/src/Aardvark.Base.FSharp/Utilities/Native.fs b/src/Aardvark.Base.FSharp/Utilities/Native.fs index 30a225e2..b85865d6 100644 --- a/src/Aardvark.Base.FSharp/Utilities/Native.fs +++ b/src/Aardvark.Base.FSharp/Utilities/Native.fs @@ -182,6 +182,14 @@ module NativeUtilities = finally gc.Free() + let inline pin (value: 'T) ([] f: nativeptr<'T> -> 'U) = + use ptr = fixed &value + f ptr + + let inline pinArr (value: 'T[]) ([] f: nativeptr<'T> -> 'U) = + use ptr = fixed value + f ptr + [] module MarshalDelegateExtensions = open System.Collections.Concurrent