public class NativeMemory extends Object implements Memory
The sub-class AllocMemory is used to allocate direct, off-heap native memory, which is then accessed by the NativeMemory methods.
These methods extend many of the sun.misc.Unsafe class methods. Unsafe is an internal, low-level class used by many Java library classes for performance reasons. To achieve high performance Unsafe DOES NOT do any bounds checking. And for the same performance reasons, the methods in this class DO NOT do any bounds checking when running in a default-configured JVM.
However, the methods in this class will perform bounds checking if the JVM is configured to enable asserts (-ea). Test enviornments such as JUnit and TestNG automatically configure the JVM to enable asserts. Thus, 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.
| Modifier | Constructor and Description |
|---|---|
|
NativeMemory(boolean[] booleanArray)
Provides access to the given boolean array using Memory interface
|
|
NativeMemory(byte[] byteArray)
Provides access to the given byte array using Memory interface
|
|
NativeMemory(ByteBuffer byteBuf)
Deprecated.
Replaced by
wrap(ByteBuffer), which supports both writable and
read-only ByteBuffers |
|
NativeMemory(char[] charArray)
Provides access to the given char array using Memory interface
|
|
NativeMemory(double[] doubleArray)
Provides access to the given double array using Memory interface
|
|
NativeMemory(float[] floatArray)
Provides access to the given float array using Memory interface
|
|
NativeMemory(int[] intArray)
Provides access to the given int array using Memory interface
|
|
NativeMemory(long[] longArray)
Provides access to the given long array using Memory interface
|
protected |
NativeMemory(long nativeBaseAddress,
long capacityBytes,
long objectBaseOffset,
Object memArray,
ByteBuffer byteBuf) |
|
NativeMemory(short[] shortArray)
Provides access to the given short array using Memory interface
|
| 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
|
Memory |
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). |
static void |
copy(Memory source,
long srcOffsetBytes,
Memory destination,
long dstOffsetBytes,
long lengthBytes)
Deprecated.
Use
Memory.copy(long, Memory, long, long) instead. |
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 |
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.
|
static Memory |
wrap(ByteBuffer byteBuffer)
Provides access to the backing store of the given ByteBuffer using Memory interface.
|
protected NativeMemory(long nativeBaseAddress,
long capacityBytes,
long objectBaseOffset,
Object memArray,
ByteBuffer byteBuf)
public NativeMemory(boolean[] booleanArray)
booleanArray - an on-heap boolean arraypublic NativeMemory(byte[] byteArray)
byteArray - an on-heap byte arraypublic NativeMemory(char[] charArray)
charArray - an on-heap char arraypublic NativeMemory(short[] shortArray)
shortArray - an on-heap short arraypublic NativeMemory(int[] intArray)
intArray - an on-heap int arraypublic NativeMemory(long[] longArray)
longArray - an on-heap long arraypublic NativeMemory(float[] floatArray)
floatArray - an on-heap float arraypublic NativeMemory(double[] doubleArray)
doubleArray - an on-heap double array@Deprecated public NativeMemory(ByteBuffer byteBuf)
wrap(ByteBuffer), which supports both writable and
read-only ByteBufferswrap(ByteBuffer).byteBuf - the given writable ByteBufferpublic static Memory wrap(ByteBuffer byteBuffer)
byteBuffer - the given ByteBufferpublic 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 Memory 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 Memory@Deprecated public static final void copy(Memory source, long srcOffsetBytes, Memory destination, long dstOffsetBytes, long lengthBytes)
Memory.copy(long, Memory, long, long) instead.System.arraycopy(Object, int, Object, int, int).
If the source and destination are derived from the same underlying base Memory use
Memory.copy(long, long, long) instead. Nonetheless, the source and destination regions
should not overlap as it could cause unpredictable results.
source - the source MemorysrcOffsetBytes - the source offsetdestination - the destination MemorydstOffsetBytes - the destination offsetlengthBytes - the number of bytes to copyCopyright © 2015–2017 Yahoo! Inc.. All rights reserved.