Class 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
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long allocateMemory​(long pLengthInBytes)
      Allocates a memory region of given length.
      static void copyFromArray​(Object pSrcArray, long pSrcOffset, long pAddressDest, long pLengthInBytes)
      Fast copying of data from a Java array into an off-heap memory region.
      static void copyMemory​(long pAddressOrg, long pAddressDest, long pLengthInBytes)
      Memory copy between two addresses.
      static void copyMemorySafely​(long pAddressOrg, long pAddressDest, long pLengthInBytes)
      Memory copy between two addresses.
      static void copyToArray​(long pAddressSrc, Object pDstArray, long pDstOffset, long pLengthInBytes)
      Fast copying of data from an off-heap memory region to a Java array.
      static void deregisterMemoryRegion​(long pAddress)
      Deregisters a memory region at a given address.
      static void fillMemory​(long pAddress, long pLengthInBytes, byte pValue)
      Fills a given memory region with a given byte value (repeated).
      static void freeAll()
      Frees all allocated off-heap memory.
      static void freeMemory​(long pAddress)
      Frees memory at a given address.
      static void fullReorderingFence()
      Full reordering fence
      static byte getByte​(long pAddress)
      Returns value at given address.
      static char getChar​(long pAddress)
      Returns value at given address.
      static double getDouble​(long pAddress)
      Returns value at given address.
      static float getFloat​(long pAddress)
      Returns value at given address.
      static int getInt​(long pAddress)
      Returns value at given address.
      static long getLong​(long pAddress)
      Returns value at given address.
      static long getMaximumAllocatableMemory()
      Returns the maximum amount of memory that can be allocated.
      static int getPageSize()
      Returns the page size.
      static short getShort​(long pAddress)
      Returns value at given address.
      static Long getSignature​(long pAddress)
      Returns the signature for a memory region allocated at a given address.
      static long getTotalAllocatedMemory()
      Returns the current total amount of memory that has been allocated.
      static boolean isAllocatedMemory​(long pAddress, long pSignature)
      Returns true if this address and signature corresponds to an allocated pointer.
      static void loadReorderingFence()
      Load reordering fence
      static void overrideTotalAllocatedMemory​(long pTotalAllocatedMemory)
      Overrides the current total amount of memory that has been allocated.
      static long reallocateMemory​(long pAddress, long pNewLengthInBytes)
      Reallocates memory.
      static void registerMemoryRegion​(long pAddress, long pLength)
      Registers a memory region at a given address of given length.
      static void setByte​(long pAddress, byte pValue)
      Sets value at given address.
      static void setChar​(long pAddress, char pValue)
      Sets value at given address.
      static void setDouble​(long pAddress, double pValue)
      Sets value at given address.
      static void setFloat​(long pAddress, float pValue)
      Sets value at given address.
      static void setInt​(long pAddress, int pValue)
      Sets value at given address.
      static void setLong​(long pAddress, long pValue)
      Sets value at given address.
      static void setMaximumAllocatableMemory​(long pMaximumAllocatableMemory)
      Sets the maximum amount of memory that can be allocated.
      static void setMemorySafely​(long pAddress, long pLengthInBytes, byte pValue)
      Fills a given memory region with a given byte value (repeated).
      static void setShort​(long pAddress, short pValue)
      Sets value at given address.
      static void storeReorderingFence()
      Store reordering fence
    • Constructor Detail

      • OffHeapMemoryAccess

        public OffHeapMemoryAccess()
    • 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 - address
        pLength - 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 InvalidNativeMemoryAccessException
        Reallocates memory.
        Parameters:
        pAddress - memory address
        pNewLengthInBytes - 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 - address
        pSignature - 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.
      • 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 array
        pSrcOffset - source offset in elements
        pAddressDest - destination address
        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.
      • copyToArray

        public static final void copyToArray​(long pAddressSrc,
                                             Object pDstArray,
                                             long pDstOffset,
                                             long pLengthInBytes)
                                      throws InvalidNativeMemoryAccessException
        Fast copying of data from an off-heap memory region to a Java array.
        Parameters:
        pAddressSrc - source address
        pDstArray - 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 address
        pAddressDest - destination address
        pLengthInBytes - length in bytes
      • copyMemorySafely

        public static final void copyMemorySafely​(long pAddressOrg,
                                                  long pAddressDest,
                                                  long pLengthInBytes)
                                           throws InvalidNativeMemoryAccessException
        Memory copy between two addresses.
        Parameters:
        pAddressOrg - source address
        pAddressDest - destination address
        pLengthInBytes - 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 address
        pLengthInBytes - length in bytes
        pValue - byte value
      • setMemorySafely

        public static final void setMemorySafely​(long pAddress,
                                                 long pLengthInBytes,
                                                 byte pValue)
                                          throws InvalidNativeMemoryAccessException
        Fills a given memory region with a given byte value (repeated).
        Parameters:
        pAddress - memory region address
        pLengthInBytes - length in bytes
        pValue - 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 - address
        pValue - value
      • setChar

        public static final void setChar​(long pAddress,
                                         char pValue)
        Sets value at given address.
        Parameters:
        pAddress - address
        pValue - value
      • setShort

        public static final void setShort​(long pAddress,
                                          short pValue)
        Sets value at given address.
        Parameters:
        pAddress - address
        pValue - value
      • setInt

        public static final void setInt​(long pAddress,
                                        int pValue)
        Sets value at given address.
        Parameters:
        pAddress - address
        pValue - value
      • setLong

        public static final void setLong​(long pAddress,
                                         long pValue)
        Sets value at given address.
        Parameters:
        pAddress - address
        pValue - value
      • setFloat

        public static final void setFloat​(long pAddress,
                                          float pValue)
        Sets value at given address.
        Parameters:
        pAddress - address
        pValue - value
      • setDouble

        public static final void setDouble​(long pAddress,
                                           double pValue)
        Sets value at given address.
        Parameters:
        pAddress - address
        pValue - 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.