aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-02-29 21:23:26 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2024-02-29 21:23:26 -0500
commitb679ddd4e647ac915febd0d5a5e488a1e8e48842 (patch)
treecf414be9a53342e8d59194198cde5bf3c2187fc1 /lib/Utils
parent2b1314c1475e7e1831c691cf349cb89c66fa320c (diff)
Squashed commit of the following:
commit 231e26e5c6731e6e156d7c0591518e84a3b82f5a Author: vnugent <public@vaughnnugent.com> Date: Thu Feb 29 20:59:42 2024 -0500 fix: #5 find and patch Windows compression bug and some deployment commit d0bfe14e0a0e27172b8dd41f468265e651784837 Author: vnugent <public@vaughnnugent.com> Date: Wed Feb 21 21:39:19 2024 -0500 fix: #4 fix readme licensing message for accuracy commit 6f37f152fcd105e40af6689192a36b87eda95f51 Author: vnugent <public@vaughnnugent.com> Date: Wed Feb 21 21:37:55 2024 -0500 fix: jwt hashalg sizing and public api for hashalg sizes
Diffstat (limited to 'lib/Utils')
-rw-r--r--lib/Utils/src/IO/VnMemoryStream.cs18
-rw-r--r--lib/Utils/src/Memory/Caching/IReusable.cs4
-rw-r--r--lib/Utils/src/Memory/MemoryUtil.CopyUtilCore.cs7
-rw-r--r--lib/Utils/src/Memory/MemoryUtil.cs38
-rw-r--r--lib/Utils/src/Memory/ProcessHeap.cs14
-rw-r--r--lib/Utils/src/Native/SafeLibraryHandle.cs20
6 files changed, 57 insertions, 44 deletions
diff --git a/lib/Utils/src/IO/VnMemoryStream.cs b/lib/Utils/src/IO/VnMemoryStream.cs
index ed8ed5a..ada2c64 100644
--- a/lib/Utils/src/IO/VnMemoryStream.cs
+++ b/lib/Utils/src/IO/VnMemoryStream.cs
@@ -88,6 +88,7 @@ namespace VNLib.Utils.IO
/// <returns>The readonly stream</returns>
public static VnMemoryStream CreateReadonly(VnMemoryStream stream)
{
+ ArgumentNullException.ThrowIfNull(stream);
//Set the readonly flag
stream._isReadonly = true;
//Return the stream
@@ -528,10 +529,19 @@ namespace VNLib.Utils.IO
/// <exception cref="OutOfMemoryException"></exception>
public byte[] ToArray()
{
- //Alloc a new array of the size of the internal buffer, may be 64 bit large block
- byte[] data = new byte[_length];
-
- //Copy the internal buffer to the new array
+ byte[] data;
+
+ if (_length < Int32.MaxValue)
+ {
+ //Alloc uninialized, since were going to overwite it anyway
+ data = GC.AllocateUninitializedArray<byte>((int)_length, false);
+ }
+ else
+ {
+ //Use new opperator if larger than 32bit
+ data = new byte[_length];
+ }
+
MemoryUtil.CopyArray(_buffer, 0, data, 0, (nuint)_length);
return data;
diff --git a/lib/Utils/src/Memory/Caching/IReusable.cs b/lib/Utils/src/Memory/Caching/IReusable.cs
index 4472ad3..cf73fa0 100644
--- a/lib/Utils/src/Memory/Caching/IReusable.cs
+++ b/lib/Utils/src/Memory/Caching/IReusable.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Utils
@@ -25,7 +25,7 @@
namespace VNLib.Utils.Memory.Caching
{
/// <summary>
- /// Allows for use within a <see cref="ObjectRental{T}"/>, this object is intended to be reused heavily
+ /// An interface that exposes event hooks for use within an <see cref="ObjectRental{T}"/>, managment.
/// </summary>
public interface IReusable
{
diff --git a/lib/Utils/src/Memory/MemoryUtil.CopyUtilCore.cs b/lib/Utils/src/Memory/MemoryUtil.CopyUtilCore.cs
index bcc5be9..f196597 100644
--- a/lib/Utils/src/Memory/MemoryUtil.CopyUtilCore.cs
+++ b/lib/Utils/src/Memory/MemoryUtil.CopyUtilCore.cs
@@ -74,7 +74,7 @@ namespace VNLib.Utils.Memory
public static bool RequiresPinning(nuint byteSize, bool forceAcceleration)
{
/*
- * Pinnin is required, if reflected memmove is not supported on the platform
+ * Pinning is required, if reflected memmove is not supported on the platform
* AND the size of the data to copy is larger than 32 bit width.
*
* Otherwise if accleration is forced, pinning will always be required.
@@ -94,9 +94,9 @@ namespace VNLib.Utils.Memory
}
/*
- * Why does this function exist. For centralized memmove operations primarily.
+ * Why does this function exist? For centralized memmove operations primarily.
*
- * When the block is known to be small, all of the brances in memmove can be
+ * When the block is known to be small, all of the branches in memmove can be
* alot of overhead including the possability of Avx2 being used for really
* small blocks if they are aligned. If the block is known to be small, we
* can just skip all of that and use the fastest method for small blocks,
@@ -118,7 +118,6 @@ namespace VNLib.Utils.Memory
Debug.Assert(!Unsafe.IsNullRef(in dstByte), "Null destination reference passed to MemmoveByRef");
_fallbackMemmove.Memmove(in srcByte, ref dstByte, byteCount);
- return;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
diff --git a/lib/Utils/src/Memory/MemoryUtil.cs b/lib/Utils/src/Memory/MemoryUtil.cs
index 774aca3..ba25104 100644
--- a/lib/Utils/src/Memory/MemoryUtil.cs
+++ b/lib/Utils/src/Memory/MemoryUtil.cs
@@ -32,7 +32,6 @@ using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics.X86;
-using VNLib.Utils.Resources;
using VNLib.Utils.Memory.Diagnostics;
namespace VNLib.Utils.Memory
@@ -86,6 +85,11 @@ namespace VNLib.Utils.Memory
/// that will use the array pool before falling back to the <see cref="Shared"/>.
/// heap.
/// </summary>
+ /// <remarks>
+ /// This value is chosen to be just under the size the CLR will promote an array to the
+ /// LOH, we can assume any heap impl will have long-term performance than the LOH for
+ /// large allocations.
+ /// </remarks>
public const int MAX_UNSAFE_POOL_SIZE = 80 * 1024;
//Cache the system page size
@@ -314,7 +318,11 @@ namespace VNLib.Utils.Memory
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void InitializeBlock<T>(T[] array) where T : struct => InitializeBlock(array, (uint)array.Length);
+ public static void InitializeBlock<T>(T[] array) where T : struct
+ {
+ ArgumentNullException.ThrowIfNull(array);
+ InitializeBlock(array, (uint)array.Length);
+ }
/// <summary>
/// Initializes the array with zeros up to the specified count
@@ -327,7 +335,7 @@ namespace VNLib.Utils.Memory
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InitializeBlock<T>(T[] array, uint count) where T: struct
{
- ArgumentNullException.ThrowIfNull(array, nameof(array));
+ ArgumentNullException.ThrowIfNull(array);
//Check bounds
CheckBounds(array, 0, count);
@@ -348,7 +356,7 @@ namespace VNLib.Utils.Memory
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void InitializeBlock<T>(ref T block, int itemCount) where T : struct
{
- ThrowIfNullRef(ref block, nameof(block));
+ ThrowIfNullRef(in block, nameof(block));
if (itemCount <= 0)
{
@@ -406,8 +414,7 @@ namespace VNLib.Utils.Memory
public static void ZeroStruct<T>(T* structPtr) where T : unmanaged
{
ArgumentNullException.ThrowIfNull(structPtr);
-
- ZeroStruct(ref *structPtr);
+ ZeroStruct(ref Unsafe.AsRef<T>(structPtr));
}
/// <summary>
@@ -431,14 +438,6 @@ namespace VNLib.Utils.Memory
#region Copy
- /*
- * Dirty little trick to access internal Buffer.Memmove method for
- * large references. May not always be supported, so optional safe
- * guards are in place.
- */
- private delegate void BigMemmove(ref byte dest, ref readonly byte src, nuint len);
- private static readonly BigMemmove? _clrMemmove = ManagedLibrary.TryGetStaticMethod<BigMemmove>(typeof(Buffer), "Memmove", System.Reflection.BindingFlags.NonPublic);
-
/// <summary>
/// Copies structure data from a source byte reference that points to a sequence of
/// of data to the target structure reference.
@@ -626,8 +625,11 @@ namespace VNLib.Utils.Memory
{
ArgumentNullException.ThrowIfNull(source);
ArgumentOutOfRangeException.ThrowIfLessThan(target.Length, sizeof(T), nameof(target));
-
- CopyStruct(ref *source, target);
+
+ CopyStruct(
+ ref Unsafe.AsRef<T>(source),
+ target
+ );
}
/// <summary>
@@ -677,7 +679,7 @@ namespace VNLib.Utils.Memory
Unsafe.CopyBlockUnaligned(
ref Refs.AsByte(ref target, 0),
- ref Refs.AsByteR(in source, 0),
+ in Refs.AsByteR(in source, 0),
(uint)sizeof(T)
);
}
@@ -1354,7 +1356,7 @@ namespace VNLib.Utils.Memory
/// <param name="size">The size of the span (the size of the block)</param>
/// <returns>A span over the block of memory pointed to by the handle of the specified size</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Span<T> GetSpan<T>(ref MemoryHandle handle, int size) => new(handle.Pointer, size);
+ public static Span<T> GetSpan<T>(ref readonly MemoryHandle handle, int size) => new(handle.Pointer, size);
/// <summary>
/// Gets a <see cref="Span{T}"/> over the block of memory pointed to by the supplied handle.
diff --git a/lib/Utils/src/Memory/ProcessHeap.cs b/lib/Utils/src/Memory/ProcessHeap.cs
index 35a2a71..f13181b 100644
--- a/lib/Utils/src/Memory/ProcessHeap.cs
+++ b/lib/Utils/src/Memory/ProcessHeap.cs
@@ -54,10 +54,7 @@ namespace VNLib.Utils.Memory
/// <summary>
/// Initalizes a new global (cross platform) process heap
/// </summary>
- public ProcessHeap()
- {
- Trace.WriteLine($"Default heap instnace created {GetHashCode():x}");
- }
+ public ProcessHeap() => Trace.WriteLine($"Default heap instnace created {GetHashCode():x}");
///<inheritdoc/>
///<exception cref="OverflowException"></exception>
@@ -78,13 +75,10 @@ namespace VNLib.Utils.Memory
return true;
}
-
+
///<inheritdoc/>
- protected override void Free()
- {
- Trace.WriteLine($"Default heap instnace disposed {GetHashCode():x}");
- }
-
+ protected override void Free() => Trace.WriteLine($"Default heap instnace disposed {GetHashCode():x}");
+
///<inheritdoc/>
///<exception cref="OverflowException"></exception>
///<exception cref="OutOfMemoryException"></exception>
diff --git a/lib/Utils/src/Native/SafeLibraryHandle.cs b/lib/Utils/src/Native/SafeLibraryHandle.cs
index 6b1af00..ed9dd16 100644
--- a/lib/Utils/src/Native/SafeLibraryHandle.cs
+++ b/lib/Utils/src/Native/SafeLibraryHandle.cs
@@ -150,11 +150,19 @@ namespace VNLib.Utils.Native
private static string? GetLibraryFile(string dirPath, string libPath, SearchOption search)
{
- //slice the lib to its file name
- libPath = Path.GetFileName(libPath);
- libPath = Path.ChangeExtension(libPath, OperatingSystem.IsWindows() ? ".dll" : ".so");
- //Select the first file that matches the name
- return Directory.EnumerateFiles(dirPath, libPath, search).FirstOrDefault();
+ //If the library path already has an extension, just search for the file
+ if (Path.HasExtension(libPath))
+ {
+ return Directory.EnumerateFiles(dirPath, libPath, search).FirstOrDefault();
+ }
+ else
+ {
+ //slice the lib to its file name
+ libPath = Path.GetFileName(libPath);
+ libPath = Path.ChangeExtension(libPath, OperatingSystem.IsWindows() ? ".dll" : ".so");
+ //Select the first file that matches the name
+ return Directory.EnumerateFiles(dirPath, libPath, search).FirstOrDefault();
+ }
}
/// <summary>
@@ -173,7 +181,7 @@ namespace VNLib.Utils.Native
bool success = false;
DangerousAddRef(ref success);
- ObjectDisposedException.ThrowIf(success == false, "The libary has been released!");
+ ObjectDisposedException.ThrowIf(success == false, this);
try
{