public class MemoryRegion extends Object implements Memory
If asserts are enabled in the JVM, the methods in this class perform bounds checking against the region's defined boundaries and then redirect the call to the parent Memory class. If the parent class is also a MemoryRegion it does a similar check and then calls its parent. The root of this hierarchy will be a NativeMemory class that ultimately performs the desired task. If asserts are not enabled the JIT compiler will eliminate all the asserts and the hierarchical calls should collapse to a call to the NativeMemory method.
Because asserts must be specifically enabled in the JVM, it is incumbent on the user of this class to make sure that their code is thoroughly tested. Violating memory bounds can cause memory segment faults, which takes down the JVM and can be very difficult to debug.
NativeMemory| Constructor and Description |
|---|
MemoryRegion(Memory memory,
long memOffsetBytes,
long capacityBytes)
Defines a region of the given parent Memory by defining an offset and capacity that are
within the boundaries of the parent.
|
MemoryRegion(Memory memory,
long memOffsetBytes,
long capacityBytes,
MemoryRequest memReq)
Defines a region of the given parent Memory by defining an offset and capacity that are
within the boundaries of the parent.
|
| Modifier and Type | Method and Description |
|---|---|
int |
addAndGetInt(long offsetBytes,
int delta)
Atomically adds the given value to the integer located at offsetBytes.
|
long |
addAndGetLong(long offsetBytes,
long delta)
Atomically adds the given value to the long located at offsetBytes.
|
Object |
array()
Returns the backing on-heap primitive array if there is one, otherwise returns null
|
MemoryRegion |
asReadOnlyMemory()
Returns a read-only version of this memory
|
ByteBuffer |
byteBuffer()
Returns the backing ByteBuffer if there is one, otherwise returns null
|
void |
clear()
Clears all bytes of this Memory to zero
|
void |
clear(long offsetBytes,
long lengthBytes)
Clears a portion of this Memory to zero.
|
void |
clearBits(long offsetBytes,
byte bitMask)
Clears the bits defined by the bitMask
|
boolean |
compareAndSwapInt(long offsetBytes,
int expect,
int update)
Atomically sets the current value at the memory location to the given updated value
if and only if the current value
== the expected value. |
boolean |
compareAndSwapLong(long offsetBytes,
long expect,
long update)
Atomically sets the current value at the memory location to the given updated value
if and only if the current value
== the expected value. |
void |
copy(long srcOffsetBytes,
long dstOffsetBytes,
long lengthBytes)
Deprecated.
|
void |
copy(long srcOffsetBytes,
Memory destination,
long dstOffsetBytes,
long lengthBytes)
Copies bytes from a source range of this Memory to a destination range of the given Memory
using the same low-level system copy function as found in
System.arraycopy(Object, int, Object, int, int). |
void |
fill(byte value)
Fills all bytes of this Memory region to the given byte value.
|
void |
fill(long offsetBytes,
long lengthBytes,
byte value)
Fills a portion of this Memory region to the given byte value.
|
void |
freeMemory()
Because the Memory classes now use the JVM Cleaner, calling freeMemory(),
which also calls Cleaner, is optional.
|
long |
getAddress(long offsetBytes)
Returns the start address of this Memory relative to its parent plus the offset in bytes.
|
int |
getAndSetInt(long offsetBytes,
int newValue)
Atomically exchanges the given value with the current value located at offsetBytes.
|
long |
getAndSetLong(long offsetBytes,
long newValue)
Atomically exchanges the given value with the current value located at offsetBytes.
|
boolean |
getBoolean(long offsetBytes)
Gets the boolean value at the given offset
|
void |
getBooleanArray(long offsetBytes,
boolean[] dstArray,
int dstOffset,
int length)
Gets the boolean array at the given offset
|
byte |
getByte(long offsetBytes)
Gets the byte at the given offset
|
void |
getByteArray(long offsetBytes,
byte[] dstArray,
int dstOffset,
int length)
Gets the byte array at the given offset
|
long |
getCapacity()
Gets the capacity of this Memory in bytes
|
char |
getChar(long offsetBytes)
Gets the char at the given offset
|
void |
getCharArray(long offsetBytes,
char[] dstArray,
int dstOffset,
int length)
Gets the char array at the given offset
|
long |
getCumulativeOffset(long offsetBytes)
Returns the cumulative offset in bytes of this Memory from the root of the Memory hierarchy
including the given offsetBytes.
|
double |
getDouble(long offsetBytes)
Gets the double at the given offset
|
void |
getDoubleArray(long offsetBytes,
double[] dstArray,
int dstOffset,
int length)
Gets the double array at the given offset
|
float |
getFloat(long offsetBytes)
Gets the float at the given offset
|
void |
getFloatArray(long offsetBytes,
float[] dstArray,
int dstOffset,
int length)
Gets the float array at the given offset
|
int |
getInt(long offsetBytes)
Gets the int at the given offset
|
void |
getIntArray(long offsetBytes,
int[] dstArray,
int dstOffset,
int length)
Gets the int array at the given offset
|
long |
getLong(long offsetBytes)
Gets the long at the given offset
|
void |
getLongArray(long offsetBytes,
long[] dstArray,
int dstOffset,
int length)
Gets the long array at the given offset
|
MemoryRequest |
getMemoryRequest()
Returns a MemoryRequest or null
|
Object |
getParent()
Gets the parent Memory or backing array.
|
short |
getShort(long offsetBytes)
Gets the short at the given offset
|
void |
getShortArray(long offsetBytes,
short[] dstArray,
int dstOffset,
int length)
Gets the short array at the given offset
|
boolean |
hasArray()
Returns true if this Memory is backed by an on-heap primitive array
|
boolean |
hasByteBuffer()
Returns true if this Memory is backed by a ByteBuffer
|
boolean |
isAllBitsClear(long offsetBytes,
byte bitMask)
Returns true if all bits defined by the bitMask are clear
|
boolean |
isAllBitsSet(long offsetBytes,
byte bitMask)
Returns true if all bits defined by the bitMask are set
|
boolean |
isAllocated()
Returns true if this Memory has a capacity greater than zero
|
boolean |
isAnyBitsClear(long offsetBytes,
byte bitMask)
Returns true if any bits defined by the bitMask are clear
|
boolean |
isAnyBitsSet(long offsetBytes,
byte bitMask)
Returns true if any bits defined by the bitMask are set
|
boolean |
isDirect()
Returns true if the backing memory is direct (off-heap) memory.
|
boolean |
isReadOnly()
Returns true if this Memory is read only
|
void |
putBoolean(long offsetBytes,
boolean srcValue)
Puts the boolean value at the given offset
|
void |
putBooleanArray(long offsetBytes,
boolean[] srcArray,
int srcOffset,
int length)
Puts the boolean array at the given offset
|
void |
putByte(long offsetBytes,
byte srcValue)
Puts the byte value at the given offset
|
void |
putByteArray(long offsetBytes,
byte[] srcArray,
int srcOffset,
int length)
Puts the byte array at the given offset
|
void |
putChar(long offsetBytes,
char srcValue)
Puts the char value at the given offset
|
void |
putCharArray(long offsetBytes,
char[] srcArray,
int srcOffset,
int length)
Puts the char array at the given offset
|
void |
putDouble(long offsetBytes,
double srcValue)
Puts the double value at the given offset
|
void |
putDoubleArray(long offsetBytes,
double[] srcArray,
int srcOffset,
int length)
Puts the double array at the given offset
|
void |
putFloat(long offsetBytes,
float srcValue)
Puts the float value at the given offset
|
void |
putFloatArray(long offsetBytes,
float[] srcArray,
int srcOffset,
int length)
Puts the float array at the given offset
|
void |
putInt(long offsetBytes,
int srcValue)
Puts the int value at the given offset
|
void |
putIntArray(long offsetBytes,
int[] srcArray,
int srcOffset,
int length)
Puts the int array at the given offset
|
void |
putLong(long offsetBytes,
long srcValue)
Puts the long value at the given offset
|
void |
putLongArray(long offsetBytes,
long[] srcArray,
int srcOffset,
int length)
Puts the long array at the given offset
|
void |
putShort(long offsetBytes,
short srcValue)
Puts the short value at the given offset
|
void |
putShortArray(long offsetBytes,
short[] srcArray,
int srcOffset,
int length)
Puts the short array at the given offset
|
void |
reassign(long memOffsetBytes,
long capacityBytes)
Deprecated.
This could have created difficult to diagnose bugs in MemoryRegion hierarchies.
This is now deprecated and will throw an exception. MemoryRegion is now immutable.
|
void |
setBits(long offsetBytes,
byte bitMask)
Sets the bits defined by the bitMask
|
void |
setMemoryRequest(MemoryRequest memReq)
Sets a MemoryRequest
|
String |
toHexString(String header,
long offsetBytes,
int lengthBytes)
Returns a formatted hex string of an area of this Memory.
|
public MemoryRegion(Memory memory, long memOffsetBytes, long capacityBytes)
memory - the parent MemorymemOffsetBytes - the starting offset in bytes of this region with respect to the
start of the parent memory.capacityBytes - the capacity in bytes of this region.public MemoryRegion(Memory memory, long memOffsetBytes, long capacityBytes, MemoryRequest memReq)
memory - the parent MemorymemOffsetBytes - the starting offset in bytes of this region with respect to the
start of the parent memory.capacityBytes - the capacity in bytes of this region.memReq - a MemoryRequest object@Deprecated public void reassign(long memOffsetBytes, long capacityBytes)
memOffsetBytes - the given offset from the parent's startcapacityBytes - the given capacity of this regionpublic boolean getBoolean(long offsetBytes)
MemorygetBoolean in interface MemoryoffsetBytes - offset bytes relative to this Memory startpublic void getBooleanArray(long offsetBytes,
boolean[] dstArray,
int dstOffset,
int length)
MemorygetBooleanArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startdstArray - The preallocated destination array.dstOffset - offset in array unitslength - number of array units to transferpublic byte getByte(long offsetBytes)
Memorypublic void getByteArray(long offsetBytes,
byte[] dstArray,
int dstOffset,
int length)
MemorygetByteArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startdstArray - The preallocated destination array.dstOffset - offset in array unitslength - number of array units to transferpublic char getChar(long offsetBytes)
Memorypublic void getCharArray(long offsetBytes,
char[] dstArray,
int dstOffset,
int length)
MemorygetCharArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startdstArray - The preallocated destination array.dstOffset - offset in array unitslength - number of array units to transferpublic double getDouble(long offsetBytes)
Memorypublic void getDoubleArray(long offsetBytes,
double[] dstArray,
int dstOffset,
int length)
MemorygetDoubleArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startdstArray - The preallocated destination array.dstOffset - offset in array unitslength - number of array units to transferpublic float getFloat(long offsetBytes)
Memorypublic void getFloatArray(long offsetBytes,
float[] dstArray,
int dstOffset,
int length)
MemorygetFloatArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startdstArray - The preallocated destination array.dstOffset - offset in array unitslength - number of array units to transferpublic int getInt(long offsetBytes)
Memorypublic void getIntArray(long offsetBytes,
int[] dstArray,
int dstOffset,
int length)
MemorygetIntArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startdstArray - The preallocated destination array.dstOffset - offset in array unitslength - number of array units to transferpublic long getLong(long offsetBytes)
Memorypublic void getLongArray(long offsetBytes,
long[] dstArray,
int dstOffset,
int length)
MemorygetLongArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startdstArray - The preallocated destination array.dstOffset - offset in array unitslength - number of array units to transferpublic short getShort(long offsetBytes)
Memorypublic void getShortArray(long offsetBytes,
short[] dstArray,
int dstOffset,
int length)
MemorygetShortArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startdstArray - The preallocated destination array.dstOffset - offset in array unitslength - number of array units to transferpublic boolean isAllBitsClear(long offsetBytes,
byte bitMask)
MemoryisAllBitsClear in interface MemoryoffsetBytes - offset bytes relative to this Memory startbitMask - bits set to one will be checkedpublic boolean isAllBitsSet(long offsetBytes,
byte bitMask)
MemoryisAllBitsSet in interface MemoryoffsetBytes - offset bytes relative to this Memory startbitMask - bits set to one will be checkedpublic boolean isAnyBitsClear(long offsetBytes,
byte bitMask)
MemoryisAnyBitsClear in interface MemoryoffsetBytes - offset bytes relative to this Memory startbitMask - bits set to one will be checkedpublic boolean isAnyBitsSet(long offsetBytes,
byte bitMask)
MemoryisAnyBitsSet in interface MemoryoffsetBytes - offset bytes relative to this Memory startbitMask - bits set to one will be checkedpublic void putBoolean(long offsetBytes,
boolean srcValue)
MemoryputBoolean in interface MemoryoffsetBytes - offset bytes relative to this Memory startsrcValue - the value to putpublic void putBooleanArray(long offsetBytes,
boolean[] srcArray,
int srcOffset,
int length)
MemoryputBooleanArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startsrcArray - The source array.srcOffset - offset in array unitslength - number of array units to transferpublic void putByte(long offsetBytes,
byte srcValue)
Memorypublic void putByteArray(long offsetBytes,
byte[] srcArray,
int srcOffset,
int length)
MemoryputByteArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startsrcArray - The source array.srcOffset - offset in array unitslength - number of array units to transferpublic void putChar(long offsetBytes,
char srcValue)
Memorypublic void putCharArray(long offsetBytes,
char[] srcArray,
int srcOffset,
int length)
MemoryputCharArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startsrcArray - The source array.srcOffset - offset in array unitslength - number of array units to transferpublic void putDouble(long offsetBytes,
double srcValue)
Memorypublic void putDoubleArray(long offsetBytes,
double[] srcArray,
int srcOffset,
int length)
MemoryputDoubleArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startsrcArray - The source array.srcOffset - offset in array unitslength - number of array units to transferpublic void putFloat(long offsetBytes,
float srcValue)
Memorypublic void putFloatArray(long offsetBytes,
float[] srcArray,
int srcOffset,
int length)
MemoryputFloatArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startsrcArray - The source array.srcOffset - offset in array unitslength - number of array units to transferpublic void putInt(long offsetBytes,
int srcValue)
Memorypublic void putIntArray(long offsetBytes,
int[] srcArray,
int srcOffset,
int length)
MemoryputIntArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startsrcArray - The source array.srcOffset - offset in array unitslength - number of array units to transferpublic void putLong(long offsetBytes,
long srcValue)
Memorypublic void putLongArray(long offsetBytes,
long[] srcArray,
int srcOffset,
int length)
MemoryputLongArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startsrcArray - The source array.srcOffset - offset in array unitslength - number of array units to transferpublic void putShort(long offsetBytes,
short srcValue)
Memorypublic void putShortArray(long offsetBytes,
short[] srcArray,
int srcOffset,
int length)
MemoryputShortArray in interface MemoryoffsetBytes - offset bytes relative to this Memory startsrcArray - The source array.srcOffset - offset in array unitslength - number of array units to transferpublic void setBits(long offsetBytes,
byte bitMask)
Memorypublic int addAndGetInt(long offsetBytes,
int delta)
MemoryaddAndGetInt in interface MemoryoffsetBytes - offset bytes relative to this Memory startdelta - the amount to addpublic long addAndGetLong(long offsetBytes,
long delta)
MemoryaddAndGetLong in interface MemoryoffsetBytes - offset bytes relative to this Memory startdelta - the amount to addpublic boolean compareAndSwapInt(long offsetBytes,
int expect,
int update)
Memory== the expected value.compareAndSwapInt in interface MemoryoffsetBytes - offset bytes relative to this Memory startexpect - the expected valueupdate - the new valuetrue if successful. False return indicates that
the current value at the memory location was not equal to the expected value.public boolean compareAndSwapLong(long offsetBytes,
long expect,
long update)
Memory== the expected value.compareAndSwapLong in interface MemoryoffsetBytes - offset bytes relative to this Memory startexpect - the expected valueupdate - the new valuetrue if successful. False return indicates that
the current value at the memory location was not equal to the expected value.public int getAndSetInt(long offsetBytes,
int newValue)
MemorygetAndSetInt in interface MemoryoffsetBytes - offset bytes relative to this Memory startnewValue - new valuepublic long getAndSetLong(long offsetBytes,
long newValue)
MemorygetAndSetLong in interface MemoryoffsetBytes - offset bytes relative to this Memory startnewValue - new valuepublic Object array()
Memorypublic MemoryRegion asReadOnlyMemory()
MemoryasReadOnlyMemory in interface Memorypublic ByteBuffer byteBuffer()
MemorybyteBuffer in interface Memorypublic void clear()
Memorypublic void clear(long offsetBytes,
long lengthBytes)
Memorypublic void clearBits(long offsetBytes,
byte bitMask)
Memory@Deprecated public void copy(long srcOffsetBytes, long dstOffsetBytes, long lengthBytes)
MemorySystem.arraycopy(Object, int, Object, int, int).
These regions may not overlap. This will be checked if asserts are enabled in the JVM.public void copy(long srcOffsetBytes,
Memory destination,
long dstOffsetBytes,
long lengthBytes)
MemorySystem.arraycopy(Object, int, Object, int, int).public void fill(byte value)
Memorypublic void fill(long offsetBytes,
long lengthBytes,
byte value)
Memorypublic long getAddress(long offsetBytes)
MemorygetAddress in interface MemoryoffsetBytes - the given offset in bytes from the start address of this Memory
relative to its parent.public long getCapacity()
MemorygetCapacity in interface Memorypublic long getCumulativeOffset(long offsetBytes)
MemorygetCumulativeOffset in interface MemoryoffsetBytes - the given offset in bytespublic MemoryRequest getMemoryRequest()
MemorygetMemoryRequest in interface Memorypublic Object getParent()
Memorypublic boolean hasArray()
Memorypublic boolean hasByteBuffer()
MemoryhasByteBuffer in interface Memorypublic boolean isAllocated()
MemoryisAllocated in interface Memorypublic boolean isDirect()
Memorypublic boolean isReadOnly()
MemoryisReadOnly in interface Memorypublic void setMemoryRequest(MemoryRequest memReq)
MemorysetMemoryRequest in interface MemorymemReq - the MemoryRequestpublic String toHexString(String header, long offsetBytes, int lengthBytes)
MemorytoHexString in interface Memoryheader - descriptive headeroffsetBytes - offset bytes relative to this Memory startlengthBytes - number of bytes to convert to a hex stringpublic void freeMemory()
MemoryAllocMemory or MemoryMappedFile.
Preemptively calling freeMemory() may reduce the load on the JVM GarbageCollector, but the significance of this will have to be verified in the target system environment. Calling freeMemory() always disables the current instance by setting the capacity to zero.
freeMemory in interface MemoryCopyright © 2015–2017 Yahoo! Inc.. All rights reserved.