public void TestAllocateAndDeallocate() { Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 0L); using (MemoryPoolStreamCore ms = new MemoryPoolStreamCore()) { BlockArguments args = new BlockArguments(); ms.GetBlock(args); Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, Globals.MemoryPool.PageSize); ms.GetBlock(args); Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, Globals.MemoryPool.PageSize); args.Position = Globals.MemoryPool.PageSize; ms.GetBlock(args); Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 2 * Globals.MemoryPool.PageSize); } Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 0L); }
public void TestAlignment() { Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 0L); using (MemoryPoolStreamCore ms = new MemoryPoolStreamCore()) { BlockArguments args = new BlockArguments(); ms.ConfigureAlignment(41211, 4096); Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 0L); args.Position = 41211; ms.GetBlock(args); Assert.AreEqual(41211L, args.FirstPosition); Assert.AreEqual(Globals.MemoryPool.AllocatedBytes - (41211 % 4096), args.Length); Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, Globals.MemoryPool.PageSize); } Assert.AreEqual(Globals.MemoryPool.AllocatedBytes, 0L); }
/// <summary> /// Populates the pointer data inside <see cref="args"/> for the desired block as specified in <see cref="args"/>. /// This function will block if needing to retrieve data from the disk. /// </summary> /// <param name="pageLock">The reusable lock information about what this block is currently using.</param> /// <param name="args">Contains what block needs to be read and when this function returns, /// it will contain the proper pointer information for this block.</param> private void GetBlock(PageReplacementAlgorithm.PageLock pageLock, BlockArguments args) { pageLock.Clear(); //Determines where the block is located. if (args.Position >= m_lengthOfCommittedData) { //If the block is in the uncommitted space, it is stored in the //MemoryPoolStreamCore. args.SupportsWriting = true; m_writeBuffer.GetBlock(args); } else if (args.Position < m_lengthOfHeader) { //If the block is in the header, error because this area of the file is not designed to be accessed. throw new ArgumentOutOfRangeException("args", "Cannot use this function to modify the file header."); } else { //If it is between the file header and uncommitted space, //it is in the committed space, which this space by design is never to be modified. if (args.IsWriting) { throw new ArgumentException("Cannot write to committed data space", "args"); } args.SupportsWriting = false; args.Length = m_diskBlockSize; //rounds to the beginning of the block to be looked up. args.FirstPosition = args.Position & ~(long)m_pool.PageMask; GetBlockFromCommittedSpace(pageLock, args.FirstPosition, out args.FirstPointer); //Make sure the block does not go beyond the end of the uncommitted space. if (args.FirstPosition + args.Length > m_lengthOfCommittedData) { args.Length = (int)(m_lengthOfCommittedData - args.FirstPosition); } } }