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

Code before:
                    // 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();
                }
            }
        }

        private Chunk newChunkAllocation(int promptingSize) {
            int size = Math.max(promptingSize * BUFS_PER_CHUNK, preferredChunkSize());
            ChunkAllocator chunkAllocator = parent.chunkAllocator;
            return new Chunk(chunkAllocator.allocate(size, size), this, true);
        }

        boolean trySetNextInLine(Chunk chunk) {
            return NEXT_IN_LINE.compareAndSet(this, null, chunk);
        }

Review comment (suggestion):
```suggestion
        private void transferChunk(Chunk current) {
```

Code after:
            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();
                    }
                }
            }
            return true;
        }

        private void restoreMagazineFreed() {
            Chunk next = NEXT_IN_LINE.getAndSet(this, MAGAZINE_FREED);
            if (next != null && next != MAGAZINE_FREED) {
                next.release(); // A chunk snuck in through a race. Release it after restoring MAGAZINE_FREED state.
            }
        }

        private void transferChunk(Chunk current) {
            if (NEXT_IN_LINE.compareAndSet(this, null, current)
                    || parent.offerToQueue(current)) {