diff options
author | vnugent <public@vaughnnugent.com> | 2024-03-31 16:43:15 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-03-31 16:43:15 -0400 |
commit | 9afed1427472da1ea13079f98dbe27339e55ee7d (patch) | |
tree | e5754db43604d99e9d4c39eed3c105bbb058d002 /lib/Utils/src | |
parent | 3ff90da4f02af47ea6d233fdd4445337ebe36452 (diff) |
perf: Deprecate unsafememoryhandle span extensions
Diffstat (limited to 'lib/Utils/src')
-rw-r--r-- | lib/Utils/src/Extensions/MemoryExtensions.cs | 4 | ||||
-rw-r--r-- | lib/Utils/src/Memory/IMemoryHandle.cs | 4 | ||||
-rw-r--r-- | lib/Utils/src/Memory/MemoryHandle.cs | 15 | ||||
-rw-r--r-- | lib/Utils/src/Memory/UnsafeMemoryHandle.cs | 26 |
4 files changed, 43 insertions, 6 deletions
diff --git a/lib/Utils/src/Extensions/MemoryExtensions.cs b/lib/Utils/src/Extensions/MemoryExtensions.cs index e81580b..65d90a0 100644 --- a/lib/Utils/src/Extensions/MemoryExtensions.cs +++ b/lib/Utils/src/Extensions/MemoryExtensions.cs @@ -270,7 +270,7 @@ namespace VNLib.Utils.Extensions /// <summary> /// Gets a reference to the element at the specified offset from the base - /// address of the <see cref="MemoryHandle{T}"/> + /// address of the <see cref="IMemoryHandle{T}"/> /// </summary> /// <param name="block"></param> /// <param name="offset">The element offset from the base address to add to the returned reference</param> @@ -833,6 +833,7 @@ namespace VNLib.Utils.Extensions /// <returns>The sub-sequence of the current handle</returns> /// <exception cref="ArgumentOutOfRangeException"></exception> [MethodImpl(MethodImplOptions.AggressiveInlining)] + [Obsolete("Functions are included directly on the type now")] public static Span<T> AsSpan<T>(this in UnsafeMemoryHandle<T> handle, int start) where T: unmanaged => handle.Span[start..]; /// <summary> @@ -845,6 +846,7 @@ namespace VNLib.Utils.Extensions /// <returns>The sub-sequence of the current handle</returns> /// <exception cref="ArgumentOutOfRangeException"></exception> [MethodImpl(MethodImplOptions.AggressiveInlining)] + [Obsolete("Functions are included directly on the type now")] public static Span<T> AsSpan<T>(this in UnsafeMemoryHandle<T> handle, int start, int count) where T : unmanaged => handle.Span.Slice(start, count); /// <summary> diff --git a/lib/Utils/src/Memory/IMemoryHandle.cs b/lib/Utils/src/Memory/IMemoryHandle.cs index f4e1a36..b538f87 100644 --- a/lib/Utils/src/Memory/IMemoryHandle.cs +++ b/lib/Utils/src/Memory/IMemoryHandle.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -46,7 +46,7 @@ namespace VNLib.Utils.Memory /// <summary> /// Gets a reference to the first element in the block /// </summary> - /// <returns>The reference</returns> + /// <returns>The reference to the first element in the block</returns> ref T GetReference(); } diff --git a/lib/Utils/src/Memory/MemoryHandle.cs b/lib/Utils/src/Memory/MemoryHandle.cs index c5cc295..16fc555 100644 --- a/lib/Utils/src/Memory/MemoryHandle.cs +++ b/lib/Utils/src/Memory/MemoryHandle.cs @@ -188,8 +188,7 @@ namespace VNLib.Utils.Memory this.ThrowIfClosed(); //Get ptr and offset it - T* bs = ((T*)handle) + elements; - return bs; + return ((T*)handle) + elements; } ///<inheritdoc/> @@ -199,6 +198,16 @@ namespace VNLib.Utils.Memory return ref MemoryUtil.GetRef<T>(handle); } + /// <summary> + /// Gets a reference to the element at the specified offset from the base + /// address of the <see cref="MemoryHandle{T}"/> + /// </summary> + /// <param name="offset">The element offset from the base address to add to the returned reference</param> + /// <returns>The reference to the item at the desired offset</returns> + /// <exception cref="ArgumentOutOfRangeException"></exception> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public unsafe ref T GetOffsetRef(nuint offset) => ref Unsafe.AsRef<T>(GetOffset(offset)); + ///<inheritdoc/> ///<exception cref="ObjectDisposedException"></exception> ///<exception cref="ArgumentOutOfRangeException"></exception> @@ -248,6 +257,6 @@ namespace VNLib.Utils.Memory public override bool Equals(object? obj) => obj is MemoryHandle<T> oHandle && Equals(oHandle); ///<inheritdoc/> - public override int GetHashCode() => base.GetHashCode(); + public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), handle.GetHashCode(), _length); } }
\ No newline at end of file diff --git a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs index 1ed995f..d93739d 100644 --- a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs +++ b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs @@ -202,6 +202,32 @@ namespace VNLib.Utils.Memory } } + /// <summary> + /// Returns a <see cref="Span{T}"/> that represents the memory block pointed to by this handle + /// </summary> + /// <returns>The memory block that is held by the internl handle</returns> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Span<T> AsSpan() => Span; + + /// <summary> + /// Returns a <see cref="Span{T}"/> that represents the memory block pointed to by this handle + /// </summary> + /// <param name="start">A starting element offset to return the span at</param> + /// <returns>The desired memory block at the desired element offset</returns> + /// <exception cref="ArgumentOutOfRangeException"></exception> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Span<T> AsSpan(int start) => Span[start..]; + + /// <summary> + /// Returns a <see cref="Span{T}"/> that represents the memory block pointed to by this handle + /// </summary> + /// <param name="start">The starting element offset</param> + /// <param name="length">The number of elements included in the returned span</param> + /// <returns>The desired memory block at the desired element offset and length</returns> + /// <exception cref="ArgumentOutOfRangeException"></exception>" + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Span<T> AsSpan(int start, int length) => Span.Slice(start, length); + ///<inheritdoc/> public readonly override int GetHashCode() { |