Home / Class/ MsgHdrMemory Class — netty Architecture

MsgHdrMemory Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  99ae51db_2948_44c9_7279_d8199f61277a["MsgHdrMemory"]
  b1c6cfdb_5397_6188_28b3_236ed92d3f2f["MsgHdrMemory.java"]
  99ae51db_2948_44c9_7279_d8199f61277a -->|defined in| b1c6cfdb_5397_6188_28b3_236ed92d3f2f
  e22439ae_a268_414c_0783_1a3ef6e6eefe["MsgHdrMemory()"]
  99ae51db_2948_44c9_7279_d8199f61277a -->|method| e22439ae_a268_414c_0783_1a3ef6e6eefe
  899d881a_ca7c_5bd1_9491_b5d24a2288d3["set()"]
  99ae51db_2948_44c9_7279_d8199f61277a -->|method| 899d881a_ca7c_5bd1_9491_b5d24a2288d3
  22809dce_8e11_9748_9050_4e22fcc285be["setScmRightsFd()"]
  99ae51db_2948_44c9_7279_d8199f61277a -->|method| 22809dce_8e11_9748_9050_4e22fcc285be
  ca02836a_c371_f474_2d14_67a4e089fae7["getScmRightsFd()"]
  99ae51db_2948_44c9_7279_d8199f61277a -->|method| ca02836a_c371_f474_2d14_67a4e089fae7
  3591e3d1_138d_7bd6_bfd4_1d18c57f0c11["prepRecvReadFd()"]
  99ae51db_2948_44c9_7279_d8199f61277a -->|method| 3591e3d1_138d_7bd6_bfd4_1d18c57f0c11
  b4cbc5e1_8daf_5d2a_22db_810c16ea38b5["hasPort()"]
  99ae51db_2948_44c9_7279_d8199f61277a -->|method| b4cbc5e1_8daf_5d2a_22db_810c16ea38b5
  ce127401_3d43_15a0_a5be_36acd7b20e1f["DatagramPacket()"]
  99ae51db_2948_44c9_7279_d8199f61277a -->|method| ce127401_3d43_15a0_a5be_36acd7b20e1f
  4e4c9114_2a47_f7c5_4e71_95fed6ba9c1c["idx()"]
  99ae51db_2948_44c9_7279_d8199f61277a -->|method| 4e4c9114_2a47_f7c5_4e71_95fed6ba9c1c
  e58d83d0_f84e_4623_857d_c9461c7ce8ea["address()"]
  99ae51db_2948_44c9_7279_d8199f61277a -->|method| e58d83d0_f84e_4623_857d_c9461c7ce8ea
  56af55c9_54e7_0d14_cf99_6bd31db2a3e2["release()"]
  99ae51db_2948_44c9_7279_d8199f61277a -->|method| 56af55c9_54e7_0d14_cf99_6bd31db2a3e2

Relationship Graph

Source Code

transport-classes-io_uring/src/main/java/io/netty/channel/uring/MsgHdrMemory.java lines 28–191

final class MsgHdrMemory {
    public static final int MSG_HDR_SIZE =
            Native.SIZEOF_MSGHDR + Native.SIZEOF_SOCKADDR_STORAGE + Native.SIZEOF_IOVEC + Native.CMSG_SPACE;
    private static final byte[] EMPTY_SOCKADDR_STORAGE = new byte[Native.SIZEOF_SOCKADDR_STORAGE];
    // It is not possible to have a zero length buffer in sendFd,
    // so we use a 1 byte buffer here.
    private static final int GLOBAL_IOV_LEN = 1;
    private static final ByteBuffer GLOBAL_IOV_BASE =  Buffer.allocateDirectWithNativeOrder(GLOBAL_IOV_LEN);
    private static final long GLOBAL_IOV_BASE_ADDRESS = Buffer.memoryAddress(GLOBAL_IOV_BASE);
    private final CleanableDirectBuffer msgHdrMemoryCleanable;
    private final CleanableDirectBuffer socketAddrMemoryCleanable;
    private final CleanableDirectBuffer iovMemoryCleanable;
    private final CleanableDirectBuffer cmsgDataMemoryCleanable;
    private final ByteBuffer msgHdrMemory;
    private final ByteBuffer socketAddrMemory;
    private final ByteBuffer iovMemory;
    private final ByteBuffer cmsgDataMemory;

