From 32240d1d07122be4a9e34684e9857ddc73acac5d Mon Sep 17 00:00:00 2001 From: vnugent Date: Mon, 6 Nov 2023 23:48:43 -0500 Subject: mostly some cache stuff --- lib/Net.Messaging.FBM/src/Client/FBMClient.cs | 5 +--- lib/Utils/tests/Memory/MemoryUtilTests.cs | 40 ++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/lib/Net.Messaging.FBM/src/Client/FBMClient.cs b/lib/Net.Messaging.FBM/src/Client/FBMClient.cs index c8319fa..ee64d53 100644 --- a/lib/Net.Messaging.FBM/src/Client/FBMClient.cs +++ b/lib/Net.Messaging.FBM/src/Client/FBMClient.cs @@ -438,10 +438,7 @@ namespace VNLib.Net.Messaging.FBM.Client } //Alloc data buffer and write initial data - VnMemoryStream responseBuffer = new(heap); - - //Copy initial data - responseBuffer.Write(recvBuffer.GetSpan()[..result.Count]); + VnMemoryStream responseBuffer = new(heap, recvBuffer.GetSpan()[..result.Count]); //Receive packets until the EOF is reached while (!result.EndOfMessage) diff --git a/lib/Utils/tests/Memory/MemoryUtilTests.cs b/lib/Utils/tests/Memory/MemoryUtilTests.cs index 0774f47..9b8ed1e 100644 --- a/lib/Utils/tests/Memory/MemoryUtilTests.cs +++ b/lib/Utils/tests/Memory/MemoryUtilTests.cs @@ -133,6 +133,11 @@ namespace VNLib.Utils.Memory.Tests Assert.IsTrue(AllZero(new ReadOnlySpan(ptr, blockSize))); } + unsafe struct BigStruct + { + public fixed byte Data[1000]; + } + [TestMethod()] public unsafe void UnsafeAllocTest() { @@ -142,22 +147,49 @@ namespace VNLib.Utils.Memory.Tests _ = handle.Span; _ = handle.Length; _ = handle.IntLength; - _ = handle.GetReference(); + + //Test references are equal + ref byte spanRef = ref MemoryMarshal.GetReference(handle.Span); + ref byte handleRef = ref handle.GetReference(); + Assert.IsTrue(Unsafe.AreSame(ref spanRef, ref handleRef)); //Test span pointer against pinned handle using (MemoryHandle pinned = handle.Pin(0)) { - fixed (void* ptr = &MemoryMarshal.GetReference(handle.Span)) + fixed (void* ptr = &spanRef) { Assert.IsTrue(ptr == pinned.Pointer); } } + //Test negative pin + Assert.ThrowsException(() => _ = handle.Pin(-1)); + + //Test pinned outsie handle size + Assert.ThrowsException(() => _ = handle.Pin(1024)); + } + + using (UnsafeMemoryHandle handle = MemoryUtil.UnsafeAlloc(1024)) + { + _ = handle.Span; + _ = handle.Length; + _ = handle.IntLength; + ref BigStruct handleRef = ref handle.GetReference(); + ref BigStruct spanRef = ref MemoryMarshal.GetReference(handle.Span); + //Test references are equal - ref byte spanRef = ref MemoryMarshal.GetReference(handle.Span); - ref byte handleRef = ref handle.GetReference(); Assert.IsTrue(Unsafe.AreSame(ref spanRef, ref handleRef)); + //Test span pointer against pinned handle + using (MemoryHandle pinned = handle.Pin(11)) + { + fixed (BigStruct* ptr = &spanRef) + { + void* offset = ptr + 11; + Assert.IsTrue(offset == pinned.Pointer); + } + } + //Test negative pin Assert.ThrowsException(() => _ = handle.Pin(-1)); -- cgit