aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-11-06 23:48:43 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2023-11-06 23:48:43 -0500
commit32240d1d07122be4a9e34684e9857ddc73acac5d (patch)
tree6075663a4331f1961ba36377f063f4295f95511b
parentc582d82d34c7cad9e4c4f874adb91246704826c5 (diff)
mostly some cache stuff
-rw-r--r--lib/Net.Messaging.FBM/src/Client/FBMClient.cs5
-rw-r--r--lib/Utils/tests/Memory/MemoryUtilTests.cs40
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<byte>(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<ArgumentOutOfRangeException>(() => _ = handle.Pin(-1));
+
+ //Test pinned outsie handle size
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() => _ = handle.Pin(1024));
+ }
+
+ using (UnsafeMemoryHandle<BigStruct> handle = MemoryUtil.UnsafeAlloc<BigStruct>(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<ArgumentOutOfRangeException>(() => _ = handle.Pin(-1));