aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils/src
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-03-25 14:25:14 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2023-03-25 14:25:14 -0400
commit6b5ca9e49e33eb3e03d6f7333661da7e6d0546fa (patch)
tree8a389e9097c256b28cac8f268b93b2101d6a4cba /lib/Utils/src
parentd066a3d7ec26fd6919c514aeaccc4d6b086dc9c7 (diff)
Defer cors to host/middleware/user code
Diffstat (limited to 'lib/Utils/src')
-rw-r--r--lib/Utils/src/Memory/MemoryHandle.cs38
1 files changed, 30 insertions, 8 deletions
diff --git a/lib/Utils/src/Memory/MemoryHandle.cs b/lib/Utils/src/Memory/MemoryHandle.cs
index 7a6b4ef..7a7cb6a 100644
--- a/lib/Utils/src/Memory/MemoryHandle.cs
+++ b/lib/Utils/src/Memory/MemoryHandle.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2022 Vaughn Nugent
+* Copyright (c) 2023 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Utils
@@ -71,7 +71,6 @@ namespace VNLib.Utils.Memory
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
- this.ThrowIfClosed();
int len = Convert.ToInt32(_length);
return _length == 0 ? Span<T>.Empty : new Span<T>(Base, len);
}
@@ -116,6 +115,22 @@ namespace VNLib.Utils.Memory
handle = initial;
}
+ /*
+ * Empty handle will disable release, and because the
+ * handle pointer is 0, its considered invalid and will now
+ * allow
+ */
+ /// <summary>
+ /// Initialzies an empty memory handle. Properties will raise exceptions
+ /// when accessed, however <see cref="IMemoryHandle{T}"/> operations are
+ /// considered "safe" meaning they should never raise excpetions
+ /// </summary>
+ public MemoryHandle():base(false)
+ {
+ _length = 0;
+ Heap = null!;
+ }
+
/// <summary>
/// Resizes the current handle on the heap
/// </summary>
@@ -131,6 +146,12 @@ namespace VNLib.Utils.Memory
//Re-alloc (Zero if required)
try
{
+ /*
+ * If resize raises an exception the current block pointer
+ * should still be valid, if its not, the pointer should
+ * be set to 0/-1, which will be considered invalid anyway
+ */
+
Heap.Resize(ref handle, Length, (nuint)sizeof(T), ZeroMemory);
}
//Catch the disposed exception so we can invalidate the current ptr
@@ -143,6 +164,7 @@ namespace VNLib.Utils.Memory
throw;
}
}
+
/// <summary>
/// Gets an offset pointer from the base postion to the number of bytes specified. Performs bounds checks
/// </summary>
@@ -195,7 +217,7 @@ namespace VNLib.Utils.Memory
///<inheritdoc/>
protected override bool ReleaseHandle()
{
- //Return result of free
+ //Return result of free, only if the handle is valid
return Heap.Free(ref handle);
}
@@ -206,14 +228,14 @@ namespace VNLib.Utils.Memory
/// <returns>true if the block of memory is the same, false if the handle's size does not
/// match or the base addresses do not match even if they point to an overlapping address space</returns>
/// <exception cref="ObjectDisposedException"></exception>
- public bool Equals(MemoryHandle<T> other)
+ public bool Equals(MemoryHandle<T>? other)
{
- this.ThrowIfClosed();
- other.ThrowIfClosed();
- return _length == other._length && handle == other.handle;
+ return other != null && IsClosed == other.IsClosed && _length == other._length && handle == other.handle;
}
+
///<inheritdoc/>
- public override bool Equals(object obj) => obj is MemoryHandle<T> oHandle && Equals(oHandle);
+ public override bool Equals(object? obj) => obj is MemoryHandle<T> oHandle && Equals(oHandle);
+
///<inheritdoc/>
public override int GetHashCode() => base.GetHashCode();