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:
+41
-14
@@ -26,12 +26,6 @@ public class SharedBuffer<T> : BufferReadWrite where T : struct
|
||||
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.");
|
||||
}
|
||||
public void Write(T[] array)
|
||||
{
|
||||
WriteWait();
|
||||
var span = DataSpan;
|
||||
array.AsSpan().CopyTo(span);
|
||||
}
|
||||
/// <summary>
|
||||
/// Prevents read operations from deadlocking by throwing a TimeoutException if the ReadWaitEvent is not available within <see cref="ReadWriteTimeout"/> milliseconds
|
||||
/// </summary>
|
||||
@@ -40,20 +34,14 @@ public class SharedBuffer<T> : BufferReadWrite where T : struct
|
||||
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.");
|
||||
}
|
||||
public void Read(T[] array)
|
||||
{
|
||||
ReadWait();
|
||||
var span = DataSpan;
|
||||
span.CopyTo(array.AsSpan());
|
||||
}
|
||||
public Span<byte> ByteSpan
|
||||
private Span<byte> ByteSpan
|
||||
{
|
||||
get
|
||||
{
|
||||
return Mmf.CreateDirectAccessor(BufferOffset, BufferSize).Bytes;
|
||||
}
|
||||
}
|
||||
public Span<T> DataSpan
|
||||
private Span<T> DataSpan
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -61,5 +49,44 @@ public class SharedBuffer<T> : BufferReadWrite where T : struct
|
||||
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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user