forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnativeptr.fs
More file actions
51 lines (37 loc) · 1.91 KB
/
Copy pathnativeptr.fs
File metadata and controls
51 lines (37 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.FSharp.NativeInterop
open Microsoft.FSharp.Core
open Microsoft.FSharp.Collections
open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
open Microsoft.FSharp.Primitives.Basics
open Microsoft.FSharp.Core.Operators
open System
open System.Diagnostics
open System.Runtime.InteropServices
[<RequireQualifiedAccess>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module NativePtr =
[<NoDynamicInvocation>]
[<CompiledName("OfNativeIntInlined")>]
let inline ofNativeInt (x:nativeint) = (# "" x : nativeptr<'T> #)
[<NoDynamicInvocation>]
[<CompiledName("ToNativeIntInlined")>]
let inline toNativeInt (x: nativeptr<'T>) = (# "" x : nativeint #)
[<NoDynamicInvocation>]
[<CompiledName("AddPointerInlined")>]
let inline add (x : nativeptr<'T>) (n:int) : nativeptr<'T> = toNativeInt x + nativeint n * (# "sizeof !0" type('T) : nativeint #) |> ofNativeInt
[<NoDynamicInvocation>]
[<CompiledName("GetPointerInlined")>]
let inline get (p : nativeptr<'T>) n = (# "ldobj !0" type ('T) (add p n) : 'T #)
[<NoDynamicInvocation>]
[<CompiledName("SetPointerInlined")>]
let inline set (p : nativeptr<'T>) n (x : 'T) = (# "stobj !0" type ('T) (add p n) x #)
[<NoDynamicInvocation>]
[<CompiledName("ReadPointerInlined")>]
let inline read (p : nativeptr<'T>) = (# "ldobj !0" type ('T) p : 'T #)
[<NoDynamicInvocation>]
[<CompiledName("WritePointerInlined")>]
let inline write (p : nativeptr<'T>) (x : 'T) = (# "stobj !0" type ('T) p x #)
[<NoDynamicInvocation>]
[<CompiledName("StackAllocate")>]
let inline stackalloc (count:int) : nativeptr<'T> = (# "localloc" (count * sizeof<'T>) : nativeptr<'T> #)