Class LinuxLayouts

java.lang.Object
express.mvp.roray.utils.functions.LinuxLayouts

public final class LinuxLayouts extends Object
Pre-defined MemoryLayout constants for common Linux/C types and structures. All layouts are compile-time constants with zero runtime cost.

These layouts are designed for the LP64 data model used by:

  • Linux x86_64 (AMD64)
  • Linux ARM64 (aarch64)
  • Linux RISC-V 64

Note: These layouts are NOT compatible with Windows (LLP64) or 32-bit systems.

Socket Server Example

// Create socket
int fd = (int) socket.invokeExact(
    (int) LinuxLayouts.AF_INET,
    LinuxLayouts.SOCK_STREAM | LinuxLayouts.SOCK_NONBLOCK,
    0
);

// Bind to address
try (Arena arena = Arena.ofConfined()) {
    MemorySegment addr = arena.allocate(LinuxLayouts.SOCKADDR_IN);
    addr.set(ValueLayout.JAVA_SHORT, 0, LinuxLayouts.AF_INET);
    addr.set(ValueLayout.JAVA_SHORT, 2, Short.reverseBytes((short) 8080));
    addr.set(ValueLayout.JAVA_INT, 4, 0);  // INADDR_ANY

    bind.invokeExact(fd, addr, (int) LinuxLayouts.SOCKADDR_IN_SIZE);
}

Scatter/Gather I/O Example

StructAccessor iovec = StructAccessor.of(LinuxLayouts.IOVEC);

