Class ArenaResourceManager

java.lang.Object
ai.tegmentum.wasmtime4j.panama.ArenaResourceManager
All Implemented Interfaces:
AutoCloseable

public final class ArenaResourceManager extends Object implements AutoCloseable
Arena-based resource management for Panama FFI operations.

This class provides automatic resource cleanup and lifecycle management for native resources using Arena-based memory allocation. It ensures proper cleanup of native resources to prevent memory leaks.

The resource manager uses Java's Cleaner API as a safety net for resources that are not properly closed, while encouraging explicit resource management through try-with-resources patterns.

  • Constructor Details

    • ArenaResourceManager

      public ArenaResourceManager()
      Creates a new arena resource manager with a shared arena and tracking enabled.
    • ArenaResourceManager

      public ArenaResourceManager(Arena arena, boolean trackingEnabled)
      Creates a new arena resource manager with the specified arena.

      The arena is NOT owned by this manager and will NOT be closed when the manager is closed. The caller is responsible for closing the arena.

      Parameters:
      arena - the arena to use for resource management
      trackingEnabled - whether to enable resource tracking
  • Method Details

    • getArena

      public Arena getArena()
      Gets the underlying arena.
      Returns:
      the arena
      Throws:
      IllegalStateException - if the manager is closed
    • manageNativeResource

      public ArenaResourceManager.ManagedNativeResource manageNativeResource(MemorySegment nativePointer, Runnable cleanup, String description)
      Creates a managed wrapper around an existing native resource.
      Parameters:
      nativePointer - pointer to the native resource
      cleanup - cleanup action to perform when resource is released
      description - description of the resource for tracking
      Returns:
      managed native resource
      Throws:
      IllegalStateException - if the manager is closed
    • registerManagedNativeResource

      public ArenaResourceManager.ManagedNativeResource registerManagedNativeResource(Object owner, MemorySegment nativeHandle, Runnable cleanupAction)
      Registers a managed native resource with a cleanup action.
      Parameters:
      owner - the resource owner
      nativeHandle - the native handle
      cleanupAction - the cleanup action to run when the resource is closed
      Returns:
      a managed native resource
    • unregisterManagedResource

      public void unregisterManagedResource(Object owner)
      Unregisters a managed resource.
      Parameters:
      owner - the resource owner to unregister
    • isValid

      public boolean isValid()
      Checks if the resource manager is valid (not closed).
      Returns:
      true if valid, false if closed
    • close

      public void close()
      Closes the resource manager and cleans up all resources.
      Specified by:
      close in interface AutoCloseable