public class PointerScope extends Object implements AutoCloseable
Pointer
objects attach themselves automatically on Pointer.init(long, long, long, long)
to the first PointerScope
found in scopeStack
that they can to based on the classes found in forClasses
. The user can then
call deallocate()
, or rely on close()
to release in a timely fashion all attached Pointer objects,
instead of relying on the garbage collector.Modifier and Type | Field and Description |
---|---|
(package private) boolean |
extend
When set to true, the next call to
close() does not release but resets this variable. |
(package private) Class<? extends Pointer>[] |
forClasses
When not empty, indicates the classes of objects that are allowed to be attached.
|
private static Logger |
logger |
(package private) Deque<Pointer> |
pointerStack
The stack keeping references to attached
Pointer objects. |
(package private) static ThreadLocal<Deque<PointerScope>> |
scopeStack
A thread-local stack of
PointerScope objects. |
Constructor and Description |
---|
PointerScope(Class<? extends Pointer>... forClasses)
Initializes
forClasses , and pushes itself on the scopeStack . |
Modifier and Type | Method and Description |
---|---|
PointerScope |
attach(Pointer p)
Pushes the Pointer onto the
pointerStack of this Scope and calls Pointer.retainReference() . |
void |
close()
Pops from
pointerStack all attached pointers,
calls Pointer.releaseReference() on them, unless extended,
in which case it only resets the extend flag instead,
and finally removes itself from scopeStack . |
void |
deallocate()
Pops from
pointerStack all attached pointers,
and calls Pointer.deallocate() on them. |
PointerScope |
detach(Pointer p)
Removes the Pointer from the
pointerStack of this Scope
and calls Pointer.releaseReference() . |
PointerScope |
extend()
|
Class<? extends Pointer>[] |
forClasses() |
static PointerScope |
getInnerScope()
Returns
scopeStack.get().peek() , the last opened scope not yet closed. |
static Iterator<PointerScope> |
getScopeIterator()
Returns
scopeStack.get().iterator() , all scopes not yet closed. |
private static final Logger logger
static final ThreadLocal<Deque<PointerScope>> scopeStack
PointerScope
objects. Pointer objects attach themselves
automatically on Pointer.init(long, long, long, long)
to the first one they can to on the stack.Class<? extends Pointer>[] forClasses
boolean extend
close()
does not release but resets this variable.public PointerScope(Class<? extends Pointer>... forClasses)
forClasses
, and pushes itself on the scopeStack
.public static PointerScope getInnerScope()
scopeStack.get().peek()
, the last opened scope not yet closed.public static Iterator<PointerScope> getScopeIterator()
scopeStack.get().iterator()
, all scopes not yet closed.public PointerScope attach(Pointer p)
pointerStack
of this Scope and calls Pointer.retainReference()
.IllegalArgumentException
- when it is not an instance of a class in forClasses
.public PointerScope detach(Pointer p)
pointerStack
of this Scope
and calls Pointer.releaseReference()
.public PointerScope extend()
public void close()
pointerStack
all attached pointers,
calls Pointer.releaseReference()
on them, unless extended,
in which case it only resets the extend
flag instead,
and finally removes itself from scopeStack
.close
in interface AutoCloseable
public void deallocate()
pointerStack
all attached pointers,
and calls Pointer.deallocate()
on them.Copyright © 2020. All rights reserved.