    private final long msgHdrMemoryAddress;
    private final short idx;
    private final int cmsgDataOffset;

    MsgHdrMemory(short idx, ByteBuffer msgHdrMemoryArray) {
        this.idx = idx;
        this.msgHdrMemoryCleanable = null;
        this.socketAddrMemoryCleanable = null;
        this.iovMemoryCleanable = null;
        this.cmsgDataMemoryCleanable = null;
        int offset = idx * MSG_HDR_SIZE;
        // ByteBuffer.slice(int, int) / duplicate() are specified to produce BIG_ENDIAN byte buffers.
        // Set native order explicitly so native structs written via putInt/putLong use the expected endianness.
        this.msgHdrMemory = PlatformDependent.offsetSlice(
                msgHdrMemoryArray, offset, Native.SIZEOF_MSGHDR
        ).order(ByteOrder.nativeOrder());
        offset += Native.SIZEOF_MSGHDR;
        this.socketAddrMemory = PlatformDependent.offsetSlice(
                msgHdrMemoryArray, offset, Native.SIZEOF_SOCKADDR_STORAGE
        ).order(ByteOrder.nativeOrder());
        offset += Native.SIZEOF_SOCKADDR_STORAGE;
        this.iovMemory = PlatformDependent.offsetSlice(
                msgHdrMemoryArray, offset, Native.SIZEOF_IOVEC
        ).order(ByteOrder.nativeOrder());
        offset += Native.SIZEOF_IOVEC;
        this.cmsgDataMemory = PlatformDependent.offsetSlice(
                msgHdrMemoryArray, offset, Native.CMSG_SPACE
        ).order(ByteOrder.nativeOrder());

        msgHdrMemoryAddress = Buffer.memoryAddress(msgHdrMemory);

        long cmsgDataMemoryAddr = Buffer.memoryAddress(cmsgDataMemory);
        long cmsgDataAddr = Native.cmsghdrData(cmsgDataMemoryAddr);
        cmsgDataOffset = (int) (cmsgDataAddr - cmsgDataMemoryAddr);
    }

    MsgHdrMemory() {
        this.idx = 0;
        // jdk will memset the memory to 0, so we don't need to do it here.
        msgHdrMemoryCleanable = Buffer.allocateDirectBufferWithNativeOrder(Native.SIZEOF_MSGHDR);
        socketAddrMemoryCleanable = null;
        iovMemoryCleanable = Buffer.allocateDirectBufferWithNativeOrder(Native.SIZEOF_IOVEC);
        cmsgDataMemoryCleanable = Buffer.allocateDirectBufferWithNativeOrder(Native.CMSG_SPACE_FOR_FD);

        msgHdrMemory = msgHdrMemoryCleanable.buffer();
        socketAddrMemory = null;
        iovMemory = iovMemoryCleanable.buffer();
        cmsgDataMemory = cmsgDataMemoryCleanable.buffer();

        msgHdrMemoryAddress = Buffer.memoryAddress(msgHdrMemory);
        // These two parameters must be set to valid values and cannot be 0,
        // otherwise the fd we get in io_uring_recvmsg is 0
        Iov.set(iovMemory, GLOBAL_IOV_BASE_ADDRESS, GLOBAL_IOV_LEN);

        long cmsgDataMemoryAddr = Buffer.memoryAddress(cmsgDataMemory);
        long cmsgDataAddr = Native.cmsghdrData(cmsgDataMemoryAddr);
        cmsgDataOffset = (int) (cmsgDataAddr - cmsgDataMemoryAddr);
    }

    void set(LinuxSocket socket, InetSocketAddress address, long bufferAddress , int length, short segmentSize) {
        int addressLength;
        if (address == null) {
            addressLength = socket.isIpv6() ? Native.SIZEOF_SOCKADDR_IN6 : Native.SIZEOF_SOCKADDR_IN;

Frequently Asked Questions

What is the MsgHdrMemory class?
MsgHdrMemory is a class in the netty codebase, defined in transport-classes-io_uring/src/main/java/io/netty/channel/uring/MsgHdrMemory.java.
Where is MsgHdrMemory defined?
MsgHdrMemory is defined in transport-classes-io_uring/src/main/java/io/netty/channel/uring/MsgHdrMemory.java at line 28.

Analyze Your Own Codebase

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

Try Supermodel Free