diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java b/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java index 442924306ee..c3f31a7f835 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractByteBufferPool.java @@ -64,13 +64,11 @@ abstract class AbstractByteBufferPool implements ByteBufferPool int capacity = buffer.capacity(); long maxMemory = direct ? _maxDirectMemory : _maxHeapMemory; AtomicLong memory = direct ? _directMemory : _heapMemory; - if (maxMemory <= 0) - return true; while (true) { long current = memory.get(); long value = current + capacity; - if (value > maxMemory) + if (maxMemory > 0 && value > maxMemory) return false; if (memory.compareAndSet(current, value)) return true; diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java index b1911b9b2c1..76abb023674 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ArrayByteBufferPool.java @@ -100,13 +100,13 @@ public class ArrayByteBufferPool extends AbstractByteBufferPool @Override public ByteBuffer acquire(int size, boolean direct) { + int capacity = size < _minCapacity ? size : (bucketFor(size) + 1) * getCapacityFactor(); ByteBufferPool.Bucket bucket = bucketFor(size, direct, null); if (bucket == null) - { - int capacity = size < _minCapacity ? size : (bucketFor(size) + 1) * getCapacityFactor(); return newByteBuffer(capacity, direct); - } - ByteBuffer buffer = bucket.acquire(direct); + ByteBuffer buffer = bucket.acquire(); + if (buffer == null) + return newByteBuffer(capacity, direct); decrementMemory(buffer); return buffer; } diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java index 1c58efdf444..352a1f18117 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java @@ -145,6 +145,22 @@ public interface ByteBufferPool _space = maxSize > 0 ? new AtomicInteger(maxSize) : null; } + public ByteBuffer acquire() + { + ByteBuffer buffer = queuePoll(); + if (buffer == null) + return null; + if (_space != null) + _space.incrementAndGet(); + return buffer; + } + + /** + * @param direct whether to create a direct buffer when none is available + * @return a ByteBuffer + * @deprecated use {@link #acquire()} instead + */ + @Deprecated public ByteBuffer acquire(boolean direct) { ByteBuffer buffer = queuePoll(); diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java b/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java index 0ddc2ed6300..e4448ab84ae 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/MappedByteBufferPool.java @@ -105,11 +105,14 @@ public class MappedByteBufferPool extends AbstractByteBufferPool public ByteBuffer acquire(int size, boolean direct) { int b = bucketFor(size); + int capacity = b * getCapacityFactor(); ConcurrentMap buffers = bucketsFor(direct); Bucket bucket = buffers.get(b); if (bucket == null) - return newByteBuffer(b * getCapacityFactor(), direct); - ByteBuffer buffer = bucket.acquire(direct); + return newByteBuffer(capacity, direct); + ByteBuffer buffer = bucket.acquire(); + if (buffer == null) + return newByteBuffer(capacity, direct); decrementMemory(buffer); return buffer; }