allocate(long, long)
method. A segment allocator defines several methods which can be useful to create
segments from several kinds of Java values such as primitives and arrays.
SegmentAllocator is a functional interface.
Clients can easily obtain a new segment allocator by using either a lambda expression
or a method reference:
SegmentAllocator autoAllocator = (byteSize, byteAlignment) -> Arena.ofAuto().allocate(byteSize, byteAlignment);
This interface defines factories for commonly used allocators:
slicingAllocator(MemorySegment) obtains an efficient slicing
allocator, where memory is allocated by repeatedly slicing the provided
memory segment;prefixAllocator(MemorySegment) obtains an allocator which wraps a
segment and recycles its content upon each new allocation request.
Passing a segment allocator to an API can be especially useful in circumstances where
a client wants to communicate where the results of a certain operation
(performed by the API) should be stored, as a memory segment. For instance,
downcall method handles
can accept an additional SegmentAllocator parameter if the underlying
foreign function is known to return a struct by-value. Effectively, the allocator
parameter tells the linker where to store the return value of the foreign function.
API Note
Unless otherwise specified, the allocate(long, long) method is
not thread-safe. Furthermore, memory segments allocated by a segment
allocator can be associated with different lifetimes, and can even be backed
by overlapping regions of memory. For these reasons, clients should
generally only interact with a segment allocator they own.
Clients should consider using an arena instead, which, provides strong thread-safety, lifetime and non-overlapping guarantees.
| Modifier and Type | Method and Description |
|---|---|
| public default MemorySegment | Returns: a new memory segment with the given layoutthe layout of the block of memory to be allocated layout)Returns a new memory segment with the given layout. |
| public default MemorySegment | Returns: a new memory segment with the givenelementLayout and countthe array element layout elementLayout, long the array element count count)Returns a new memory segment with the given |
| public default MemorySegment | Returns: a new memory segment with the givenbyteSizethe size (in bytes) of the block of memory to be allocated byteSize)Returns a new memory segment with the given |
| public MemorySegment | Returns: a new memory segment with the givenbyteSize and
byteAlignmentthe size (in bytes) of the block of memory
to be allocated byteSize, long the alignment (in bytes) of the block of memory
to be allocated byteAlignment)Returns a new memory segment with the given |
| public default MemorySegment | Returns: a new native segment containing the converted C stringthe Java string to be converted into a C string str)Converts a Java string into a null-terminated C string using the UTF-8 charset, storing the result into a memory segment. |
| public default MemorySegment | Returns: a new native segment containing the converted C stringthe Java string to be converted into a C string str, Charset the charset used to encode the
string bytes charset)Converts a Java string into a null-terminated C string using the provided charset, and storing the result into a memory segment. |
| public default MemorySegment | Returns: a new native segment containing the encoded stringthe Java string to be encoded str, Charset the charset used to encode the
string bytes charset, int the starting index of the source string srcIndex, int the number of characters to be copied numChars)Encodes a Java string using the provided charset and stores the resulting byte array into a memory segment. |
| public default MemorySegment | Returns: a new memory segment initialized with the provided byte valuethe layout of the block of memory to be allocated layout,the value to be set in the newly allocated memory segment value)Returns a new memory segment initialized with the provided byte value. |
| public default MemorySegment | Returns: a new memory segment initialized with the provided char valuethe layout of the block of memory to be allocated layout,the value to be set in the newly allocated memory segment value)Returns a new memory segment initialized with the provided char value. |
| public default MemorySegment | Returns: a new memory segment initialized with the provided short valuethe layout of the block of memory to be allocated layout,the value to be set in the newly allocated memory segment value)Returns a new memory segment initialized with the provided short value. |
| public default MemorySegment | Returns: a new memory segment initialized with the provided int valuethe layout of the block of memory to be allocated layout,the value to be set in the newly allocated memory segment value)Returns a new memory segment initialized with the provided int value. |
| public default MemorySegment | Returns: a new memory segment initialized with the provided float valuethe layout of the block of memory to be allocated layout,the value to be set in the newly allocated memory segment value)Returns a new memory segment initialized with the provided float value. |
| public default MemorySegment | Returns: a new memory segment initialized with the provided long valuethe layout of the block of memory to be allocated layout,the value to be set in the newly allocated memory segment value)Returns a new memory segment initialized with the provided long value. |
| public default MemorySegment | Returns: a new memory segment initialized with the provided double valuethe layout of the block of memory to be allocated layout,the value to be set in the newly allocated memory segment value)Returns a new memory segment initialized with the provided double value. |
| public default MemorySegment | Returns: a new memory segment initialized with the address of the provided memory segmentthe layout of the block of memory to be allocated layout, MemorySegment the value to be set in the newly allocated memory segment value)Returns a new memory segment initialized with the address of the provided memory segment. |
| public default MemorySegment | Returns: a new memory segment initialized with the contents of the provided segmentthe element layout of the allocated array elementLayout, MemorySegment the source segment source, ValueLayout the element layout of the source segment sourceElementLayout, long the starting offset, in bytes, of the source segment sourceOffset, long the number of elements in the source segment to be copied elementCount)Returns a new memory segment initialized with the contents of the provided segment. |
| public default MemorySegment | Returns: a new memory segment initialized with the elements in the provided byte arraythe element layout of the array to be allocated elementLayout,the byte elements to be copied to the newly allocated
memory block elements)Returns a new memory segment initialized with the elements in the provided byte array. |
| public default MemorySegment | Returns: a new memory segment initialized with the elements in the provided short arraythe element layout of the array to be allocated elementLayout,the short elements to be copied to the newly allocated
memory block elements)Returns a new memory segment initialized with the elements in the provided short array. |
| public default MemorySegment | Returns: a new memory segment initialized with the elements in the provided char arraythe element layout of the array to be allocated elementLayout,the char elements to be copied to the newly allocated
memory block elements)Returns a new memory segment initialized with the elements in the provided char array. |
| public default MemorySegment | Returns: a new memory segment initialized with the elements in the provided int arraythe element layout of the array to be allocated elementLayout,the int elements to be copied to the newly allocated
memory block elements)Returns a new memory segment initialized with the elements in the provided int array. |
| public default MemorySegment | Returns: a new memory segment initialized with the elements in the provided float arraythe element layout of the array to be allocated elementLayout,the float elements to be copied to the newly allocated
memory block elements)Returns a new memory segment initialized with the elements in the provided float array. |
| public default MemorySegment | Returns: a new memory segment initialized with the elements in the provided long arraythe element layout of the array to be allocated elementLayout,the long elements to be copied to the newly allocated
memory block elements)Returns a new memory segment initialized with the elements in the provided long array. |
| public default MemorySegment | Returns: a new memory segment initialized with the elements in the provided double arraythe element layout of the array to be allocated elementLayout,the double elements to be copied to the newly allocated
memory block elements)Returns a new memory segment initialized with the elements in the provided double array. |
| private MemorySegment | |
| private MemorySegment | |
| private MemorySegment | |
| private static void | |
| public static SegmentAllocator | Returns: an allocator that recycles an existing segment upon each new allocation requestthe memory segment to be recycled by the returned allocator segment)Returns a segment allocator that responds to allocation requests by recycling a single segment. |
| public static SegmentAllocator | Returns: a new slicing allocatorthe segment from which the returned allocator should slice from segment)Returns a segment allocator that responds to allocation requests by returning consecutive slices obtained from the provided segment. |
| allocate | back to summary |
|---|---|
| public default MemorySegment allocate(MemoryLayout layout) Returns a new memory segment with the given layout. Implementation Specification The default implementation for this method calls
| |
| allocate | back to summary |
|---|---|
| public default MemorySegment allocate(MemoryLayout elementLayout, long count) Returns a new memory segment with the given Implementation Specification The default implementation for this method calls
| |
| allocate | back to summary |
|---|---|
| public default MemorySegment allocate(long byteSize) Returns a new memory segment with the given Implementation Specification The default implementation for this method calls
| |
| allocate | back to summary |
|---|---|
| public MemorySegment allocate(long byteSize, long byteAlignment) Returns a new memory segment with the given
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(String str) Converts a Java string into a null-terminated C string using the UTF-8 charset, storing the result into a memory segment. Calling this method is equivalent to the following code: allocateFrom(str, StandardCharsets.UTF_8);
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(String str, Charset charset) Converts a Java string into a null-terminated C string using the provided charset, and storing the result into a memory segment.
This method always replaces malformed-input and unmappable-character
sequences with this charset's default replacement byte array. The
If the given string contains any Implementation Specification The default implementation for this method copies the contents of the
provided Java string into a new memory segment obtained by calling
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(String str, Charset charset, int srcIndex, int numChars) Encodes a Java string using the provided charset and stores the resulting byte array into a memory segment.
This method always replaces malformed-input and unmappable-character
sequences with this charset's default replacement byte array. The
If the given string contains any Implementation Specification The default implementation for this method copies the contents of the
provided Java string into a new memory segment obtained by calling
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the provided byte value. The size of the allocated memory segment is the size of the given layout. The given value is written into the segment according to the byte order and alignment constraint of the given layout. Implementation Specification The default implementation is equivalent to: MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg;
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the provided char value. The size of the allocated memory segment is the size of the given layout. The given value is written into the segment according to the byte order and alignment constraint of the given layout. Implementation Specification The default implementation is equivalent to: MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg;
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the provided short value. The size of the allocated memory segment is the size of the given layout. The given value is written into the segment according to the byte order and alignment constraint of the given layout. Implementation Specification The default implementation is equivalent to: MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg;
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the provided int value. The size of the allocated memory segment is the size of the given layout. The given value is written into the segment according to the byte order and alignment constraint of the given layout. Implementation Specification The default implementation is equivalent to: MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg;
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the provided float value. The size of the allocated memory segment is the size of the given layout. The given value is written into the segment according to the byte order and alignment constraint of the given layout. Implementation Specification The default implementation is equivalent to: MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg;
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the provided long value. The size of the allocated memory segment is the size of the given layout. The given value is written into the segment according to the byte order and alignment constraint of the given layout. Implementation Specification The default implementation is equivalent to: MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg;
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the provided double value. The size of the allocated memory segment is the size of the given layout. The given value is written into the segment according to the byte order and alignment constraint of the given layout. Implementation Specification The default implementation is equivalent to: MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg;
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(AddressLayout layout, MemorySegment value) Returns a new memory segment initialized with the address of the provided memory segment.
The address value might be narrowed according to the platform address size
(see The size of the allocated memory segment is the size of the given layout. The given value is written into the segment according to the byte order and alignment constraint of the given layout. Implementation Specification The default implementation is equivalent to: Objects.requireNonNull(value); MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg;
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout elementLayout, MemorySegment source, ValueLayout sourceElementLayout, long sourceOffset, long elementCount) Returns a new memory segment initialized with the contents of the provided segment.
The size of the allocated memory segment is the
Implementation Specification The default implementation for this method is equivalent to the following code: MemorySegment dest = this.allocate(elementLayout, elementCount); MemorySegment.copy(source, sourceElementLayout, sourceOffset, dest, elementLayout, 0, elementCount); return dest;
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the elements in the provided byte array.
The size of the allocated memory segment is
Implementation Specification The default implementation for this method is equivalent to the following code: this.allocateFrom(layout, MemorySegment.ofArray(array), ValueLayout.JAVA_BYTE, 0, array.length)
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the elements in the provided short array.
The size of the allocated memory segment is
Implementation Specification The default implementation for this method is equivalent to the following code: this.allocateFrom(layout, MemorySegment.ofArray(array), ValueLayout.JAVA_SHORT, 0, array.length)
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the elements in the provided char array.
The size of the allocated memory segment is
Implementation Specification The default implementation for this method is equivalent to the following code: this.allocateFrom(layout, MemorySegment.ofArray(array), ValueLayout.JAVA_CHAR, 0, array.length)
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the elements in the provided int array.
The size of the allocated memory segment is
Implementation Specification The default implementation for this method is equivalent to the following code: this.allocateFrom(layout, MemorySegment.ofArray(array), ValueLayout.JAVA_INT, 0, array.length)
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the elements in the provided float array.
The size of the allocated memory segment is
Implementation Specification The default implementation for this method is equivalent to the following code: this.allocateFrom(layout, MemorySegment.ofArray(array), ValueLayout.JAVA_FLOAT, 0, array.length)
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the elements in the provided long array.
The size of the allocated memory segment is
Implementation Specification The default implementation for this method is equivalent to the following code: this.allocateFrom(layout, MemorySegment.ofArray(array), ValueLayout.JAVA_LONG, 0, array.length)
| |
| allocateFrom | back to summary |
|---|---|
| public default MemorySegment allocateFrom(ValueLayout. Returns a new memory segment initialized with the elements in the provided double array.
The size of the allocated memory segment is
Implementation Specification The default implementation for this method is equivalent to the following code: this.allocateFrom(layout, MemorySegment.ofArray(array), ValueLayout.JAVA_DOUBLE, 0, array.length)
| |
| allocateNoInit | back to summary |
|---|---|
private MemorySegment allocateNoInit(long byteSize)
| |
| allocateNoInit | back to summary |
|---|---|
private MemorySegment allocateNoInit(MemoryLayout layout)
| |
| allocateNoInit | back to summary |
|---|---|
private MemorySegment allocateNoInit(MemoryLayout layout, long size)
| |
| assertWritable | back to summary |
|---|---|
| private static void assertWritable(MemorySegment segment) | |
| prefixAllocator | back to summary |
|---|---|
| public static SegmentAllocator prefixAllocator(MemorySegment segment) Returns a segment allocator that responds to allocation requests by recycling a
single segment. Each new allocation request will return a new slice starting at
the segment offset Equivalent to (but likely more efficient than) the following code: MemorySegment segment = ... SegmentAllocator prefixAllocator = (size, align) -> segment.asSlice(0, size, align); IndexOutOfBoundsException when a slice of
the provided segment with the requested size and alignment cannot be found.
API Note A prefix allocator can be useful to limit allocation requests in case a client knows that they have fully processed the contents of the allocated segment before the subsequent allocation request takes place. Implementation Note While a prefix allocator is thread-safe, concurrent access on the same recycling allocator might cause a thread to overwrite contents written to the underlying segment by a different thread.
| |
| slicingAllocator | back to summary |
|---|---|
| public static SegmentAllocator slicingAllocator(MemorySegment segment) Returns a segment allocator that responds to allocation requests by returning consecutive slices obtained from the provided segment. Each new allocation request will return a new slice starting at the current offset (modulo additional padding to satisfy alignment constraint), with given size.
The returned allocator throws Implementation Note A slicing allocator is not thread-safe.
| |