Class BufferRefImpl
- All Implemented Interfaces:
BufferRef
BufferRef with atomic reference counting.
This implementation uses VarHandle for lock-free CAS operations on the reference
count, providing thread-safe retain/release without blocking.
Memory Layout
- segment: Immutable reference to off-heap memory
- address: Cached native address for fast access
- poolIndex: Index in the parent pool's buffer array
- registrationId: io_uring buffer registration ID
- refCount: Atomic reference count (volatile int)
- length: Current valid data length (mutable)
Reference Count States
- 0: Buffer is in pool (available for acquisition)
- 1: Buffer is acquired by one owner
- >1: Buffer is shared across multiple owners
Thread Safety
retain() and release() use CAS loops for lock-free atomic updates. The
length field is not synchronized and should only be modified by the current owner.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionBufferRefImpl(MemorySegment segment, int poolIndex, short registrationId, IntConsumer releaseAction) Creates a new buffer reference. -
Method Summary
Modifier and TypeMethodDescriptionlongaddress()Returns the raw native address of the segment.intlength()Returns the current length of valid data in the buffer.voidlength(int newLength) Sets the length of valid data in the buffer.intReturns the unique index of this buffer in the global pool.shortReturns the io_uring registration ID if registered.voidrelease()Decrements the reference count.voidreset()Resets the buffer for reuse after acquisition from pool.voidretain()Increments the reference count.segment()Returns the underlying FFM MemorySegment.toString()
-
Constructor Details
-
BufferRefImpl
public BufferRefImpl(MemorySegment segment, int poolIndex, short registrationId, IntConsumer releaseAction) Creates a new buffer reference.Initially created with refCount=0 (in pool state). Call
reset()when acquiring from pool.- Parameters:
segment- the memory segment backing this bufferpoolIndex- the buffer's index in the poolregistrationId- the io_uring registration IDreleaseAction- callback to return buffer to pool
-
-
Method Details
-
reset
public void reset()Resets the buffer for reuse after acquisition from pool.Note: Should only be called by the pool during acquire. Sets refCount to 1 and length to 0. Uses CAS to ensure the buffer was actually in the pool (refCount=0).
- Throws:
IllegalStateException- if refCount was not 0 (indicates a bug)
-
segment
Description copied from interface:BufferRefReturns 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. -
address
-
poolIndex
public int poolIndex()Description copied from interface:BufferRefReturns the unique index of this buffer in the global pool.This enables "Structure of Arrays" (SoA) patterns where metadata (refCounts, timestamps, operation tokens) is stored in separate primitive arrays indexed by this ID, rather than in object fields.
-
registrationId
public short registrationId()Description copied from interface:BufferRefReturns the io_uring registration ID if registered.For io_uring backends, this is the buffer index used with
IORING_OP_READ_FIXEDandIORING_OP_WRITE_FIXED.- Specified by:
registrationIdin interfaceBufferRef- Returns:
- the registration ID, or -1 if not registered
-
length
-
length
-
retain
-
release
-
toString
-