Skip to content

Instantly share code, notes, and snippets.

@outro56
Created January 22, 2016 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save outro56/5e5c1f7c5e9c0460ed0f to your computer and use it in GitHub Desktop.
Save outro56/5e5c1f7c5e9c0460ed0f to your computer and use it in GitHub Desktop.
quickly read bytes from a memory mapped file
// Copied from
// http://stackoverflow.com/questions/7956167/how-can-i-quickly-read-bytes-from-a-memory-mapped-file-in-net
public static MemoryMapppedIOExtensions
{
public unsafe byte[] ReadBytesFast(this MemoryMappedViewAccessor view, int offset, int num)
{
try
{
byte[] arr = new byte[num];
byte *ptr = (byte*)0;
_view.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
Marshal.Copy(IntPtr.Add(new IntPtr(ptr), offset), arr, 0, num);
}
finally
{
_view.SafeMemoryMappedViewHandle.ReleasePointer();
}
return arr;
}
public unsafe void WriteBytesFast(this MemoryMappedViewAccessor view, int offset, byte[] data)
{
try
{
byte* ptr = (byte*)0;
_view.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
Marshal.Copy(data, 0, IntPtr.Add(new IntPtr(ptr), offset), data.Length);
}
finally
{
_view.SafeMemoryMappedViewHandle.ReleasePointer();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment