Skip to content

Instantly share code, notes, and snippets.

@i-e-b
Created December 1, 2022 09:43
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 i-e-b/be3c6c5c0a63000fa29c95259814d07d to your computer and use it in GitHub Desktop.
Save i-e-b/be3c6c5c0a63000fa29c95259814d07d to your computer and use it in GitHub Desktop.
Quick demo of mmap in dotnet
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
namespace MemMapTests;
internal static class Program
{
private const long Megabyte = 1048576;
private const long Kilobyte = 1024;
private static void Main()
{
Console.WriteLine("Hello World!");
var offset = 128 * Megabyte;
var length = 1 * Kilobyte;
using var mmf = MemoryMappedFile.CreateFromFile(@"C:\Users\Iain\Desktop\bigfile.zip", FileMode.Open, null);
using var accessor = mmf.CreateViewAccessor(offset, length);
int colorSize = Marshal.SizeOf<SomeData>();
for (long i = 0; i < length; i += colorSize)
{
accessor.Read<SomeData>(i, out var data);
//accessor.Write(i, ref data);
Console.WriteLine($"Chunk of data at {offset:X8}+{i:X4} -> {data.A:X8} {data.B:X8} {data.C:X8} {data.D:X8}");
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SomeData
{
public uint A;
public uint B;
public uint C;
public uint D;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment