public static PooledBufferHolder GetPooledOrNew(int byteCount, bool useGlobalPool = false) { if (byteCount > 1 << 16) { throw new IndexOutOfRangeException("byteCount is greater than max allowed value"); } byte byteCountPowerOf2 = Log2BitPosition(byteCount); PooledBufferHolder obj; if (useGlobalPool) { lock (sharedBufferPool) { obj = GetPooledInternal(byteCountPowerOf2, sharedBufferPool); } } else { if (null == bufferPool) { bufferPool = CreateBufferPool(); } obj = GetPooledInternal(byteCountPowerOf2, bufferPool); } if (null == obj) { obj = new PooledBufferHolder(new byte[1 << byteCountPowerOf2], byteCountPowerOf2, useGlobalPool); } return(obj); }
internal BitBuffer(PooledBufferHolder pooledBuffeHolder, int length) : this() { this.pooledBufferHolder = pooledBuffeHolder; lifeCounter = pooledBuffeHolder.lifeCounter; data = pooledBuffeHolder.buffer; absLength = length; }
/// <summary> /// Get a buffer from the pool. It should be recycled later calling at <see cref="Recycle"/> /// </summary> /// <param name="byteCountPowerOf2">minimum byte count = 1 << this</param> /// <param name="useGlobalPool">Use a pool unique for each thread (false) or a shared pool between threads (true)</param> public static BitBuffer GetPooled(int bitCount, bool useGlobalPool = false) { int byteCount = bitCount / 8; if ((bitCount & 0x7) != 0) { byteCount += 1; } var obj = PooledBufferHolder.GetPooledOrNew(byteCount, useGlobalPool); return(new BitBuffer(obj, bitCount)); }
/// <summary> /// Get a buffer from the pool. It should be recycled later calling at <see cref="Recycle"/> /// </summary> /// <param name="byteCountPowerOf2">minimum byte count = 1 << this</param> /// <param name="useGlobalPool">Use a pool unique for each thread (false) or a shared pool between threads (true)</param> public static ByteBuffer GetPooled(int byteCount, bool useGlobalPool = false) { var obj = PooledBufferHolder.GetPooledOrNew(byteCount, useGlobalPool); return(new ByteBuffer(obj, byteCount)); }