Class BumpAllocator

java.lang.Object
ai.tegmentum.webassembly4j.runtime.marshal.BumpAllocator
All Implemented Interfaces:
MemoryAllocator

public final class BumpAllocator extends Object implements 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

    Constructors
    Constructor
    Description
    BumpAllocator(int startOffset, int limit)
    Creates a bump allocator starting at the given offset.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    allocate(int size, int alignment)
    Allocates a block of memory with the given size and alignment.
    int
    Returns the current allocation offset (high-water mark).
    void
    free(int pointer, int size, int alignment)
    Frees a previously allocated block of memory.
    void
    reset(int newOffset)
    Resets the allocator to the given offset, freeing all allocations past that point.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 memory
      limit - the maximum offset (exclusive) that can be allocated
  • Method Details

    • allocate

      public int allocate(int size, int alignment)
      Description copied from interface: MemoryAllocator
      Allocates a block of memory with the given size and alignment.
      Specified by:
      allocate in interface MemoryAllocator
      Parameters:
      size - the number of bytes to allocate
      alignment - 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: MemoryAllocator
      Frees a previously allocated block of memory.
      Specified by:
      free in interface MemoryAllocator
      Parameters:
      pointer - the offset of the block to free
      size - the size of the block in bytes
      alignment - 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