Interface AddressLayout
- All Superinterfaces:
MemoryLayout, ValueLayout
A value layout used to model the address of some region of memory. The carrier
associated with an address layout is
MemorySegment.class. The size and
alignment of an address layout are platform-dependent (e.g. on a 64-bit platform,
the size and alignment of an address layout are set to 8 bytes).
An address layout may optionally feature a target layout.
An address layout with target layout T can be used to model the address of a
region of memory whose layout is T. For instance, an address layout with
target layout ValueLayout.JAVA_INT can be used to model the address of a
region of memory that is 4 bytes long. Specifying a target layout can be useful in
the following situations:
- When accessing a memory segment that has been obtained by reading an address from
another memory segment, e.g. using
MemorySegment.getAtIndex(AddressLayout, long); - When creating a downcall method handle, using
Linker.downcallHandle(FunctionDescriptor, Option...)RESTRICTED; - When creating an upcall stub, using
Linker.upcallStub(MethodHandle, FunctionDescriptor, Arena, Option...)RESTRICTED.
- Implementation Requirements:
- Implementations of this interface are immutable, thread-safe and value-based.
- Since:
- 22
- See Also:
-
Nested Class Summary
Nested classes/interfaces declared in interface MemoryLayout
MemoryLayout.PathElementNested classes/interfaces declared in interface ValueLayout
ValueLayout.OfBoolean, ValueLayout.OfByte, ValueLayout.OfChar, ValueLayout.OfDouble, ValueLayout.OfFloat, ValueLayout.OfInt, ValueLayout.OfLong, ValueLayout.OfShortModifier and TypeInterfaceDescriptionstatic interfaceA value layout whose carrier isboolean.class.static interfaceA value layout whose carrier isbyte.class.static interfaceA value layout whose carrier ischar.class.static interfaceA value layout whose carrier isdouble.class.static interfaceA value layout whose carrier isfloat.class.static interfaceA value layout whose carrier isint.class.static interfaceA value layout whose carrier islong.class.static interfaceA value layout whose carrier isshort.class. -
Field Summary
Fields declared in interface ValueLayout
ADDRESS, ADDRESS_UNALIGNED, JAVA_BOOLEAN, JAVA_BYTE, JAVA_CHAR, JAVA_CHAR_UNALIGNED, JAVA_DOUBLE, JAVA_DOUBLE_UNALIGNED, JAVA_FLOAT, JAVA_FLOAT_UNALIGNED, JAVA_INT, JAVA_INT_UNALIGNED, JAVA_LONG, JAVA_LONG_UNALIGNED, JAVA_SHORT, JAVA_SHORT_UNALIGNEDModifier and TypeFieldDescriptionstatic final AddressLayoutAn address layout constant whose size is the same as that of a machine address (size_t), byte alignment set tosizeof(size_t), byte order set toByteOrder.nativeOrder().static final AddressLayoutAn unaligned address layout constant whose size is the same as that of a machine address (size_t), and byte order set toByteOrder.nativeOrder().static final ValueLayout.OfBooleanA value layout constant whose size is the same as that of a Javaboolean, byte alignment set to 1, and byte order set toByteOrder.nativeOrder().static final ValueLayout.OfByteA value layout constant whose size is the same as that of a Javabyte, byte alignment set to 1, and byte order set toByteOrder.nativeOrder().static final ValueLayout.OfCharA value layout constant whose size is the same as that of a Javachar, byte alignment set to 2, and byte order set toByteOrder.nativeOrder().static final ValueLayout.OfCharAn unaligned value layout constant whose size is the same as that of a Javacharand byte order set toByteOrder.nativeOrder().static final ValueLayout.OfDoubleA value layout constant whose size is the same as that of a Javadouble, byte alignment set to 8, and byte order set toByteOrder.nativeOrder().static final ValueLayout.OfDoubleAn unaligned value layout constant whose size is the same as that of a Javadoubleand byte order set toByteOrder.nativeOrder().static final ValueLayout.OfFloatA value layout constant whose size is the same as that of a Javafloat, byte alignment set to 4, and byte order set toByteOrder.nativeOrder().static final ValueLayout.OfFloatAn unaligned value layout constant whose size is the same as that of a Javafloatand byte order set toByteOrder.nativeOrder().static final ValueLayout.OfIntA value layout constant whose size is the same as that of a Javaint, byte alignment set to 4, and byte order set toByteOrder.nativeOrder().static final ValueLayout.OfIntAn unaligned value layout constant whose size is the same as that of a Javaintand byte order set toByteOrder.nativeOrder().static final ValueLayout.OfLongA value layout constant whose size is the same as that of a Javalong, byte alignment set to 8, and byte order set toByteOrder.nativeOrder().static final ValueLayout.OfLongAn unaligned value layout constant whose size is the same as that of a Javalongand byte order set toByteOrder.nativeOrder().static final ValueLayout.OfShortA value layout constant whose size is the same as that of a Javashort, byte alignment set to 2, and byte order set toByteOrder.nativeOrder().static final ValueLayout.OfShortAn unaligned value layout constant whose size is the same as that of a Javashortand byte order set toByteOrder.nativeOrder(). -
Method Summary
Modifier and TypeMethodDescriptionReturns the target layout associated with this address layout (if any).withByteAlignment(long byteAlignment) Returns a memory layout with the same characteristics as this layout, but with the given alignment constraint (in bytes).Returns a memory layout with the same characteristics as this layout, but with the given name.Returns a value layout with the same characteristics as this layout, but with the given byte order.Returns a memory layout with the same characteristics as this layout, but with no name.Returns an address layout with the same carrier, alignment constraint, name and order as this address layout, but with no target layout.withTargetLayout(MemoryLayout layout) Restricted.Returns an address layout with the same carrier, alignment constraint, name and order as this address layout, but associated with the specified target layout.Methods declared in interface MemoryLayout
arrayElementVarHandle, byteAlignment, byteOffset, byteOffsetHandle, byteSize, equals, hashCode, name, scale, scaleHandle, select, sliceHandle, toString, varHandleModifier and TypeMethodDescriptionarrayElementVarHandle(MemoryLayout.PathElement... elements) Creates a var handle that accesses adjacent elements in a memory segment at offsets selected by the given layout path, where the accessed elements have this layout, and where the initial layout in the path is this layout.longReturns the alignment constraint associated with this layout, expressed in bytes.longbyteOffset(MemoryLayout.PathElement... elements) Computes the offset, in bytes, of the layout selected by the given layout path, where the initial layout in the path is this layout.byteOffsetHandle(MemoryLayout.PathElement... elements) Creates a method handle that computes the offset, in bytes, of the layout selected by the given layout path, where the initial layout in the path is this layout.longbyteSize()Returns the layout size, in bytes.booleanCompares the specified object with this layout for equality.inthashCode()Returns the hash code value for this layout.name()Returns the name (if any) associated with this layout.longscale(long offset, long index) Returnsoffset + (byteSize() * index).Returns a method handle that can be used to invokeMemoryLayout.scale(long, long)on this layout.select(MemoryLayout.PathElement... elements) Returns the layout selected from the provided path, where the initial layout in the path is this layout.sliceHandle(MemoryLayout.PathElement... elements) Creates a method handle which, given a memory segment, returns a slice corresponding to the layout selected by the given layout path, where the initial layout in the path is this layout.toString()Returns the string representation of this layout.varHandle(MemoryLayout.PathElement... elements) Creates a var handle that accesses a memory segment at the offset selected by the given layout path, where the initial layout in the path is this layout.Methods declared in interface ValueLayout
carrier, order, varHandle
-
Method Details
-
withName
Returns a memory layout with the same characteristics as this layout, but with the given name.- Specified by:
withNamein interfaceMemoryLayout- Specified by:
withNamein interfaceValueLayout- Parameters:
name- the layout name- Returns:
- a memory layout with the same characteristics as this layout, but with the given name
- See Also:
-
withoutName
AddressLayout withoutName()Returns a memory layout with the same characteristics as this layout, but with no name.- Specified by:
withoutNamein interfaceMemoryLayout- Specified by:
withoutNamein interfaceValueLayout- Returns:
- a memory layout with the same characteristics as this layout, but with no name
- See Also:
-
withByteAlignment
Returns a memory layout with the same characteristics as this layout, but with the given alignment constraint (in bytes).- Specified by:
withByteAlignmentin interfaceMemoryLayout- Specified by:
withByteAlignmentin interfaceValueLayout- Parameters:
byteAlignment- the layout alignment constraint, expressed in bytes- Returns:
- a memory layout with the same characteristics as this layout, but with the given alignment constraint (in bytes)
-
withOrder
Returns a value layout with the same characteristics as this layout, but with the given byte order.- Specified by:
withOrderin interfaceValueLayout- Parameters:
order- the desired byte order- Returns:
- a value layout with the same characteristics as this layout, but with the given byte order
-
withTargetLayout
withTargetLayoutis a restricted method of the Java platform.Restricted methods are unsafe, and, if used incorrectly, might crash the JVM or result in memory corruption.Returns an address layout with the same carrier, alignment constraint, name and order as this address layout, but associated with the specified target layout. The returned address layout allows raw addresses to be accessed as memory segments whose size is set to the size of the specified layout. Moreover, if the accessed raw address is not compatible with the alignment constraint in the provided layout, IllegalArgumentException will be thrown.- API Note:
- This method can also be used to create an address layout which, when used, creates
native memory segments with maximal size (e.g. Long.MAX_VALUE). This
can be done by using a target sequence layout with unspecified size, as follows:
AddressLayout addressLayout = ... AddressLayout unboundedLayout = addressLayout.withTargetLayout( MemoryLayout.sequenceLayout(Long.MAX_VALUE, ValueLayout.JAVA_BYTE)); - Parameters:
layout- the target layout- Returns:
- an address layout with same characteristics as this layout, but with the provided target layout
- Throws:
IllegalCallerException- if the caller is in a module that does not have native access enabled- See Also:
-
withoutTargetLayout
AddressLayout withoutTargetLayout()Returns an address layout with the same carrier, alignment constraint, name and order as this address layout, but with no target layout.- API Note:
- This can be useful to compare two address layouts that have different target layouts, but are otherwise equal.
- Returns:
- an address layout with same characteristics as this layout, but with no target layout
- See Also:
-
targetLayout
Optional<MemoryLayout> targetLayout()Returns the target layout associated with this address layout (if any).- Returns:
- the target layout associated with this address layout (if any)
-
withTargetLayoutwhen access to restricted methods is enabled.