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); }
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); }
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)); }
public bool PoolHasFreeBlocks(StreamManagerPool p_Pool) { // Fallback Pool has always free space if (p_Pool == m_FallbackPool) { return(true); } return(DoPoolHasFreeBlocks(p_Pool)); }
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; }
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)); }
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); }
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); }
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)); }
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()); }
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); }
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)); }
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)); }
protected override bool DoPoolHasFreeBlocks(StreamManagerPool p_Pool) { return(p_Pool.HasFreeBlocks()); }
public PoolWatcherHandleErrorEventArgs(StreamManagerPool p_Pool, Exception p_Exception) { Pool = p_Pool; Exception = p_Exception; }
public PoolWatcherPoolStateChangedEventArgs(StreamManagerPool p_Pool, PoolWatcherPoolState p_OldState, PoolWatcherPoolState p_NewState) { Pool = p_Pool; OldState = p_OldState; NewState = p_NewState; }
protected abstract bool DoPoolHasFreeBlocks(StreamManagerPool p_Pool);
private int SortByBlockSize(StreamManagerPool p_X, StreamManagerPool p_Y) { return(p_X.GetBlockSize().CompareTo(p_X.GetBlockSize())); }
public FreeSpaceAwarePoolChooserPolicy(List <PoolChooserPolicyPoolItem> p_Pools, StreamManagerPool p_FallbackPool) : base(p_Pools, p_FallbackPool) { }
public IPoolBuilder UseFallBackPool(StreamManagerPool p_Pool) { m_FallbackPool = p_Pool; return(this); }
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); }
protected abstract void HandleError(StreamManagerPool p_Pool, Exception p_Exception);
public ArrayMemoryBlock(StreamManagerPool p_Pool, byte[] p_Data, int p_Length) : base(p_Pool) { m_Data = p_Data; m_Length = p_Length; }
protected abstract void PoolStateChanged(StreamManagerPool p_Statistic, PoolWatcherPoolState p_OldState, PoolWatcherPoolState p_NewState);
public IPoolBuilder AddPool(StreamManagerPool p_Pool) { m_Pools.Add(p_Pool); return(this); }
protected PoolChooserPolicyBase(List <StreamManagerPool> p_Pools, StreamManagerPool p_FallbackPool) { m_FallbackPool = p_FallbackPool; m_Pools = CreatePools(p_Pools); }