Created
December 1, 2022 09:43
-
-
Save i-e-b/be3c6c5c0a63000fa29c95259814d07d to your computer and use it in GitHub Desktop.
Quick demo of mmap in dotnet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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