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

Code before:

        void attachToMagazine(Magazine magazine) {
            assert this.magazine == null;
            this.magazine = magazine;
        }

        /**
         * Called when a magazine is done using this chunk, probably because it was emptied.
         */
        boolean releaseFromMagazine() {
            // Chunks can be reused before they become empty.
            // We can therefor put them in the shared queue as soon as the magazine is done with this chunk.
            Magazine mag = magazine;
            detachFromMagazine();
            if (!mag.offerToQueue(this)) {
                return release();
            }
            return false;
        }

        /**
         * Called when a ByteBuf is done using its allocation in this chunk.
         */
        void releaseSegment(int ignoredSegmentId, int size) {
            release();
        }

        void markToDeallocate() {
            release();
        }

        private void retain() {
                RefCnt.retain(refCnt);
        }

        protected boolean release() {
            boolean deallocate = RefCnt.release(refCnt);
            if (deallocate) {
                deallocate();
            }
            return deallocate;
        }

        protected void deallocate() {
            onRelease();
            allocator.chunkRegistry.remove(this);
            delegate.release();
        }

        private void onRelease() {
            if (PlatformDependent.isJfrEnabled() && FreeChunkEvent.isEventEnabled()) {

Review comment (suggestion):
Could `Chunk.releaseFromMagazine()` call this instead of calling `release()` directly? Then we wouldn't need a nearly identical `releaseFromMagazine` override in `SizeClassedChunk`.

Code after:
        void attachToMagazine(Magazine magazine) {
            assert this.magazine == null;
            this.magazine = magazine;
        }

        /**
         * Called when a magazine is done using this chunk, probably because it was emptied.
         */
        void releaseFromMagazine() {
            // Chunks can be reused before they become empty.
            // We can therefor put them in the shared queue as soon as the magazine is done with this chunk.
            Magazine mag = magazine;
            detachFromMagazine();
            if (!mag.offerToQueue(this)) {
                markToDeallocate();
            }
        }

        /**
         * Called when a ByteBuf is done using its allocation in this chunk.
         */
        void releaseSegment(int ignoredSegmentId, int size) {
            release();
        }

        void markToDeallocate() {
            release();
        }

        private void retain() {
                RefCnt.retain(refCnt);
        }

        protected boolean release() {
            boolean deallocate = RefCnt.release(refCnt);
            if (deallocate) {
                deallocate();
            }
            return deallocate;
        }

        protected void deallocate() {
            onRelease();
            allocator.chunkRegistry.remove(this);
            delegate.release();
        }

        private void onRelease() {
            if (PlatformDependent.isJfrEnabled() && FreeChunkEvent.isEventEnabled()) {
                FreeChunkEvent event = new FreeChunkEvent();
                if (event.shouldCommit()) {