Skip to content

Commit

Permalink
[FSharp] Add pin, pinArr, and NativePtr.Address
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Sep 3, 2024
1 parent bc07532 commit 5d1e589
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Aardvark.Base.FSharp/Utilities/Interop/FSLibExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/Aardvark.Base.FSharp/Utilities/Native.fs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ module NativeUtilities =
finally
gc.Free()

let inline pin ([<InlineIfLambda>] f: nativeptr<'T> -> 'U) (value: 'T) =
use ptr = fixed &value
f ptr

let inline pinArr ([<InlineIfLambda>] f: nativeptr<'T> -> 'U) (array: 'T[]) =
use ptr = fixed array
f ptr

[<AutoOpen>]
module MarshalDelegateExtensions =
open System.Collections.Concurrent
Expand Down

0 comments on commit 5d1e589

Please sign in to comment.