コード例 #1
0
        protected internal virtual List <MemoryBlock> GetBlock(long p_Capacity, long p_TargetCapacity)
        {
            List <MemoryBlock> l_Blocks = new List <MemoryBlock>();

            long l_NeededBytes = p_TargetCapacity - p_Capacity;

            while (l_NeededBytes > 0)
            {
                // Find Best Pool
                StreamManagerPool l_Pool = m_ChooserPolicy.FindBestPool(p_Capacity, p_TargetCapacity);

                if (l_Pool == null)
                {
                    throw new Exception($"No Pool found. Capacity {p_Capacity}, TargetCapacity {p_TargetCapacity}, Remaining Bytes {l_NeededBytes}");
                }

                // Allocated all necessary blocks
                while (l_NeededBytes > 0)
                {
                    // If if max usage reached restart and search for a different pool
                    if (!m_ChooserPolicy.PoolHasFreeBlocks(l_Pool))
                    {
                        break;
                    }

                    MemoryBlock l_MemoryBlock = l_Pool.GetBlock();
                    l_NeededBytes = l_NeededBytes - l_MemoryBlock.GetLength();
                    l_Blocks.Add(l_MemoryBlock);
                }
            }

            return(l_Blocks);
        }
コード例 #2
0
        public void FindBestNoFreeSpaceTest()
        {
            List <PoolChooserPolicyPoolItem> l_Items = new List <PoolChooserPolicyPoolItem>();
            PoolChooserPolicyPoolItem        l_Item  = new PoolChooserPolicyPoolItem();

            l_Item.Start = 0;
            l_Item.End   = 10;
            l_Item.Pool  = new StreamManagerArrayPool("1", 1, 0);
            l_Items.Add(l_Item);
            l_Item       = new PoolChooserPolicyPoolItem();
            l_Item.Start = 11;
            l_Item.End   = 20;
            l_Item.Pool  = new StreamManagerArrayPool("2", 1, 0);
            l_Items.Add(l_Item);
            l_Item       = new PoolChooserPolicyPoolItem();
            l_Item.Start = 21;
            l_Item.End   = 30;
            l_Item.Pool  = new StreamManagerArrayPool("3", 1, 0);
            l_Items.Add(l_Item);

            FreeSpaceAwarePoolChooserPolicy l_Chooser = new FreeSpaceAwarePoolChooserPolicy(l_Items);
            StreamManagerPool l_Pool = l_Chooser.FindBestPool(15, 15);

            Assert.Null(l_Pool);
        }
コード例 #3
0
        public void FindBestFallBackToFristTest()
        {
            List <PoolChooserPolicyPoolItem> l_Items = new List <PoolChooserPolicyPoolItem>();
            PoolChooserPolicyPoolItem        l_Item  = new PoolChooserPolicyPoolItem();

            l_Item.Start = 0;
            l_Item.End   = 10;
            l_Item.Pool  = new StreamManagerArrayPool("1", 1, 1);
            l_Items.Add(l_Item);
            l_Item       = new PoolChooserPolicyPoolItem();
            l_Item.Start = 11;
            l_Item.End   = 20;
            l_Item.Pool  = new StreamManagerArrayPool("2", 1, 0);
            l_Items.Add(l_Item);
            l_Item       = new PoolChooserPolicyPoolItem();
            l_Item.Start = 21;
            l_Item.End   = 30;
            l_Item.Pool  = new StreamManagerArrayPool("3", 1, 0);
            l_Items.Add(l_Item);

            FreeSpaceAwarePoolChooserPolicy l_Chooser = new FreeSpaceAwarePoolChooserPolicy(l_Items);
            StreamManagerPool l_Pool = l_Chooser.FindBestPool(15, 15);

            Assert.Equal(l_Items[0].Pool, l_Pool);
            Assert.True(l_Chooser.PoolHasFreeBlocks(l_Pool));
        }
コード例 #4
0
        public bool PoolHasFreeBlocks(StreamManagerPool p_Pool)
        {
            // Fallback Pool has always free space
            if (p_Pool == m_FallbackPool)
            {
                return(true);
            }

            return(DoPoolHasFreeBlocks(p_Pool));
        }
コード例 #5
0
        protected PoolChooserPolicyBase(List <PoolChooserPolicyPoolItem> p_Pools, StreamManagerPool p_FallbackPool)
        {
            if (p_Pools.Count == 0)
            {
                throw new ArgumentException("Pool without Items is not supported", nameof(p_Pools));
            }

            m_FallbackPool = p_FallbackPool;
            m_Pools        = p_Pools;
        }
