The code in question (below) reads much faster (30x), tahn regular: MemoryMappedViewAccessor.ReadArray() I'm trying to modify the code to be able to read from long offset, not int (!)
public unsafe byte[] ReadBytes(int offset, int num)
{
byte[] arr = new byte[num];
byte *ptr = (byte*)0;
this._view.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
Marshal.Copy(IntPtr.Add(new IntPtr(ptr), offset), arr, 0, num);
this._view.SafeMemoryMappedViewHandle.ReleasePointer();
return arr;
}
original code is here: How can I quickly read bytes from a memory mapped file in .NET?_
I need to adjust IntPtr.Add and Marshal.Copy to correctly work with long offset Thank you in advance!
CodePudding user response:
new IntPtr(intPtr.ToInt64() longOffset)
Of course, this works in 64 bit processes only.
CodePudding user response:
Thank you, Klaus Gütter! Complete working code, based on your suggested method:
public static unsafe byte[] ReadBytes(long l_offset, int i_read_buf_size, MemoryMappedViewAccessor mmva)
{
byte[] arr = new byte[i_read_buf_size];
byte* ptr = (byte*)0;
mmva.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
IntPtr p = new(ptr);
Marshal.Copy(new IntPtr(p.ToInt64() l_offset), arr, 0, i_read_buf_size);
mmva.SafeMemoryMappedViewHandle.ReleasePointer();
return arr;
}