Class BumpAllocator
java.lang.Object
ai.tegmentum.webassembly4j.runtime.marshal.BumpAllocator
- All Implemented Interfaces:
MemoryAllocator
Simple bump allocator for WebAssembly linear memory.
Allocates memory by advancing a pointer through linear memory. Does not support freeing individual allocations. Useful for testing and one-shot function calls where memory reclamation is handled by discarding the entire instance.
-
Constructor Summary
ConstructorsConstructorDescriptionBumpAllocator(int startOffset, int limit) Creates a bump allocator starting at the given offset. -
Method Summary
Modifier and TypeMethodDescriptionintallocate(int size, int alignment) Allocates a block of memory with the given size and alignment.intReturns the current allocation offset (high-water mark).voidfree(int pointer, int size, int alignment) Frees a previously allocated block of memory.voidreset(int newOffset) Resets the allocator to the given offset, freeing all allocations past that point.
-
Constructor Details
-
BumpAllocator
public BumpAllocator(int startOffset, int limit) Creates a bump allocator starting at the given offset.- Parameters:
startOffset- the starting offset in linear memorylimit- the maximum offset (exclusive) that can be allocated
-
-
Method Details
-
allocate
public int allocate(int size, int alignment) Description copied from interface:MemoryAllocatorAllocates a block of memory with the given size and alignment.- Specified by:
allocatein interfaceMemoryAllocator- Parameters:
size- the number of bytes to allocatealignment- the required alignment in bytes (must be a power of 2)- Returns:
- the offset in linear memory where the block was allocated
-
free
public void free(int pointer, int size, int alignment) Description copied from interface:MemoryAllocatorFrees a previously allocated block of memory.- Specified by:
freein interfaceMemoryAllocator- Parameters:
pointer- the offset of the block to freesize- the size of the block in bytesalignment- the alignment of the block
-
currentOffset
public int currentOffset()Returns the current allocation offset (high-water mark).- Returns:
- the current offset
-
reset
public void reset(int newOffset) Resets the allocator to the given offset, freeing all allocations past that point.- Parameters:
newOffset- the offset to reset to
-