예제 #1
0
        public void LargestFreeBlockSizeTest()
        {
            IMemorySlab target = CreateIMemorySlab();
            long        actual;

            //LargestFreeBlockSize should be initially the total size of the slab
            actual = target.LargestFreeBlockSize;
            Assert.AreEqual <long>(totalSize, actual);

            //Allocate a small block, LargestFreeBlockSize should be the difference from the total slab size
            IMemoryBlock allocatedBlock;

            target.TryAllocate(4321, out allocatedBlock);
            actual = target.LargestFreeBlockSize;
            Assert.AreEqual <long>(totalSize - 4321, actual);

            //Allocate a bigger block, LargestFreeBlockSize should decrease by that block size
            IMemoryBlock allocatedBlock2;

            target.TryAllocate(19500, out allocatedBlock2);
            actual = target.LargestFreeBlockSize;
            Assert.AreEqual <long>(totalSize - 4321 - 19500, actual);

            //Free the original small allocation
            target.Free(allocatedBlock);

            //LargestFreeBlockSize should be the greater of it's previous and the freed block
            Assert.AreEqual <long>(Math.Max(4321, (totalSize - 4321 - 19500)), actual);

            target.Free(allocatedBlock2);
        }
예제 #2
0
        public void TryAllocateTest()
        {
            IMemorySlab target = CreateIMemorySlab();

            //allocate 1 byte
            long         smallLength    = 1;
            IMemoryBlock allocatedBlock = null;
            bool         result1        = target.TryAllocate(smallLength, out allocatedBlock);

            Assert.AreEqual <long>(smallLength, allocatedBlock.Length);
            Assert.AreEqual <long>(0, allocatedBlock.StartLocation);
            Assert.AreEqual <bool>(true, result1);

            //allocate rest of slab
            long         restLength      = totalSize - 1;
            IMemoryBlock allocatedBlock2 = null;
            bool         result2         = target.TryAllocate(restLength, out allocatedBlock2);

            Assert.AreEqual <long>(restLength, allocatedBlock2.Length);
            Assert.AreEqual <long>(1, allocatedBlock2.StartLocation);
            Assert.AreEqual <bool>(true, result2);

            //Now try to allocate another byte and expect it to fail
            IMemoryBlock allocatedBlock3 = null;
            bool         result3         = target.TryAllocate(smallLength, out allocatedBlock3);

            Assert.AreEqual <bool>(false, result3);

            //Clean up
            target.Free(allocatedBlock);
            target.Free(allocatedBlock2);
        }
예제 #3
0
        public void TryAllocateTest3()
        {
            IMemorySlab target = CreateIMemorySlab();

            //allocate invalid length
            long         badLength      = -1;
            IMemoryBlock allocatedBlock = null;
            bool         result1        = target.TryAllocate(badLength, out allocatedBlock);
        }
예제 #4
0
        //Gets a single slab buffer
        private static ManagedBuffer GetNewBuffer(IMemorySlab Slab)
        {
            IMemoryBlock allocatedMemoryBlock;

            Slab.TryAllocate(blockSize, out allocatedMemoryBlock);

            ManagedBuffer target = new ManagedBuffer(new IMemoryBlock[] { allocatedMemoryBlock });

            return(target);
        }
예제 #5
0
        public void FreeTest()
        {
            IMemorySlab  target = CreateIMemorySlab();
            IMemoryBlock block1, block2, block3, block4, block5, block6;

            target.TryAllocate(10, out block1);
            target.TryAllocate(10, out block2);
            target.TryAllocate(10, out block3);
            target.TryAllocate(10, out block4);
            target.TryAllocate(10, out block5);
            target.TryAllocate(target.LargestFreeBlockSize, out block6);
            //entire slab is now used up

            //Free block1
            target.Free(block1);
            //reassign block1 to be 4 bytes;
            target.TryAllocate(4, out block1);
            //Free space should be 6 bytes now;
            Assert.AreEqual <long>(6, target.LargestFreeBlockSize);
            //Free Block2
            target.Free(block2);
            //Free space should now be 16 bytes;
            Assert.AreEqual <long>(16, target.LargestFreeBlockSize);
            //Free Block5
            target.Free(block5);
            //Free Block4
            target.Free(block4);
            //Largest free space should now be 20 bytes;
            Assert.AreEqual <long>(20, target.LargestFreeBlockSize);
            //Free Block3
            target.Free(block3);
            //Largest free space should now be 20 + 10 + 16 = 46 bytes
            Assert.AreEqual <long>(46, target.LargestFreeBlockSize);
            //Free Block6
            target.Free(block6);
            //Largest free block should be slab size - block1 size
            Assert.AreEqual <long>(target.Size - block1.Length, target.LargestFreeBlockSize);
            //Free Block1
            target.Free(block1);
            //Largest free block should now be slab size
            Assert.AreEqual <long>(target.Size, target.LargestFreeBlockSize);
        }
예제 #6
0
        private static ManagedBuffer GetNewBuffer(IMemorySlab Slab)
        {
            IMemoryBlock allocatedMemoryBlock;
            Slab.TryAllocate(blockSize, out allocatedMemoryBlock);

            ManagedBuffer target = new ManagedBuffer(allocatedMemoryBlock);
            return target;
        }