diff --git a/buffer/src/main/java/io/netty/buffer/PoolArena.java b/buffer/src/main/java/io/netty/buffer/PoolArena.java index a9ae9ddc26..6275f9d9e6 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolArena.java +++ b/buffer/src/main/java/io/netty/buffer/PoolArena.java @@ -144,8 +144,8 @@ abstract class PoolArena { table = smallSubpagePools; } + final PoolSubpage head = table[tableIdx]; synchronized (this) { - final PoolSubpage head = table[tableIdx]; final PoolSubpage s = head.next; if (s != head) { assert s.doNotDestroy && s.elemSize == normCapacity; @@ -154,21 +154,25 @@ abstract class PoolArena { s.chunk.initBufWithSubpage(buf, handle, reqCapacity); return; } + allocateNormal(buf, reqCapacity, normCapacity); + return; } - } else if (normCapacity <= chunkSize) { + } + if (normCapacity <= chunkSize) { if (cache.allocateNormal(this, buf, reqCapacity, normCapacity)) { // was able to allocate out of the cache so move on return; } + synchronized (this) { + allocateNormal(buf, reqCapacity, normCapacity); + } } else { // Huge allocations are never served via the cache so just call allocateHuge allocateHuge(buf, reqCapacity); - return; } - allocateNormal(buf, reqCapacity, normCapacity); } - private synchronized void allocateNormal(PooledByteBuf buf, int reqCapacity, int normCapacity) { + private void allocateNormal(PooledByteBuf buf, int reqCapacity, int normCapacity) { if (q050.allocate(buf, reqCapacity, normCapacity) || q025.allocate(buf, reqCapacity, normCapacity) || q000.allocate(buf, reqCapacity, normCapacity) || qInit.allocate(buf, reqCapacity, normCapacity) || q075.allocate(buf, reqCapacity, normCapacity) || q100.allocate(buf, reqCapacity, normCapacity)) {