Class LinuxLayouts
java.lang.Object
express.mvp.roray.utils.functions.LinuxLayouts
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final shortAF_INET - IPv4 Internet protocols.static final shortAF_INET6 - IPv6 Internet protocols.static final shortAF_UNIX / AF_LOCAL - Local communication.static final ValueLayoutCchar- 8 bits, signed.static final ValueLayoutCdouble- 64-bit IEEE 754.static final ValueLayoutCfloat- 32-bit IEEE 754.static final ValueLayoutCgid_t- group ID (32-bit unsigned).static final ValueLayoutCint- 32 bits, signed.static final ValueLayoutClong- 64 bits on LP64 Linux, signed.static final ValueLayoutClong long- 64 bits, signed.static final ValueLayoutCoff_t- file offset type (64-bit on modern Linux).static final ValueLayoutCpid_t- process ID (32-bit signed).static final ValueLayoutC pointer - 64 bits on LP64.static final ValueLayoutCshort- 16 bits, signed.static final ValueLayoutCsize_t- unsigned, pointer-sized.static final ValueLayoutCssize_t- signed, pointer-sized.static final ValueLayoutCunsigned char- 8 bits, unsigned.static final ValueLayoutCuid_t- user ID (32-bit unsigned).static final ValueLayoutCunsigned int- 32 bits, unsigned.static final ValueLayoutCunsigned long- 64 bits on LP64 Linux, unsigned.static final ValueLayoutCunsigned long long- 64 bits, unsigned.static final ValueLayoutCunsigned short- 16 bits, unsigned.static final intEPOLL_CTL_ADD - Add entry to epoll.static final intEPOLL_CTL_DEL - Remove entry from epoll.static final intEPOLL_CTL_MOD - Modify entry in epoll.static final StructLayoutstruct epoll_event- epoll event structure.static final longSize ofEPOLL_EVENTin bytes (16 with padding for alignment).static final intEPOLLERR - Error condition.static final intEPOLLET - Edge-triggered.static final intEPOLLHUP - Hang up.static final intEPOLLIN - Available for read.static final intEPOLLONESHOT - One-shot behavior.static final intEPOLLOUT - Available for write.static final ValueLayoutFile descriptor type - 32-bit signed integer.static final StructLayoutstruct iovec- scatter/gather I/O vector.static final longOffset ofiov_baseinIOVEC.static final longOffset ofiov_leninIOVEC.static final StructLayoutstruct msghdr- message header for sendmsg/recvmsg.static final longSize ofMSGHDRin bytes (56).static final intSOCK_CLOEXEC - Set close-on-exec flag.static final intSOCK_DGRAM - Connectionless, unreliable datagrams.static final intSOCK_NONBLOCK - Set O_NONBLOCK on the new socket.static final intSOCK_STREAM - Sequenced, reliable, connection-based byte streams.static final StructLayoutstruct sockaddr_in- IPv4 socket address.static final longSize ofSOCKADDR_INin bytes (16).static final StructLayoutstruct sockaddr_in6- IPv6 socket address.static final longSize ofSOCKADDR_IN6in bytes (28).static final StructLayoutstruct timespec- time specification.static final longSize ofTIMESPECin bytes (16). -
Method Summary
-
Field Details
-
C_CHAR
Cchar- 8 bits, signed. -
C_UCHAR
Cunsigned char- 8 bits, unsigned. -
C_SHORT
Cshort- 16 bits, signed. -
C_USHORT
Cunsigned short- 16 bits, unsigned. -
C_INT
Cint- 32 bits, signed. -
C_UINT
Cunsigned int- 32 bits, unsigned. -
C_LONG
Clong- 64 bits on LP64 Linux, signed. -
C_ULONG
Cunsigned long- 64 bits on LP64 Linux, unsigned. -
C_LONG_LONG
Clong long- 64 bits, signed. -
C_ULONG_LONG
Cunsigned long long- 64 bits, unsigned. -
C_FLOAT
Cfloat- 32-bit IEEE 754. -
C_DOUBLE
Cdouble- 64-bit IEEE 754. -
C_POINTER
C pointer - 64 bits on LP64. -
C_SIZE_T
Csize_t- unsigned, pointer-sized. -
C_SSIZE_T
Cssize_t- signed, pointer-sized. -
C_OFF_T
Coff_t- file offset type (64-bit on modern Linux). -
C_PID_T
Cpid_t- process ID (32-bit signed). -
C_UID_T
Cuid_t- user ID (32-bit unsigned). -
C_GID_T
Cgid_t- group ID (32-bit unsigned). -
FD
File descriptor type - 32-bit signed integer. -
IOVEC
struct iovec- scatter/gather I/O vector.Used with
readv,writev,sendmsg,recvmsgfor 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
-
IOVEC_IOV_LEN
-
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_SIZESize ofSOCKADDR_INin bytes (16). -
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_SIZESize ofSOCKADDR_IN6in bytes (28). -
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_SIZESize ofMSGHDRin bytes (56). -
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_SIZESize ofEPOLL_EVENTin bytes (16 with padding for alignment). -
TIMESPEC
struct timespec- time specification.struct timespec { time_t tv_sec; // seconds long tv_nsec; // nanoseconds }; -
TIMESPEC_SIZE
public static final long TIMESPEC_SIZESize ofTIMESPECin bytes (16). -
AF_UNIX
public static final short AF_UNIXAF_UNIX / AF_LOCAL - Local communication.- See Also:
-
AF_INET
public static final short AF_INETAF_INET - IPv4 Internet protocols.- See Also:
-
AF_INET6
public static final short AF_INET6AF_INET6 - IPv6 Internet protocols.- See Also:
-
SOCK_STREAM
public static final int SOCK_STREAMSOCK_STREAM - Sequenced, reliable, connection-based byte streams.- See Also:
-
SOCK_DGRAM
public static final int SOCK_DGRAMSOCK_DGRAM - Connectionless, unreliable datagrams.- See Also:
-
SOCK_NONBLOCK
public static final int SOCK_NONBLOCKSOCK_NONBLOCK - Set O_NONBLOCK on the new socket.- See Also:
-
SOCK_CLOEXEC
public static final int SOCK_CLOEXECSOCK_CLOEXEC - Set close-on-exec flag.- See Also:
-
EPOLLIN
public static final int EPOLLINEPOLLIN - Available for read.- See Also:
-
EPOLLOUT
public static final int EPOLLOUTEPOLLOUT - Available for write.- See Also:
-
EPOLLERR
public static final int EPOLLERREPOLLERR - Error condition.- See Also:
-
EPOLLHUP
public static final int EPOLLHUPEPOLLHUP - Hang up.- See Also:
-
EPOLLET
public static final int EPOLLETEPOLLET - Edge-triggered.- See Also:
-
EPOLLONESHOT
public static final int EPOLLONESHOTEPOLLONESHOT - One-shot behavior.- See Also:
-
EPOLL_CTL_ADD
public static final int EPOLL_CTL_ADDEPOLL_CTL_ADD - Add entry to epoll.- See Also:
-
EPOLL_CTL_DEL
public static final int EPOLL_CTL_DELEPOLL_CTL_DEL - Remove entry from epoll.- See Also:
-
EPOLL_CTL_MOD
public static final int EPOLL_CTL_MODEPOLL_CTL_MOD - Modify entry in epoll.- See Also:
-