public Win32PageFileBackedMemoryMappedPager(string name, long?initialFileSize = null) { _name = name; Win32NativeMethods.SYSTEM_INFO systemInfo; Win32NativeMethods.GetSystemInfo(out systemInfo); AllocationGranularity = systemInfo.allocationGranularity; _totalAllocationSize = initialFileSize.HasValue ? NearestSizeToAllocationGranularity(initialFileSize.Value) : systemInfo.allocationGranularity; _instanceId = Interlocked.Increment(ref _counter); PagerState.Release(); Debug.Assert(AllocationGranularity % PageSize == 0); NumberOfAllocatedPages = _totalAllocationSize / PageSize; PagerState = CreateInitialPagerState(_totalAllocationSize); }
private bool TryFindContinuousMemory(ulong size, out byte *foundAddressPtr) { foundAddressPtr = null; try { foundAddressPtr = Win32NativeMethods.VirtualAlloc(null, new UIntPtr(size), Win32NativeMethods.AllocationType.RESERVE, Win32NativeMethods.MemoryProtection.READWRITE); return(foundAddressPtr != null && foundAddressPtr != (byte *)0); } finally { if (foundAddressPtr != null && foundAddressPtr != (byte *)0) { Win32NativeMethods.VirtualFree(foundAddressPtr, UIntPtr.Zero, Win32NativeMethods.FreeType.MEM_RELEASE); } } }
public override void TryPrefetchingWholeFile() { if (CanPrefetch == false) { return; // not supported } var entries = stackalloc Win32MemoryMapNativeMethods.WIN32_MEMORY_RANGE_ENTRY[PagerState.AllocationInfos.Length]; for (int i = 0; i < PagerState.AllocationInfos.Length; i++) { entries[i].VirtualAddress = PagerState.AllocationInfos[i].BaseAddress; entries[i].NumberOfBytes = (IntPtr)PagerState.AllocationInfos[i].Size; } if (Win32MemoryMapNativeMethods.PrefetchVirtualMemory(Win32NativeMethods.GetCurrentProcess(), (UIntPtr)PagerState.AllocationInfos.Length, entries, 0) == false) { throw new Win32Exception(); } }
public override void MaybePrefetchMemory(List <long> pagesToPrefetch) { if (CanPrefetch == false) { return; // not supported } if (pagesToPrefetch.Count == 0) { return; } var entries = new Win32MemoryMapNativeMethods.WIN32_MEMORY_RANGE_ENTRY[pagesToPrefetch.Count]; for (int i = 0; i < entries.Length; i++) { entries[i].NumberOfBytes = (IntPtr)(4 * PageSize); entries[i].VirtualAddress = AcquirePagePointer(null, pagesToPrefetch[i]); } fixed(Win32MemoryMapNativeMethods.WIN32_MEMORY_RANGE_ENTRY *entriesPtr = entries) { Win32MemoryMapNativeMethods.PrefetchVirtualMemory(Win32NativeMethods.GetCurrentProcess(), (UIntPtr)PagerState.AllocationInfos.Length, entriesPtr, 0); } }
public override void MaybePrefetchMemory(List <Page> sortedPages) { if (sortedPages.Count == 0) { return; } if (IsWindows8OrNewer() == false) { return; // not supported } var list = new List <Win32MemoryMapNativeMethods.WIN32_MEMORY_RANGE_ENTRY>(); long lastPage = -1; const int numberOfPagesInBatch = 8; int sizeInPages = numberOfPagesInBatch; // OS uses 32K when you touch a page, let us reuse this foreach (var page in sortedPages) { if (lastPage == -1) { lastPage = page.PageNumber; } var numberOfPagesInLastPage = page.IsOverflow == false ? 1 : GetNumberOfOverflowPages(page.OverflowSize); var endPage = page.PageNumber + numberOfPagesInLastPage - 1; if (endPage <= lastPage + sizeInPages) { continue; // already within the allocation granularity we have } if (page.PageNumber <= lastPage + sizeInPages + numberOfPagesInBatch) { while (endPage > lastPage + sizeInPages) { sizeInPages += numberOfPagesInBatch; } continue; } list.Add(new Win32MemoryMapNativeMethods.WIN32_MEMORY_RANGE_ENTRY { NumberOfBytes = (IntPtr)(sizeInPages * AbstractPager.PageSize), VirtualAddress = AcquirePagePointer(lastPage) }); lastPage = page.PageNumber; sizeInPages = numberOfPagesInBatch; while (endPage > lastPage + sizeInPages) { sizeInPages += numberOfPagesInBatch; } } list.Add(new Win32MemoryMapNativeMethods.WIN32_MEMORY_RANGE_ENTRY { NumberOfBytes = (IntPtr)(sizeInPages * PageSize), VirtualAddress = AcquirePagePointer(lastPage) }); fixed(Win32MemoryMapNativeMethods.WIN32_MEMORY_RANGE_ENTRY *entries = list.ToArray()) { Win32MemoryMapNativeMethods.PrefetchVirtualMemory(Win32NativeMethods.GetCurrentProcess(), (UIntPtr)list.Count, entries, 0); } }