aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils/src
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-11-03 01:51:25 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2023-11-03 01:51:25 -0400
commit4e13804bb9e0480bbf2c0753de994482246cada8 (patch)
tree47fecba668630296a9409e3ec02816391550b843 /lib/Utils/src
parent59599b0f3e57d1881639d482bf1758c7f93d0dc2 (diff)
patch zero overrun
Diffstat (limited to 'lib/Utils/src')
-rw-r--r--lib/Utils/src/Memory/MemoryUtil.cs32
-rw-r--r--lib/Utils/src/Memory/UnsafeMemoryHandle.cs15
2 files changed, 24 insertions, 23 deletions
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));
}