public byte[] GetBuffer()
 {
     if (_availableCells > 0)
     {
         MemoryManager.MemoryCell cellsListHeadCell = null;
         lock (_cellsListLock)
         {
             if (_availableCells > 0)
             {
                 if (_cellsListHeadCell != null)
                 {
                     cellsListHeadCell          = _cellsListHeadCell;
                     _cellsListHeadCell         = cellsListHeadCell.NextCell;
                     cellsListHeadCell.NextCell = null;
                 }
                 else
                 {
                     cellsListHeadCell = new MemoryManager.MemoryCell(BufferSize);
                     _allocatedCells++;
                 }
                 _availableCells--;
             }
         }
         if (cellsListHeadCell != null)
         {
             _cellsInUse.TryAdd(cellsListHeadCell.Buffer, cellsListHeadCell);
             return(cellsListHeadCell.Buffer);
         }
     }
     return(null);
 }
 public MemoryPool(int cellsCount, int bufferSize)
 {
     BufferSize         = bufferSize;
     _availableCells    = cellsCount;
     _allocatedCells    = 0;
     _cellsListLock     = new object();
     _cellsListHeadCell = null;
     _cellsInUse        = new ConcurrentDictionary <byte[], MemoryManager.MemoryCell>();
 }
 public void ReleaseUnusedBuffers()
 {
     lock (_cellsListLock)
     {
         while (_cellsListHeadCell != null)
         {
             MemoryManager.MemoryCell cell = _cellsListHeadCell;
             _cellsListHeadCell = _cellsListHeadCell.NextCell;
             cell.NextCell      = null;
         }
     }
 }
 public void AddBuffer(byte[] buffer)
 {
     MemoryManager.MemoryCell cell;
     if (buffer == null)
     {
         throw new ArgumentNullException("buffer");
     }
     if (buffer.Length != BufferSize)
     {
         throw new ArgumentException("Buffer is not of right size. Expected size " + BufferSize, "buffer");
     }
     if (!_cellsInUse.TryRemove(buffer, out cell))
     {
         throw new ArgumentException("Buffer not created by this pool", "buffer");
     }
     lock (_cellsListLock)
     {
         cell.NextCell      = _cellsListHeadCell;
         _cellsListHeadCell = cell;
         _availableCells++;
     }
 }
 public void ReleaseUnusedBuffers()
 {
     lock (_cellsListLock)
     {
         while (_cellsListHeadCell != null)
         {
             MemoryManager.MemoryCell cell = _cellsListHeadCell;
             _cellsListHeadCell = _cellsListHeadCell.NextCell;
             cell.NextCell = null;
         }
     }
 }
 public byte[] GetBuffer()
 {
     if (_availableCells > 0)
     {
         MemoryManager.MemoryCell cellsListHeadCell = null;
         lock (_cellsListLock)
         {
             if (_availableCells > 0)
             {
                 if (_cellsListHeadCell != null)
                 {
                     cellsListHeadCell = _cellsListHeadCell;
                     _cellsListHeadCell = cellsListHeadCell.NextCell;
                     cellsListHeadCell.NextCell = null;
                 }
                 else
                 {
                     cellsListHeadCell = new MemoryManager.MemoryCell(BufferSize);
                     _allocatedCells++;
                 }
                 _availableCells--;
             }
         }
         if (cellsListHeadCell != null)
         {
             _cellsInUse.TryAdd(cellsListHeadCell.Buffer, cellsListHeadCell);
             return cellsListHeadCell.Buffer;
         }
     }
     return null;
 }
 public void AddBuffer(byte[] buffer)
 {
     MemoryManager.MemoryCell cell;
     if (buffer == null)
     {
         throw new ArgumentNullException("buffer");
     }
     if (buffer.Length != BufferSize)
     {
         throw new ArgumentException("Buffer is not of right size. Expected size " + BufferSize, "buffer");
     }
     if (!_cellsInUse.TryRemove(buffer, out cell))
     {
         throw new ArgumentException("Buffer not created by this pool", "buffer");
     }
     lock (_cellsListLock)
     {
         cell.NextCell = _cellsListHeadCell;
         _cellsListHeadCell = cell;
         _availableCells++;
     }
 }
 public MemoryPool(int cellsCount, int bufferSize)
 {
     BufferSize = bufferSize;
     _availableCells = cellsCount;
     _allocatedCells = 0;
     _cellsListLock = new object();
     _cellsListHeadCell = null;
     _cellsInUse = new ConcurrentDictionary<byte[], MemoryManager.MemoryCell>();
 }