コード例 #6
0
        protected override void HandleError(StreamManagerPool p_Pool, Exception p_Exception)
        {
            PoolWatcherHandleErrorEvent l_HandleError = OnHandleError;

            if (l_HandleError == null)
            {
                return;
            }

            l_HandleError(this, new PoolWatcherHandleErrorEventArgs(p_Pool, p_Exception));
        }
コード例 #7
0
        public StreamManagerPool FindBestPool(long p_CurrentCapacity, long p_TargetCapacity)
        {
            StreamManagerPool l_Pool = DoFindBestPool(p_CurrentCapacity, p_TargetCapacity);

            if (l_Pool == null)
            {
                return(m_FallbackPool);
            }

            return(l_Pool);
        }
コード例 #8
0
        public void ReuseInstanceTest()
        {
            StreamManagerPool l_StreamManagerPool = CreatePool();
            MemoryBlock       l_Block             = l_StreamManagerPool.GetBlock();

            l_Block.ReturnBlock();

            MemoryBlock l_Block2 = l_StreamManagerPool.GetBlock();

            Assert.Equal(l_Block, l_Block2);
        }
コード例 #9
0
        protected override void PoolStateChanged(StreamManagerPool p_Statistic, PoolWatcherPoolState p_OldState,
                                                 PoolWatcherPoolState p_NewState)
        {
            PoolWatcherPoolStateChangedEvent l_Event = OnPoolStateChanged;

            if (l_Event == null)
            {
                return;
            }

            l_Event(this, new PoolWatcherPoolStateChangedEventArgs(p_Statistic, p_OldState, p_NewState));
        }
コード例 #10
0
        public void GetAndReturnTest()
        {
            StreamManagerPool l_Pool = CreatePool();

            Assert.Equal(0, l_Pool.GetBlocksInUse());
            Assert.True(l_Pool.HasFreeBlocks());

            MemoryBlock l_Block = l_Pool.GetBlock();

            Assert.NotNull(l_Block);
            Assert.Equal(1, l_Pool.GetBlocksInUse());

            l_Block.ReturnBlock();
            Assert.Equal(0, l_Pool.GetBlocksInUse());
        }
コード例 #11
0
        protected override StreamManagerPool DoFindBestPool(long p_CurrentCapacity, long p_TargetCapacity)
        {
            Boolean           l_CheckRange             = true;
            StreamManagerPool l_LastPoolWithFreeBlocks = null;

            // To find the correct Buffersize we using the capacity
            // Simplified: Large Capacity -> Large Buffer

            foreach (PoolChooserPolicyPoolItem l_PoolItem in m_Pools)
            {
                if (l_CheckRange)
                {
                    if (l_PoolItem.IsInRange(p_TargetCapacity))
                    {
                        // Check if Space left in the Pool
                        if (l_PoolItem.Pool.HasFreeBlocks())
                        {
                            return(l_PoolItem.Pool);
                        }

                        // Disable Range check for next checks
                        l_CheckRange = false;
                    }
                    else
                    {
                        if (l_PoolItem.Pool.HasFreeBlocks())
                        {
                            l_LastPoolWithFreeBlocks = l_PoolItem.Pool;
                        }
                    }
                }
                else
                {
                    if (l_PoolItem.Pool.HasFreeBlocks())
                    {
                        return(l_PoolItem.Pool);
                    }
                }
            }

            //if we dont find a pool with matching size or large we return the last with some free space
            return(l_LastPoolWithFreeBlocks);
        }
コード例 #12
0
        public void IgnoreFreeSpaceTest()
        {
            List <PoolChooserPolicyPoolItem> l_Items = new List <PoolChooserPolicyPoolItem>();
            PoolChooserPolicyPoolItem        l_Item  = new PoolChooserPolicyPoolItem();

            l_Item.Start = 0;
            l_Item.End   = 10;
            l_Item.Pool  = new StreamManagerArrayPool("1", 1, 1);
            l_Items.Add(l_Item);
            l_Item       = new PoolChooserPolicyPoolItem();
            l_Item.Start = 11;
            l_Item.End   = 20;
            l_Item.Pool  = new StreamManagerArrayPool("2", 1, 0);
            l_Items.Add(l_Item);

            IgnoreFreeSpacePoolChooserPolicy l_Chooser = new IgnoreFreeSpacePoolChooserPolicy(l_Items);
            StreamManagerPool l_Pool = l_Chooser.FindBestPool(15, 15);

            Assert.Equal(l_Items[1].Pool, l_Pool);
            Assert.True(l_Chooser.PoolHasFreeBlocks(l_Pool));
        }
