Enum Class LibcArena
- All Implemented Interfaces:
Serializable
,AutoCloseable
,Comparable<LibcArena>
,Constable
,Arena
,SegmentAllocator
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum Constants -
Method Summary
Modifier and TypeMethodDescription@NotNull MemorySegment
allocate
(long byteSize, long byteAlignment) Allocates memory using system libc
aligned_alloc
Note that you're in charge of freeing the memory using
free(java.lang.foreign.MemorySegment)
, otherwise there will be a memory leak.void
close()
void
free
(@NotNull MemorySegment ms) Frees memory that was allocated byallocate(long, long)
.void
freeNonAllocated
(@NotNull MemorySegment ms) Frees memory that was allocated by libc allocator, but not viaallocate(long, long)
.scope()
static LibcArena
Returns the enum constant of this class with the specified name.static LibcArena[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.Methods inherited from class java.lang.Enum
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
Methods inherited from interface java.lang.foreign.SegmentAllocator
allocate, allocate, allocate, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom
-
Enum Constant Details
-
INSTANCE
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum class has no constant with the specified nameNullPointerException
- if the argument is null
-
allocate
Allocates memory using system libc
aligned_alloc
Note that you're in charge of freeing the memory using
free(java.lang.foreign.MemorySegment)
, otherwise there will be a memory leak. Be extra careful when usingLibcArena
in combination withallocate
series methods.- Specified by:
allocate
in interfaceArena
- Specified by:
allocate
in interfaceSegmentAllocator
- Parameters:
byteSize
- The size of the memory to allocatebyteAlignment
- The alignment of the memory to allocate- Returns:
- A
MemorySegment
representing the allocated memory with all bytes zeroed - Throws:
IllegalArgumentException
- If the byte size or alignment is invalidOutOfMemoryError
- If the memory allocation fails
-
free
Frees memory that was allocated byallocate(long, long)
.- Parameters:
ms
- The memory segment to free
-
freeNonAllocated
Frees memory that was allocated by libc allocator, but not via
allocate(long, long)
.Some libraries, like
stb_vorbis
, may allocate memory using libc allocators internally, and require you to free that memory usingfree
from the same libc implementation. The best way to do this is to use thefree
method encapsulation provided by that library integration. But if that is not available, this method can be used as an alternative with some risk.This function is marked as
Unsafe
, because if the memory was not exactly allocated by the same allocatorLibcArena
found, it will lead to undefined behavior. This relates with platforms, build system and many other factors. Double check before really doing this. -
scope
-
close
public void close()- Specified by:
close
in interfaceArena
- Specified by:
close
in interfaceAutoCloseable
-