// ----------------------------------------------------------------------------- // file="UnmanagedArray" // Project: StgSharp // Copyright (c) Nitload. // SPDX-License-Identifier: MIT // ----------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StgSharp.Collections { /// /// Zero-overhead view over a contiguous region of unmanaged memory. Does not own the memory — /// caller is responsible for lifetime management. /// /// /// Element type. Must be unmanaged. /// public readonly unsafe struct UnmanagedArray where T : unmanaged { private static readonly nint ElementSize = sizeof(T); [MethodImpl(MethodImplOptions.AggressiveInlining)] public UnmanagedArray( T* pointer, int length ) { Address = pointer; Length = length; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public UnmanagedArray( nuint address, int length ) { Address = (T*)address; Length = length; } /// /// Indexed access. No bounds check in Release builds. /// public ref T this[ int index ] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { Debug.Assert((uint)index < (uint)Length, "UnmanagedArray index out of range"); return ref Address[index]; } } /// /// Raw pointer to the first element. /// public T* Address { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; init; } /// /// Element count. /// public int Length { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; init; } /// /// Total byte size of the array (for allocation calculations). /// public int ByteSize => Length * sizeof(T); /// /// Wraps the array as a . /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Span AsSpan() { return new Span(Address, Length); } /// /// Wraps a sub-range as a . /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Span AsSpan( int start, int length ) { Debug.Assert(((uint)start) + ((uint)length) <= (uint)Length, "UnmanagedArray.AsSpan range out of bounds"); return new Span(Address + start, length); } /// /// Zero-fill all elements. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Clear() { Unsafe.InitBlock(Address, 0, (uint)(Length * sizeof(T))); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int IndexOf( T* ptr ) { int offset = (int)((((nint)ptr) - ((nint)Address)) / ElementSize); ArgumentOutOfRangeException.ThrowIfNegative(offset, nameof(ptr)); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(offset, Length, nameof(ptr)); return offset; } /// /// Returns a pointer to element at . Useful for feeding SIMD /// load instructions (e.g. Sse2.LoadVector128). /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public T* PtrAt( int index ) { Debug.Assert((uint)index < (uint)Length, "UnmanagedArray.PtrAt index out of range"); return Address + index; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int UncheckedIndexOf( T* ptr ) { return (int)((((nint)ptr) - ((nint)Address)) / ElementSize); } } }