//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // file="UnmanagedArray" // Project: StgSharp // AuthorGroup: Nitload // Copyright (c) Nitload. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- 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); } } }