public void TestMemoryReuse() { RenderCommandTempMemPool pool = (RenderCommandTempMemPool)Activator.CreateInstance(typeof(RenderCommandTempMemPool), true); IntPtr tenBytes = pool.Reserve(10U); IntPtr twentyBytes = pool.Reserve(20U); IntPtr thirtyBytes = pool.Reserve(30U); IntPtr fortyBytes = pool.Reserve(40U); pool.FreeAll(); Assert.AreEqual(tenBytes, pool.Reserve(9U)); Assert.AreEqual(thirtyBytes, pool.Reserve(21U)); Assert.AreEqual(fortyBytes, pool.Reserve(40U)); Assert.AreEqual(twentyBytes, pool.Reserve(1U)); IntPtr newAllocationA = pool.Reserve(1U); IntPtr newAllocationB = pool.Reserve(1U); Assert.AreNotEqual(tenBytes, newAllocationA); Assert.AreNotEqual(twentyBytes, newAllocationA); Assert.AreNotEqual(thirtyBytes, newAllocationA); Assert.AreNotEqual(fortyBytes, newAllocationA); Assert.AreNotEqual(tenBytes, newAllocationB); Assert.AreNotEqual(twentyBytes, newAllocationB); Assert.AreNotEqual(thirtyBytes, newAllocationB); Assert.AreNotEqual(fortyBytes, newAllocationB); Assert.AreNotEqual(newAllocationA, newAllocationB); pool.FreeAll(); }
private static IntPtr AllocAndZeroTemp(uint numBytes) { IntPtr result = RenderCommandTempMemPool.GetLocalPool().Reserve(numBytes); UnsafeUtils.ZeroMem(result, numBytes); return(result); }
public static RenderCommandTempMemPool GetLocalPool() { if (threadLocalMemPool == null) { threadLocalMemPool = new RenderCommandTempMemPool(); } return(threadLocalMemPool); }
public override unsafe void Flush() { IntPtr commandListHandleMem = RenderCommandTempMemPool.GetLocalPool().Reserve((uint)IntPtr.Size); QueueCommand(new RenderCommand(RenderCommandInstruction.FinishCommandList, (IntPtr)(&commandListHandleMem))); uint offset = 0U; bool success; for (int i = 0; i < DeferredActions.Count; i++) { KeyValuePair <uint, Action> curAction = DeferredActions[i]; char *failReason = stackalloc char[InteropUtils.MAX_INTEROP_FAIL_REASON_STRING_LENGTH + 1]; success = NativeMethods.RenderPassManager_FlushInstructions( (IntPtr)failReason, RenderingModule.DeviceContext, RenderCommandList.AlignedPointer + (int)offset * sizeof(RenderCommand), curAction.Key - offset ); if (!success) { throw new NativeOperationFailedException(Marshal.PtrToStringUni((IntPtr)failReason)); } offset = curAction.Key; curAction.Value(); } char *failReason2 = stackalloc char[InteropUtils.MAX_INTEROP_FAIL_REASON_STRING_LENGTH + 1]; success = NativeMethods.RenderPassManager_FlushInstructions( (IntPtr)failReason2, RenderingModule.DeviceContext, RenderCommandList.AlignedPointer + (int)offset * sizeof(RenderCommand), CurListIndex - offset ); if (!success) { throw new NativeOperationFailedException(Marshal.PtrToStringUni((IntPtr)failReason2)); } lastCommandListHandle = commandListHandleMem; LosgapSystem.InvokeOnMaster(invokeOnMasterAction); CurListIndex = 0U; DeferredActions.Clear(); RenderCommandTempMemPool.GetLocalPool().FreeAll(); }