Class OffHeapMemoryAccess
- java.lang.Object
-
- net.haesleinhuepf.clij.coremem.offheap.OffHeapMemoryAccess
-
public final class OffHeapMemoryAccess extends Object
OfHeam memory access. This class offers static methods for memory allocation, dealocation and read/write access.- Author:
- royer
-
-
Constructor Summary
Constructors Constructor Description OffHeapMemoryAccess()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static longallocateMemory(long pLengthInBytes)Allocates a memory region of given length.static voidcopyFromArray(Object pSrcArray, long pSrcOffset, long pAddressDest, long pLengthInBytes)Fast copying of data from a Java array into an off-heap memory region.static voidcopyMemory(long pAddressOrg, long pAddressDest, long pLengthInBytes)Memory copy between two addresses.static voidcopyMemorySafely(long pAddressOrg, long pAddressDest, long pLengthInBytes)Memory copy between two addresses.static voidcopyToArray(long pAddressSrc, Object pDstArray, long pDstOffset, long pLengthInBytes)Fast copying of data from an off-heap memory region to a Java array.static voidderegisterMemoryRegion(long pAddress)Deregisters a memory region at a given address.static voidfillMemory(long pAddress, long pLengthInBytes, byte pValue)Fills a given memory region with a given byte value (repeated).static voidfreeAll()Frees all allocated off-heap memory.static voidfreeMemory(long pAddress)Frees memory at a given address.static voidfullReorderingFence()Full reordering fencestatic bytegetByte(long pAddress)Returns value at given address.static chargetChar(long pAddress)Returns value at given address.static doublegetDouble(long pAddress)Returns value at given address.static floatgetFloat(long pAddress)Returns value at given address.static intgetInt(long pAddress)Returns value at given address.static longgetLong(long pAddress)Returns value at given address.static longgetMaximumAllocatableMemory()Returns the maximum amount of memory that can be allocated.static intgetPageSize()Returns the page size.static shortgetShort(long pAddress)Returns value at given address.static LonggetSignature(long pAddress)Returns the signature for a memory region allocated at a given address.static longgetTotalAllocatedMemory()Returns the current total amount of memory that has been allocated.static booleanisAllocatedMemory(long pAddress, long pSignature)Returns true if this address and signature corresponds to an allocated pointer.static voidloadReorderingFence()Load reordering fencestatic voidoverrideTotalAllocatedMemory(long pTotalAllocatedMemory)Overrides the current total amount of memory that has been allocated.static longreallocateMemory(long pAddress, long pNewLengthInBytes)Reallocates memory.static voidregisterMemoryRegion(long pAddress, long pLength)Registers a memory region at a given address of given length.static voidsetByte(long pAddress, byte pValue)Sets value at given address.static voidsetChar(long pAddress, char pValue)Sets value at given address.static voidsetDouble(long pAddress, double pValue)Sets value at given address.static voidsetFloat(long pAddress, float pValue)Sets value at given address.static voidsetInt(long pAddress, int pValue)Sets value at given address.static voidsetLong(long pAddress, long pValue)Sets value at given address.static voidsetMaximumAllocatableMemory(long pMaximumAllocatableMemory)Sets the maximum amount of memory that can be allocated.static voidsetMemorySafely(long pAddress, long pLengthInBytes, byte pValue)Fills a given memory region with a given byte value (repeated).static voidsetShort(long pAddress, short pValue)Sets value at given address.static voidstoreReorderingFence()Store reordering fence
-
-
-
Method Detail
-
registerMemoryRegion
public static final void registerMemoryRegion(long pAddress, long pLength)Registers a memory region at a given address of given length.- Parameters:
pAddress- addresspLength- length in bytes
-
deregisterMemoryRegion
public static final void deregisterMemoryRegion(long pAddress)
Deregisters a memory region at a given address.- Parameters:
pAddress- address to deregister
-
getMaximumAllocatableMemory
public static long getMaximumAllocatableMemory()
Returns the maximum amount of memory that can be allocated.- Returns:
- max allocatable memory
-
setMaximumAllocatableMemory
public static void setMaximumAllocatableMemory(long pMaximumAllocatableMemory)
Sets the maximum amount of memory that can be allocated.- Parameters:
pMaximumAllocatableMemory- new max allocatable memory
-
overrideTotalAllocatedMemory
public static final void overrideTotalAllocatedMemory(long pTotalAllocatedMemory)
Overrides the current total amount of memory that has been allocated.- Parameters:
pTotalAllocatedMemory- new current allocated memory
-
getTotalAllocatedMemory
public static final long getTotalAllocatedMemory()
Returns the current total amount of memory that has been allocated.- Returns:
- total amount of allocated memory
-
getPageSize
public static final int getPageSize()
Returns the page size. https://en.wikipedia.org/wiki/Page_(computer_memory)- Returns:
- page size in bytes
-
allocateMemory
public static final long allocateMemory(long pLengthInBytes)
Allocates a memory region of given length.- Parameters:
pLengthInBytes- length in bytes- Returns:
- address
-
reallocateMemory
public static final long reallocateMemory(long pAddress, long pNewLengthInBytes) throws InvalidNativeMemoryAccessExceptionReallocates memory.- Parameters:
pAddress- memory addresspNewLengthInBytes- new length in bytes- Returns:
- new pointer
- Throws:
InvalidNativeMemoryAccessException- thrown if pointer is invalid.
-
isAllocatedMemory
public static final boolean isAllocatedMemory(long pAddress, long pSignature)Returns true if this address and signature corresponds to an allocated pointer. Signatures provide away to distinguish different allocated memory regions and prevent that the garbage collector try to release already released memory.- Parameters:
pAddress- addresspSignature- signature for given address- Returns:
- true if memory allocated at given address with given signature.
-
getSignature
public static Long getSignature(long pAddress)
Returns the signature for a memory region allocated at a given address.- Parameters:
pAddress- address- Returns:
- signature.
-
freeMemory
public static final void freeMemory(long pAddress) throws InvalidNativeMemoryAccessExceptionFrees memory at a given address.- Parameters:
pAddress- address- Throws:
InvalidNativeMemoryAccessException- thrown if no memory is allocated at that address.
-
copyFromArray
public static final void copyFromArray(Object pSrcArray, long pSrcOffset, long pAddressDest, long pLengthInBytes) throws InvalidNativeMemoryAccessException
Fast copying of data from a Java array into an off-heap memory region.- Parameters:
pSrcArray- source arraypSrcOffset- source offset in elementspAddressDest- destination addresspLengthInBytes- length in bytes- Throws:
InvalidNativeMemoryAccessException- thrown if the length in bytes is not the same as the size in bytes of the Java array.
-
copyToArray
public static final void copyToArray(long pAddressSrc, Object pDstArray, long pDstOffset, long pLengthInBytes) throws InvalidNativeMemoryAccessExceptionFast copying of data from an off-heap memory region to a Java array.- Parameters:
pAddressSrc- source addresspDstArray- destination array.pDstOffset- destination array offset in bytes.pLengthInBytes- length in bytes- Throws:
InvalidNativeMemoryAccessException- thrown if the length in bytes is not the same as the size in bytes of the Java array.
-
copyMemory
public static final void copyMemory(long pAddressOrg, long pAddressDest, long pLengthInBytes)Memory copy between two addresses.- Parameters:
pAddressOrg- source addresspAddressDest- destination addresspLengthInBytes- length in bytes
-
copyMemorySafely
public static final void copyMemorySafely(long pAddressOrg, long pAddressDest, long pLengthInBytes) throws InvalidNativeMemoryAccessExceptionMemory copy between two addresses.- Parameters:
pAddressOrg- source addresspAddressDest- destination addresspLengthInBytes- length in bytes- Throws:
InvalidNativeMemoryAccessException- thrown if the pointers are invalid or the lengths incompatible.
-
fillMemory
public static final void fillMemory(long pAddress, long pLengthInBytes, byte pValue)Fills a given memory region with a given byte value (repeated).- Parameters:
pAddress- memory region addresspLengthInBytes- length in bytespValue- byte value
-
setMemorySafely
public static final void setMemorySafely(long pAddress, long pLengthInBytes, byte pValue) throws InvalidNativeMemoryAccessExceptionFills a given memory region with a given byte value (repeated).- Parameters:
pAddress- memory region addresspLengthInBytes- length in bytespValue- byte value- Throws:
InvalidNativeMemoryAccessException- thrown if the pointer is invalid or the lengths incompatible.
-
storeReorderingFence
public static final void storeReorderingFence()
Store reordering fence
-
loadReorderingFence
public static final void loadReorderingFence()
Load reordering fence
-
fullReorderingFence
public static final void fullReorderingFence()
Full reordering fence
-
getByte
public static final byte getByte(long pAddress)
Returns value at given address.- Parameters:
pAddress- address- Returns:
- value
-
getChar
public static final char getChar(long pAddress)
Returns value at given address.- Parameters:
pAddress- address- Returns:
- value
-
getShort
public static final short getShort(long pAddress)
Returns value at given address.- Parameters:
pAddress- address- Returns:
- value
-
getInt
public static final int getInt(long pAddress)
Returns value at given address.- Parameters:
pAddress- address- Returns:
- value
-
getLong
public static final long getLong(long pAddress)
Returns value at given address.- Parameters:
pAddress- address- Returns:
- value
-
getFloat
public static final float getFloat(long pAddress)
Returns value at given address.- Parameters:
pAddress- address- Returns:
- value
-
getDouble
public static final double getDouble(long pAddress)
Returns value at given address.- Parameters:
pAddress- address- Returns:
- value
-
setByte
public static final void setByte(long pAddress, byte pValue)Sets value at given address.- Parameters:
pAddress- addresspValue- value
-
setChar
public static final void setChar(long pAddress, char pValue)Sets value at given address.- Parameters:
pAddress- addresspValue- value
-
setShort
public static final void setShort(long pAddress, short pValue)Sets value at given address.- Parameters:
pAddress- addresspValue- value
-
setInt
public static final void setInt(long pAddress, int pValue)Sets value at given address.- Parameters:
pAddress- addresspValue- value
-
setLong
public static final void setLong(long pAddress, long pValue)Sets value at given address.- Parameters:
pAddress- addresspValue- value
-
setFloat
public static final void setFloat(long pAddress, float pValue)Sets value at given address.- Parameters:
pAddress- addresspValue- value
-
setDouble
public static final void setDouble(long pAddress, double pValue)Sets value at given address.- Parameters:
pAddress- addresspValue- value
-
freeAll
public static void freeAll()
Frees all allocated off-heap memory. Highly non-recommended as this will invalidate memory that other objects might rely upon.
-
-