Class SegmentStack
- All Implemented Interfaces:
AutoCloseable, SegmentAllocator, StackAllocator<SegmentStack>
This class should be used in a thread-local manner for stack allocations.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionallocate(long byteSize) Same asSegmentAllocator.allocate(long), but withValueLayout.ADDRESSalignment and no zero-initialization.allocate(long byteSize, long byteAlignment) Same asArena.allocate(long, long), but without zero-initialization.allocate(MemoryLayout layout) Same asSegmentAllocator.allocate(MemoryLayout), but without zero-initialization.allocate(MemoryLayout elementLayout, long count) Same asSegmentAllocator.allocate(MemoryLayout, long), but without zero-initialization.calloc(long byteSize) Same asSegmentAllocator.allocate(long).calloc(long byteSize, long byteAlignment) Same asArena.allocate(long, long).calloc(MemoryLayout layout) calloc(MemoryLayout elementLayout, long count) voidclose()Callspop()on thisMemoryStack.static SegmentStackcreate()Creates a newMemoryStackwith the default size.static SegmentStackcreate(long capacity) Creates a newMemoryStackwith the specified size.static SegmentStackcreate(MemorySegment segment) Creates a newMemoryStackbacked by the specified memory segment.longReturns the address of the backing off-heap memory.intReturns the current frame index.longReturns the current stack pointer.longReturns the memory address at the current stack pointer.longgetSize()Returns the size of the backing off-heap memory.pop()Pops the current stack frame and moves the stack pointer to the end of the previous stack frame.push()Stores the current stack pointer and pushes a new frame to the stack.voidsetPointer(long pointer) Sets the current stack pointer.static SegmentStackstackGet()Returns the stack of the current thread.static SegmentStackCallspush()on the stack of the current thread.Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface SegmentAllocator
allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom, allocateFrom
-
Method Details
-
create
Creates a newMemoryStackwith the default size.In the initial state, there is no active stack frame. The
push()method must be used before any allocations. -
create
Creates a newMemoryStackwith the specified size.In the initial state, there is no active stack frame. The
push()method must be used before any allocations.- Parameters:
capacity- the maximum number of bytes that may be allocated on the stack
-
create
Creates a newMemoryStackbacked by the specified memory segment.In the initial state, there is no active stack frame. The
push()method must be used before any allocations.- Parameters:
segment- the backing memory segment
-
push
Stores the current stack pointer and pushes a new frame to the stack.This method should be called when entering a method, before doing any stack allocations. When exiting a method, call the
pop()method to restore the previous stack frame.Pairs of push/pop calls may be nested. Care must be taken to:
- match every push with a pop
- not call pop before push has been called at least once
- not nest push calls to more than the maximum supported depth
- Specified by:
pushin interfaceStackAllocator<SegmentStack>- Returns:
- this stack
-
pop
Pops the current stack frame and moves the stack pointer to the end of the previous stack frame.- Specified by:
popin interfaceStackAllocator<SegmentStack>- Returns:
- this stack
-
close
public void close()Callspop()on thisMemoryStack.This method should not be used directly. It is called automatically when the
MemoryStackis used as a resource in a try-with-resources statement.- Specified by:
closein interfaceAutoCloseable
-
getAddress
public long getAddress()Returns the address of the backing off-heap memory.The stack grows "downwards", so the bottom of the stack is at
address + size, while the top is ataddress. -
getSize
public long getSize()Returns the size of the backing off-heap memory.This is the maximum number of bytes that may be allocated on the stack.
-
getFrameIndex
public int getFrameIndex()Returns the current frame index.This is the current number of nested
push()calls. -
getPointerAddress
public long getPointerAddress()Returns the memory address at the current stack pointer. -
getPointer
public long getPointer()Returns the current stack pointer.The stack grows "downwards", so when the stack is empty
pointeris equal tosize. On every allocationpointeris reduced by the allocated size (after alignment) andaddress + pointerpoints to the first byte of the last allocation.Effectively, this methods returns how many more bytes may be allocated on the stack.
-
setPointer
public void setPointer(long pointer) Sets the current stack pointer.This method directly manipulates the stack pointer. Using it irresponsibly may break the internal state of the stack. It should only be used in rare cases or in auto-generated code.
-
allocate
Same asSegmentAllocator.allocate(long), but withValueLayout.ADDRESSalignment and no zero-initialization.- Specified by:
allocatein interfaceSegmentAllocator
-
allocate
Same asArena.allocate(long, long), but without zero-initialization.- Specified by:
allocatein interfaceSegmentAllocator
-
allocate
Same asSegmentAllocator.allocate(MemoryLayout), but without zero-initialization.- Specified by:
allocatein interfaceSegmentAllocator
-
allocate
Same asSegmentAllocator.allocate(MemoryLayout, long), but without zero-initialization.- Specified by:
allocatein interfaceSegmentAllocator
-
calloc
Same asSegmentAllocator.allocate(long). -
calloc
Same asArena.allocate(long, long). -
calloc
-
calloc
-
stackGet
Returns the stack of the current thread. -
stackPush
Callspush()on the stack of the current thread.- Returns:
- the stack of the current thread.
-