示例#1
0
        public unsafe void SmallHeapMultiPageAllocationTest()
        {
            var xRAM = new byte[1024 * 1024]; // 4 MB

            fixed(byte *xPtr = xRAM)
            {
                RAT.Debug = true;

                RAT.Init(xPtr, (uint)xRAM.Length);

                uint smallPages = RAT.GetPageCount(RAT.PageType.HeapSmall);


                var ptr1 = Heap.Alloc(HeapSmall.mMaxItemSize); // 4 of them should fit on one page
                var ptr2 = Heap.Alloc(HeapSmall.mMaxItemSize);
                var ptr3 = Heap.Alloc(HeapSmall.mMaxItemSize);
                var ptr4 = Heap.Alloc(HeapSmall.mMaxItemSize);

                Assert.AreEqual((uint)ptr2 - (uint)ptr1, (uint)ptr4 - (uint)ptr3);
                Assert.AreEqual((uint)RAT.GetPagePtr(ptr1), (uint)RAT.GetPagePtr(ptr2));
                Assert.AreEqual(4, HeapSmall.GetAllocatedObjectCount());
                Heap.Free(ptr4);
                var nptr4 = Heap.Alloc(HeapSmall.mMaxItemSize);

                Assert.AreEqual((uint)RAT.GetPagePtr(ptr1), (uint)RAT.GetPagePtr(ptr4));
                var ptr5 = Heap.Alloc(HeapSmall.mMaxItemSize); // this should cause a new page to have to be created

                uint largePages = RAT.GetPageCount(RAT.PageType.HeapLarge);

                Assert.AreEqual((uint)0, largePages);
                Assert.AreEqual(smallPages + 1, RAT.GetPageCount(RAT.PageType.HeapSmall));
                Assert.AreNotEqual((uint)ptr1, (uint)ptr5);
                Assert.IsTrue(((uint)ptr5 - (uint)ptr1) % RAT.PageSize == 0);
                Assert.AreEqual(5, HeapSmall.GetAllocatedObjectCount());


                // now lets force them to allocate 2 more pages
                for (int i = 0; i < 8; i++)
                {
                    Heap.Alloc(HeapSmall.mMaxItemSize - 2);
                }
                Assert.AreEqual(smallPages + 3, RAT.GetPageCount(RAT.PageType.HeapSmall));
                Assert.AreEqual(13, HeapSmall.GetAllocatedObjectCount());
            }
        }
示例#2
0
        public unsafe void TestRATHeapMethods()
        {
            var xRAM = new byte[10 * RAT.PageSize]; // 10 Pages - 1 for RAT and 9 for values

            fixed(byte *xPtr = xRAM)
            {
                RAT.Debug = true;
                RAT.Init(xPtr, (uint)xRAM.Length);

                var ptr1 = Heap.Alloc(10);
                var ptr2 = Heap.Alloc(10);
                var ptr3 = Heap.Alloc(10);

                Assert.AreNotEqual((uint)ptr1, (uint)ptr2);
                Assert.AreNotEqual((uint)ptr1, (uint)ptr3);
                Assert.AreNotEqual((uint)ptr2, (uint)ptr3);
                Assert.AreEqual(RAT.GetFirstRATIndex(ptr1), RAT.GetFirstRATIndex(ptr2));
                Assert.AreEqual(RAT.GetFirstRATIndex(ptr1), RAT.GetFirstRATIndex(ptr3));
                Assert.AreEqual((uint)RAT.GetPagePtr(ptr1), (uint)RAT.GetPagePtr(ptr2));
                Assert.AreEqual((uint)RAT.GetPagePtr(ptr1), (uint)RAT.GetPagePtr(ptr3));
            }
        }