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

Code before:
                restoreMagazineFreed();
                return false;
            }
            if (curr == null) {
                curr = parent.centralQueue.poll();
                if (curr == null) {
                    return false;
                }
                curr.attachToMagazine(this);
            }
            if (curr.remainingCapacity() >= size) {
                curr.readInitInto(buf, size, maxCapacity);
            }
            try {
                if (curr.remainingCapacity() < RETIRE_CAPACITY) {
                    curr.release();
                } else {
                    transferToNextInLineOrRelease(curr);
                }
                curr = null;
            } finally {
                if (curr != null) {
                    // Something went wrong, let's ensure we not leak the 'curr' chunk.
                    curr.release();
                }
            }
            return true;
        }

        private boolean allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) {
            recordAllocationSize(sizeBucket);
            Chunk curr = current;
            if (curr != null) {
                // We have a Chunk that has some space left.
                if (curr.remainingCapacity() > size) {
                    curr.readInitInto(buf, size, maxCapacity);
                    // We still have some bytes left that we can use for the next allocation, just early return.
                    return true;
                }

                // At this point we know that this will be the last time current will be used, so directly set it to
                // null and release it once we are done.
                current = null;
                if (curr.remainingCapacity() == size) {
                    try {
                        curr.readInitInto(buf, size, maxCapacity);
                        return true;
                    } finally {
                        curr.release();
                    }
                }

Review comment (suggestion):
```suggestion
            try {
                if (curr.remainingCapacity() >= RETIRE_CAPACITY) {
                    transferToNextInLineOrRelease(curr);
                    cur = null;
                }
            } finally {
                if (curr != null) {
                    curr.release();
                }
            }
```

Code after:
                restoreMagazineFreed();
                return false;
            }
            if (curr == null) {
                curr = parent.centralQueue.poll();
                if (curr == null) {
                    return false;
                }
                curr.attachToMagazine(this);
            }
            if (curr.remainingCapacity() >= size) {
                curr.readInitInto(buf, size, maxCapacity);
            }
            try {
                if (curr.remainingCapacity() >= RETIRE_CAPACITY) {
                    transferToNextInLineOrRelease(curr);
                    curr = null;
                }
            } finally {
                if (curr != null) {
                    curr.release();
                }
            }
            return true;
        }

        private boolean allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) {
            recordAllocationSize(sizeBucket);
            Chunk curr = current;
            if (curr != null) {
                // We have a Chunk that has some space left.
                if (curr.remainingCapacity() > size) {
                    curr.readInitInto(buf, size, maxCapacity);
                    // We still have some bytes left that we can use for the next allocation, just early return.
                    return true;
                }

                // At this point we know that this will be the last time current will be used, so directly set it to
                // null and release it once we are done.
                current = null;
                if (curr.remainingCapacity() == size) {
                    try {
                        curr.readInitInto(buf, size, maxCapacity);
                        return true;
                    } finally {
                        curr.release();
                    }
                }

                // Check if we either retain the chunk in the nextInLine cache or releasing it.
                if (curr.remainingCapacity() < RETIRE_CAPACITY) {