Class BufferRefImpl

java.lang.Object
express.mvp.roray.ffm.pool.BufferRefImpl
All Implemented Interfaces:
BufferRef

public final class BufferRefImpl extends Object implements BufferRef
Lock-free implementation of BufferRef with atomic reference counting.
  • Constructor Summary

    Constructors
    Constructor
    Description
    BufferRefImpl(MemorySegment segment, int poolIndex, IntConsumer releaseAction)
    Creates a new buffer reference for a pooled segment.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the raw native address of the segment.
    int
    Returns the current length of valid data in the buffer (bytes).
    void
    length(int newLength)
    Sets the length of valid data in the buffer (bytes).
    int
    Returns the unique index of this buffer in its pool.
    void
    Decrements the reference count (auto-returns to pool at 0).
    void
    Resets this buffer for reuse after acquisition from pool (refCount 0 -> 1).
    void
    Increments the reference count.
    Returns the underlying FFM MemorySegment.
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • BufferRefImpl

      public BufferRefImpl(MemorySegment segment, int poolIndex, IntConsumer releaseAction)
      Creates a new buffer reference for a pooled segment.
      Parameters:
      segment - the pooled memory segment
      poolIndex - the index used by the pool
      releaseAction - callback invoked when the buffer returns to the pool
  • Method Details

    • reset

      public void reset()
      Resets this buffer for reuse after acquisition from pool (refCount 0 -> 1).
    • segment

      public MemorySegment segment()
      Description copied from interface: BufferRef
      Returns the underlying FFM MemorySegment.

      Warning: Accessing this segment after BufferRef.release() has reduced the refCount to 0 is undefined behavior. The segment may be reused by another thread or the underlying memory may be invalidated.

      Specified by:
      segment in interface BufferRef
    • address

      public long address()
      Description copied from interface: BufferRef
      Returns the raw native address of the segment.

      This is cached to avoid repeated FFM overhead on hot paths.

      Specified by:
      address in interface BufferRef
    • poolIndex

      public int poolIndex()
      Description copied from interface: BufferRef
      Returns the unique index of this buffer in its pool.
      Specified by:
      poolIndex in interface BufferRef
      Returns:
      pool index (0-based)
    • length

      public int length()
      Description copied from interface: BufferRef
      Returns the current length of valid data in the buffer (bytes).
      Specified by:
      length in interface BufferRef
    • length

      public void length(int newLength)
      Description copied from interface: BufferRef
      Sets the length of valid data in the buffer (bytes).
      Specified by:
      length in interface BufferRef
    • retain

      public void retain()
      Description copied from interface: BufferRef
      Increments the reference count.
      Specified by:
      retain in interface BufferRef
    • release

      public void release()
      Description copied from interface: BufferRef
      Decrements the reference count (auto-returns to pool at 0).
      Specified by:
      release in interface BufferRef
    • toString

      public String toString()
      Overrides:
      toString in class Object