Repository: netty/netty
File: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java (Java)

Code before:
                if (last.remainingCapacity() < RETIRE_CAPACITY) {
                    last.release();
                } else {
                    transChunk(last);
                }
            }
            if (curr.remainingCapacity() > size) {
                curr.readInitInto(buf, size, maxCapacity);
            } else if (curr.remainingCapacity() == size) {
                try {
                    curr.readInitInto(buf, size, maxCapacity);
                } finally {
                    // Release in a finally block so even if readInitInto(...) would throw we would still correctly
                    // release the current chunk before null it out.
                    curr.release();
                    current = null;
                }
            } else {
                Chunk newChunk = newChunkAllocation(size);
                try {
                    newChunk.readInitInto(buf, size, maxCapacity);
                    if (curr.remainingCapacity() < RETIRE_CAPACITY) {
                        curr.release();
                        current = newChunk;
                    } else {
                        transChunk(newChunk);
                    }
                    newChunk = null;
                } finally {
                    if (newChunk != null) {
                        assert current == null;
                        // Something went wrong, let's ensure we not leak the newChunk.
                        newChunk.release();
                    }
                }
            }
        }

        private void transChunk(Chunk current) {
            if (NEXT_IN_LINE.compareAndSet(this, null, current)
                    || parent.offerToQueue(current)) {
                return;
            }
            Chunk nextChunk = NEXT_IN_LINE.get(this);
            if (current.remainingCapacity() > nextChunk.remainingCapacity()) {
                if (NEXT_IN_LINE.compareAndSet(this, nextChunk, current)) {
                    nextChunk.release();
                } else {
                    // Next-in-line is occupied AND the central queue is full.
                    // Rare that we should get here, but we'll only do one allocation out of this chunk, then.
                    current.release();

Review comment (suggestion):
```suggestion
                        transferChunk(newChunk);
```

Code after:
                curr = NEXT_IN_LINE.getAndSet(this, null);
                if (curr == MAGAZINE_FREED) {
                    // Allocation raced with a stripe-resize that freed this magazine.
                    restoreMagazineFreed();
                    return false;
                }
            } else {
                curr = parent.centralQueue.poll();
                if (curr == null) {
                    curr = newChunkAllocation(size);
                }
            }
            current = curr;
            assert current != null;
            if (last != null) {
                if (last.remainingCapacity() < RETIRE_CAPACITY) {
                    last.release();
                } else {
                    transferChunk(last);
                }
            }
            if (curr.remainingCapacity() > size) {
                curr.readInitInto(buf, size, maxCapacity);
            } else if (curr.remainingCapacity() == size) {
                try {
                    curr.readInitInto(buf, size, maxCapacity);
                } finally {
                    // Release in a finally block so even if readInitInto(...) would throw we would still correctly
                    // release the current chunk before null it out.
                    curr.release();
                    current = null;
                }
            } else {
                Chunk newChunk = newChunkAllocation(size);
                try {
                    newChunk.readInitInto(buf, size, maxCapacity);
                    if (curr.remainingCapacity() < RETIRE_CAPACITY) {
                        curr.release();
                        current = newChunk;
                    } else {
                        transferChunk(newChunk);
                    }
                    newChunk = null;
                } finally {
                    if (newChunk != null) {
                        assert current == null;
                        // Something went wrong, let's ensure we not leak the newChunk.
                        newChunk.release();
                    }
                }
            }