Home / Class/ MemoryRegionCache Class — netty Architecture

MemoryRegionCache Class — netty Architecture

Architecture documentation for the MemoryRegionCache class in PoolThreadCache.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  a4d6715f_8e89_41fa_05cc_d305ad068060["MemoryRegionCache"]
  2a7d96ed_6f58_dca1_f092_d9cac20e28ae["PoolThreadCache.java"]
  a4d6715f_8e89_41fa_05cc_d305ad068060 -->|defined in| 2a7d96ed_6f58_dca1_f092_d9cac20e28ae
  798cdedc_e084_f92d_45fb_9fa3c0897251["MemoryRegionCache()"]
  a4d6715f_8e89_41fa_05cc_d305ad068060 -->|method| 798cdedc_e084_f92d_45fb_9fa3c0897251
  fd5403bf_84dc_665d_0863_1a784edbddd7["initBuf()"]
  a4d6715f_8e89_41fa_05cc_d305ad068060 -->|method| fd5403bf_84dc_665d_0863_1a784edbddd7
  f0dd6d53_8cde_9782_233c_ebff914c0232["add()"]
  a4d6715f_8e89_41fa_05cc_d305ad068060 -->|method| f0dd6d53_8cde_9782_233c_ebff914c0232
  a1659e79_8236_9951_f1d0_62b19f3b2ca7["allocate()"]
  a4d6715f_8e89_41fa_05cc_d305ad068060 -->|method| a1659e79_8236_9951_f1d0_62b19f3b2ca7
  81ffb468_a88d_4b96_03a3_87154836c556["free()"]
  a4d6715f_8e89_41fa_05cc_d305ad068060 -->|method| 81ffb468_a88d_4b96_03a3_87154836c556
  562c77b5_107e_e3e8_4481_0717613d42f3["trim()"]
  a4d6715f_8e89_41fa_05cc_d305ad068060 -->|method| 562c77b5_107e_e3e8_4481_0717613d42f3
  122c5e2c_94a5_d47e_1b3c_8d56e9b6e004["freeEntry()"]
  a4d6715f_8e89_41fa_05cc_d305ad068060 -->|method| 122c5e2c_94a5_d47e_1b3c_8d56e9b6e004
  cd561301_617a_4366_3732_67cb5881a1ad["Entry()"]
  a4d6715f_8e89_41fa_05cc_d305ad068060 -->|method| cd561301_617a_4366_3732_67cb5881a1ad

Relationship Graph

Source Code

buffer/src/main/java/io/netty/buffer/PoolThreadCache.java lines 328–472

    private abstract static class MemoryRegionCache<T> {
        private final int size;
        private final Queue<Entry<T>> queue;
        private final SizeClass sizeClass;
        private int allocations;

        MemoryRegionCache(int size, SizeClass sizeClass) {
            this.size = MathUtil.safeFindNextPositivePowerOfTwo(size);
            queue = PlatformDependent.newFixedMpscUnpaddedQueue(this.size);
            this.sizeClass = sizeClass;
        }

        /**
         * Init the {@link PooledByteBuf} using the provided chunk and handle with the capacity restrictions.
         */
        protected abstract void initBuf(PoolChunk<T> chunk, ByteBuffer nioBuffer, long handle,
                                        PooledByteBuf<T> buf, int reqCapacity, PoolThreadCache threadCache);

        /**
         * Add to cache if not already full.
         */
        @SuppressWarnings("unchecked")
        public final boolean add(PoolChunk<T> chunk, ByteBuffer nioBuffer, long handle, int normCapacity) {
            Entry<T> entry = newEntry(chunk, nioBuffer, handle, normCapacity);
            boolean queued = queue.offer(entry);
            if (!queued) {
                // If it was not possible to cache the chunk, immediately recycle the entry
                entry.unguardedRecycle();
            }

            return queued;
        }

        /**
         * Allocate something out of the cache if possible and remove the entry from the cache.
         */
        public final boolean allocate(PooledByteBuf<T> buf, int reqCapacity, PoolThreadCache threadCache) {
            Entry<T> entry = queue.poll();
            if (entry == null) {
                return false;
            }
            initBuf(entry.chunk, entry.nioBuffer, entry.handle, buf, reqCapacity, threadCache);
            entry.unguardedRecycle();

            // allocations is not thread-safe which is fine as this is only called from the same thread all time.
            ++ allocations;
            return true;
        }

        /**
         * Clear out this cache and free up all previous cached {@link PoolChunk}s and {@code handle}s.
         */
        public final int free(boolean finalizer) {
            return free(Integer.MAX_VALUE, finalizer);
        }

        private int free(int max, boolean finalizer) {
            int numFreed = 0;
            for (; numFreed < max; numFreed++) {
                Entry<T> entry = queue.poll();
                if (entry != null) {
                    freeEntry(entry, finalizer);
                } else {
                    // all cleared
                    return numFreed;
                }
            }
            return numFreed;
        }

        /**
         * Free up cached {@link PoolChunk}s if not allocated frequently enough.
         */
        public final void trim() {
            int free = size - allocations;
            allocations = 0;

            // We not even allocated all the number that are
            if (free > 0) {
                free(free, false);
            }

Frequently Asked Questions

What is the MemoryRegionCache class?
MemoryRegionCache is a class in the netty codebase, defined in buffer/src/main/java/io/netty/buffer/PoolThreadCache.java.
Where is MemoryRegionCache defined?
MemoryRegionCache is defined in buffer/src/main/java/io/netty/buffer/PoolThreadCache.java at line 328.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free