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

Code before:
            if (curr.capacity() > size) {
                result = curr.split(size);
            } else if (curr.capacity() == size) {
                result = curr;
                current = null;
            } else {
                Buffer buffer = newChunkAllocation(size);
                result = buffer.split(size);
                if (curr.capacity() < RETIRE_CAPACITY) {
                    curr.close();
                    current = buffer;
                } else {
                    transferChunk(buffer);
                }
            }
            return result;
        }

        private void transferChunk(Buffer current) {
            if (NEXT_IN_LINE.compareAndSet(this, null, current)
                    || parent.offerToQueue(current)) {
                return;
            }

            Buffer nextChunk = NEXT_IN_LINE.get(this);
            if (current.capacity() > nextChunk.capacity()) {
                if (NEXT_IN_LINE.compareAndSet(this, nextChunk, current)) {
                    nextChunk.close();
                    return;
                }
            }
            // 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.close();
        }

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

        private Buffer newChunkAllocation(int promptingSize) {
            int size = Math.max(promptingSize * BUFS_PER_CHUNK, preferredChunkSize());
            return parent.manager.allocateShared(parent.allocatorControl, size, this::decorate, parent.allocationType);
        }

        private Drop<Buffer> decorate(Drop<Buffer> drop) {
            if (drop instanceof ArcDrop) {
                drop = ((ArcDrop<Buffer>) drop).unwrap();

Review comment (suggestion):
I think we should add a null check
```suggestion
            if (nextChunk != null && current.capacity() > nextChunk.capacity()) {
```

Code after:
            if (curr.capacity() > size) {
                result = curr.split(size);
            } else if (curr.capacity() == size) {
                result = curr;
                current = null;
            } else {
                Buffer buffer = newChunkAllocation(size);
                result = buffer.split(size);
                if (curr.capacity() < RETIRE_CAPACITY) {
                    curr.close();
                    current = buffer;
                } else {
                    transferChunk(buffer);
                }
            }
            return result;
        }

        private void transferChunk(Buffer current) {
            if (NEXT_IN_LINE.compareAndSet(this, null, current)
                    || parent.offerToQueue(current)) {
                return;
            }

            Buffer nextChunk = (Buffer) NEXT_IN_LINE.get(this);
            if (nextChunk != null && current.capacity() > nextChunk.capacity()) {
                if (NEXT_IN_LINE.compareAndSet(this, nextChunk, current)) {
                     if (nextChunk != MAGAZINE_FREED) {
                        nextChunk.close();
                    }
                    return;
                }
            }
            // 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.close();
        }

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

        private Buffer newChunkAllocation(int promptingSize) {
            int size = Math.max(promptingSize * BUFS_PER_CHUNK, preferredChunkSize());
            return parent.manager.allocateShared(parent.allocatorControl, size, this::decorate, parent.allocationType);
        }

        private Drop<Buffer> decorate(Drop<Buffer> drop) {