internal static MemoryMappedView CreateView(SafeMemoryMappedFileHandle safeMemoryMappedFileHandle, MemoryMappedFileAccess access, long offset, long size) { // http://msdn.microsoft.com/en-us/library/windows/desktop/aa366548(v=vs.85).aspx UnsafeNativeMethods.SYSTEM_INFO info = new UnsafeNativeMethods.SYSTEM_INFO(); UnsafeNativeMethods.GetSystemInfo(ref info); // To calculate where to start the file mapping, round down the // offset of the data into the memory-mapped file to the nearest multiple of the // system allocation granularity. long fileMapStart = (offset / info.dwAllocationGranularity) * info.dwAllocationGranularity; // How large will the file mapping object be? long mapViewSize = (offset % info.dwAllocationGranularity) + size; // The data of interest is not necessarily at the beginning of the // view, so determine how far into the view to set the pointer. long viewDelta = offset - fileMapStart; SafeMemoryMappedViewHandle safeHandle = UnsafeNativeMethods.MapViewOfFile(safeMemoryMappedFileHandle, access.ToMapViewFileAccess(), (ulong)fileMapStart, new UIntPtr((ulong)mapViewSize)); var lastWin32Error = Marshal.GetLastWin32Error(); if (safeHandle.IsInvalid) { if (lastWin32Error == UnsafeNativeMethods.ERROR_FILE_NOT_FOUND) { throw new FileNotFoundException(); } throw new System.IO.IOException(UnsafeNativeMethods.GetMessage(lastWin32Error)); } return(new MemoryMappedView(safeHandle, viewDelta, size)); }
internal static Int32 GetSystemPageAllocationGranularity() { UnsafeNativeMethods.SYSTEM_INFO info = new UnsafeNativeMethods.SYSTEM_INFO(); UnsafeNativeMethods.GetSystemInfo(ref info); return(info.dwAllocationGranularity); }