I suggest we ...

Additional intrinsics for the NativePtr module

When interoperating with native code, it would be handy if the NativePtr module included some additional "intrinsic" functions for taking advantage of low-level IL instructions; specifically, I'd like to be able to use 'cpblk', 'initblk', 'initobj', and 'copyobj'.

It would also be nice to have an easy way of checking for null pointer values.

Example implementation of these functions:

[<RequireQualifiedAccess>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module NativePtr =
[<GeneralizableValue>]
[<NoDynamicInvocation>]
[<CompiledName("Zero")>]
let inline zero<'T when 'T : unmanaged> : nativeptr<'T> =
(# "ldnull" : nativeptr<'T> #)

[<NoDynamicInvocation>]
[<CompiledName("IsNull")>]
let inline isNull<'T when 'T : unmanaged> (ptr : nativeptr<'T>) =
(# "ceq" zero<'T> ptr : bool #)

[<Unverifiable>]
[<NoDynamicInvocation>]
[<CompiledName("InitBlockInlined")>]
let inline initBlock (p : nativeptr<'T>) (value : byte) (size : uint32) =
(# "initblk" p value size #)

[<Unverifiable>]
[<NoDynamicInvocation>]
[<CompiledName("CopyBlockInlined")>]
let inline memcpy (destPtr : nativeptr<'T>) (srcPtr : nativeptr<'T>) (count : int) =
(# "cpblk" destPtr srcPtr (count * sizeof<'T>) #)

[<Unverifiable>]
[<NoDynamicInvocation>]
[<CompiledName("InitPointerInlined")>]
let inline clear (p : nativeptr<'T>) =
(# "initobj !0" type ('T) p #)

[<Unverifiable>]
[<NoDynamicInvocation>]
[<CompiledName("CopyPointerInlined")>]
let inline copy (destPtr : nativeptr<'T>) (srcPtr : nativeptr<'T>) =
(# "copyobj !0" type ('T) destPtr srcPtr #)

10 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Jack Pappas shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

8 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...

F# Language

Feedback and Knowledge Base