diff --git a/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs b/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs new file mode 100644 index 00000000..48f31b83 --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/AllocationPtr.cs @@ -0,0 +1,96 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using TerraFX.Interop; + +namespace ComputeSharp.Graphics.Interop +{ + /// + /// A -equivalent type to safely work with pointers. + /// + /// + /// While this type is not marked as so that it can also be used in fields, make sure to + /// keep the reference counts properly tracked if you do store instances on the heap. + /// + internal unsafe struct AllocationPtr : IDisposable + { + /// + /// The raw pointer to an object, if existing. + /// + private Allocation* pointer; + + /// + /// Creates a new instance from a raw pointer. + /// + /// The raw pointer to wrap. + public AllocationPtr(Allocation* other) + { + pointer = other; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Dispose() + { + Allocation* pointer = this.pointer; + + if (pointer != null) + { + this.pointer = null; + + pointer->Release(); + } + } + + /// + /// Gets the currently wrapped raw pointer to an object. + /// + /// The raw pointer wrapped by the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Allocation* Get() + { + return this.pointer; + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// This method is only valid when the current instance is on the stack or pinned. + /// + /// The raw pointer to the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Allocation** GetAddressOf() + { + return (Allocation**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// + /// The raw pointer to the input instance. + /// This method is only valid when the current instance is on the stack or pinned. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly void** GetVoidAddressOf() + where T : unmanaged + { + return (void**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// + /// The raw pointer to the input instance. + /// This method is only valid when the current instance is on the stack or pinned. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public AllocationPtr Move() + { + AllocationPtr copy = this; + + this = default; + + return copy; + } + } +} diff --git a/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs b/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs new file mode 100644 index 00000000..97b75867 --- /dev/null +++ b/src/ComputeSharp/Graphics/Interop/AllocatorPtr.cs @@ -0,0 +1,96 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using TerraFX.Interop; + +namespace ComputeSharp.Graphics.Interop +{ + /// + /// A -equivalent type to safely work with pointers. + /// + /// + /// While this type is not marked as so that it can also be used in fields, make sure to + /// keep the reference counts properly tracked if you do store instances on the heap. + /// + internal unsafe struct AllocatorPtr : IDisposable + { + /// + /// The raw pointer to an object, if existing. + /// + private Allocator* pointer; + + /// + /// Creates a new instance from a raw pointer. + /// + /// The raw pointer to wrap. + public AllocatorPtr(Allocator* other) + { + pointer = other; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Dispose() + { + Allocator* pointer = this.pointer; + + if (pointer != null) + { + this.pointer = null; + + pointer->Release(); + } + } + + /// + /// Gets the currently wrapped raw pointer to an object. + /// + /// The raw pointer wrapped by the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Allocator* Get() + { + return this.pointer; + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// This method is only valid when the current instance is on the stack or pinned. + /// + /// The raw pointer to the current instance. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Allocator** GetAddressOf() + { + return (Allocator**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// + /// The raw pointer to the input instance. + /// This method is only valid when the current instance is on the stack or pinned. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly void** GetVoidAddressOf() + where T : unmanaged + { + return (void**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + /// + /// Gets the address of the current instance as a raw double pointer. + /// + /// The raw pointer to the input instance. + /// This method is only valid when the current instance is on the stack or pinned. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public AllocatorPtr Move() + { + AllocatorPtr copy = this; + + this = default; + + return copy; + } + } +}