A context for scoping the lifetime of foreign objects.
The need for this mechanism is best explained with an example. When folding a GETFIELD bytecode
denoting a
final static non-primitive field, libgraal can create a
JavaConstant
wrapping a handle to the field's value in the HotSpot heap. This handle must be released before
HotSpot can reclaim the object it references. Performing a compilation in the scope of a
local context ensures the handle is
released once the compilation completes, allowing the HotSpot GC to subsequently reclaim the
HotSpot object. When libgraal creates data structures that outlive a single compilation and may
contain foreign object references (e.g. snippet graphs), it must enter the
global context. Foreign object
handles created in the global context are only released once their
JavaConstant wrappers
are reclaimed by the libgraal GC.
CompilationContexts have no impact on
JavaConstants that do not encapsulate a
foreign object reference.
The object returned by
HotSpotGraalServices.enterGlobalCompilationContext() or
HotSpotGraalServices.openLocalCompilationContext(java.lang.Object) should be used in a try-with-resources
statement. Failure to close a context will almost certainly result in foreign objects being
leaked.