Class ArenaResourceManager
java.lang.Object
ai.tegmentum.wasmtime4j.panama.ArenaResourceManager
- All Implemented Interfaces:
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classManaged native resource that tracks its lifecycle. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new arena resource manager with a shared arena and tracking enabled.ArenaResourceManager(Arena arena, boolean trackingEnabled) Creates a new arena resource manager with the specified arena. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the resource manager and cleans up all resources.getArena()Gets the underlying arena.booleanisValid()Checks if the resource manager is valid (not closed).manageNativeResource(MemorySegment nativePointer, Runnable cleanup, String description) Creates a managed wrapper around an existing native resource.registerManagedNativeResource(Object owner, MemorySegment nativeHandle, Runnable cleanupAction) Registers a managed native resource with a cleanup action.voidunregisterManagedResource(Object owner) Unregisters a managed resource.
-
Constructor Details
-
ArenaResourceManager
public ArenaResourceManager()Creates a new arena resource manager with a shared arena and tracking enabled. -
ArenaResourceManager
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 managementtrackingEnabled- whether to enable resource tracking
-
-
Method Details
-
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 resourcecleanup- cleanup action to perform when resource is releaseddescription- 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 ownernativeHandle- the native handlecleanupAction- the cleanup action to run when the resource is closed- Returns:
- a managed native resource
-
unregisterManagedResource
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:
closein interfaceAutoCloseable
-