Add checked shared-buffer span operations

Use `SharedBuffer<int>` across the reader and writer apps, with callback-based buffer access. Add bounds checks to array and span read/write operations.
This commit is contained in:
Matthias Heil
2026-11-19 10:16:51 +01:00
parent a42b3393d4
commit 2ce631940d
4 changed files with 60 additions and 24 deletions
+8 -6
View File
@@ -1,12 +1,14 @@
using SharedMemory;
try try
{ {
using var numbers = new SharedArray<int>("MySharedArray"); using var consumer = new WriterApp.SharedBuffer<int>("MySharedBuffer");
Console.WriteLine(numbers.Length); consumer.BufferAction((span) =>
Console.WriteLine(numbers[0]); {
Console.WriteLine(numbers[^1]); for (var i = 0; i < span.Length; i++)
{
Console.WriteLine($"Read {span[i]} from the shared buffer");
}
});
} }
catch (FileNotFoundException) catch (FileNotFoundException)
{ {
+4
View File
@@ -11,4 +11,8 @@
<PackageReference Include="SharedMemory" Version="2.3.4" /> <PackageReference Include="SharedMemory" Version="2.3.4" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WriterApp\WriterApp.csproj" />
</ItemGroup>
</Project> </Project>
+7 -4
View File
@@ -7,12 +7,15 @@ using var producer = new WriterApp.SharedBuffer<int>("MySharedBuffer", length: 1
var intArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var intArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
producer.Write(intArray); producer.Write(intArray);
using var consumer = new WriterApp.SharedBuffer<int>("MySharedBuffer"); using var consumer = new WriterApp.SharedBuffer<int>("MySharedBuffer");
var intSpan = consumer.DataSpan; consumer.BufferAction((span) =>
for (var i = 0; i < intSpan.Length; i++)
{ {
Debug.Assert(intSpan[i] == intArray[i]); for (var i = 0; i < span.Length; i++)
{
Debug.Assert(span[i] == intArray[i]);
} }
});
Console.WriteLine("Start the second program, then press Enter here to close the array"); Console.WriteLine("Start the second program, then press Enter here to close the array");
Console.ReadLine(); Console.ReadLine();
+41 -14
View File
@@ -26,12 +26,6 @@ public class SharedBuffer<T> : BufferReadWrite where T : struct
if (!WriteWaitEvent.WaitOne(ReadWriteTimeout)) if (!WriteWaitEvent.WaitOne(ReadWriteTimeout))
throw new TimeoutException("The write operation timed out waiting for the write lock WaitEvent. Check your usage of AcquireWriteLock/ReleaseWriteLock and AcquireReadLock/ReleaseReadLock."); throw new TimeoutException("The write operation timed out waiting for the write lock WaitEvent. Check your usage of AcquireWriteLock/ReleaseWriteLock and AcquireReadLock/ReleaseReadLock.");
} }
public void Write(T[] array)
{
WriteWait();
var span = DataSpan;
array.AsSpan().CopyTo(span);
}
/// <summary> /// <summary>
/// Prevents read operations from deadlocking by throwing a TimeoutException if the ReadWaitEvent is not available within <see cref="ReadWriteTimeout"/> milliseconds /// Prevents read operations from deadlocking by throwing a TimeoutException if the ReadWaitEvent is not available within <see cref="ReadWriteTimeout"/> milliseconds
/// </summary> /// </summary>
@@ -40,20 +34,14 @@ public class SharedBuffer<T> : BufferReadWrite where T : struct
if (!ReadWaitEvent.WaitOne(ReadWriteTimeout)) if (!ReadWaitEvent.WaitOne(ReadWriteTimeout))
throw new TimeoutException("The read operation timed out waiting for the read lock WaitEvent. Check your usage of AcquireWriteLock/ReleaseWriteLock and AcquireReadLock/ReleaseReadLock."); throw new TimeoutException("The read operation timed out waiting for the read lock WaitEvent. Check your usage of AcquireWriteLock/ReleaseWriteLock and AcquireReadLock/ReleaseReadLock.");
} }
public void Read(T[] array) private Span<byte> ByteSpan
{
ReadWait();
var span = DataSpan;
span.CopyTo(array.AsSpan());
}
public Span<byte> ByteSpan
{ {
get get
{ {
return Mmf.CreateDirectAccessor(BufferOffset, BufferSize).Bytes; return Mmf.CreateDirectAccessor(BufferOffset, BufferSize).Bytes;
} }
} }
public Span<T> DataSpan private Span<T> DataSpan
{ {
get get
{ {
@@ -61,5 +49,44 @@ public class SharedBuffer<T> : BufferReadWrite where T : struct
return spanT; return spanT;
} }
} }
public void Write(T[] array)
{
WriteWait();
var span = DataSpan;
if (span.Length < array.Length)
throw new ArgumentException($"The array length {array.Length} is greater than the buffer length {span.Length}");
array.AsSpan().CopyTo(span);
}
public void Read(T[] array)
{
ReadWait();
var span = DataSpan;
if (span.Length > array.Length)
throw new ArgumentException($"The array length {array.Length} is less than the buffer length {span.Length}");
span.CopyTo(array.AsSpan());
}
public void Write(ReadOnlySpan<T> span)
{
WriteWait();
var dataSpan = DataSpan;
if (dataSpan.Length < span.Length)
throw new ArgumentException($"The span length {span.Length} is greater than the buffer length {dataSpan.Length}");
span.CopyTo(dataSpan);
}
public void Read(Span<T> span)
{
ReadWait();
var dataSpan = DataSpan;
if (dataSpan.Length > span.Length)
throw new ArgumentException($"The span length {span.Length} is less than the buffer length {dataSpan.Length}");
dataSpan.CopyTo(span);
}
public void BufferAction(Action<Span<T>> action)
{
ReadWait();
var dataSpan = DataSpan;
action(dataSpan);
}
public T First => DataSpan[0]; public T First => DataSpan[0];
} }