public unsafe void TempAllocatorMultithreadUnique() { Thread[] threads = new Thread[16]; System.Random rand = new System.Random(); ManualResetEvent mre = new ManualResetEvent(false); for (int i = 0; i < threads.Length; i++) { threads[i] = new Thread((object ra) => { int r = (int)ra; UnsafeUtility.EnterTempScope(); int oldSize = UnsafeUtility.GetTempUsed(); void *mem = UnsafeUtility.Malloc(r, 0, Collections.Allocator.Temp); mre.WaitOne(); Assert.GreaterOrEqual(UnsafeUtility.GetTempUsed(), oldSize + r); Assert.Less(UnsafeUtility.GetTempUsed(), oldSize + r + 128); UnsafeUtility.Free(mem, Collections.Allocator.Temp); UnsafeUtility.ExitTempScope(); }); threads[i].Start(rand.Next(1, 32768)); } Thread.Sleep(200); mre.Set(); for (int i = 0; i < threads.Length; i++) { threads[i].Join(); } }
public unsafe void TempAllocatorRenestingDoesntLeak() { void *[] mem = new void *[4096]; int tempUsedBase = UnsafeUtility.GetTempUsed(); int tempUsedTop = -1; int tempCapTop = -1; for (int j = 0; j < 20; j++) { for (int i = 0; i < mem.Length; i++) { UnsafeUtility.EnterTempScope(); mem[i] = UnsafeUtility.Malloc(4096, 0, Collections.Allocator.Temp); } if (tempUsedTop == -1) { tempUsedTop = UnsafeUtility.GetTempUsed(); tempCapTop = UnsafeUtility.GetTempCapacity(); } Assert.AreEqual(tempUsedTop, UnsafeUtility.GetTempUsed()); Assert.AreEqual(tempCapTop, UnsafeUtility.GetTempCapacity()); for (int i = mem.Length - 1; i >= 0; i--) { UnsafeUtility.Free(mem[i], Collections.Allocator.Temp); UnsafeUtility.ExitTempScope(); } Assert.AreEqual(tempUsedBase, UnsafeUtility.GetTempUsed()); } }
public unsafe void TempAllocatorRewindFreesMemUsage() { ExitTempScopesLocally(); void *[] mem = new void *[4096]; for (int i = 0; i < mem.Length; i++) { TempMemoryScope.EnterScope(); mem[i] = UnsafeUtility.Malloc(4096, 0, Collections.Allocator.Temp); } int memUsage = UnsafeUtility.GetTempUsed(); for (int i = mem.Length - 1; i >= 0; i--) { UnsafeUtility.Free(mem[i], Collections.Allocator.Temp); Assert.AreEqual(memUsage, UnsafeUtility.GetTempUsed()); TempMemoryScope.ExitScope(); Assert.Less(UnsafeUtility.GetTempUsed(), memUsage); memUsage = UnsafeUtility.GetTempUsed(); } Assert.Zero(memUsage); EnterTempScopesLocally(); }
public unsafe void TempAllocatorLargerThanDefaultSize() { const int kSize = 1024 * 1024 * 4; ExitTempScopesLocally(); UnsafeUtility.EnterTempScope(); int oldCap = UnsafeUtility.GetTempCapacity(); void *largeTemp = UnsafeUtility.Malloc(kSize, 0, Collections.Allocator.Temp); Assert.Greater(UnsafeUtility.GetTempCapacity(), oldCap); Assert.GreaterOrEqual(UnsafeUtility.GetTempUsed(), kSize); UnsafeUtility.Free(largeTemp, Collections.Allocator.Temp); UnsafeUtility.ExitTempScope(); Assert.AreEqual(UnsafeUtility.GetTempCapacity(), oldCap); EnterTempScopesLocally(); }
static void EnterTempScopesLocally() { Assert.Zero(UnsafeUtility.GetTempUsed()); TempMemoryScope.EnterScope(); // Per-test scope }
static void ExitTempScopesLocally() { TempMemoryScope.ExitScope(); // Per-test scope Assert.Zero(UnsafeUtility.GetTempUsed()); }