aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils/src
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-06-21 16:02:34 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-06-21 16:02:34 -0400
commit92e182ceaf843f8d859d38faa8b2c0ff53207ff6 (patch)
tree17711a2c87fc10c42d4f9d30cd6ee293fae24d0e /lib/Utils/src
parentee3620b8168a42c8e571e853c751ad5999a9b907 (diff)
feat: Multi transport listeners
Diffstat (limited to 'lib/Utils/src')
-rw-r--r--lib/Utils/src/Memory/MemoryHandle.cs8
-rw-r--r--lib/Utils/src/Memory/MemoryUtil.cs3
-rw-r--r--lib/Utils/src/Memory/MemoryUtilAlloc.cs10
-rw-r--r--lib/Utils/src/Memory/UnsafeMemoryHandle.cs45
4 files changed, 39 insertions, 27 deletions
diff --git a/lib/Utils/src/Memory/MemoryHandle.cs b/lib/Utils/src/Memory/MemoryHandle.cs
index 16fc555..fbaae95 100644
--- a/lib/Utils/src/Memory/MemoryHandle.cs
+++ b/lib/Utils/src/Memory/MemoryHandle.cs
@@ -229,8 +229,7 @@ namespace VNLib.Utils.Memory
//If adding ref failed, the handle is closed
ObjectDisposedException.ThrowIf(!addRef, this);
-
- //Create a new system.buffers memory handle from the offset ptr address
+
return new MemoryHandle(ptr, pinnable: this);
}
@@ -250,7 +249,10 @@ namespace VNLib.Utils.Memory
/// <exception cref="ObjectDisposedException"></exception>
public bool Equals(MemoryHandle<T>? other)
{
- return other != null && (IsClosed | other.IsClosed) == false && _length == other._length && handle == other.handle;
+ return other != null
+ && (IsClosed | other.IsClosed) == false
+ && _length == other._length
+ && handle == other.handle;
}
///<inheritdoc/>
diff --git a/lib/Utils/src/Memory/MemoryUtil.cs b/lib/Utils/src/Memory/MemoryUtil.cs
index d10efc8..34489a4 100644
--- a/lib/Utils/src/Memory/MemoryUtil.cs
+++ b/lib/Utils/src/Memory/MemoryUtil.cs
@@ -143,7 +143,8 @@ namespace VNLib.Utils.Memory
* get the heap's stats, otherwise return an empty handle
*/
return _lazyHeap.IsLoaded && _lazyHeap.Instance is TrackedHeapWrapper h
- ? h.GetCurrentStats() : default;
+ ? h.GetCurrentStats()
+ : default;
}
/// <summary>
diff --git a/lib/Utils/src/Memory/MemoryUtilAlloc.cs b/lib/Utils/src/Memory/MemoryUtilAlloc.cs
index 6e4f9b0..742346c 100644
--- a/lib/Utils/src/Memory/MemoryUtilAlloc.cs
+++ b/lib/Utils/src/Memory/MemoryUtilAlloc.cs
@@ -34,7 +34,7 @@ namespace VNLib.Utils.Memory
#region alloc
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static bool UseUnmanagedHeap<T>(IUnmangedHeap heap, nuint elements)
+ private static bool CanUseUnmanagedHeap<T>(IUnmangedHeap heap, nuint elements)
{
/*
* We may allocate from the share heap only if the heap is not using locks
@@ -123,7 +123,7 @@ namespace VNLib.Utils.Memory
return default;
}
- return UseUnmanagedHeap<T>(Shared, (uint)elements)
+ return CanUseUnmanagedHeap<T>(Shared, (uint)elements)
? UnsafeAlloc<T>(Shared, elements, zero)
: UnsafeAlloc(ArrayPool<T>.Shared, elements, zero);
}
@@ -229,7 +229,7 @@ namespace VNLib.Utils.Memory
{
ArgumentOutOfRangeException.ThrowIfNegative(elements);
- if (UseUnmanagedHeap<T>(Shared, elements))
+ if (CanUseUnmanagedHeap<T>(Shared, elements))
{
return SafeAlloc<T>(Shared, elements, zero);
}
@@ -425,7 +425,7 @@ namespace VNLib.Utils.Memory
return default;
}
- return UseUnmanagedHeap<byte>(Shared, (uint)elements)
+ return CanUseUnmanagedHeap<byte>(Shared, (uint)elements)
? UnsafeAlloc<byte>(Shared, elements, zero)
: UnsafeAlloc(ArrayPool<byte>.Shared, elements, zero);
}
@@ -462,7 +462,7 @@ namespace VNLib.Utils.Memory
{
ArgumentOutOfRangeException.ThrowIfNegative(elements);
- return UseUnmanagedHeap<byte>(Shared, (uint)elements)
+ return CanUseUnmanagedHeap<byte>(Shared, (uint)elements)
? SafeAlloc<byte>(Shared, (nuint)elements, zero)
: SafeAlloc(ArrayPool<byte>.Shared, elements, zero);
}
diff --git a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs
index d93739d..bda8e2e 100644
--- a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs
+++ b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs
@@ -29,8 +29,6 @@ using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
-using VNLib.Utils.Extensions;
-
namespace VNLib.Utils.Memory
{
@@ -62,16 +60,7 @@ namespace VNLib.Utils.Memory
public readonly Span<T> Span
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return _handleType switch
- {
- HandleType.None => Span<T>.Empty,
- HandleType.Pool => _poolArr!.AsSpan(0, _length),
- HandleType.PrivateHeap => MemoryUtil.GetSpan<T>(_memoryPtr, _length),
- _ => throw new InvalidOperationException("Invalid handle type"),
- };
- }
+ get => AsSpan();
}
/// <summary>
@@ -153,7 +142,7 @@ namespace VNLib.Utils.Memory
IntPtr unalloc = _memoryPtr;
//Free the unmanaged handle
bool unsafeFreed = _heap!.Free(ref unalloc);
- Debug.Assert(unsafeFreed, "A previously allocated unsafe memhandle failed to free");
+ Debug.Assert(unsafeFreed, "A previously allocated unsafe memhandle failed to free block");
}
break;
}
@@ -193,6 +182,8 @@ namespace VNLib.Utils.Memory
{
switch (_handleType)
{
+ case HandleType.None:
+ return ref Unsafe.NullRef<T>();
case HandleType.Pool:
return ref MemoryMarshal.GetArrayDataReference(_poolArr!);
case HandleType.PrivateHeap:
@@ -207,7 +198,7 @@ namespace VNLib.Utils.Memory
/// </summary>
/// <returns>The memory block that is held by the internl handle</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly Span<T> AsSpan() => Span;
+ public readonly Span<T> AsSpan() => AsSpan(0, _length);
/// <summary>
/// Returns a <see cref="Span{T}"/> that represents the memory block pointed to by this handle
@@ -216,7 +207,7 @@ namespace VNLib.Utils.Memory
/// <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..];
+ public readonly Span<T> AsSpan(int start) => AsSpan(start, _length - start);
/// <summary>
/// Returns a <see cref="Span{T}"/> that represents the memory block pointed to by this handle
@@ -226,7 +217,23 @@ namespace VNLib.Utils.Memory
/// <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);
+ public readonly Span<T> AsSpan(int start, int length)
+ {
+ ArgumentOutOfRangeException.ThrowIfNegative(start);
+ ArgumentOutOfRangeException.ThrowIfNegative(length);
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(length - start, _length);
+
+ /*
+ * If the handle is empty, a null ref should be returned. The
+ * check above will gaurd against calling this function on non-empty
+ * handles. So adding 0 to 0 on the reference should not cause any issues.
+ */
+
+ return MemoryMarshal.CreateSpan(
+ ref Unsafe.Add(ref GetReference(), start),
+ length
+ );
+ }
///<inheritdoc/>
public readonly override int GetHashCode()
@@ -248,7 +255,9 @@ namespace VNLib.Utils.Memory
/// <returns>True if the other handle points to the same block of memory as the current handle</returns>
public readonly bool Equals(in UnsafeMemoryHandle<T> other)
{
- return _handleType == other._handleType && Length == other.Length && GetHashCode() == other.GetHashCode();
+ return _handleType == other._handleType
+ && Length == other.Length
+ && GetHashCode() == other.GetHashCode();
}
/// <summary>
@@ -288,4 +297,4 @@ namespace VNLib.Utils.Memory
public static bool operator !=(in UnsafeMemoryHandle<T> left, in UnsafeMemoryHandle<T> right) => !left.Equals(right);
}
-} \ No newline at end of file
+}