Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/fsharp/FSharp.Core/nativeptr.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ module NativePtr =
[<CompiledName("StackAllocate")>]
let inline stackalloc (count:int) : nativeptr<'T> = (# "localloc" (count * sizeof<'T>) : nativeptr<'T> #)

[<NoDynamicInvocation>]
[<CompiledName("ToByRefInlined")>]
let inline toByRef (address: nativeptr<'T>) : byref<'T> = (# "" address : 'T byref #)
8 changes: 8 additions & 0 deletions src/fsharp/FSharp.Core/nativeptr.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ namespace Microsoft.FSharp.NativeInterop
[<NoDynamicInvocation>]
[<CompiledName("StackAllocate")>]
val inline stackalloc : count:int -> nativeptr<'T>

/// <summary>Converts a given typed native pointer to a managed pointer.</summary>
/// <param name="address">The input pointer.</param>
/// <returns>The managed pointer.</returns>
[<Unverifiable>]
[<NoDynamicInvocation>]
[<CompiledName("ToByRefInlined")>]
val inline toByRef : nativeptr<'T> -> byref<'T>
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ module M3 =
if not (NativeInterop.NativePtr.get data i = now) then
noerr <- false

let later = now.AddDays 1.
for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
datai <- later
for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
if not (datai = later) then
noerr <- false

(if noerr then 0 else 1) |> exit
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ module M6 =
if not (NativeInterop.NativePtr.get data i = (if (i % 2)=0 then E.A else E.B)) then
noerr <- false

for i = 0 to 9 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
datai <- (if (i % 2)=1 then E.A else E.B)

for i = 0 to 9 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
if not (datai = (if (i % 2)=1 then E.A else E.B)) then
noerr <- false

(if noerr then 0 else 1) |> exit
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,14 @@ module M1 =
if not (NativeInterop.NativePtr.get data i = (i*i)) then
noerr <- false

for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
datai <- 1-i

for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
if not (datai = 1-i) then
noerr <- false

(if noerr then 0 else 1) |> exit

Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@ module M2 =
if not (NativeInterop.NativePtr.get data i = (int64 (i*i))) then
noerr <- false

for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
datai <- int64 (1-i)

for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
if not (datai = int64 (1-i)) then
noerr <- false

(if noerr then 0 else 1) |> exit