コード例 #13
0
        public void FallBackTest()
        {
            StreamManagerArrayPool l_FallBack = new StreamManagerArrayPool("Fallback", 1, 1);

            List <PoolChooserPolicyPoolItem> l_Items = new List <PoolChooserPolicyPoolItem>();
            PoolChooserPolicyPoolItem        l_Item  = new PoolChooserPolicyPoolItem();

            l_Item.Start = 0;
            l_Item.End   = 10;
            l_Item.Pool  = new StreamManagerArrayPool("1", 1, 1);
            l_Items.Add(l_Item);
            l_Item       = new PoolChooserPolicyPoolItem();
            l_Item.Start = 11;
            l_Item.End   = 20;
            l_Item.Pool  = new StreamManagerArrayPool("2", 1, 0);
            l_Items.Add(l_Item);

            IgnoreFreeSpacePoolChooserPolicy l_Chooser = new IgnoreFreeSpacePoolChooserPolicy(l_Items, l_FallBack);
            StreamManagerPool l_Pool = l_Chooser.FindBestPool(25, 25);

            Assert.Equal(l_FallBack, l_Pool);
            Assert.True(l_Chooser.PoolHasFreeBlocks(l_Pool));
        }
コード例 #14
0
 protected override bool DoPoolHasFreeBlocks(StreamManagerPool p_Pool)
 {
     return(p_Pool.HasFreeBlocks());
 }
コード例 #15
0
 public PoolWatcherHandleErrorEventArgs(StreamManagerPool p_Pool, Exception p_Exception)
 {
     Pool      = p_Pool;
     Exception = p_Exception;
 }
コード例 #16
0
 public PoolWatcherPoolStateChangedEventArgs(StreamManagerPool p_Pool, PoolWatcherPoolState p_OldState, PoolWatcherPoolState p_NewState)
 {
     Pool     = p_Pool;
     OldState = p_OldState;
     NewState = p_NewState;
 }
コード例 #17
0
 protected abstract bool DoPoolHasFreeBlocks(StreamManagerPool p_Pool);
 private int SortByBlockSize(StreamManagerPool p_X, StreamManagerPool p_Y)
 {
     return(p_X.GetBlockSize().CompareTo(p_X.GetBlockSize()));
 }
コード例 #19
0
 public FreeSpaceAwarePoolChooserPolicy(List <PoolChooserPolicyPoolItem> p_Pools, StreamManagerPool p_FallbackPool) : base(p_Pools, p_FallbackPool)
 {
 }
コード例 #20
0
 public IPoolBuilder UseFallBackPool(StreamManagerPool p_Pool)
 {
     m_FallbackPool = p_Pool;
     return(this);
 }
コード例 #21
0
 public FileMemoryBlock(StreamManagerPool p_Pool, Stream p_Stream, long p_Offset, StreamManagerFilePoolDeleteHandler p_DeleteHandler) : base(p_Pool)
 {
     m_Stream        = p_Stream;
     m_Offset        = p_Offset;
     m_DeleteHandler = p_DeleteHandler;
 }
 public IgnoreFreeSpacePoolChooserPolicy(List <StreamManagerPool> p_Pools, StreamManagerPool p_FallbackPool) : base(p_Pools, p_FallbackPool)
 {
 }
 protected override bool DoPoolHasFreeBlocks(StreamManagerPool p_Pool)
 {
     // This Policy ignore if the pool hast some free Blocks or not
     return(true);
 }
コード例 #24
0
 protected abstract void HandleError(StreamManagerPool p_Pool, Exception p_Exception);
コード例 #25
0
 public ArrayMemoryBlock(StreamManagerPool p_Pool, byte[] p_Data, int p_Length) : base(p_Pool)
 {
     m_Data   = p_Data;
     m_Length = p_Length;
 }
コード例 #26
0
 protected abstract void PoolStateChanged(StreamManagerPool p_Statistic,
                                          PoolWatcherPoolState p_OldState, PoolWatcherPoolState p_NewState);
コード例 #27
0
 public IPoolBuilder AddPool(StreamManagerPool p_Pool)
 {
     m_Pools.Add(p_Pool);
     return(this);
 }
コード例 #28
0
 protected PoolChooserPolicyBase(List <StreamManagerPool> p_Pools, StreamManagerPool p_FallbackPool)
 {
     m_FallbackPool = p_FallbackPool;
     m_Pools        = CreatePools(p_Pools);
 }