public abstract class StackContext extends AllocatorContext
This class represents a stack allocator context
;
(using thread-local pools or RTSJ ScopedMemory
).
Stacks allocations reduce heap memory allocation and often result in faster execution time for almost all objects but the smallest one.
Stack allocated objects should never be assigned to static members
(see ImmortalContext
). Also, methods entering/exiting stack
contexts should ensure that stack allocated objects do not escape from
their context scope. If necessary, stack objects can be exported using
AllocatorContext.outerExecute(java.lang.Runnable)
or AllocatorContext.outerCopy(T)
:
public class LargeInteger implements ValueType, Realtime {
public LargeInteger sqrt() {
StackContext.enter();
try {
LargeInteger result = ZERO;
LargeInteger k = this.shiftRight(this.bitLength() / 2)); // First approximation.
while (true) { // Newton Iteration.
result = (k.plus(this.divide(k))).shiftRight(1);
if (result.equals(k)) return StackContext.outerCopy(result); // Exports result.
k = result;
}
} finally {
StackContext.exit();
}
}
}
AllocatorContext.Reference<T>
Modifier and Type | Field and Description |
---|---|
static Configurable<java.lang.Class<? extends StackContext>> |
DEFAULT
Holds the default implementation.
|
Constructor and Description |
---|
StackContext() |
Modifier and Type | Method and Description |
---|---|
static void |
enter()
Enters the
DEFAULT stack context. |
static void |
enter(boolean condition)
Enters a stack context only if the specified condition is verified.
|
static void |
exit()
Exits the current stack context.
|
static void |
exit(boolean condition)
Exits a stack context only if the specified condition is verified.
|
deactivate, getAllocator, getCurrentAllocatorContext, getDefault, outerCopy, outerCopy, outerExecute
enter, enter, enterAction, exit, exit, exitAction, getCurrentContext, getOuter, getOwner, setConcurrentContext, toString
public static final Configurable<java.lang.Class<? extends StackContext>> DEFAULT
ScopedMemory
for their stack allocations.
Users may also disable stack allocation by providing a class allocating
on the heap.public static void enter()
DEFAULT
stack context.public static void enter(boolean condition)
condition
- true
to enter a stack context;
false
otherwise.public static void exit()
java.lang.ClassCastException
- if the context is not a stack context.public static void exit(boolean condition)
condition
- true
to exit a stack context;
false
otherwise.Copyright © 2005 - 2007 Javolution.