42 lines
No EOL
1.3 KiB
C#
42 lines
No EOL
1.3 KiB
C#
using System.IO.MemoryMappedFiles;
|
|
|
|
namespace StitchATon2.Infra;
|
|
|
|
internal static class Utils
|
|
{
|
|
private static unsafe uint AlignedSizeOf<T>() where T : unmanaged
|
|
{
|
|
uint size = (uint)sizeof(T);
|
|
return size is 1 or 2 ? size : (uint)((size + 3) & ~3);
|
|
}
|
|
|
|
internal static void DangerousReadSpan<T>(this MemoryMappedViewAccessor view, long position, Span<T> span, int offset, int count)
|
|
where T : unmanaged
|
|
{
|
|
uint sizeOfT = AlignedSizeOf<T>();
|
|
int n = count;
|
|
long spaceLeft = view.Capacity - position;
|
|
if (spaceLeft < 0)
|
|
{
|
|
n = 0;
|
|
}
|
|
else
|
|
{
|
|
ulong spaceNeeded = (ulong)(sizeOfT * count);
|
|
if ((ulong)spaceLeft < spaceNeeded)
|
|
{
|
|
n = (int)(spaceLeft / sizeOfT);
|
|
}
|
|
}
|
|
|
|
var byteOffset = (ulong)(view.PointerOffset + position);
|
|
view.SafeMemoryMappedViewHandle.ReadSpan(byteOffset, span.Slice(offset, n));
|
|
}
|
|
|
|
internal static void DangerousWriteSpan<T>(this MemoryMappedViewAccessor view, long position, Span<T> span, int offset, int count)
|
|
where T : unmanaged
|
|
{
|
|
var byteOffset = (ulong)(view.PointerOffset + position);
|
|
view.SafeMemoryMappedViewHandle.WriteSpan<T>(byteOffset, span.Slice(offset, count));
|
|
}
|
|
} |