Skip to content

Commit

Permalink
Fix errors in Mapped pool and javadoc (#8264)
Browse files Browse the repository at this point in the history
* Fix errors in javadoc
* Further RBBP improvements
  • Loading branch information
gregw committed Jul 7, 2022
1 parent c6264ab commit c1c2bdb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ public interface ByteBufferPool
{
/**
* <p>Requests a {@link ByteBuffer} of the given size.</p>
* <p>The returned buffer may have a bigger capacity than the size being
* requested but it will have the limit set to the given size.</p>
* <p>The returned buffer may have a bigger capacity than the size being requested.</p>
*
* @param size the size of the buffer
* @param direct whether the buffer must be direct or not
* @return the requested buffer
* @return a buffer with at least the requested capacity, with position and limit set to 0.
* @see #release(ByteBuffer)
*/
ByteBuffer acquire(int size, boolean direct);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ private MappedByteBufferPool(int factor, int maxBucketSize, Function<Integer, Bu
_newBucket = newBucket;
}

@Override
protected RetainableByteBufferPool newRetainableByteBufferPool(int factor, int maxCapacity, int maxBucketSize, long retainedHeapMemory, long retainedDirectMemory)
{
return new Retained(factor, maxCapacity, maxBucketSize, retainedHeapMemory, retainedDirectMemory);
}

private Bucket newBucket(int key, boolean direct)
{
return (_newBucket != null) ? _newBucket.apply(key) : new Bucket(capacityFor(key), getMaxBucketSize(), updateMemory(direct));
Expand Down Expand Up @@ -302,4 +308,30 @@ public String toString()
getMaxBucketSize(),
getCapacityFactor());
}

protected class Retained extends ArrayRetainableByteBufferPool
{
public Retained(int factor, int maxCapacity, int maxBucketSize, long retainedHeapMemory, long retainedDirectMemory)
{
super(0, factor, maxCapacity, maxBucketSize, retainedHeapMemory, retainedDirectMemory);
}

@Override
protected ByteBuffer allocate(int capacity)
{
return MappedByteBufferPool.this.acquire(capacity, false);
}

@Override
protected ByteBuffer allocateDirect(int capacity)
{
return MappedByteBufferPool.this.acquire(capacity, true);
}

@Override
protected void removed(RetainableByteBuffer retainedBuffer)
{
MappedByteBufferPool.this.release(retainedBuffer.getBuffer());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,49 @@ public interface RetainableByteBufferPool
* Acquires a memory buffer from the pool.
* @param size The size of the buffer. The returned buffer will have at least this capacity.
* @param direct true if a direct memory buffer is needed, false otherwise.
* @return a memory buffer.
* @return a memory buffer with position and size set to 0.
*/
RetainableByteBuffer acquire(int size, boolean direct);

void clear();

static RetainableByteBufferPool from(ByteBufferPool byteBufferPool)
{
return new RetainableByteBufferPool()
return new NotRetainedByteBufferPool(byteBufferPool);
}

class NotRetainedByteBufferPool implements RetainableByteBufferPool
{
private final ByteBufferPool _byteBufferPool;

public NotRetainedByteBufferPool(ByteBufferPool byteBufferPool)
{
@Override
public RetainableByteBuffer acquire(int size, boolean direct)
{
ByteBuffer byteBuffer = byteBufferPool.acquire(size, direct);
RetainableByteBuffer retainableByteBuffer = new RetainableByteBuffer(byteBuffer, this::release);
retainableByteBuffer.acquire();
return retainableByteBuffer;
}
_byteBufferPool = byteBufferPool;
}

private void release(RetainableByteBuffer retainedBuffer)
{
byteBufferPool.release(retainedBuffer.getBuffer());
}
@Override
public RetainableByteBuffer acquire(int size, boolean direct)
{
ByteBuffer byteBuffer = _byteBufferPool.acquire(size, direct);
RetainableByteBuffer retainableByteBuffer = new RetainableByteBuffer(byteBuffer, this::release);
retainableByteBuffer.acquire();
return retainableByteBuffer;
}

@Override
public void clear()
{
}
private void release(RetainableByteBuffer retainedBuffer)
{
_byteBufferPool.release(retainedBuffer.getBuffer());
}

@Override
public void clear()
{
}

@Override
public String toString()
{
return String.format("NonRetainableByteBufferPool@%x{%s}", hashCode(), byteBufferPool.toString());
}
};
@Override
public String toString()
{
return String.format("NonRetainableByteBufferPool@%x{%s}", hashCode(), _byteBufferPool.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public AbstractConnector(
}
}
_byteBufferPool = pool;
addBean(pool.asRetainableByteBufferPool());

addEventListener(new Container.Listener()
{
Expand Down

0 comments on commit c1c2bdb

Please sign in to comment.