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

Code before:
            int index = (int) (threadId & mask);
            for (int i = 0, m = Integer.numberOfTrailingZeros(~mask); i < m; i++) {
                Magazine mag = mags[index + i & mask];
                if (mag.tryAllocate(size, sizeBucket, maxCapacity, buf)) {
                    // Was able to allocate.
                    return true;
                }
            }
            expansions++;
        } while (expansions <= EXPANSION_ATTEMPTS && tryExpandMagazines(mags.length));
        return false;
    }

    /**
     * Allocate into the given buffer. Used by {@link AdaptiveByteBuf#capacity(int)}.
     */
    void allocate(int size, int maxCapacity, AdaptiveByteBuf into) {
        Magazine magazine = into.chunk.magazine;
        if (!allocate(size, maxCapacity, Thread.currentThread(), into)) {
            // Create a one-off chunk for this allocation as the previous allocate call did not work out.
            AbstractByteBuf innerChunk = chunkAllocator.allocate(size, maxCapacity);
            Chunk chunk = new Chunk(innerChunk, magazine, false);
            try {
                chunk.readInitInto(into, size, maxCapacity);
            } finally {
                // As the chunk is an one-off we need to always call release explicitly as readInitInto(...)
                // will take care of retain once when successful. Once The AdaptiveByteBuf is released it will
                // completely release the Chunk and so the contained innerChunk.
                chunk.release();
            }
        }
    }

    @Override
    public long usedMemory() {
        long sum = 0;
        for (Chunk chunk : centralQueue) {
            sum += chunk.capacity();
        }
        for (Magazine magazine : magazines) {
            sum += magazine.usedMemory.get();
        }
        if (liveCachedMagazines != null) {
            for (Magazine magazine : liveCachedMagazines) {
                sum += magazine.usedMemory.get();
            }
        }
        return sum;
    }

    private boolean tryExpandMagazines(int currentLength) {

Review comment (suggestion):
imho this usage is really odd as if this methods returns non null its a `ByteBuf` which has a lifetime. I know that this would just be the same as `into` and so its fine to not release but this is more an implementation detail and very confusing and fragile. 

Code after:
                        return buf;
                    }
                }
                expansions++;
            } while (expansions <= EXPANSION_ATTEMPTS && tryExpandMagazines(mags.length));
        }

        // The magazines failed us, or the buffer is too big to be pooled.
        return allocateFallback(size, maxCapacity, currentThread, buf);
    }

    private AdaptiveByteBuf allocateFallback(int size, int maxCapacity, Thread currentThread, AdaptiveByteBuf buf) {
        // If we don't already have a buffer, obtain one from the most conveniently available magazine.
        Magazine magazine;
        if (buf != null) {
            Chunk chunk = buf.chunk;
            magazine = chunk != null && chunk != Magazine.MAGAZINE_FREED?
                    chunk.magazine : getFallbackMagazine(currentThread);
        } else {
            magazine = getFallbackMagazine(currentThread);
            buf = magazine.newBuffer();
        }
        // Create a one-off chunk for this allocation.
        AbstractByteBuf innerChunk = chunkAllocator.allocate(size, maxCapacity);
        Chunk chunk = new Chunk(innerChunk, magazine, false);
        try {
            chunk.readInitInto(buf, size, maxCapacity);
        } finally {
            // As the chunk is an one-off we need to always call release explicitly as readInitInto(...)
            // will take care of retain once when successful. Once The AdaptiveByteBuf is released it will
            // completely release the Chunk and so the contained innerChunk.
            chunk.release();
        }
        return buf;
    }

    private Magazine getFallbackMagazine(Thread currentThread) {
        Object tlMag;
        FastThreadLocal<Object> threadLocalMagazine = this.threadLocalMagazine;
        if (threadLocalMagazine != null &&
                currentThread instanceof FastThreadLocalThread &&
                (tlMag = threadLocalMagazine.get()) != NO_MAGAZINE) {
            return (Magazine) tlMag;
        }
        Magazine[] mags = magazines;
        return mags[(int) currentThread.getId() & mags.length - 1];
    }

    /**
     * Allocate into the given buffer. Used by {@link AdaptiveByteBuf#capacity(int)}.
     */