public abstract class MemoryArea
extends java.lang.Object
MemoryArea
is the abstract base class of all classes
dealing with the representations of allocatable memory areas,
including the immortal memory area, physical memory and scoped memory
areas.
This is an abstract class, but no method in this class is abstract.
An application should not subclass MemoryArea
without
complete knowledge of its implementation details.Modifier | Constructor and Description |
---|---|
protected |
MemoryArea(long size)
Equivalent to
MemoryArea(long, Runnable) with the argument
list (size, null) . |
protected |
MemoryArea(long size,
java.lang.Runnable logic)
Creates an instance of
MemoryArea . |
protected |
MemoryArea(SizeEstimator size)
Equivalent to
MemoryArea(long, Runnable) with the argument
list (size.getEstimate(), null) . |
protected |
MemoryArea(SizeEstimator size,
java.lang.Runnable logic)
Equivalent to
MemoryArea(long, Runnable) with the argument
list (size.getEstimate(), logic) . |
Modifier and Type | Method and Description |
---|---|
void |
enter()
Associates this memory area with the current
schedulable for the duration of the execution of the
run() method of the instance of Runnable given in
the constructor. |
boolean |
enter(java.util.function.BooleanSupplier logic)
|
double |
enter(java.util.function.DoubleSupplier logic)
|
int |
enter(java.util.function.IntSupplier logic)
|
long |
enter(java.util.function.LongSupplier logic)
|
void |
enter(java.lang.Runnable logic)
Associates this memory area with the current
schedulable for the duration of the execution of the
run() method of the given Runnable . |
<T> T |
enter(java.util.function.Supplier<T> logic)
Same as
enter(Runnable) except that the executed method is called
get and an object is returned. |
boolean |
executeInArea(java.util.function.BooleanSupplier logic)
Same as
executeInArea(Runnable) except that the executed method
is called get and a boolean is returned. |
double |
executeInArea(java.util.function.DoubleSupplier logic)
Same as
executeInArea(Runnable) except that the executed method
is called get and a double is returned. |
int |
executeInArea(java.util.function.IntSupplier logic)
Same as
executeInArea(Runnable) except that the executed method
is called get and an int is returned. |
long |
executeInArea(java.util.function.LongSupplier logic)
Same as
executeInArea(Runnable) except that the executed method
is called get and a long is returned. |
void |
executeInArea(java.lang.Runnable logic)
Executes the
run() method from the logic parameter using
this memory area as the current allocation context. |
<T> T |
executeInArea(java.util.function.Supplier<T> logic)
Same as
executeInArea(Runnable) except that the executed method
is called get and an object is returned. |
static MemoryArea |
getMemoryArea(java.lang.Object object)
Gets the
MemoryArea in which the given
object is located. |
boolean |
mayHoldReferenceTo()
Determines whether an object
A allocated in the memory area
represented by this can hold a reference to an object
B allocated in the current memory area. |
boolean |
mayHoldReferenceTo(java.lang.Object value)
Determines whether an object
A allocated in the memory area
represented by this can hold a reference to the object
value . |
long |
memoryConsumed()
For memory areas where memory is freed under program control this
returns an exact count, in bytes, of the memory currently
used by the system for the allocated objects.
|
long |
memoryRemaining()
An approximation of the total amount of memory currently
available for future allocated objects, measured in bytes.
|
java.lang.Object |
newArray(java.lang.Class<?> type,
int number)
Allocates an array of the given type in this memory area.
|
static java.lang.Object |
newArrayInArea(java.lang.Object object,
java.lang.Class<?> type,
int size)
A helper method to create an array of type
type in the memory
area containing object . |
<T> T |
newInstance(java.lang.Class<T> type)
Allocates an object in this memory area.
|
<T> T |
newInstance(java.lang.reflect.Constructor<T> c,
java.lang.Object[] args)
Allocates an object in this memory area.
|
long |
size()
Queries the size of the memory area.
|
protected MemoryArea(long size, java.lang.Runnable logic) throws StaticIllegalArgumentException, StaticOutOfMemoryError, IllegalAssignmentError
MemoryArea
.size
- The size of MemoryArea
to allocate,
in bytes.logic
- A runnable, whose run()
method will be
called whenever enter()
is called.
When logic
is null
, this constructor is equivalent
to MemoryArea(long size)
.StaticIllegalArgumentException
- when the size
parameter is
less than zero.StaticOutOfMemoryError
- when there is insufficient memory for the
MemoryArea
object or for its allocation
area in its backing store.IllegalAssignmentError
- when storing logic
in this
would
violate the assignment rules.protected MemoryArea(SizeEstimator size, java.lang.Runnable logic) throws StaticIllegalArgumentException, StaticOutOfMemoryError, IllegalAssignmentError
MemoryArea(long, Runnable)
with the argument
list (size.getEstimate(), logic)
.size
- A SizeEstimator
object which indicates the
amount of memory required by this MemoryArea
.logic
- A runnable, whose run()
method will be
called whenever enter()
is called.
When logic
is null
, this constructor is equivalent
to MemoryArea(SizeEstimator size)
.StaticIllegalArgumentException
- when size
is null
or
size.getEstimate()
is negative.StaticOutOfMemoryError
- when there is insufficient memory for the
MemoryArea
object or for its allocation area in
its backing store.IllegalAssignmentError
- when storing logic
in this
would
violate the assignment rules.protected MemoryArea(long size) throws StaticIllegalArgumentException, StaticOutOfMemoryError
MemoryArea(long, Runnable)
with the argument
list (size, null)
.size
- The size of MemoryArea
to allocate,
in bytes.StaticIllegalArgumentException
- when size
is less
than zero.StaticOutOfMemoryError
- when there is insufficient memory for the
MemoryArea
object or for its allocation area in its
backing store.protected MemoryArea(SizeEstimator size) throws StaticIllegalArgumentException, StaticOutOfMemoryError
MemoryArea(long, Runnable)
with the argument
list (size.getEstimate(), null)
.size
- A SizeEstimator
object which indicates the
amount of memory required by this MemoryArea
.StaticIllegalArgumentException
- when the size
parameter is
null
, or size.getEstimate()
is negative.StaticOutOfMemoryError
- when there is insufficient memory for the
MemoryArea
object or for its allocation area in
its backing store.public void enter() throws IllegalTaskStateException, StaticIllegalArgumentException, ThrowBoundaryError, MemoryAccessError
run()
method of the instance of Runnable
given in
the constructor.
During this period of execution, this memory area
becomes the default allocation context until another
default allocation context is selected (using enter
,
or executeInArea(java.lang.Runnable)
) or
the enter
method exits.IllegalTaskStateException
- when the caller context is not an
instance of Schedulable
.StaticIllegalArgumentException
- when the caller is a schedulable and a null
value
for logic
was supplied when the memory area was
constructed.ThrowBoundaryError
- Thrown when the JVM needs to propagate an exception allocated
in this
scope to (or through) the memory area of the
caller. Storing a reference to that exception would cause
an IllegalAssignmentError
, so the JVM cannot be
permitted to deliver the exception.
The ThrowBoundaryError
instance is preallocated by the
VM to avoid cascading creation of ThrowBoundaryError
.MemoryAccessError
- when caller is a schedulable that may not use the heap and
this memory area's logic value is allocated in heap memory.public void enter(java.lang.Runnable logic)
run()
method of the given Runnable
.
During this period of execution, this memory area
becomes the default allocation context until another
default allocation context is selected (using enter
,
or executeInArea(java.lang.Runnable)
) or
the enter
method exits.logic
- The Runnable object whose run()
method should be invoked.IllegalTaskStateException
- when the caller context is not an instance of Schedulable
.StaticIllegalArgumentException
- when the caller is a schedulable and logic
is
null
.ThrowBoundaryError
- Thrown when the JVM needs to propagate an exception allocated in
this
scope to (or through) the memory area of the
caller. Storing a reference to that exception would cause
an IllegalAssignmentError
, so the JVM cannot be
permitted to deliver the exception. The
ThrowBoundaryError
instance is preallocated by the
VM to avoid cascading creation of ThrowBoundaryError
.public <T> T enter(java.util.function.Supplier<T> logic)
enter(Runnable)
except that the executed method is called
get
and an object is returned. The Supplier.get()
method
must ensure that the returned object is allocated outside the area, when
the area is not a PerennialMemory
.T
- The type of the object returned.logic
- The object whose get method will be executed.IllegalAssignmentError
- when the return value allocated in area
and area is not a PerennialMemory
.public boolean enter(java.util.function.BooleanSupplier logic)
logic
- The object whose get method will be executed.public int enter(java.util.function.IntSupplier logic)
logic
- The object whose get method will be executed.public long enter(java.util.function.LongSupplier logic)
logic
- The object whose get method will be executed.public double enter(java.util.function.DoubleSupplier logic)
logic
- The object whose get method will be executed.public static MemoryArea getMemoryArea(java.lang.Object object)
MemoryArea
in which the given
object is located.MemoryArea
from which
object
was allocated.StaticIllegalArgumentException
- when the value of object
is null
.public long memoryConsumed()
public long memoryRemaining()
public java.lang.Object newArray(java.lang.Class<?> type, int number) throws StaticIllegalArgumentException, StaticOutOfMemoryError, StaticSecurityException
type
- The class of the elements of the new array. To create an array
of a primitive type use a type
such as
Integer.TYPE
(which
would call for an array of the primitive int type.)number
- The number of elements in the new array.StaticIllegalArgumentException
- when number
is less than zero,
type
is null
,
or type
is java.lang.Void.TYPE
.StaticOutOfMemoryError
- when space in the memory area is exhausted.StaticSecurityException
- when the caller does not have
permission to create a new instance.public static java.lang.Object newArrayInArea(java.lang.Object object, java.lang.Class<?> type, int size)
type
in the memory
area containing object
.object
- is the reference for determining the area in which to
allocate the array.type
- is the type of the array element for the returned
array.size
- is the size of the array to return.type
with size
elements.public <T> T newInstance(java.lang.Class<T> type) throws java.lang.IllegalAccessException, StaticIllegalArgumentException, java.lang.InstantiationException, StaticOutOfMemoryError, java.lang.ExceptionInInitializerError, StaticSecurityException
T
- The type of the created objecttype
- The class of which to create a new instance.type
.java.lang.IllegalAccessException
- The class or initializer is inaccessible.StaticIllegalArgumentException
- when type
is null
.java.lang.InstantiationException
- when the specified class object could not
be instantiated. Possible causes are it is an interface, it is
abstract, or it is an array.ConstructorCheckedException
- a checked exception was thrown by
the constructor.StaticOutOfMemoryError
- when space in the memory area is exhausted.java.lang.ExceptionInInitializerError
- when an unexpected exception has
occurred in a static initializer.StaticSecurityException
- when the caller does not have
permission to create a new instance.public <T> T newInstance(java.lang.reflect.Constructor<T> c, java.lang.Object[] args) throws java.lang.ExceptionInInitializerError, java.lang.IllegalAccessException, StaticIllegalArgumentException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException, StaticOutOfMemoryError, StaticSecurityException
T
- The type of the created objectc
- The constructor for the new instance.args
- An array of arguments to pass to the constructor.c
.java.lang.ExceptionInInitializerError
- when an unexpected exception has occurred in a static
initializerjava.lang.IllegalAccessException
- when the class or initializer is inaccessible under Java
access control.StaticIllegalArgumentException
- when c
is
null
, or the args
array does
not contain the number of arguments required by
c
. A null
value of
args
is treated like an array of length 0.java.lang.InstantiationException
- when the specified class object could not be
instantiated. Possible causes are it is an interface, it is
abstract,
it is an array.java.lang.reflect.InvocationTargetException
- when the underlying constructor throws an exception.StaticOutOfMemoryError
- when space in the memory area is exhausted.StaticSecurityException
- when the caller does not have
permission to create a new instance.public long size()
public void executeInArea(java.lang.Runnable logic) throws StaticIllegalArgumentException
run()
method from the logic
parameter using
this memory area as the current allocation context.
The effect of executeInArea
on the scope stack
is specified in the subclasses of MemoryArea
.logic
- The runnable object whose run()
method should
be executed.StaticIllegalArgumentException
- when logic
is null
.public <T> T executeInArea(java.util.function.Supplier<T> logic)
executeInArea(Runnable)
except that the executed method
is called get
and an object is returned. For a memory are that
is not a PerennialMemory
, care must be taken that the returned
value is assignable to an object allocated in the current area.T
- the type of the returned object.logic
- The object whose get method will be executed.IllegalAssignmentError
- when the return value is not assignable to
an object allocated in the current area.public boolean executeInArea(java.util.function.BooleanSupplier logic)
executeInArea(Runnable)
except that the executed method
is called get
and a boolean
is returned.logic
- The object whose get method will be executed.public int executeInArea(java.util.function.IntSupplier logic)
executeInArea(Runnable)
except that the executed method
is called get
and an int
is returned.logic
- the object whose get method will be executed.public long executeInArea(java.util.function.LongSupplier logic)
executeInArea(Runnable)
except that the executed method
is called get
and a long
is returned.logic
- The object whose get method will be executed.public double executeInArea(java.util.function.DoubleSupplier logic)
executeInArea(Runnable)
except that the executed method
is called get
and a double
is returned.logic
- The object whose get method will be executed.public boolean mayHoldReferenceTo()
A
allocated in the memory area
represented by this
can hold a reference to an object
B
allocated in the current memory area.true
when B
can be assigned to a field
of A
, otherwise false
.public boolean mayHoldReferenceTo(java.lang.Object value)
A
allocated in the memory area
represented by this
can hold a reference to the object
value
.value
- The object to test.true
when value
can be assigned
to a field of A
, otherwise false
.