diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Plugins.Essentials.ServiceStack/src/Construction/SsBuilderExtensions.cs | 1 | ||||
-rw-r--r-- | lib/Utils/src/Memory/MemoryUtil.cs | 32 | ||||
-rw-r--r-- | lib/Utils/src/Memory/UnsafeMemoryHandle.cs | 15 |
3 files changed, 24 insertions, 24 deletions
diff --git a/lib/Plugins.Essentials.ServiceStack/src/Construction/SsBuilderExtensions.cs b/lib/Plugins.Essentials.ServiceStack/src/Construction/SsBuilderExtensions.cs index 7b391a0..ee49f99 100644 --- a/lib/Plugins.Essentials.ServiceStack/src/Construction/SsBuilderExtensions.cs +++ b/lib/Plugins.Essentials.ServiceStack/src/Construction/SsBuilderExtensions.cs @@ -26,7 +26,6 @@ using System; using System.IO; using System.Net; using System.Linq; -using System.Threading.Tasks; using System.Collections.Generic; using System.Security.Cryptography.X509Certificates; diff --git a/lib/Utils/src/Memory/MemoryUtil.cs b/lib/Utils/src/Memory/MemoryUtil.cs index a1ad0c1..52a9528 100644 --- a/lib/Utils/src/Memory/MemoryUtil.cs +++ b/lib/Utils/src/Memory/MemoryUtil.cs @@ -350,17 +350,25 @@ namespace VNLib.Utils.Memory /// Zeroes a block of memory pointing to the structure /// </summary> /// <typeparam name="T">The structure type</typeparam> - /// <param name="structPtr">The pointer to the allocated structure</param> + /// <param name="structRef">The reference to the allocated structure</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void ZeroStruct<T>(void* structPtr) where T: unmanaged => InitializeBlock((T*)structPtr, Unsafe.SizeOf<T>()); + public static void ZeroStruct<T>(ref T structRef) where T : unmanaged => InitializeBlock(ref structRef, 1); /// <summary> /// Zeroes a block of memory pointing to the structure /// </summary> /// <typeparam name="T">The structure type</typeparam> - /// <param name="block">The pointer to the allocated structure</param> + /// <param name="structPtr">The pointer to the allocated structure</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void ZeroStruct<T>(IntPtr block) where T : unmanaged => ZeroStruct<T>(block.ToPointer()); + public static void ZeroStruct<T>(T* structPtr) where T : unmanaged + { + if (structPtr == null) + { + throw new ArgumentNullException(nameof(structPtr)); + } + + ZeroStruct(ref *structPtr); + } /// <summary> /// Zeroes a block of memory pointing to the structure @@ -368,24 +376,17 @@ namespace VNLib.Utils.Memory /// <typeparam name="T">The structure type</typeparam> /// <param name="structPtr">The pointer to the allocated structure</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void ZeroStruct<T>(T* structPtr) where T : unmanaged => ZeroStruct<T>((void*)structPtr); + public static void ZeroStruct<T>(void* structPtr) where T: unmanaged => ZeroStruct((T*)structPtr); /// <summary> /// Zeroes a block of memory pointing to the structure /// </summary> /// <typeparam name="T">The structure type</typeparam> - /// <param name="structRef">The reference to the allocated structure</param> + /// <param name="block">The pointer to the allocated structure</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void ZeroStruct<T>(ref T structRef) where T : unmanaged - { - if(Unsafe.IsNullRef(ref structRef)) - { - throw new ArgumentNullException(nameof(structRef)); - } + public static void ZeroStruct<T>(IntPtr block) where T : unmanaged => ZeroStruct<T>(block.ToPointer()); + - Unsafe.InitBlock(ref Unsafe.As<T, byte>(ref structRef), 0, (uint)sizeof(T)); - } - #endregion #region Copy @@ -526,7 +527,6 @@ namespace VNLib.Utils.Memory public static void CopyStruct<T>(ReadOnlySpan<byte> sourceData, void* target) where T: unmanaged => CopyStruct(sourceData, (T*)target); - /// <summary> /// Copies the memory of the structure pointed to by the source pointer to the target /// reference data sequence diff --git a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs index e4857d1..6a1fcc8 100644 --- a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs +++ b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs @@ -46,19 +46,20 @@ namespace VNLib.Utils.Memory Pool, PrivateHeap } - - private readonly T[]? _poolArr; + private readonly IntPtr _memoryPtr; + private readonly int _length; + private readonly HandleType _handleType; + + private readonly T[]? _poolArr; private readonly ArrayPool<T>? _pool; private readonly IUnmangedHeap? _heap; - private readonly HandleType _handleType; - private readonly int _length; ///<inheritdoc/> public readonly Span<T> Span { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => _handleType == HandleType.Pool ? _poolArr.AsSpan(0, IntLength) : MemoryUtil.GetSpan<T>(_memoryPtr, IntLength); + get => _handleType == HandleType.Pool ? _poolArr.AsSpan(0, _length) : MemoryUtil.GetSpan<T>(_memoryPtr, _length); } /// <summary> /// Gets the integer number of elements of the block of memory pointed to by this handle @@ -153,7 +154,7 @@ namespace VNLib.Utils.Memory ///<inheritdoc/> public readonly override int GetHashCode() => _handleType == HandleType.Pool ? _poolArr!.GetHashCode() : _memoryPtr.GetHashCode(); ///<inheritdoc/> - public readonly unsafe MemoryHandle Pin(int elementIndex) + public readonly MemoryHandle Pin(int elementIndex) { //guard empty handle if (_handleType == HandleType.None) @@ -162,7 +163,7 @@ namespace VNLib.Utils.Memory } //Guard size - if (elementIndex < 0 || elementIndex >= IntLength) + if (elementIndex < 0 || elementIndex >= _length) { throw new ArgumentOutOfRangeException(nameof(elementIndex)); } |