Enum Class LibcArena

java.lang.Object
java.lang.Enum<LibcArena>
club.doki7.ffm.LibcArena
All Implemented Interfaces:
Serializable, AutoCloseable, Comparable<LibcArena>, Constable, Arena, SegmentAllocator

public enum LibcArena extends Enum<LibcArena> implements Arena
  • Enum Constant Details

    • INSTANCE

      public static final LibcArena INSTANCE
  • Method Details

    • values

      public static LibcArena[] 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

      public static LibcArena valueOf(String name)
      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 name
      NullPointerException - if the argument is null
    • allocate

      @NotNull public @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. Be extra careful when using LibcArena in combination with allocate series methods.

      Specified by:
      allocate in interface Arena
      Specified by:
      allocate in interface SegmentAllocator
      Parameters:
      byteSize - The size of the memory to allocate
      byteAlignment - 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 invalid
      OutOfMemoryError - If the memory allocation fails
    • free

      public void free(@NotNull @NotNull MemorySegment ms)
      Frees memory that was allocated by allocate(long, long).
      Parameters:
      ms - The memory segment to free
    • freeNonAllocated

      @Unsafe public void freeNonAllocated(@NotNull @NotNull MemorySegment ms)

      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 using free from the same libc implementation. The best way to do this is to use the free 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 allocator LibcArena found, it will lead to undefined behavior. This relates with platforms, build system and many other factors. Double check before really doing this.

    • scope

      @Nullable public MemorySegment.Scope scope()
      Specified by:
      scope in interface Arena
    • close

      public void close()
      Specified by:
      close in interface Arena
      Specified by:
      close in interface AutoCloseable