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

Code before:
            IntStack localFreeList = this.localFreeList;
            if (localFreeList != null && Thread.currentThread() == ownerThread) {
                localFreeList.push(startIndex);
            } else {
                boolean segmentReturned = externalFreeList.offer(startIndex);
                assert segmentReturned : "Unable to return segment " + startIndex + " to free list";
            }
        }

        @Override
        void releaseSegment(int startIndex, int size) {
            IntStack localFreeList = this.localFreeList;
            if (localFreeList != null && Thread.currentThread() == ownerThread) {
                localFreeList.push(startIndex);
                int state = this.state;
                if (state != AVAILABLE) {
                    updateStateOnLocalReleaseSegment(state, localFreeList);
                }
            } else {
                boolean segmentReturned = externalFreeList.offer(startIndex);
                assert segmentReturned;
                // implicit StoreLoad barrier from MPSC offer()
                int state = this.state;
                if (state != AVAILABLE) {
                    handleStateOnExternalReleaseSegment(state);
                }
            }
        }

        private void handleStateOnExternalReleaseSegment(int localFreeListSize) {
            // localFreeListSize is the number of segments that were in the local free list when
            // markToDeallocate() was called. Check if all segments have been returned.
            int totalFreeSegments = localFreeListSize + externalFreeList.size();
            if (totalFreeSegments == segments) {
                if (STATE.compareAndSet(this, localFreeListSize, DEALLOCATED)) {
                    deallocate();
                }
            }
        }

        private void updateStateOnLocalReleaseSegment(int previousLocalSize, IntStack localFreeList) {
            // Update the state to reflect the new local free list size.
            int newLocalSize = localFreeList.size();
            if (!STATE.compareAndSet(this, previousLocalSize, newLocalSize)) {
                // State was concurrently changed by another external releaseSegment that deallocated.
                return;
            }
            // Check if all segments have been returned.
            int totalFreeSegments = newLocalSize + externalFreeList.size();
            if (totalFreeSegments == segments) {
                if (STATE.compareAndSet(this, newLocalSize, DEALLOCATED)) {

Review comment (refactor):
Consider adding a new helper method and reuse. 

```suggestion
    private void deallocateIfNeeded(int local) {
        // Check if all segments have been returned.
        int totalFreeSegments = local + externalFreeList.size();
        if (totalFreeSegments == segments && STATE.compareAndSet(this, local, DEALLOCATED)) {
                deallocate();
        }
    }
```

Code after:
            } else {
                boolean segmentReturned = externalFreeList.offer(startIndex);
                assert segmentReturned : "Unable to return segment " + startIndex + " to free list";
            }
        }

        @Override
        void releaseSegment(int startIndex, int size) {
            IntStack localFreeList = this.localFreeList;
            if (localFreeList != null && Thread.currentThread() == ownerThread) {
                localFreeList.push(startIndex);
                int state = this.state;
                if (state != AVAILABLE) {
                    updateStateOnLocalReleaseSegment(state, localFreeList);
                }
            } else {
                boolean segmentReturned = externalFreeList.offer(startIndex);
                assert segmentReturned;
                // implicit StoreLoad barrier from MPSC offer()
                int state = this.state;
                if (state != AVAILABLE) {
                    deallocateIfNeeded(state);
                }
            }
        }

        private void updateStateOnLocalReleaseSegment(int previousLocalSize, IntStack localFreeList) {
            int newLocalSize = localFreeList.size();
            boolean alwaysTrue = STATE.compareAndSet(this, previousLocalSize, newLocalSize);
            assert alwaysTrue : "this shouldn't happen unless double release in the local free list";
            deallocateIfNeeded(newLocalSize);
        }

        private void deallocateIfNeeded(int localSize) {
            // Check if all segments have been returned.
            int totalFreeSegments = localSize + externalFreeList.size();
            if (totalFreeSegments == segments && STATE.compareAndSet(this, localSize, DEALLOCATED)) {
                deallocate();
            }
        }

        @Override
        void markToDeallocate() {
            IntStack localFreeList = this.localFreeList;
            int localSize = localFreeList != null ? localFreeList.size() : 0;
            STATE.set(this, localSize);
            deallocateIfNeeded(localSize);
        }
    }

    private static final class BuddyChunk extends Chunk implements IntConsumer {