- All Implemented Interfaces:
AutoCloseable
,SegmentAllocatorPREVIEW
,Addressable
MemoryStack
relies on preview features of the Java platform:
MemoryStack
refers to one or more preview APIs:Arena
,MemoryLayout
,MemorySegment
,SegmentAllocator
.
This class should be used in a thread-local manner for stack allocations.
- Since:
- 0.1.0
- Author:
- Spasi, lwjgl3, squid233
- See Also:
-
Field Summary
Fields inherited from class overrungl.Pointer
address, allocator, managedSegment
-
Constructor Summary
ModifierConstructorDescriptionprotected
MemoryStack
(@Nullable MemorySegmentPREVIEW container, MemorySegmentPREVIEW address, long size, ArenaPREVIEW arena) Creates a newMemoryStack
backed by the specified memory region. -
Method Summary
Modifier and TypeMethodDescriptionaddress()
Returns the address of the backing off-heap memory.allocate
(long byteSize, long byteAlignment) bytes
(byte x) Single value version ofmalloc(long, long)
.bytes
(byte... values) Vararg version ofmalloc(long, long)
.bytes
(byte x, byte y) Two value version ofmalloc(long, long)
.bytes
(byte x, byte y, byte z) Three value version ofmalloc(long, long)
.bytes
(byte x, byte y, byte z, byte w) Four value version ofmalloc(long, long)
.calloc
(long size) Calloc version ofmalloc(long)
.calloc
(long alignment, long size) Calloc version ofmalloc(long, long)
.calloc
(MemoryLayoutPREVIEW layout) calloc
(MemoryLayoutPREVIEW layout, long num) void
close()
Callspop()
on thisMemoryStack
.static MemoryStackPREVIEW
create()
Creates a newMemoryStack
with the default size.static MemoryStackPREVIEW
create
(long capacity) Creates a newMemoryStack
with the specified size.static MemoryStackPREVIEW
create
(MemorySegmentPREVIEW buffer, long size, ArenaPREVIEW arena) Creates a newMemoryStack
backed by the specified memory buffer.doubles
(double x) Single value version ofmalloc(long, long)
.doubles
(double... values) Vararg version ofmalloc(long, long)
.doubles
(double x, double y) Two value version ofmalloc(long, long)
.doubles
(double x, double y, double z) Three value version ofmalloc(long, long)
.doubles
(double x, double y, double z, double w) Four value version ofmalloc(long, long)
.floats
(float x) Single value version ofmalloc(long, long)
.floats
(float... values) Vararg version ofmalloc(long, long)
.floats
(float x, float y) Two value version ofmalloc(long, long)
.floats
(float x, float y, float z) Three value version ofmalloc(long, long)
.floats
(float x, float y, float z, float w) Four value version ofmalloc(long, long)
.int
Returns the current frame index.long
Returns the current stack pointer.Returns the memory address at the current stack pointer.long
getSize()
Returns the size of the backing off-heap memory.ints
(int x) Single value version ofmalloc(long, long)
.ints
(int... values) Vararg version ofmalloc(long, long)
.ints
(int x, int y) Two value version ofmalloc(long, long)
.ints
(int x, int y, int z) Three value version ofmalloc(long, long)
.ints
(int x, int y, int z, int w) Four value version ofmalloc(long, long)
.longs
(long x) Single value version ofmalloc(long, long)
.longs
(long... values) Vararg version ofmalloc(long, long)
.longs
(long x, long y) Two value version ofmalloc(long, long)
.longs
(long x, long y, long z) Three value version ofmalloc(long, long)
.longs
(long x, long y, long z, long w) Four value version ofmalloc(long, long)
.malloc
(long size) malloc
(long alignment, long size) Allocates an alignedMemorySegment
PREVIEW on the stack.malloc
(MemoryLayoutPREVIEW layout) ncalloc
(long alignment, long num, long size) Allocates a block of memory on the stack for an array ofnum
elements, each of themsize
bytes long, and initializes all its bits to zero.static MemoryStackPREVIEW
ncreate
(MemorySegmentPREVIEW address, long size, ArenaPREVIEW arena) Creates a newMemoryStack
backed by the specified memory region.nmalloc
(long alignment, long size) Allocates a block ofsize
bytes of memory on the stack.static MemorySegmentPREVIEW
nstackCalloc
(long alignment, long num, long size) Thread-local version ofncalloc(long, long, long)
.static MemorySegmentPREVIEW
nstackMalloc
(long alignment, long size) Thread-local version ofnmalloc(long, long)
.Single value version ofmalloc(long, long)
.pointers
(MemorySegmentPREVIEW... values) Vararg version ofmalloc(long, long)
.Two value version ofmalloc(long, long)
.Three value version ofmalloc(long, long)
.pointers
(MemorySegmentPREVIEW x, MemorySegmentPREVIEW y, MemorySegmentPREVIEW z, MemorySegmentPREVIEW w) Four value version ofmalloc(long, long)
.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.void
setPointer
(long pointer) Sets the current stack pointer.shorts
(short x) Single value version ofmalloc(long, long)
.shorts
(short... values) Vararg version ofmalloc(long, long)
.shorts
(short x, short y) Two value version ofmalloc(long, long)
.shorts
(short x, short y, short z) Three value version ofmalloc(long, long)
.shorts
(short x, short y, short z, short w) Four value version ofmalloc(long, long)
.static MemorySegmentPREVIEW
stackCalloc
(long size) Thread-local version ofcalloc
.static MemoryStackPREVIEW
stackGet()
Returns the stack of the current thread.static MemorySegmentPREVIEW
stackMalloc
(long size) Thread-local version ofmalloc
.static MemoryStackPREVIEW
stackPop()
Callspop()
on the stack of the current thread.static MemoryStackPREVIEW
Callspush()
on the stack of the current thread.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.foreign.SegmentAllocatorPREVIEW
allocate, allocate, allocate, allocate, allocate, allocate, allocate, allocate, allocate, allocate, allocateArray, allocateArray, allocateArray, allocateArray, allocateArray, allocateArray, allocateArray, allocateArray, allocateUtf8String
-
Field Details
-
frameIndex
protected int frameIndex
-
-
Constructor Details
-
MemoryStack
protected MemoryStack(@Nullable @Nullable MemorySegmentPREVIEW container, MemorySegmentPREVIEW address, long size, ArenaPREVIEW arena) Creates a newMemoryStack
backed by the specified memory region.In the initial state, there is no active stack frame. The
push()
method must be used before any allocations.- Parameters:
container
- the backing memory buffer, may be nulladdress
- the backing memory addresssize
- the backing memory sizearena
- the backing arena
-
-
Method Details
-
create
Creates a newMemoryStack
with 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 newMemoryStack
with 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 newMemoryStack
backed by the specified memory buffer.In the initial state, there is no active stack frame. The
push()
method must be used before any allocations.- Parameters:
buffer
- the backing memory buffersize
- the memory buffer sizearena
- the backing arena
-
ncreate
public static MemoryStackPREVIEW ncreate(MemorySegmentPREVIEW address, long size, ArenaPREVIEW arena) Creates a newMemoryStack
backed by the specified memory region.In the initial state, there is no active stack frame. The
push()
method must be used before any allocations.- Parameters:
address
- the backing memory addresssize
- the backing memory sizearena
- the backing arena
-
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
- Returns:
- this stack
-
pop
Pops the current stack frame and moves the stack pointer to the end of the previous stack frame.- Returns:
- this stack
-
close
public void close()Callspop()
on thisMemoryStack
.This method should not be used directly. It is called automatically when the
MemoryStack
is used as a resource in a try-with-resources statement.- Specified by:
close
in interfaceAutoCloseable
-
address
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
.- Specified by:
address
in interfaceAddressable
- Overrides:
address
in classPointer
- Returns:
- the address of the backing off-heap memory
-
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.
- Returns:
- the size of the backing off-heap memory
-
getFrameIndex
public int getFrameIndex()Returns the current frame index.This is the current number of nested
push()
calls.- Returns:
- the current frame index
-
getPointerAddress
Returns the memory address at the current stack pointer.- 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
pointer
is equal tosize
. On every allocationpointer
is reduced by the allocated size (after alignment) andaddress + pointer
points to the first byte of the last allocation.Effectively, this methods returns how many more bytes may be allocated on the stack.
- Returns:
- the current stack pointer
-
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.
-
nmalloc
Allocates a block ofsize
bytes of memory on the stack. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.- Parameters:
alignment
- the required alignmentsize
- the allocation size- Returns:
- the memory address on the stack for the requested allocation
-
ncalloc
Allocates a block of memory on the stack for an array ofnum
elements, each of themsize
bytes long, and initializes all its bits to zero.- Parameters:
alignment
- the required element alignmentnum
- the number of elements to allocatesize
- the size of each element- Returns:
- the memory address on the stack for the requested allocation
-
malloc
Allocates an alignedMemorySegment
PREVIEW on the stack.- Parameters:
alignment
- the required buffer alignmentsize
- the number of elements in the buffer- Returns:
- the allocated buffer
-
calloc
Calloc version ofmalloc(long, long)
. -
malloc
- Parameters:
size
- the allocation size- Returns:
- the memory address on the stack for the requested allocation
-
malloc
-
calloc
Calloc version ofmalloc(long)
. -
calloc
-
calloc
-
allocate
public MemorySegmentPREVIEW allocate(long byteSize, long byteAlignment) throws IllegalArgumentException - Specified by:
allocate
in interfaceSegmentAllocatorPREVIEW
- Throws:
IllegalArgumentException
-
bytes
Single value version ofmalloc(long, long)
. -
bytes
Two value version ofmalloc(long, long)
. -
bytes
Three value version ofmalloc(long, long)
. -
bytes
Four value version ofmalloc(long, long)
. -
bytes
Vararg version ofmalloc(long, long)
. -
shorts
Single value version ofmalloc(long, long)
. -
shorts
Two value version ofmalloc(long, long)
. -
shorts
Three value version ofmalloc(long, long)
. -
shorts
Four value version ofmalloc(long, long)
. -
shorts
Vararg version ofmalloc(long, long)
. -
ints
Single value version ofmalloc(long, long)
. -
ints
Two value version ofmalloc(long, long)
. -
ints
Three value version ofmalloc(long, long)
. -
ints
Four value version ofmalloc(long, long)
. -
ints
Vararg version ofmalloc(long, long)
. -
longs
Single value version ofmalloc(long, long)
. -
longs
Two value version ofmalloc(long, long)
. -
longs
Three value version ofmalloc(long, long)
. -
longs
Four value version ofmalloc(long, long)
. -
longs
Vararg version ofmalloc(long, long)
. -
floats
Single value version ofmalloc(long, long)
. -
floats
Two value version ofmalloc(long, long)
. -
floats
Three value version ofmalloc(long, long)
. -
floats
Four value version ofmalloc(long, long)
. -
floats
Vararg version ofmalloc(long, long)
. -
doubles
Single value version ofmalloc(long, long)
. -
doubles
Two value version ofmalloc(long, long)
. -
doubles
Three value version ofmalloc(long, long)
. -
doubles
Four value version ofmalloc(long, long)
. -
doubles
Vararg version ofmalloc(long, long)
. -
pointers
Single value version ofmalloc(long, long)
. -
pointers
Two value version ofmalloc(long, long)
. -
pointers
public MemorySegmentPREVIEW pointers(MemorySegmentPREVIEW x, MemorySegmentPREVIEW y, MemorySegmentPREVIEW z) Three value version ofmalloc(long, long)
. -
pointers
public MemorySegmentPREVIEW pointers(MemorySegmentPREVIEW x, MemorySegmentPREVIEW y, MemorySegmentPREVIEW z, MemorySegmentPREVIEW w) Four value version ofmalloc(long, long)
. -
pointers
Vararg version ofmalloc(long, long)
. -
stackGet
Returns the stack of the current thread.- Returns:
- the stack of the current thread
-
stackPush
Callspush()
on the stack of the current thread.- Returns:
- the stack of the current thread.
-
stackPop
Callspop()
on the stack of the current thread.- Returns:
- the stack of the current thread.
-
nstackMalloc
Thread-local version ofnmalloc(long, long)
. -
nstackCalloc
Thread-local version ofncalloc(long, long, long)
. -
stackMalloc
Thread-local version ofmalloc
. -
stackCalloc
Thread-local version ofcalloc
.
-
MemoryStack
when preview features are enabled.