public PooledByteBuffer allocate ( ) { final Queue < Slice > sliceQueue = this . sliceQueue ; final Slice slice = sliceQueue . poll ( ) ; if ( slice == null && ( maxRegions <= 0 || regionUpdater . getAndIncrement ( this ) < maxRegions ) ) { final int bufferSize = this . bufferSize ; final int buffersPerRegion = this . buffersPerRegion ; final ByteBuffer region = allocator . allocate ( buffersPerRegion * bufferSize ) ; int idx = bufferSize ; for ( int i = 1 ; i < buffersPerRegion ; i ++ ) { sliceQueue . add ( new Slice ( region , idx , bufferSize ) ) ; idx += bufferSize ; } final Slice newSlice = new Slice ( region , 0 , bufferSize ) ; return new PooledByteBuffer ( newSlice , newSlice . slice ( ) , sliceQueue ) ; } if ( slice == null ) { return null ; } return new PooledByteBuffer ( slice , slice . slice ( ) , sliceQueue ) ; }
Allocates a new byte buffer if possible