There are two kinds of memory segments:
MemorySegment#ofArray(int[])
factory methods. These methods return a memory segment backed by the on-heap region
that holds the specified Java array.
Native segments can be obtained by calling one of the Arena#allocate(long, long)
factory methods, which return a memory segment backed by a newly allocated off-heap
region with the given size and aligned to the given alignment constraint.
Alternatively, native segments can be obtained by
mapping
a file into a new off-heap
region (in some systems, this operation is sometimes referred to as mmap
).
Segments obtained in this way are called mapped segments, and their contents
can be persisted and loaded to and from the
underlying memory-mapped file.
Both kinds of segments are read and written using the same methods, known as access operations. An access operation on a memory segment always and only provides access to the region for which the segment was obtained.
long
value. The nature of a segment's address depends on the kind of the
segment:
MemorySegment
API who
see a stable virtualized address for a heap segment backed by the region.
A heap segment obtained from one of the ofArray(int[])
factory methods has
an address of zero.
Every memory segment has a maximum byte alignment,
expressed as a long
value. The maximum alignment is always a power of two,
derived from the segment address, and the segment type, as explained in more detail
below.
Every memory segment has a size. The size of a heap segment
is derived from the Java array from which it is obtained. This size is predictable
across Java runtimes. The size of a native segment is either passed explicitly
(as in Arena#allocate(long, long)
) or derived from a MemoryLayout
(as in Arena#allocate(MemoryLayout)
). The size of a memory segment is typically
a positive number but may be zero, but never negative.
The address and size of a memory segment jointly ensure that access operations on the segment cannot fall outside the boundaries of the region of memory that backs the segment. That is, a memory segment has spatial bounds.
Every memory segment is associated with a scope. This ensures that access operations on a memory segment cannot occur when the region of memory that backs the memory segment is no longer available (e.g., after the scope associated with the accessed memory segment is no longer alive). That is, a memory segment has temporal bounds.
Finally, access operations on a memory segment can be subject to additional thread-confinement checks. Heap segments can be accessed from any thread. Conversely, native segments can only be accessed compatibly with the confinement characteristics of the arena used to obtain them.
get(ValueLayout.OfInt, long)
). Each access operation takes
a value layout, which specifies the size and shape of the
value, and an offset, expressed in bytes. For instance, to read an int
from
a segment, using default endianness, the
following code can be used:
MemorySegment segment = ... int value = segment.get(ValueLayout.JAVA_INT, 0);
int value = segment.get(ValueLayout.JAVA_INT.withOrder(BIG_ENDIAN), 0);
ValueLayout#varHandle()
method can be used to obtain a var handle that can be
used to get/set values represented by the given value layout on a memory segment at
the given offset:
VarHandle intAtOffsetHandle = ValueLayout.JAVA_INT.varHandle(); // (MemorySegment, long) int value = (int) intAtOffsetHandle.get(segment, 10L); // segment.get(ValueLayout.JAVA_INT, 10L)
int
array at a given logical index can be created as follows:
VarHandle intAtOffsetAndIndexHandle = ValueLayout.JAVA_INT.arrayElementVarHandle(); // (MemorySegment, long, long) int value = (int) intAtOffsetAndIndexHandle.get(segment, 2L, 3L); // segment.get(ValueLayout.JAVA_INT, 2L + (3L * 4L))
Clients can also drop the base offset parameter, in order to make the access
expression simpler. This can be used to implement access operations such as
getAtIndex(OfInt, long)
:
VarHandle intAtIndexHandle = MethodHandles.insertCoordinates(intAtOffsetAndIndexHandle, 1, 0L); // (MemorySegment, long) int value = (int) intAtIndexHandle.get(segment, 3L); // segment.getAtIndex(ValueLayout.JAVA_INT, 3L);
Arena arena = ... MemorySegment segment = arena.allocate(100); MemorySegment slice = segment.asSlice(50, 10); slice.get(ValueLayout.JAVA_INT, 20); // Out of bounds! arena.close(); slice.get(ValueLayout.JAVA_INT, 0); // Already closed!
segment
, and is 10 bytes long. That is, the
address of the slice
is segment.address() + 50
, and its size is 10.
As a result, attempting to read an int value at offset 20 of the slice
segment
will result in an exception. The temporal bounds of the original
segment is inherited by its slices; that is, when the scope associated with
segment
is no longer alive, slice
will
also become inaccessible.
A client might obtain a Stream
from a segment, which can then be used to slice
the segment (according to a given element layout) and even allow multiple threads to
work in parallel on disjoint segment slices (to do this, the segment has to be
accessible from multiple threads).
The following code can be used to sum all int values in a memory segment in parallel:
try (Arena arena = Arena.ofShared()) { SequenceLayout SEQUENCE_LAYOUT = MemoryLayout.sequenceLayout(1024, ValueLayout.JAVA_INT); MemorySegment segment = arena.allocate(SEQUENCE_LAYOUT); int sum = segment.elements(ValueLayout.JAVA_INT).parallel() .mapToInt(s -> s.get(ValueLayout.JAVA_INT, 0)) .sum(); }
java.lang.invoke.VarHandle
are only permitted at aligned
addresses. In addition, alignment applies to an access operation whether the segment
being accessed is a native segment or a heap segment.
If the segment being accessed is a native segment, then its address in physical memory can be combined with the offset to obtain the target address in physical memory. The pseudo-function below demonstrates this:
boolean isAligned(MemorySegment segment, long offset, MemoryLayout layout) { return ((segment.address() + offset) % layout.byteAlignment()) == 0; }
The alignment constraint used to access a segment is typically dictated by the shape
of the data structure stored in the segment. For example, if the programmer wishes to
store a sequence of 8-byte values in a native segment, then the segment should be
allocated by specifying an 8-byte alignment constraint, either via
Arena#allocate(long, long)
or Arena#allocate(MemoryLayout)
. These
factories ensure that the off-heap region of memory backing the returned segment
has a starting address that is 8-byte aligned. Subsequently, the programmer can access
the segment at the offsets of interest -- 0, 8, 16, 24, etc -- in the knowledge that
every such access is aligned.
If the segment being accessed is a heap segment, then determining whether access is aligned is more complex. The address of the segment in physical memory is not known and is not even fixed (it may change when the segment is relocated during garbage collection). This means that the address cannot be combined with the specified offset to determine a target address in physical memory. Since the alignment constraint always refers to alignment of addresses in physical memory, it is not possible in principle to determine if any offset in a heap segment is aligned. For example, suppose the programmer chooses an 8-byte alignment constraint and tries to access offset 16 in a heap segment. If the heap segment's address 0 corresponds to physical address 1000, then the target address (1016) would be aligned, but if address 0 corresponds to physical address 1004, then the target address (1020) would not be aligned. It is undesirable to allow access to target addresses that are aligned according to the programmer's chosen alignment constraint, but might not be predictably aligned in physical memory (e.g. because of platform considerations and/or garbage collection behavior).
In practice, the Java runtime lays out arrays in memory so that each n-byte element occurs at an n-byte aligned physical address. The runtime preserves this invariant even if the array is relocated during garbage collection. Access operations rely on this invariant to determine if the specified offset in a heap segment refers to an aligned address in physical memory. For example:
short[]
array will be 2-byte aligned
(e.g. 1006) so that successive short elements occur at 2-byte aligned addresses
(e.g. 1006, 1008, 1010, 1012, etc). A heap segment backed by a short[]
array can be accessed at offsets 0, 2, 4, 6, etc under a 2-byte alignment
constraint. The segment cannot be accessed at any offset under a 4-byte
alignment constraint, because there is no guarantee that the target address would
be 4-byte aligned, e.g., offset 0 would correspond to physical address 1006 while
offset 1 would correspond to physical address 1007. Similarly, the segment cannot
be accessed at any offset under an 8-byte alignment constraint, because there is
no guarantee that the target address would be 8-byte aligned, e.g., offset 2
would correspond to physical address 1008 but offset 4 would correspond to
physical address 1010.long[]
array will be 8-byte aligned
(e.g. 1000), so that successive long elements occur at 8-byte aligned addresses
(e.g., 1000, 1008, 1016, 1024, etc.) A heap segment backed by a long[]
array can be accessed at offsets 0, 8, 16, 24, etc under an 8-byte alignment
constraint. In addition, the segment can be accessed at offsets 0, 4, 8, 12,
etc under a 4-byte alignment constraint, because the target addresses (1000, 1004,
1008, 1012) are 4-byte aligned. And, the segment can be accessed at offsets 0, 2,
4, 6, etc under a 2-byte alignment constraint, because the target addresses (e.g.
1000, 1002, 1004, 1006) are 2-byte aligned.In other words, heap segments feature a maximum alignment which is derived from the size of the elements of the Java array backing the segment, as shown in the following table:
Heap segments can only be accessed using a layout whose alignment is smaller or equal to the maximum alignment associated with the heap segment. Attempting to access a heap segment using a layout whose alignment is greater than the maximum alignment associated with the heap segment will fail, as demonstrated in the following example:
Array type (of backing region) Maximum supported alignment (in bytes) boolean[]
ValueLayout.JAVA_BOOLEAN.byteAlignment()
byte[]
ValueLayout.JAVA_BYTE.byteAlignment()
char[]
ValueLayout.JAVA_CHAR.byteAlignment()
short[]
ValueLayout.JAVA_SHORT.byteAlignment()
int[]
ValueLayout.JAVA_INT.byteAlignment()
float[]
ValueLayout.JAVA_FLOAT.byteAlignment()
long[]
ValueLayout.JAVA_LONG.byteAlignment()
double[]
ValueLayout.JAVA_DOUBLE.byteAlignment()
MemorySegment byteSegment = MemorySegment.ofArray(new byte[10]); byteSegment.get(ValueLayout.JAVA_INT, 0); // fails: ValueLayout.JAVA_INT.byteAlignment() > ValueLayout.JAVA_BYTE.byteAlignment()
long[]
), capable of supporting greater maximum
alignment. More specifically, the maximum alignment associated with long[]
is
set to ValueLayout.JAVA_LONG.byteAlignment()
, which is 8 bytes:
MemorySegment longSegment = MemorySegment.ofArray(new long[10]); longSegment.get(ValueLayout.JAVA_INT, 0); // ok: ValueLayout.JAVA_INT.byteAlignment() <= ValueLayout.JAVA_LONG.byteAlignment()
ValueLayout#JAVA_INT_UNALIGNED
) have
their alignment constraint set to 1:
MemorySegment byteSegment = MemorySegment.ofArray(new byte[10]); byteSegment.get(ValueLayout.JAVA_INT_UNALIGNED, 0); // ok: ValueLayout.JAVA_INT_UNALIGNED.byteAlignment() == ValueLayout.JAVA_BYTE.byteAlignment()
MemoryLayout layout = ... MemorySegment segment = ... boolean isAligned = segment.maxByteAlignment() >= layout.byteAlignment();
char*
might return a pointer to a region containing a single
char
value, or to a region containing an array of char
values, where
the size of the array might be provided in a separate parameter. The size of the
array is not readily apparent to the code calling the foreign function and hoping to
use its result. In addition to having no insight into the size of the region of
memory backing a pointer returned from a foreign function, it also has no insight
into the lifetime intended for said region of memory by the foreign function that
allocated it.
The MemorySegment
API uses zero-length memory segments to represent:
IndexOutOfBoundsException
. This is a crucial safety feature: as
these segments are associated with a region of memory whose size is not known, any
access operations involving these segments cannot be validated. In effect, a
zero-length memory segment wraps an address, and it cannot be used
without explicit intent (see below);
To demonstrate how clients can work with zero-length memory segments, consider the
case of a client that wants to read a pointer from some memory segment. This can be
done via the MemorySegment#get(AddressLayout, long) access method. This
method accepts an address layout
(e.g. ValueLayout#ADDRESS
), the layout of the pointer to be read. For instance,
on a 64-bit platform, the size of an address layout is 8 bytes. The access operation
also accepts an offset, expressed in bytes, which indicates the position (relative to
the start of the memory segment) at which the pointer is stored. The access operation
returns a zero-length native memory segment, backed by a region
of memory whose starting address is the 64-bit value read at the specified offset.
The returned zero-length memory segment cannot be accessed directly by the client:
since the size of the segment is zero, any access operation would result in
out-of-bounds access. Instead, the client must, unsafely, assign new spatial
bounds to the zero-length memory segment. This can be done via the
reinterpret(long)
method, as follows:
MemorySegment z = segment.get(ValueLayout.ADDRESS, ...); // size = 0 MemorySegment ptr = z.reinterpret(16); // size = 16 int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3); // ok
In some cases, the client might additionally want to assign new temporal bounds to a
zero-length memory segment. This can be done via the
reinterpret(long, Arena, Consumer)
method, which returns a new native segment
with the desired size and the same temporal bounds as those of the provided arena:
MemorySegment ptr = null; try (Arena arena = Arena.ofConfined()) { MemorySegment z = segment.get(ValueLayout.ADDRESS, ...); // size = 0, scope = always alive ptr = z.reinterpret(16, arena, null); // size = 4, scope = arena.scope() int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3); // ok } int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3); // throws IllegalStateException
AddressLayout intArrPtrLayout = ValueLayout.ADDRESS.withTargetLayout( MemoryLayout.sequenceLayout(4, ValueLayout.JAVA_INT)); // layout for int (*ptr)[4] MemorySegment ptr = segment.get(intArrPtrLayout, ...); // size = 16 int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3); // ok
All the methods that can be used to manipulate zero-length memory segments
(reinterpret(long)
, reinterpret(Arena, Consumer)
, reinterpret(long, Arena, Consumer)
and
AddressLayout#withTargetLayout(MemoryLayout)
) are
restricted methods, and should
be used with caution: assigning a segment incorrect spatial and/or temporal bounds
could result in a VM crash when attempting to access the memory segment.
Implementation Specification
Implementations of this interface are immutable, thread-safe and value-based.
Modifier and Type | Class and Description |
---|---|
public static interface | MemorySegment.
A scope models the lifetime of all the memory segments associated with it. |
Modifier and Type | Field and Description |
---|---|
public static final MemorySegment | NULL
A zero-length native segment modelling the |
Modifier and Type | Method and Description |
---|---|
public long | |
public ByteBuffer | |
public Optional | Returns: a slice of this segment (where overlapping occurs)the segment to test for an overlap with this segment other)Returns a slice of this segment that is the overlap between this and the provided segment. |
public MemorySegment | |
public MemorySegment | Returns: a slice of this memory segmentThe new segment base offset (relative to the address of this segment),
specified in bytes offset, long The new segment size, specified in bytes newSize)Returns a slice of this memory segment, at the given offset. |
public MemorySegment | Returns: a slice of this memory segmentThe new segment base offset (relative to the address of this segment),
specified in bytes offset, long The new segment size, specified in bytes newSize, long The alignment constraint (in bytes) of the returned slice byteAlignment)Returns a slice of this memory segment, at the given offset, with the provided alignment constraint. |
public MemorySegment | Returns: a slice of this memory segmentThe new segment base offset (relative to the address of this segment),
specified in bytes offset, MemoryLayout The layout of the segment slice layout)Returns a slice of this memory segment with the given layout, at the given offset. |
public MemorySegment | Returns: a slice of this memory segmentThe new segment base offset (relative to the address of this segment),
specified in bytes offset)Returns a slice of this memory segment, at the given offset. |
public long | Returns: the size (in bytes) of this memory segmentReturns the size (in bytes) of this memory segment. |
public static void | copy(MemorySegment
the source segment srcSegment, long the starting offset, in bytes, of the source segment srcOffset, MemorySegment the destination segment dstSegment, long the starting offset, in bytes, of the destination segment dstOffset, long the number of bytes to be copied bytes)Performs a bulk copy from source segment to destination segment. |
public static void | copy(MemorySegment
the source segment srcSegment, ValueLayout the element layout associated with the source segment srcElementLayout, long the starting offset, in bytes, of the source segment srcOffset, MemorySegment the destination segment dstSegment, ValueLayout the element layout associated with the destination segment dstElementLayout, long the starting offset, in bytes, of the destination segment dstOffset, long the number of elements to be copied elementCount)Performs a bulk copy from source segment to destination segment. |
public static void | copy(MemorySegment
the source segment srcSegment, ValueLayout the source element layout. If the byte order associated with the
layout is different from the
native order, a byte swap
operation will be performed on each array element srcLayout, long the starting offset, in bytes, of the source segment srcOffset, Object the destination array dstArray, int the starting index of the destination array dstIndex, int the number of array elements to be copied elementCount)Copies a number of elements from a source memory segment to a destination array. |
public static void | copy(Object
the source array srcArray, int the starting index of the source array srcIndex, MemorySegment the destination segment dstSegment, ValueLayout the destination element layout. If the byte order associated
with the layout is different from the
native order, a byte swap
operation will be performed on each array element. dstLayout, long the starting offset, in bytes, of the destination segment dstOffset, int the number of array elements to be copied elementCount)Copies a number of elements from a source array to a destination memory segment. |
public MemorySegment | Returns: this segmentthe source segment src)Performs a bulk copy from the given source segment to this segment. |
public Stream | Returns: a sequentialStream over disjoint slices in this segmentthe layout to be used for splitting elementLayout)Returns a sequential |
public boolean | |
public MemorySegment | Returns: this memory segmentthe value to write into this segment value)Fills the contents of this memory segment with the given value. |
public void | force()
Forces any changes made to the contents of this mapped segment to be written to the storage device described by the mapped segment's file descriptor. |
public byte | Returns: a byte value read from this segmentthe layout of the region of memory to be read layout,the offset in bytes (relative to this segment address) at which
this access operation will occur. offset)Reads a byte from this segment at the given offset, with the given layout. |
public boolean | Returns: a boolean value read from this segmentthe layout of the region of memory to be read layout,the offset in bytes (relative to this segment address) at which
this access operation will occur offset)Reads a boolean from this segment at the given offset, with the given layout. |
public char | Returns: a char value read from this segmentthe layout of the region of memory to be read layout,the offset in bytes (relative to this segment address) at which
this access operation will occur offset)Reads a char from this segment at the given offset, with the given layout. |
public short | Returns: a short value read from this segmentthe layout of the region of memory to be read layout,the offset in bytes (relative to this segment address) at which this
access operation will occur offset)Reads a short from this segment at the given offset, with the given layout. |
public int | Returns: an int value read from this segmentthe layout of the region of memory to be read layout,the offset in bytes (relative to this segment address) at which
this access operation will occur. offset)Reads an int from this segment at the given offset, with the given layout. |
public float | Returns: a float value read from this segmentthe layout of the region of memory to be read layout,the offset in bytes (relative to this segment address) at which
this access operation will occur offset)Reads a float from this segment at the given offset, with the given layout. |
public long | Returns: a long value read from this segmentthe layout of the region of memory to be read layout,the offset in bytes (relative to this segment address) at which
this access operation will occur. offset)Reads a long from this segment at the given offset, with the given layout. |
public double | Returns: a double value read from this segmentthe layout of the region of memory to be read layout,the offset in bytes (relative to this segment address) at which
this access operation will occur offset)Reads a double from this segment at the given offset, with the given layout. |
public MemorySegment | Returns: a native segment wrapping an address read from this segmentthe layout of the region of memory to be read layout, long the offset in bytes (relative to this segment address) at which
this access operation will occur offset)Reads an address from this segment at the given offset, with the given layout. |
public byte | Returns: a byte value read from this segmentthe layout of the region of memory to be read layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index)(index * layout.byteSize()) .Reads a byte from this segment at the given index, scaled by the given layout size. |
public boolean | Returns: a boolean value read from this segmentthe layout of the region of memory to be read layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index)(index * layout.byteSize()) .Reads a boolean from this segment at the given index, scaled by the given layout size. |
public char | Returns: a char value read from this segmentthe layout of the region of memory to be read layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index)(index * layout.byteSize()) .Reads a char from this segment at the given index, scaled by the given layout size. |
public short | Returns: a short value read from this segmentthe layout of the region of memory to be read layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index)(index * layout.byteSize()) .Reads a short from this segment at the given index, scaled by the given layout size. |
public int | Returns: an int value read from this segmentthe layout of the region of memory to be read. layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index)(index * layout.byteSize()) .Reads an int from this segment at the given index, scaled by the given layout size. |
public float | Returns: a float value read from this segmentthe layout of the region of memory to be read layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index)(index * layout.byteSize()) .Reads a float from this segment at the given index, scaled by the given layout size. |
public long | Returns: a long value read from this segmentthe layout of the region of memory to be read layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index)(index * layout.byteSize()) .Reads a long from this segment at the given index, scaled by the given layout size. |
public double | Returns: a double value read from this segmentthe layout of the region of memory to be read layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index)(index * layout.byteSize()) .Reads a double from this segment at the given index, scaled by the given layout size. |
public MemorySegment | Returns: a native segment wrapping an address read from this segmentthe layout of the region of memory to be read layout, long a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index)(index * layout.byteSize()) .Reads an address from this segment at the given at the given index, scaled by the given layout size. |
public String | Returns: a Java string constructed from the bytes read from the given starting address up to (but not including) the first'\0' terminator
character (assuming one is found)the offset in bytes (relative to this segment address) at which
this access operation will occur offset)Reads a null-terminated string from this segment at the given offset, using the UTF-8 charset. |
public String | Returns: a Java string constructed from the bytes read from the given starting address up to (but not including) the first'\0' terminator
character (assuming one is found)offset in bytes (relative to this segment address) at which this
access operation will occur offset, Charset the charset used to decode the
string bytes charset)Reads a null-terminated string from this segment at the given offset, using the provided charset. |
public int | Returns: the hash code value for this memory segmentReturns the hash code value for this memory segment. |
public Optional | Returns: the Java object associated with this memory segment, if anyReturns the Java object stored in the on-heap region of memory backing this memory segment, if any. |
public boolean | Returns: true if this segment can be accessed from the provided threadthe thread to be tested thread)Returns |
public boolean | Returns: true if it is likely that the contents of this segment
are resident in physical memoryDetermines whether all the contents of this mapped segment are resident in physical memory. |
public boolean | Returns: true if this segment is a mapped segmentReturns |
public boolean | Returns: true if this segment is a native segmentReturns |
public boolean | |
public void | |
public long | Returns: the maximum byte alignment associated with this memory segmentReturns the maximum byte alignment associated with this memory segment. |
public long | Returns: the relative offset, in bytes, of the first mismatch between this and the given other segment, otherwise -1 if no mismatchthe segment to be tested for a mismatch with this segment other)Finds and returns the offset, in bytes, of the first mismatch between this segment and the given other segment. |
public static long | Returns: the relative offset, in bytes, of the first mismatch between the source and destination segments, otherwise -1 if no mismatchthe source segment. srcSegment, long the offset (inclusive) of the first byte in the
source segment to be tested srcFromOffset, long the offset (exclusive) of the last byte in the
source segment to be tested srcToOffset, MemorySegment the destination segment dstSegment, long the offset (inclusive) of the first byte in the
destination segment to be tested dstFromOffset, long the offset (exclusive) of the last byte in the
destination segment to be tested dstToOffset)Finds and returns the relative offset, in bytes, of the first mismatch between the source and the destination segments. |
public static MemorySegment | Returns: a zero-length native segment with the given addressthe address of the returned native segment address)Creates a zero-length native segment from the given address value. |
public static MemorySegment | Returns: a heap memory segment backed by a byte arraythe primitive array backing the heap memory segment byteArray)Creates a heap segment backed by the on-heap region of memory that holds the given byte array. |
public static MemorySegment | Returns: a heap memory segment backed by a char arraythe primitive array backing the heap segment charArray)Creates a heap segment backed by the on-heap region of memory that holds the given char array. |
public static MemorySegment | Returns: a heap memory segment backed by a short arraythe primitive array backing the heap segment shortArray)Creates a heap segment backed by the on-heap region of memory that holds the given short array. |
public static MemorySegment | Returns: a heap memory segment backed by an int arraythe primitive array backing the heap segment intArray)Creates a heap segment backed by the on-heap region of memory that holds the given int array. |
public static MemorySegment | Returns: a heap memory segment backed by a float arraythe primitive array backing the heap segment floatArray)Creates a heap segment backed by the on-heap region of memory that holds the given float array. |
public static MemorySegment | Returns: a heap memory segment backed by a long arraythe primitive array backing the heap segment longArray)Creates a heap segment backed by the on-heap region of memory that holds the given long array. |
public static MemorySegment | Returns: a heap memory segment backed by a double arraythe primitive array backing the heap segment doubleArray)Creates a heap segment backed by the on-heap region of memory that holds the given double array. |
public static MemorySegment | |
public MemorySegment | Returns: a new memory segment that has the same address and scope as this segment, but the new provided sizethe size of the returned segment newSize)Returns a new memory segment that has the same address and scope as this segment, but with the provided size. |
public MemorySegment | Returns: a new memory segment with unbounded sizethe arena to be associated with the returned segment arena, Consumer<MemorySegment> the cleanup action that should be executed when the provided arena
is closed (can be cleanup)null )Returns a new memory segment with the same address and size as this segment, but with the provided scope. |
public MemorySegment | Returns: a new segment that has the same address as this segment, but with the new size and its scope set to that of the provided arena.the size of the returned segment newSize, Arena the arena to be associated with the returned segment arena, Consumer<MemorySegment> the cleanup action that should be executed when the provided arena
is closed (can be cleanup)null ).Returns a new segment with the same address as this segment, but with the provided size and scope. |
public MemorySegment. | Returns: the scope associated with this memory segmentReturns the scope associated with this memory segment. |
public void | set(ValueLayout.
the layout of the region of memory to be written layout,the offset in bytes (relative to this segment address) at which
this access operation will occur. offset, byte the byte value to be written. value)Writes a byte into this segment at the given offset, with the given layout. |
public void | set(ValueLayout.
the layout of the region of memory to be written layout,the offset in bytes (relative to this segment address) at which
this access operation will occur offset, boolean the boolean value to be written value)Writes a boolean into this segment at the given offset, with the given layout. |
public void | set(ValueLayout.
the layout of the region of memory to be written layout,the offset in bytes (relative to this segment address) at which
this access operation will occur. offset, char the char value to be written value)Writes a char into this segment at the given offset, with the given layout. |
public void | set(ValueLayout.
the layout of the region of memory to be written layout,the offset in bytes (relative to this segment address) at which
this access operation will occur. offset, short the short value to be written value)Writes a short into this segment at the given offset, with the given layout. |
public void | set(ValueLayout.
the layout of the region of memory to be written layout,the offset in bytes (relative to this segment address) at which this
access operation will occur offset, int the int value to be written value)Writes an int into this segment at the given offset, with the given layout. |
public void | set(ValueLayout.
the layout of the region of memory to be written layout,the offset in bytes (relative to this segment address) at which this
access operation will occur offset, float the float value to be written value)Writes a float into this segment at the given offset, with the given layout. |
public void | set(ValueLayout.
the layout of the region of memory to be written layout,the offset in bytes (relative to this segment address) at which
this access operation will occur. offset, long the long value to be written. value)Writes a long into this segment at the given offset, with the given layout. |
public void | set(ValueLayout.
the layout of the region of memory to be written layout,the offset in bytes (relative to this segment address) at which
this access operation will occur offset, double the double value to be written value)Writes a double into this segment at the given offset, with the given layout. |
public void | set(AddressLayout
the layout of the region of memory to be written layout, long the offset in bytes (relative to this segment address) at which
this access operation will occur. offset, MemorySegment the address value to be written. value)Writes an address into this segment at the given offset, with the given layout. |
public void | setAtIndex(ValueLayout.
the layout of the region of memory to be written layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index, char (index * layout.byteSize()) .the char value to be written value)Writes a char into this segment at the given index, scaled by the given layout size. |
public void | setAtIndex(ValueLayout.
the layout of the region of memory to be written layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index, byte (index * layout.byteSize()) .the short value to be written value)Writes a byte into this segment at the given index, scaled by the given layout size. |
public void | setAtIndex(ValueLayout.
the layout of the region of memory to be written layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index, boolean (index * layout.byteSize()) .the short value to be written value)Writes a boolean into this segment at the given index, scaled by the given layout size. |
public void | setAtIndex(ValueLayout.
the layout of the region of memory to be written layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index, short (index * layout.byteSize()) .the short value to be written value)Writes a short into this segment at the given index, scaled by the given layout size. |
public void | setAtIndex(ValueLayout.
the layout of the region of memory to be written layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation
will occur can be expressed as index, int (index * layout.byteSize()) .the int value to be written value)Writes an int into this segment at the given index, scaled by the given layout size. |
public void | setAtIndex(ValueLayout.
the layout of the region of memory to be written layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index, float (index * layout.byteSize()) .the float value to be written value)Writes a float into this segment at the given index, scaled by the given layout size. |
public void | setAtIndex(ValueLayout.
the layout of the region of memory to be written layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index, long (index * layout.byteSize()) .the long value to be written value)Writes a long into this segment at the given index, scaled by the given layout size. |
public void | setAtIndex(ValueLayout.
the layout of the region of memory to be written layout,a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index, double (index * layout.byteSize()) .the double value to be written value)Writes a double into this segment at the given index, scaled by the given layout size. |
public void | setAtIndex(AddressLayout
the layout of the region of memory to be written layout, long a logical index. The offset in bytes (relative to this
segment address) at which the access operation will occur can be
expressed as index, MemorySegment (index * layout.byteSize()) .the address value to be written value)Writes an address into this segment at the given index, scaled by the given layout size. |
public void | setString(long
the offset in bytes (relative to this segment address) at which
this access operation will occur, the final address of this write
operation can be expressed as offset, String address() + offset .the Java string to be written into this segment str)Writes the given string into this segment at the given offset, converting it to a null-terminated byte sequence using the UTF-8 charset. |
public void | setString(long
offset in bytes (relative to this segment address) at which this
access operation will occur, the final address of this write
operation can be expressed as offset, String address() + offset the Java string to be written into this segment str, Charset the charset used to encode the string bytes charset)Writes the given string into this segment at the given offset, converting it to a null-terminated byte sequence using the provided charset. |
public Spliterator | Returns: the element spliterator for this segmentthe layout to be used for splitting elementLayout)Returns a spliterator for this memory segment. |
public byte[] | Returns: a new byte array whose contents are copied from this memory segmentthe source element layout. If the byte order associated with
the layout is different from the
native order, a byte swap
operation will be performed on each array element elementLayoutCopy the contents of this memory segment into a new byte array. |
public short[] | Returns: a new short array whose contents are copied from this memory segmentthe source element layout. If the byte order associated with
the layout is different from the
native order, a byte swap
operation will be performed on each array element elementLayoutCopy the contents of this memory segment into a new short array. |
public char[] | Returns: a new char array whose contents are copied from this memory segmentthe source element layout. If the byte order associated with
the layout is different from the
native order, a byte swap
operation will be performed on each array element elementLayoutCopy the contents of this memory segment into a new char array. |
public int[] | Returns: a new int array whose contents are copied from this memory segmentthe source element layout. If the byte order associated with
the layout is different from the
native order, a byte swap
operation will be performed on each array element. elementLayoutCopy the contents of this memory segment into a new int array. |
public float[] | Returns: a new float array whose contents are copied from this memory segmentthe source element layout. If the byte order associated with
the layout is different from the
native order, a byte swap
operation will be performed on each array element elementLayoutCopy the contents of this memory segment into a new float array. |
public long[] | Returns: a new long array whose contents are copied from this memory segmentthe source element layout. If the byte order associated with
the layout is different from the
native order, a byte swap
operation will be performed on each array element elementLayoutCopy the contents of this memory segment into a new long array. |
public double[] | Returns: a new double array whose contents are copied from this memory segmentthe source element layout. If the byte order associated with
the layout is different from the
native order, a byte swap
operation will be performed on each array element elementLayoutCopy the contents of this memory segment into a new double array. |
public void |