Class OffHeapMemory
- java.lang.Object
-
- net.haesleinhuepf.clij.coremem.rgc.FreeableBase
-
- net.haesleinhuepf.clij.coremem.MemoryBase
-
- net.haesleinhuepf.clij.coremem.offheap.OffHeapMemory
-
- All Implemented Interfaces:
ContiguousMemoryInterface,BridJPointerWrappable,ByteBufferWrappable,Copyable<ContiguousMemoryInterface>,CopyFromToJavaArray,CopyFromToNIOBuffers,CopyRangeFromToJavaArray,JNAPointerWrappable,MemoryTyped,PointerAccessible,RangeCopyable<MemoryBase>,ReadAt,ReadAtAligned,ReadWriteBytesFileChannel,Resizable,SizedInBytes,WriteAt,WriteAtAligned,Cleanable,Freeable
public class OffHeapMemory extends MemoryBase implements Resizable, ContiguousMemoryInterface
Instances of this class represent contguous regions of off-heap memory.- Author:
- royer
-
-
Field Summary
Fields Modifier and Type Field Description protected StackTraceElement[]mAllocationStackTraceprotected StringmNameprotected ObjectmParentprotected LongmSignature-
Fields inherited from class net.haesleinhuepf.clij.coremem.MemoryBase
mAddressInBytes, mIsFree, mLengthInBytes
-
-
Constructor Summary
Constructors Constructor Description OffHeapMemory(long pLengthInBytes)Allocates an off-heap memory region of given length in bytes.OffHeapMemory(Object pParent, long pAddress, long pLengthInBytes)Warps an off-heap memory region of given parent, address, and length in bytes.OffHeapMemory(String pName, long pLengthInBytes)Allocates an off-heap memory region of given name and length in bytes.OffHeapMemory(String pName, Object pParent, long pAddress, long pLengthInBytes)Warps an off-heap memory region of given name, parent, address, and length in bytes.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static OffHeapMemoryallocateAlignedBytes(String pName, long pNumberOfBytes, long pAlignment)Allocates off-heap memory that can hold a given number of bytes, with a given alignment.static OffHeapMemoryallocateBytes(long pNumberOfBytes)Allocates off-heap memory that can hold a given number of bytes.static OffHeapMemoryallocateBytes(String pName, long pNumberOfBytes)Allocates off-heap memory that can hold a given number of bytes.static OffHeapMemoryallocateChars(long pNumberOfChars)Allocates off-heap memory that can hold a given number of chars.static OffHeapMemoryallocateChars(String pName, long pNumberOfChars)Allocates off-heap memory that can hold a given number of chars.static OffHeapMemoryallocateDoubles(long pNumberOfDoubles)Allocates off-heap memory that can hold a given number of doubles.static OffHeapMemoryallocateDoubles(String pName, long pNumberOfDoubles)Allocates off-heap memory that can hold a given number of doubles.static OffHeapMemoryallocateFloats(long pNumberOfFloats)Allocates off-heap memory that can hold a given number of floats.static OffHeapMemoryallocateFloats(String pName, long pNumberOfFloats)Allocates off-heap memory that can hold a given number of floats.static OffHeapMemoryallocateInts(long pNumberOfInts)Allocates off-heap memory that can hold a given number of ints.static OffHeapMemoryallocateInts(String pName, long pNumberOfInts)Allocates off-heap memory that can hold a given number of ints.static OffHeapMemoryallocateLongs(long pNumberOfLongs)Allocates off-heap memory that can hold a given number of longs.static OffHeapMemoryallocateLongs(String pName, long pNumberOfLongs)Allocates off-heap memory that can hold a given number of longs.static OffHeapMemoryallocatePageAlignedBytes(String pName, long pNumberOfBytes)Allocates page=aligned off-heap memory that can hold a given number of bytes.static OffHeapMemoryallocateShorts(long pNumberOfShorts)Allocates off-heap memory that can hold a given number of shorts.static OffHeapMemoryallocateShorts(String pName, long pNumberOfShorts)Allocates off-heap memory that can hold a given number of shorts.static OffHeapMemorycopyFromArray(byte[] pBuffer)Creates a off-heap memory object initialized by copying the contents of a byte array.voidfree()Free this object, which means that the underlying ressources are released.CleanergetCleaner()Returns the cleaner for this object.MemoryTypegetMemoryType()Returns this memory object type.longresize(long pNewLength)Resizes this memory object.OffHeapMemorysubRegion(long pOffset, long pLenghInBytes)Returns a contiguous memory object representing for a memory sub region.StringtoString()static OffHeapMemorywrapBuffer(Buffer pBuffer)Wraps a NIO buffer.static OffHeapMemorywrapPointer(com.sun.jna.Memory pJNAMemory)Deprecated.static OffHeapMemorywrapPointer(com.sun.jna.Pointer pJNAPointer, long pTargetSizeInBytes)Wraps a JNA pointer.static OffHeapMemorywrapPointer(Object pParent, long pAddress, long pLengthInBytes)Wraps a 'raw' pointer i.e.static OffHeapMemorywrapPointer(String pName, Object pParent, long pAddress, long pLengthInBytes)Wraps a 'raw' pointer i.e.static OffHeapMemorywrapPointer(org.bridj.Pointer<Byte> pBridJPointer)Wraps a bridj pointer.-
Methods inherited from class net.haesleinhuepf.clij.coremem.MemoryBase
copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyRangeTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, getAddress, getBridJPointer, getByte, getByteAligned, getByteBuffer, getChar, getCharAligned, getDouble, getDoubleAligned, getFloat, getFloatAligned, getInt, getIntAligned, getJNAPointer, getLong, getLongAligned, getShort, getShortAligned, getSizeInBytes, isFree, readBytesFromFileChannel, readBytesFromFileChannel, setByte, setByteAligned, setChar, setCharAligned, setDouble, setDoubleAligned, setFloat, setFloatAligned, setInt, setIntAligned, setLong, setLongAligned, setShort, setShortAligned, writeBytesToFileChannel, writeBytesToFileChannel
-
Methods inherited from class net.haesleinhuepf.clij.coremem.rgc.FreeableBase
complainIfFreed
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.BridJPointerWrappable
getBridJPointer
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.ByteBufferWrappable
getByteBuffer
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.Copyable
copyFrom, copyTo
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.CopyFromToJavaArray
copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.CopyFromToNIOBuffers
copyFrom, copyTo
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.CopyRangeFromToJavaArray
copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo, copyTo
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.rgc.Freeable
complainIfFreed, isFree
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.JNAPointerWrappable
getJNAPointer
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.PointerAccessible
getAddress
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.ReadAt
getByte, getChar, getDouble, getFloat, getInt, getLong, getShort
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.ReadAtAligned
getByteAligned, getCharAligned, getDoubleAligned, getFloatAligned, getIntAligned, getLongAligned, getShortAligned
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.ReadWriteBytesFileChannel
readBytesFromFileChannel, readBytesFromFileChannel, writeBytesToFileChannel, writeBytesToFileChannel
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.SizedInBytes
getSizeInBytes
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.WriteAt
setByte, setChar, setDouble, setFloat, setInt, setLong, setShort
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.interfaces.WriteAtAligned
setByteAligned, setCharAligned, setDoubleAligned, setFloatAligned, setIntAligned, setLongAligned, setShortAligned
-
-
-
-
Field Detail
-
mAllocationStackTrace
protected StackTraceElement[] mAllocationStackTrace
-
mName
protected String mName
-
mSignature
protected Long mSignature
-
mParent
protected Object mParent
-
-
Constructor Detail
-
OffHeapMemory
public OffHeapMemory(long pLengthInBytes)
Allocates an off-heap memory region of given length in bytes.- Parameters:
pLengthInBytes- length in bytes
-
OffHeapMemory
public OffHeapMemory(String pName, long pLengthInBytes)
Allocates an off-heap memory region of given name and length in bytes.- Parameters:
pName- namepLengthInBytes- length in bytes
-
OffHeapMemory
public OffHeapMemory(Object pParent, long pAddress, long pLengthInBytes)
Warps an off-heap memory region of given parent, address, and length in bytes.- Parameters:
pParent- parentpAddress- addresspLengthInBytes- length in bytes
-
-
Method Detail
-
wrapPointer
public static final OffHeapMemory wrapPointer(Object pParent, long pAddress, long pLengthInBytes)
Wraps a 'raw' pointer i.e. a long pointer value and a length. This is used to wrap a non-coremem memory region. A parent can be given, to prevent its garbage collection and the possible release of the underlying memory resource.- Parameters:
pParent- parent reference to prevent the parent's garbage collection.pAddress- addresspLengthInBytes- length in bytes- Returns:
- off-heap memory object
-
wrapPointer
public static final OffHeapMemory wrapPointer(String pName, Object pParent, long pAddress, long pLengthInBytes)
Wraps a 'raw' pointer i.e. a long pointer value and a length. This is used to wrap a non-coremem memory region. A parent can be given, to prevent its garbage collection and the possible release of the underlying memory resource.- Parameters:
pName- memory region namepParent- parent reference to prevent the parent's garbage collection.pAddress- addresspLengthInBytes- length in bytes- Returns:
- off-heap memory object
-
wrapPointer
public static OffHeapMemory wrapPointer(com.sun.jna.Pointer pJNAPointer, long pTargetSizeInBytes)
Wraps a JNA pointer.- Parameters:
pJNAPointer- JNA pointerpTargetSizeInBytes- ?- Returns:
- off-heap memory object
-
wrapPointer
@Deprecated public static OffHeapMemory wrapPointer(com.sun.jna.Memory pJNAMemory)
Deprecated.In an ideal world, this would wraps a JNA memory or pointer. But, because of a fatal design flaw in JNA: no automatic freeing on garbage collection. They do use 'finalise' horror! this is highly discouraged and a mostly deprecated feature of the JVM, it kills GC performance, why are they doing this?This static method here is meant to be educative and a deterrent to this pattern. It should not be used, cannot be used and is not functional anyway. You should always use CoreMem to allocate memory, because CoreMem does the right thing by freeing memory upon garbage collection. If you need a JNA memory, allocate a OffHeapMemory and pass a JNA pointer with getJNAPointer(). If you don't use this pattern, then you have to free the
- Parameters:
pJNAMemory- JNA memory- Returns:
- off-heap memory object
-
wrapPointer
public static OffHeapMemory wrapPointer(org.bridj.Pointer<Byte> pBridJPointer)
Wraps a bridj pointer.- Parameters:
pBridJPointer- BridJ pointer- Returns:
- off-heap memory object
-
wrapBuffer
public static final OffHeapMemory wrapBuffer(Buffer pBuffer)
Wraps a NIO buffer.- Parameters:
pBuffer- NIO buffer- Returns:
- off-heap memory object
-
copyFromArray
public static final OffHeapMemory copyFromArray(byte[] pBuffer)
Creates a off-heap memory object initialized by copying the contents of a byte array.- Parameters:
pBuffer- buffer to copy contents from- Returns:
- off-heap memory object
-
allocateBytes
public static OffHeapMemory allocateBytes(long pNumberOfBytes)
Allocates off-heap memory that can hold a given number of bytes.- Parameters:
pNumberOfBytes- number of bytes- Returns:
- off-heap memory object
-
allocateChars
public static OffHeapMemory allocateChars(long pNumberOfChars)
Allocates off-heap memory that can hold a given number of chars.- Parameters:
pNumberOfChars- number of chars- Returns:
- off-heap memory object
-
allocateShorts
public static OffHeapMemory allocateShorts(long pNumberOfShorts)
Allocates off-heap memory that can hold a given number of shorts.- Parameters:
pNumberOfShorts- number of shorts- Returns:
- off-heap memory object
-
allocateInts
public static OffHeapMemory allocateInts(long pNumberOfInts)
Allocates off-heap memory that can hold a given number of ints.- Parameters:
pNumberOfInts- number of ints- Returns:
- off-heap memory object
-
allocateLongs
public static OffHeapMemory allocateLongs(long pNumberOfLongs)
Allocates off-heap memory that can hold a given number of longs.- Parameters:
pNumberOfLongs- number of longs- Returns:
- off-heap memory object
-
allocateFloats
public static OffHeapMemory allocateFloats(long pNumberOfFloats)
Allocates off-heap memory that can hold a given number of floats.- Parameters:
pNumberOfFloats- number of floats- Returns:
- off-heap memory object
-
allocateDoubles
public static OffHeapMemory allocateDoubles(long pNumberOfDoubles)
Allocates off-heap memory that can hold a given number of doubles.- Parameters:
pNumberOfDoubles- number of doubles- Returns:
- off-heap memory object
-
allocateBytes
public static OffHeapMemory allocateBytes(String pName, long pNumberOfBytes)
Allocates off-heap memory that can hold a given number of bytes.- Parameters:
pName- name (can be used to track allocation origin)pNumberOfBytes- number of bytes- Returns:
- off-heap memory object
-
allocateChars
public static OffHeapMemory allocateChars(String pName, long pNumberOfChars)
Allocates off-heap memory that can hold a given number of chars.- Parameters:
pName- name (can be used to track allocation origin)pNumberOfChars- number of chars- Returns:
- off-heap memory object
-
allocateShorts
public static OffHeapMemory allocateShorts(String pName, long pNumberOfShorts)
Allocates off-heap memory that can hold a given number of shorts.- Parameters:
pName- name (can be used to track allocation origin)pNumberOfShorts- number of shorts- Returns:
- off-heap memory object
-
allocateInts
public static OffHeapMemory allocateInts(String pName, long pNumberOfInts)
Allocates off-heap memory that can hold a given number of ints.- Parameters:
pName- name (can be used to track allocation origin)pNumberOfInts- number of ints- Returns:
- off-heap memory object
-
allocateLongs
public static OffHeapMemory allocateLongs(String pName, long pNumberOfLongs)
Allocates off-heap memory that can hold a given number of longs.- Parameters:
pName- name (can be used to track allocation origin)pNumberOfLongs- number of longs- Returns:
- off-heap memory object
-
allocateFloats
public static OffHeapMemory allocateFloats(String pName, long pNumberOfFloats)
Allocates off-heap memory that can hold a given number of floats.- Parameters:
pName- name (can be used to track allocation origin)pNumberOfFloats- number of floats- Returns:
- off-heap memory object
-
allocateDoubles
public static OffHeapMemory allocateDoubles(String pName, long pNumberOfDoubles)
Allocates off-heap memory that can hold a given number of doubles.- Parameters:
pName- name (can be used to track allocation origin)pNumberOfDoubles- number of doubles- Returns:
- off-heap memory object
-
allocatePageAlignedBytes
public static OffHeapMemory allocatePageAlignedBytes(String pName, long pNumberOfBytes)
Allocates page=aligned off-heap memory that can hold a given number of bytes.- Parameters:
pName- name (can be used to track allocation origin)pNumberOfBytes- number of bytes- Returns:
- off-heap memory object
-
allocateAlignedBytes
public static OffHeapMemory allocateAlignedBytes(String pName, long pNumberOfBytes, long pAlignment)
Allocates off-heap memory that can hold a given number of bytes, with a given alignment.- Parameters:
pName- name (can be used to track allocation origin)pNumberOfBytes- number of bytespAlignment- byte boundary to align to.- Returns:
- off-heap memory object
-
subRegion
public OffHeapMemory subRegion(long pOffset, long pLenghInBytes)
Description copied from interface:ContiguousMemoryInterfaceReturns a contiguous memory object representing for a memory sub region.- Specified by:
subRegionin interfaceContiguousMemoryInterface- Parameters:
pOffset- offset in bytespLenghInBytes- length in bytes- Returns:
- contiguous memory for sub region
-
getMemoryType
public MemoryType getMemoryType()
Description copied from interface:MemoryTypedReturns this memory object type.- Specified by:
getMemoryTypein interfaceMemoryTyped- Specified by:
getMemoryTypein classMemoryBase- Returns:
- memory type.
-
resize
public long resize(long pNewLength)
Description copied from interface:ResizableResizes this memory object.
-
free
public void free()
Description copied from interface:FreeableFree this object, which means that the underlying ressources are released.- Specified by:
freein interfaceFreeable- Overrides:
freein classMemoryBase
-
getCleaner
public Cleaner getCleaner()
Description copied from interface:CleanableReturns the cleaner for this object.- Specified by:
getCleanerin interfaceCleanable- Returns:
- cleaner runnable
-
-