Home / Class/ RefCnt Class — netty Architecture

RefCnt Class — netty Architecture

Architecture documentation for the RefCnt class in RefCnt.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  9f3f6929_64f5_00fb_95fc_73dbc8960435["RefCnt"]
  532a5dc8_b6b3_1a54_7fbd_94734ca113fd["RefCnt.java"]
  9f3f6929_64f5_00fb_95fc_73dbc8960435 -->|defined in| 532a5dc8_b6b3_1a54_7fbd_94734ca113fd
  f9aed3a3_565e_3d5d_98a6_00a872fe33b7["RefCnt()"]
  9f3f6929_64f5_00fb_95fc_73dbc8960435 -->|method| f9aed3a3_565e_3d5d_98a6_00a872fe33b7
  f7a52891_9536_020e_129c_232b9aa92b42["refCnt()"]
  9f3f6929_64f5_00fb_95fc_73dbc8960435 -->|method| f7a52891_9536_020e_129c_232b9aa92b42
  f5c4d93d_2551_72f4_aa6a_80da4520d3f6["retain()"]
  9f3f6929_64f5_00fb_95fc_73dbc8960435 -->|method| f5c4d93d_2551_72f4_aa6a_80da4520d3f6
  24443a32_86a2_b72d_c75d_dc986162786b["release()"]
  9f3f6929_64f5_00fb_95fc_73dbc8960435 -->|method| 24443a32_86a2_b72d_c75d_dc986162786b
  76f11d1e_7572_363c_3f5a_dda142d3104d["isLiveNonVolatile()"]
  9f3f6929_64f5_00fb_95fc_73dbc8960435 -->|method| 76f11d1e_7572_363c_3f5a_dda142d3104d
  82b842f8_2748_4fe0_2636_484c86559cbf["setRefCnt()"]
  9f3f6929_64f5_00fb_95fc_73dbc8960435 -->|method| 82b842f8_2748_4fe0_2636_484c86559cbf
  ed7c5701_74e9_8adc_cde1_618b30cb1b87["resetRefCnt()"]
  9f3f6929_64f5_00fb_95fc_73dbc8960435 -->|method| ed7c5701_74e9_8adc_cde1_618b30cb1b87
  430389be_c1ee_77a2_9ce8_43f5fe849582["throwIllegalRefCountOnRelease()"]
  9f3f6929_64f5_00fb_95fc_73dbc8960435 -->|method| 430389be_c1ee_77a2_9ce8_43f5fe849582

Relationship Graph

Source Code

common/src/main/java/io/netty/util/internal/RefCnt.java lines 31–476

@SuppressWarnings("deprecation")
public final class RefCnt {

    private static final int UNSAFE = 0;
    private static final int VAR_HANDLE = 1;
    private static final int ATOMIC_UPDATER = 2;
    private static final int REF_CNT_IMPL;

    static {
        if (PlatformDependent.hasUnsafe()) {
            REF_CNT_IMPL = UNSAFE;
        } else if (PlatformDependent.hasVarHandle()) {
            REF_CNT_IMPL = VAR_HANDLE;
        } else {
            REF_CNT_IMPL = ATOMIC_UPDATER;
        }
    }

    /*
     * Implementation notes:
     *
     * For the updated int field:
     *   Even => "real" refcount is (refCnt >>> 1)
     *   Odd  => "real" refcount is 0
     *
     * This field is package-private so that the AtomicRefCnt implementation can reach it, even on native-image.
     */
    volatile int value;

    public RefCnt() {
        switch (REF_CNT_IMPL) {
        case UNSAFE:
            UnsafeRefCnt.init(this);
            break;
        case VAR_HANDLE:
            VarHandleRefCnt.init(this);
            break;
        case ATOMIC_UPDATER:
        default:
            AtomicRefCnt.init(this);
            break;
        }
    }

    /**
     * Returns the current reference count of the given {@code RefCnt} instance with a load acquire semantic.
     *
     * @param ref the target RefCnt instance
     * @return the reference count
     */
    public static int refCnt(RefCnt ref) {
        switch (REF_CNT_IMPL) {
        case UNSAFE:
            return UnsafeRefCnt.refCnt(ref);
        case VAR_HANDLE:
            return VarHandleRefCnt.refCnt(ref);
        case ATOMIC_UPDATER:
        default:
            return AtomicRefCnt.refCnt(ref);
        }
    }

    /**
     * Increases the reference count of the given {@code RefCnt} instance by 1.
     *
     * @param ref the target RefCnt instance
     */
    public static void retain(RefCnt ref) {
        switch (REF_CNT_IMPL) {
        case UNSAFE:
            UnsafeRefCnt.retain(ref);
            break;
        case VAR_HANDLE:
            VarHandleRefCnt.retain(ref);
            break;
        case ATOMIC_UPDATER:
        default:
            AtomicRefCnt.retain(ref);
            break;
        }
    }

Frequently Asked Questions

What is the RefCnt class?
RefCnt is a class in the netty codebase, defined in common/src/main/java/io/netty/util/internal/RefCnt.java.
Where is RefCnt defined?
RefCnt is defined in common/src/main/java/io/netty/util/internal/RefCnt.java at line 31.

Analyze Your Own Codebase

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

Try Supermodel Free