コード例 #1
0
        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();
            }
        }
コード例 #2
0
        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());
            }
        }
コード例 #3
0
        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();
        }
コード例 #4
0
        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();
        }
コード例 #5
0
        static void EnterTempScopesLocally()
        {
            Assert.Zero(UnsafeUtility.GetTempUsed());

            TempMemoryScope.EnterScope();  // Per-test scope
        }
コード例 #6
0
        static void ExitTempScopesLocally()
        {
            TempMemoryScope.ExitScope();  // Per-test scope

            Assert.Zero(UnsafeUtility.GetTempUsed());
        }