public unsafe void VirtualMemory_Decommit() { BaselibErrorState errorState = default; // Reserve 4GB var addressSpace = VirtualMemoryUtility.ReserveAddressSpace(1024ul * 1024ul, VirtualMemoryUtility.DefaultPageSizeInBytes, out errorState); { // Commit 4KB for an allocator VMRange page = new VMRange { ptr = addressSpace.ptr, log2PageSize = addressSpace.log2PageSize, pageCount = 1 }; VirtualMemoryUtility.CommitMemory(page, out errorState); var allocator = new UnsafeScratchAllocator((void *)page.ptr, 4096); var numbers0 = (int *)allocator.Allocate <int>(256); for (int i = 0; i < 256; i++) { numbers0[i] = i; } // Decommit and try to use VirtualMemoryUtility.DecommitMemory(page, out errorState); Assert.Throws <NullReferenceException>(() => numbers0[1] = 1); } VirtualMemoryUtility.FreeAddressSpace(addressSpace, out errorState); }
public void Execute(int index) { BaselibErrorState errorState = default; VMRange addressRange = new VMRange { ptr = jobAddressRangePtr + index, log2PageSize = jobLog2PageSize, pageCount = jobPageCount }; VirtualMemoryUtility.DecommitMemory(addressRange, out errorState); jobErrorStates[index] = errorState; }
/// <summary> /// Frees the buffer represented by the given index. /// </summary> /// <param name="index">Index to buffer.</param> /// <exception cref="ArgumentException">Thrown when index is less than zero or when greater than or equal to BufferCapacity</exception> /// <exception cref="InvalidOperationException">Thrown if the buffer cannot be decommitted.</exception> public void Free(int index) { #if ENABLE_UNITY_COLLECTIONS_CHECKS if (index < 0 || index >= BufferCapacity) { throw new ArgumentException($"Cannot free index {index}, it is outside the expected range [0, {BufferCapacity})."); } #endif BaselibErrorState errorState; var range = new VMRange((IntPtr)this[index], (uint)BufferSizeInBytes, VirtualMemoryUtility.DefaultPageSizeInBytes); VirtualMemoryUtility.DecommitMemory(range, out errorState); if (!errorState.Success) { #if ENABLE_UNITY_COLLECTIONS_CHECKS throw new InvalidOperationException($"Failed to decommit address range {range}."); #endif } FreeList.Add(index); }