aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-06-18 21:17:28 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-06-18 21:17:28 -0400
commitee3620b8168a42c8e571e853c751ad5999a9b907 (patch)
tree4134b47ace8b5b46ff4909ab198fcd6f50c3bd18 /lib/Utils
parentff0926be56fc6eafdce36411847d73bf4ce9f183 (diff)
feat: Add file path caching support
Diffstat (limited to 'lib/Utils')
-rw-r--r--lib/Utils/src/Extensions/MemoryExtensions.cs17
-rw-r--r--lib/Utils/src/IO/VnMemoryStream.cs32
2 files changed, 42 insertions, 7 deletions
diff --git a/lib/Utils/src/Extensions/MemoryExtensions.cs b/lib/Utils/src/Extensions/MemoryExtensions.cs
index 99d4cf1..f2399a2 100644
--- a/lib/Utils/src/Extensions/MemoryExtensions.cs
+++ b/lib/Utils/src/Extensions/MemoryExtensions.cs
@@ -268,6 +268,23 @@ namespace VNLib.Utils.Extensions
}
/// <summary>
+ /// Gets a reference to the element at the specified offset from the base
+ /// address of the <see cref="MemoryHandle{T}"/> and casts it to a byte reference
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="block"></param>
+ /// <param name="offset">The number of elements to offset the base reference by</param>
+ /// <returns>The reinterpreted byte reference at the first byte of the element offset</returns>
+ /// <exception cref="ArgumentNullException"></exception>
+ /// <exception cref="ArgumentOutOfRangeException"></exception>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ref byte GetOffsetByteRef<T>(this IMemoryHandle<T> block, nint offset)
+ {
+ ArgumentOutOfRangeException.ThrowIfNegative(offset);
+ return ref GetOffsetByteRef(block, (nuint)offset);
+ }
+
+ /// <summary>
/// Gets a 64bit friendly span offset for the current <see cref="MemoryHandle{T}"/>
/// </summary>
/// <typeparam name="T"></typeparam>
diff --git a/lib/Utils/src/IO/VnMemoryStream.cs b/lib/Utils/src/IO/VnMemoryStream.cs
index 23df869..2c604b2 100644
--- a/lib/Utils/src/IO/VnMemoryStream.cs
+++ b/lib/Utils/src/IO/VnMemoryStream.cs
@@ -87,8 +87,12 @@ namespace VNLib.Utils.IO
/// <summary>
/// Converts a writable <see cref="VnMemoryStream"/> to readonly to allow shallow copies
/// </summary>
+ /// <remarks>
+ /// This funciton will convert the stream passed into it to a readonly stream.
+ /// The function passes through the input stream as the return value
+ /// </remarks>
/// <param name="stream">The stream to make readonly</param>
- /// <returns>The readonly stream</returns>
+ /// <returns>A reference to the modified input stream</returns>
public static VnMemoryStream CreateReadonly(VnMemoryStream stream)
{
ArgumentNullException.ThrowIfNull(stream);
@@ -103,7 +107,7 @@ namespace VNLib.Utils.IO
/// global heap instance.
/// </summary>
public VnMemoryStream() : this(MemoryUtil.Shared) { }
-
+
/// <summary>
/// Create a new memory stream where buffers will be allocated from the specified heap
/// </summary>
@@ -111,7 +115,13 @@ namespace VNLib.Utils.IO
/// <exception cref="OutOfMemoryException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public VnMemoryStream(IUnmangedHeap heap) : this(heap, DefaultBufferSize, false) { }
-
+
+ /// <summary>
+ /// Creates a new memory stream using the <see cref="MemoryUtil.Shared"/>
+ /// global heap instance.
+ /// </summary>
+ public VnMemoryStream(nuint bufferSize, bool zero) : this(MemoryUtil.Shared, bufferSize, zero) { }
+
/// <summary>
/// Creates a new memory stream and pre-allocates the internal
/// buffer of the specified size on the specified heap to avoid resizing.
@@ -356,7 +366,7 @@ namespace VNLib.Utils.IO
}
///<inheritdoc/>
- public override unsafe int ReadByte()
+ public override int ReadByte()
{
if (LenToPosDiff == 0)
{
@@ -364,7 +374,7 @@ namespace VNLib.Utils.IO
}
//get the value at the current position
- ref byte ptr = ref _buffer.GetOffsetByteRef((nuint)_position);
+ ref byte ptr = ref _buffer.GetOffsetByteRef(_position);
//Increment position
_position++;
@@ -488,7 +498,7 @@ namespace VNLib.Utils.IO
throw new NotSupportedException("Write operation is not allowed on readonly stream!");
}
//Calculate the new final position
- nint newPos = (_position + buffer.Length);
+ nint newPos = checked(_position + buffer.Length);
//Determine if the buffer needs to be expanded
if (buffer.Length > LenToPosDiff)
{
@@ -497,8 +507,16 @@ namespace VNLib.Utils.IO
//Update length
_length = newPos;
}
+
//Copy the input buffer to the internal buffer
- MemoryUtil.Copy(buffer, 0, _buffer, (nuint)_position, buffer.Length);
+ MemoryUtil.Copy(
+ source: buffer,
+ sourceOffset: 0,
+ dest: _buffer,
+ destOffset: (nuint)_position,
+ count: buffer.Length
+ );
+
//Update the position
_position = newPos;
}