Module java.base

Interface SegmentAllocator

All Known Subinterfaces:
Arena
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface SegmentAllocator
An object that may be used to allocate memory segments. Clients implementing this interface must implement the 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:

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 handlesRESTRICTED 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.

Since:
22