- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
SegmentAllocator is a preview API of the Java platform.
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 handlesPREVIEW can accept an additional
SegmentAllocatorPREVIEW 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 arenaPREVIEW instead, which, provides strong thread-safety, lifetime and non-overlapping guarantees.
- Since:
- 19
-
Method Summary
Modifier and TypeMethodDescriptiondefault MemorySegmentPREVIEWallocate(long byteSize) Returns a new memory segment with the givenbyteSize.allocate(long byteSize, long byteAlignment) Returns a new memory segment with the givenbyteSizeandbyteAlignment.default MemorySegmentPREVIEWallocate(AddressLayoutPREVIEW layout, MemorySegmentPREVIEW value) Returns a new memory segment initialized with the address of the providedvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocate(MemoryLayoutPREVIEW layout) Returns a new memory segment with the given layout.default MemorySegmentPREVIEWallocate(ValueLayout.OfBytePREVIEW layout, byte value) Returns a new memory segment initialized with the providedbytevalueas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocate(ValueLayout.OfCharPREVIEW layout, char value) Returns a new memory segment initialized with the providedcharvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocate(ValueLayout.OfDoublePREVIEW layout, double value) Returns a new memory segment initialized with the provideddoublevalueas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocate(ValueLayout.OfFloatPREVIEW layout, float value) Returns a new memory segment initialized with the providedfloatvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocate(ValueLayout.OfIntPREVIEW layout, int value) Returns a new memory segment initialized with the providedintvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocate(ValueLayout.OfLongPREVIEW layout, long value) Returns a new memory segment initialized with the providedlongvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocate(ValueLayout.OfShortPREVIEW layout, short value) Returns a new memory segment initialized with the providedshortvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocateArray(MemoryLayoutPREVIEW elementLayout, long count) Returns a new memory segment with the givenelementLayoutandcount.default MemorySegmentPREVIEWallocateArray(ValueLayout.OfBytePREVIEW elementLayout, byte... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEbyteelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocateArray(ValueLayout.OfCharPREVIEW elementLayout, char... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEcharelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocateArray(ValueLayout.OfDoublePREVIEW elementLayout, double... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEdoubleelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocateArray(ValueLayout.OfFloatPREVIEW elementLayout, float... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEfloatelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocateArray(ValueLayout.OfIntPREVIEW elementLayout, int... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEintelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocateArray(ValueLayout.OfLongPREVIEW elementLayout, long... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedElongelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocateArray(ValueLayout.OfShortPREVIEW elementLayout, short... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEshortelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).default MemorySegmentPREVIEWallocateUtf8String(String str) Returns a new memory segment with a Java string converted into a UTF-8 encoded, null-terminated C string.static SegmentAllocatorPREVIEWprefixAllocator(MemorySegmentPREVIEW segment) Returns a segment allocator which responds to allocation requests by recycling a single segment.static SegmentAllocatorPREVIEWslicingAllocator(MemorySegmentPREVIEW segment) Returns a segment allocator which responds to allocation requests by returning consecutive slices obtained from the provided segment.
-
Method Details
-
allocateUtf8String
Returns a new memory segment with a Java string converted into a UTF-8 encoded, null-terminated C string.This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array. The
CharsetEncoderclass should be used when more control over the encoding process is required.If the given string contains any
'\0'characters, they will be copied as well. This means that, depending on the method used to read the string, such asMemorySegment.getUtf8String(long)PREVIEW, the string will appear truncated when read again.- Implementation Requirements:
- The default implementation for this method copies the contents of the provided Java string
into a new memory segment obtained by calling
this.allocate(str.length() + 1). - Parameters:
str- the Java string to be converted into a C string.- Returns:
- a new memory segment with a Java string converted into a UTF-8 encoded, null-terminated C string
-
allocate
Returns a new memory segment initialized with the providedbytevalueas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg; - Parameters:
layout- the layout of the block of memory to be allocated.value- the value to be set in the newly allocated memory segment.- Returns:
- a new memory segment initialized with the provided
bytevalueas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocate
Returns a new memory segment initialized with the providedcharvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg; - Parameters:
layout- the layout of the block of memory to be allocated.value- the value to be set in the newly allocated memory segment.- Returns:
- a new memory segment initialized with the provided
charvalueas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocate
Returns a new memory segment initialized with the providedshortvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg; - Parameters:
layout- the layout of the block of memory to be allocated.value- the value to be set in the newly allocated memory segment.- Returns:
- a new memory segment initialized with the provided
shortvalueas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocate
Returns a new memory segment initialized with the providedintvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg; - Parameters:
layout- the layout of the block of memory to be allocated.value- the value to be set in the newly allocated memory segment.- Returns:
- a new memory segment initialized with the provided
intvalueas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocate
Returns a new memory segment initialized with the providedfloatvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg; - Parameters:
layout- the layout of the block of memory to be allocated.value- the value to be set in the newly allocated memory segment.- Returns:
- a new memory segment initialized with the provided
floatvalueas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocate
Returns a new memory segment initialized with the providedlongvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg; - Parameters:
layout- the layout of the block of memory to be allocated.value- the value to be set in the newly allocated memory segment.- Returns:
- a new memory segment initialized with the provided
longvalueas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocate
Returns a new memory segment initialized with the provideddoublevalueas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg; - Parameters:
layout- the layout of the block of memory to be allocated.value- the value to be set in the newly allocated memory segment.- Returns:
- a new memory segment initialized with the provided
doublevalueas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocate
Returns a new memory segment initialized with the address of the providedvalueas specified by the providedlayout(i.e. byte ordering, alignment and size).The address value might be narrowed according to the platform address size (see
ValueLayout.ADDRESSPREVIEW).- Implementation Requirements:
- The default implementation is equivalent to:
Objects.requireNonNull(value); MemorySegment seg = allocate(Objects.requireNonNull(layout)); seg.set(layout, 0, value); return seg; - Parameters:
layout- the layout of the block of memory to be allocated.value- the value to be set in the newly allocated memory segment.- Returns:
- a new memory segment initialized with the address of the provided
valueas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocateArray
default MemorySegmentPREVIEW allocateArray(ValueLayout.OfBytePREVIEW elementLayout, byte... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEbyteelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
int size = Objects.requireNonNull(elements).length; MemorySegment seg = allocateArray(Objects.requireNonNull(elementLayout), size); MemorySegment.copy(elements, 0, seg, elementLayout, 0, size); return seg; - Parameters:
elementLayout- the element layout of the array to be allocated.elements- the short elements to be copied to the newly allocated memory block.- Returns:
- a new memory segment with a byteSize()PREVIEW of
E*layout.byteSize()initialized with the providedEbyteelementsas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocateArray
default MemorySegmentPREVIEW allocateArray(ValueLayout.OfShortPREVIEW elementLayout, short... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEshortelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
int size = Objects.requireNonNull(elements).length; MemorySegment seg = allocateArray(Objects.requireNonNull(elementLayout), size); MemorySegment.copy(elements, 0, seg, elementLayout, 0, size); return seg; - Parameters:
elementLayout- the element layout of the array to be allocated.elements- the short elements to be copied to the newly allocated memory block.- Returns:
- a new memory segment with a byteSize()PREVIEW of
E*layout.byteSize()initialized with the providedEshortelementsas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocateArray
default MemorySegmentPREVIEW allocateArray(ValueLayout.OfCharPREVIEW elementLayout, char... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEcharelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
int size = Objects.requireNonNull(elements).length; MemorySegment seg = allocateArray(Objects.requireNonNull(elementLayout), size); MemorySegment.copy(elements, 0, seg, elementLayout, 0, size); return seg; - Parameters:
elementLayout- the element layout of the array to be allocated.elements- the short elements to be copied to the newly allocated memory block.- Returns:
- a new memory segment with a byteSize()PREVIEW of
E*layout.byteSize()initialized with the providedEcharelementsas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocateArray
Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEintelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
int size = Objects.requireNonNull(elements).length; MemorySegment seg = allocateArray(Objects.requireNonNull(elementLayout), size); MemorySegment.copy(elements, 0, seg, elementLayout, 0, size); return seg; - Parameters:
elementLayout- the element layout of the array to be allocated.elements- the short elements to be copied to the newly allocated memory block.- Returns:
- a new memory segment with a byteSize()PREVIEW of
E*layout.byteSize()initialized with the providedEintelementsas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocateArray
default MemorySegmentPREVIEW allocateArray(ValueLayout.OfFloatPREVIEW elementLayout, float... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEfloatelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
int size = Objects.requireNonNull(elements).length; MemorySegment seg = allocateArray(Objects.requireNonNull(elementLayout), size); MemorySegment.copy(elements, 0, seg, elementLayout, 0, size); return seg; - Parameters:
elementLayout- the element layout of the array to be allocated.elements- the short elements to be copied to the newly allocated memory block.- Returns:
- a new memory segment with a byteSize()PREVIEW of
E*layout.byteSize()initialized with the providedEfloatelementsas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocateArray
default MemorySegmentPREVIEW allocateArray(ValueLayout.OfLongPREVIEW elementLayout, long... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedElongelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
int size = Objects.requireNonNull(elements).length; MemorySegment seg = allocateArray(Objects.requireNonNull(elementLayout), size); MemorySegment.copy(elements, 0, seg, elementLayout, 0, size); return seg; - Parameters:
elementLayout- the element layout of the array to be allocated.elements- the short elements to be copied to the newly allocated memory block.- Returns:
- a new memory segment with a byteSize()PREVIEW of
E*layout.byteSize()initialized with the providedElongelementsas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocateArray
default MemorySegmentPREVIEW allocateArray(ValueLayout.OfDoublePREVIEW elementLayout, double... elements) Returns a new memory segment with a byteSize()PREVIEW ofE*layout.byteSize()initialized with the providedEdoubleelementsas specified by the providedlayout(i.e. byte ordering, alignment and size).- Implementation Requirements:
- The default implementation is equivalent to:
int size = Objects.requireNonNull(elements).length; MemorySegment seg = allocateArray(Objects.requireNonNull(elementLayout), size); MemorySegment.copy(elements, 0, seg, elementLayout, 0, size); return seg; - Parameters:
elementLayout- the element layout of the array to be allocated.elements- the short elements to be copied to the newly allocated memory block.- Returns:
- a new memory segment with a byteSize()PREVIEW of
E*layout.byteSize()initialized with the providedEdoubleelementsas specified by the providedlayout(i.e. byte ordering, alignment and size)
-
allocate
Returns a new memory segment with the given layout.- Implementation Requirements:
- The default implementation for this method calls
this.allocate(layout.byteSize(), layout.byteAlignment()). - Parameters:
layout- the layout of the block of memory to be allocated.- Returns:
- a new memory segment with the given layout
-
allocateArray
Returns a new memory segment with the givenelementLayoutandcount.- Implementation Requirements:
- The default implementation for this method calls
this.allocate(MemoryLayout.sequenceLayout(count, elementLayout)). - Parameters:
elementLayout- the array element layout.count- the array element count.- Returns:
- a new memory segment with the given
elementLayoutandcount - Throws:
IllegalArgumentException- ifelementLayout.byteSize() * countoverflows.IllegalArgumentException- ifcount < 0.
-
allocate
Returns a new memory segment with the givenbyteSize.- Implementation Requirements:
- The default implementation for this method calls
this.allocate(byteSize, 1). - Parameters:
byteSize- the size (in bytes) of the block of memory to be allocated.- Returns:
- a new memory segment with the given
byteSize - Throws:
IllegalArgumentException- ifbyteSize < 0
-
allocate
Returns a new memory segment with the givenbyteSizeandbyteAlignment.- Parameters:
byteSize- the size (in bytes) of the block of memory to be allocated.byteAlignment- the alignment (in bytes) of the block of memory to be allocated.- Returns:
- a new memory segment with the given
byteSizeandbyteAlignment - Throws:
IllegalArgumentException- ifbyteSize < 0,byteAlignment <= 0, or ifbyteAlignmentis not a power of 2.
-
slicingAllocator
Returns a segment allocator which 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
IndexOutOfBoundsExceptionwhen a slice of the provided segment with the requested size and alignment cannot be found.- Implementation Note:
- A slicing allocator is not thread-safe.
- Parameters:
segment- the segment which the returned allocator should slice from.- Returns:
- a new slicing allocator
-
prefixAllocator
Returns a segment allocator which responds to allocation requests by recycling a single segment. Each new allocation request will return a new slice starting at the segment offset0, hence the name prefix allocator. Equivalent to (but likely more efficient than) the following code:The returned allocator throwsMemorySegment segment = ... SegmentAllocator prefixAllocator = (size, align) -> segment.asSlice(0, size, align);IndexOutOfBoundsExceptionwhen 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.
- Parameters:
segment- the memory segment to be recycled by the returned allocator.- Returns:
- an allocator which recycles an existing segment upon each new allocation request.
-
SegmentAllocatorwhen preview features are enabled.