try (Arena arena = Arena.ofConfined()) {
    MemorySegment iovecs = iovec.allocateArray(arena, 2);
    MemorySegment buf1 = arena.allocateFrom("Hello ");
    MemorySegment buf2 = arena.allocateFrom("World!\n");

    iovec.setPointer(iovec.elementAt(iovecs, 0), "iov_base", buf1);
    iovec.setLong(iovec.elementAt(iovecs, 0), "iov_len", buf1.byteSize());
    iovec.setPointer(iovec.elementAt(iovecs, 1), "iov_base", buf2);
    iovec.setLong(iovec.elementAt(iovecs, 1), "iov_len", buf2.byteSize());

    writev.invokeExact(1, iovecs, 2);  // stdout
}
  • Field Details

    • C_CHAR

      public static final ValueLayout C_CHAR
      C char - 8 bits, signed.
    • C_UCHAR

      public static final ValueLayout C_UCHAR
      C unsigned char - 8 bits, unsigned.
    • C_SHORT

      public static final ValueLayout C_SHORT
      C short - 16 bits, signed.
    • C_USHORT

      public static final ValueLayout C_USHORT
      C unsigned short - 16 bits, unsigned.
    • C_INT

      public static final ValueLayout C_INT
      C int - 32 bits, signed.
    • C_UINT

      public static final ValueLayout C_UINT
      C unsigned int - 32 bits, unsigned.
    • C_LONG

      public static final ValueLayout C_LONG
      C long - 64 bits on LP64 Linux, signed.
    • C_ULONG

      public static final ValueLayout C_ULONG
      C unsigned long - 64 bits on LP64 Linux, unsigned.
    • C_LONG_LONG

      public static final ValueLayout C_LONG_LONG
      C long long - 64 bits, signed.
    • C_ULONG_LONG

      public static final ValueLayout C_ULONG_LONG
      C unsigned long long - 64 bits, unsigned.
    • C_FLOAT

      public static final ValueLayout C_FLOAT
      C float - 32-bit IEEE 754.
    • C_DOUBLE

      public static final ValueLayout C_DOUBLE
      C double - 64-bit IEEE 754.
    • C_POINTER

      public static final ValueLayout C_POINTER
      C pointer - 64 bits on LP64.
    • C_SIZE_T

      public static final ValueLayout C_SIZE_T
      C size_t - unsigned, pointer-sized.
    • C_SSIZE_T

      public static final ValueLayout C_SSIZE_T
      C ssize_t - signed, pointer-sized.
    • C_OFF_T

      public static final ValueLayout C_OFF_T
      C off_t - file offset type (64-bit on modern Linux).
    • C_PID_T

      public static final ValueLayout C_PID_T
      C pid_t - process ID (32-bit signed).
    • C_UID_T

      public static final ValueLayout C_UID_T
      C uid_t - user ID (32-bit unsigned).
    • C_GID_T

      public static final ValueLayout C_GID_T
      C gid_t - group ID (32-bit unsigned).
    • FD

      public static final ValueLayout FD
      File descriptor type - 32-bit signed integer.
    • IOVEC

      public static final StructLayout IOVEC
      struct iovec - scatter/gather I/O vector.

      Used with readv, writev, sendmsg, recvmsg for efficient multi-buffer I/O operations.

      struct iovec {
          void  *iov_base;  // Starting address
          size_t iov_len;   // Number of bytes
      };
      

      Example: writev with multiple buffers

      StructAccessor iov = StructAccessor.of(LinuxLayouts.IOVEC);
      MemorySegment iovecs = iov.allocateArray(arena, 2);
      
      MemorySegment iov0 = iov.elementAt(iovecs, 0);
      iov.setPointer(iov0, "iov_base", headerBuf);
      iov.setLong(iov0, "iov_len", headerBuf.byteSize());
      
      MemorySegment iov1 = iov.elementAt(iovecs, 1);
      iov.setPointer(iov1, "iov_base", dataBuf);
      iov.setLong(iov1, "iov_len", dataBuf.byteSize());
      
      long written = (long) writev.invokeExact(fd, iovecs, 2);
      
    • IOVEC_IOV_BASE

      public static final long IOVEC_IOV_BASE
      Offset of iov_base in IOVEC.
      See Also:
    • IOVEC_IOV_LEN

      public static final long IOVEC_IOV_LEN
      Offset of iov_len in IOVEC.
      See Also:
    • SOCKADDR_IN

      public static final StructLayout SOCKADDR_IN
      struct sockaddr_in - IPv4 socket address.
      struct sockaddr_in {
          sa_family_t    sin_family;  // AF_INET
          in_port_t      sin_port;    // Port number (network byte order)
          struct in_addr sin_addr;    // IPv4 address
          char           sin_zero[8]; // Padding
      };
      
    • SOCKADDR_IN_SIZE

      public static final long SOCKADDR_IN_SIZE
      Size of SOCKADDR_IN in bytes (16).
    • SOCKADDR_IN6

      public static final StructLayout SOCKADDR_IN6
      struct sockaddr_in6 - IPv6 socket address.
      struct sockaddr_in6 {
          sa_family_t     sin6_family;   // AF_INET6
          in_port_t       sin6_port;     // Port number
          uint32_t        sin6_flowinfo; // IPv6 flow information
          struct in6_addr sin6_addr;     // IPv6 address (16 bytes)
          uint32_t        sin6_scope_id; // Scope ID
      };
      
    • SOCKADDR_IN6_SIZE

      public static final long SOCKADDR_IN6_SIZE
      Size of SOCKADDR_IN6 in bytes (28).
    • MSGHDR

      public static final StructLayout MSGHDR
      struct msghdr - message header for sendmsg/recvmsg.
      struct msghdr {
          void         *msg_name;       // Optional address
          socklen_t     msg_namelen;    // Size of address
          struct iovec *msg_iov;        // Scatter/gather array
          size_t        msg_iovlen;     // # elements in msg_iov
          void         *msg_control;    // Ancillary data
          size_t        msg_controllen; // Ancillary data length
          int           msg_flags;      // Flags on received message
      };
      
    • MSGHDR_SIZE

      public static final long MSGHDR_SIZE
      Size of MSGHDR in bytes (56).
    • EPOLL_EVENT

      public static final StructLayout EPOLL_EVENT
      struct epoll_event - epoll event structure.
      struct epoll_event {
          uint32_t     events;  // Epoll events
          epoll_data_t data;    // User data (union, 8 bytes)
      };
      

      Note: This struct is packed on x86_64 (__attribute__((packed))), so we add explicit padding to maintain 4-byte alignment for the data field.

    • EPOLL_EVENT_SIZE

      public static final long EPOLL_EVENT_SIZE
      Size of EPOLL_EVENT in bytes (16 with padding for alignment).
    • TIMESPEC

      public static final StructLayout TIMESPEC
      struct timespec - time specification.
      struct timespec {
          time_t tv_sec;  // seconds
          long   tv_nsec; // nanoseconds
      };
      
    • TIMESPEC_SIZE

      public static final long TIMESPEC_SIZE
      Size of TIMESPEC in bytes (16).
    • AF_UNIX

      public static final short AF_UNIX
      AF_UNIX / AF_LOCAL - Local communication.
      See Also:
    • AF_INET

      public static final short AF_INET
      AF_INET - IPv4 Internet protocols.
      See Also:
    • AF_INET6

      public static final short AF_INET6
      AF_INET6 - IPv6 Internet protocols.
      See Also:
    • SOCK_STREAM

      public static final int SOCK_STREAM
      SOCK_STREAM - Sequenced, reliable, connection-based byte streams.
      See Also:
    • SOCK_DGRAM

      public static final int SOCK_DGRAM
      SOCK_DGRAM - Connectionless, unreliable datagrams.
      See Also:
    • SOCK_NONBLOCK

      public static final int SOCK_NONBLOCK
      SOCK_NONBLOCK - Set O_NONBLOCK on the new socket.
      See Also:
    • SOCK_CLOEXEC

      public static final int SOCK_CLOEXEC
      SOCK_CLOEXEC - Set close-on-exec flag.
      See Also:
    • EPOLLIN

      public static final int EPOLLIN
      EPOLLIN - Available for read.
      See Also:
    • EPOLLOUT

      public static final int EPOLLOUT
      EPOLLOUT - Available for write.
      See Also:
    • EPOLLERR

      public static final int EPOLLERR
      EPOLLERR - Error condition.
      See Also:
    • EPOLLHUP

      public static final int EPOLLHUP
      EPOLLHUP - Hang up.
      See Also:
    • EPOLLET

      public static final int EPOLLET
      EPOLLET - Edge-triggered.
      See Also:
    • EPOLLONESHOT

      public static final int EPOLLONESHOT
      EPOLLONESHOT - One-shot behavior.
      See Also:
    • EPOLL_CTL_ADD

      public static final int EPOLL_CTL_ADD
      EPOLL_CTL_ADD - Add entry to epoll.
      See Also:
    • EPOLL_CTL_DEL

      public static final int EPOLL_CTL_DEL
      EPOLL_CTL_DEL - Remove entry from epoll.
      See Also:
    • EPOLL_CTL_MOD

      public static final int EPOLL_CTL_MOD
      EPOLL_CTL_MOD - Modify entry in epoll.
      See Also: