From ee3620b8168a42c8e571e853c751ad5999a9b907 Mon Sep 17 00:00:00 2001 From: vnugent Date: Tue, 18 Jun 2024 21:17:28 -0400 Subject: feat: Add file path caching support --- lib/Utils/src/Extensions/MemoryExtensions.cs | 17 +++++++++++++++ lib/Utils/src/IO/VnMemoryStream.cs | 32 ++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 7 deletions(-) (limited to 'lib/Utils') 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 @@ -267,6 +267,23 @@ namespace VNLib.Utils.Extensions return ref Unsafe.As(ref offsetRef); } + /// + /// Gets a reference to the element at the specified offset from the base + /// address of the and casts it to a byte reference + /// + /// + /// + /// The number of elements to offset the base reference by + /// The reinterpreted byte reference at the first byte of the element offset + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ref byte GetOffsetByteRef(this IMemoryHandle block, nint offset) + { + ArgumentOutOfRangeException.ThrowIfNegative(offset); + return ref GetOffsetByteRef(block, (nuint)offset); + } + /// /// Gets a 64bit friendly span offset for the current /// 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 /// /// Converts a writable to readonly to allow shallow copies /// + /// + /// This funciton will convert the stream passed into it to a readonly stream. + /// The function passes through the input stream as the return value + /// /// The stream to make readonly - /// The readonly stream + /// A reference to the modified input stream public static VnMemoryStream CreateReadonly(VnMemoryStream stream) { ArgumentNullException.ThrowIfNull(stream); @@ -103,7 +107,7 @@ namespace VNLib.Utils.IO /// global heap instance. /// public VnMemoryStream() : this(MemoryUtil.Shared) { } - + /// /// Create a new memory stream where buffers will be allocated from the specified heap /// @@ -111,7 +115,13 @@ namespace VNLib.Utils.IO /// /// public VnMemoryStream(IUnmangedHeap heap) : this(heap, DefaultBufferSize, false) { } - + + /// + /// Creates a new memory stream using the + /// global heap instance. + /// + public VnMemoryStream(nuint bufferSize, bool zero) : this(MemoryUtil.Shared, bufferSize, zero) { } + /// /// 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 } /// - 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; } -- cgit