Class MemorySegmentAllocator

java.lang.Object
eu.cloudnetservice.driver.impl.network.netty.memory.MemorySegmentAllocator

final class MemorySegmentAllocator extends Object
Unsafe allocator implementation for memory segments. Introduced to prevent checks when a shared arena (usually used to allocate memory segments) is closed. This implementation just frees the provided memory segment, regardless of possible in-flight usages.
Since:
4.0
  • Field Details

  • Constructor Details

    • MemorySegmentAllocator

      MemorySegmentAllocator()
  • Method Details

    • validateAllocatedSegment

      private static void validateAllocatedSegment(@NonNull @NonNull MemorySegment segment, long byteCount)
      Validates that the given memory segment was successfully allocated, throwing an exception if that is not the case.
      Parameters:
      segment - the segment to validate.
      byteCount - the requested byte count that should be allocated.
      Throws:
      NullPointerException - if the given segment is null.
      OutOfMemoryError - if the given segment was incorrectly allocated.
    • malloc

      @NonNull public static @NonNull MemorySegment malloc(long byteCount)
      Allocates an off-heap memory block of the given requested size and returns an MemorySegment that wraps it. The memory is zeroed out.
      Parameters:
      byteCount - the count of bytes to allocate.
      Returns:
      a memory segment wrapping the allocated block of memory.
      Throws:
      IllegalStateException - if the invocation of the native malloc method failed.
      OutOfMemoryError - if the given byte count couldn't be reserved by the operating system.
    • free

      public static void free(@NonNull @NonNull MemorySegment segment)
      Frees the memory block associated with the given memory segment.
      Parameters:
      segment - the segment representing the memory block to free.
      Throws:
      NullPointerException - if the given memory segment is null.
      IllegalStateException - if the invocation of the native free method failed.