Package com.sun.jna
Class Memory
- java.lang.Object
-
- com.sun.jna.Pointer
-
- com.sun.jna.Memory
-
- Direct Known Subclasses:
StringArray
public class Memory extends Pointer
APointer
to memory obtained from the native heap via a call tomalloc
.In some cases it might be necessary to use memory obtained from
malloc
. For example,Memory
helps accomplish the following idiom:void *buf = malloc(BUF_LEN * sizeof(char)); call_some_function(buf); free(buf);
The
finalize()
method will free allocated memory when this object is no longer referenced.- Author:
- Sheng Liang, originator, Todd Fast, suitability modifications, Timothy Wall
- See Also:
Pointer
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Memory
align(int byteBoundary)
Provide a view onto this structure with the given alignment.protected void
boundsCheck(long off, long sz)
Check that indirection won't cause us to write outside the malloc'ed space.void
clear()
Zero the full extent of this memory region.protected void
dispose()
Free the native memory and set peer to zerostatic void
disposeAll()
Dispose of all allocated memory.String
dump()
Dumps the contents of this memory object.protected void
finalize()
Properly dispose of native memory when this object is GC'd.protected static void
free(long p)
byte
getByte(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getByte
.ByteBuffer
getByteBuffer(long offset, long length)
Get a ByteBuffer mapped to a portion of this memory.char
getChar(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getByte
.double
getDouble(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getDouble
.float
getFloat(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getFloat
.int
getInt(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getInt
.long
getLong(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getLong
.Pointer
getPointer(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getPointer
.short
getShort(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getShort
.String
getString(long offset, String encoding)
Copy native memory to a Java String using the requested encoding.String
getWideString(long offset)
Read a wide (const wchar_t *
) string from memory.protected static long
malloc(long size)
static void
purge()
Force cleanup of memory that has associated NIO Buffers which have been GC'd.void
read(long bOff, byte[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
.void
read(long bOff, char[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
.void
read(long bOff, double[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
.void
read(long bOff, float[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
.void
read(long bOff, int[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
.void
read(long bOff, long[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
.void
read(long bOff, short[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
.void
setByte(long offset, byte value)
Indirect the native pointer tomalloc
space, a laPointer.setByte
.void
setChar(long offset, char value)
Indirect the native pointer tomalloc
space, a laPointer.setChar
.void
setDouble(long offset, double value)
Indirect the native pointer tomalloc
space, a laPointer.setDouble
.void
setFloat(long offset, float value)
Indirect the native pointer tomalloc
space, a laPointer.setFloat
.void
setInt(long offset, int value)
Indirect the native pointer tomalloc
space, a laPointer.setInt
.void
setLong(long offset, long value)
Indirect the native pointer tomalloc
space, a laPointer.setLong
.void
setPointer(long offset, Pointer value)
Indirect the native pointer tomalloc
space, a laPointer.setPointer
.void
setShort(long offset, short value)
Indirect the native pointer tomalloc
space, a laPointer.setShort
.void
setString(long offset, String value, String encoding)
Copy stringvalue
to the location being pointed to, using the requested encoding.void
setWideString(long offset, String value)
Copy stringvalue
to the location being pointed to as a wide string (wchar_t*
).Pointer
share(long offset)
Provide a view of this memory using the given offset as the base address.Pointer
share(long offset, long sz)
Provide a view of this memory using the given offset as the base address, bounds-limited with the given size.long
size()
String
toString()
boolean
valid()
Returns false if the memory has been freed.void
write(long bOff, byte[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
.void
write(long bOff, char[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
.void
write(long bOff, double[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
.void
write(long bOff, float[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
.void
write(long bOff, int[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
.void
write(long bOff, long[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
.void
write(long bOff, short[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
.-
Methods inherited from class com.sun.jna.Pointer
clear, createConstant, createConstant, dump, equals, getByteArray, getCharArray, getDoubleArray, getFloatArray, getIntArray, getLongArray, getNativeLong, getPointerArray, getPointerArray, getShortArray, getString, getStringArray, getStringArray, getStringArray, getStringArray, getValue, getWideStringArray, getWideStringArray, hashCode, indexOf, nativeValue, nativeValue, read, setMemory, setNativeLong, setString, setString, setValue, write
-
-
-
-
Method Detail
-
purge
public static void purge()
Force cleanup of memory that has associated NIO Buffers which have been GC'd.
-
disposeAll
public static void disposeAll()
Dispose of all allocated memory.
-
share
public Pointer share(long offset)
Provide a view of this memory using the given offset as the base address. The returnedPointer
will have a size equal to that of the original minus the offset.- Overrides:
share
in classPointer
- Throws:
IndexOutOfBoundsException
- if the requested memory is outside the allocated bounds.
-
share
public Pointer share(long offset, long sz)
Provide a view of this memory using the given offset as the base address, bounds-limited with the given size. Maintains a reference to the originalMemory
object to avoid GC as long as the shared memory is referenced.- Overrides:
share
in classPointer
- Throws:
IndexOutOfBoundsException
- if the requested memory is outside the allocated bounds.
-
align
public Memory align(int byteBoundary)
Provide a view onto this structure with the given alignment.- Parameters:
byteBoundary
- Align memory to this number of bytes; should be a power of two.- Throws:
IndexOutOfBoundsException
- if the requested alignment can not be met.IllegalArgumentException
- if the requested alignment is not a positive power of two.
-
finalize
protected void finalize()
Properly dispose of native memory when this object is GC'd.
-
dispose
protected void dispose()
Free the native memory and set peer to zero
-
clear
public void clear()
Zero the full extent of this memory region.
-
valid
public boolean valid()
Returns false if the memory has been freed.
-
size
public long size()
-
boundsCheck
protected void boundsCheck(long off, long sz)
Check that indirection won't cause us to write outside the malloc'ed space.
-
read
public void read(long bOff, byte[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
read
in classPointer
- Parameters:
bOff
- byte offset from pointer from which data is copiedbuf
-byte
array into which data is copiedindex
- array index to which data is copiedlength
- number of elements from native pointer that must be copied- See Also:
Pointer.read(long,byte[],int,int)
-
read
public void read(long bOff, short[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
read
in classPointer
- Parameters:
bOff
- byte offset from pointer from which data is copiedbuf
-short
array into which data is copiedindex
- array index to which data is copiedlength
- number of elements from native pointer that must be copied- See Also:
Pointer.read(long,short[],int,int)
-
read
public void read(long bOff, char[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
read
in classPointer
- Parameters:
bOff
- byte offset from pointer from which data is copiedbuf
-char
array into which data is copiedindex
- array index to which data is copiedlength
- number of elements from native pointer that must be copied- See Also:
Pointer.read(long,char[],int,int)
-
read
public void read(long bOff, int[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
read
in classPointer
- Parameters:
bOff
- byte offset from pointer from which data is copiedbuf
-int
array into which data is copiedindex
- array index to which data is copiedlength
- number of elements from native pointer that must be copied- See Also:
Pointer.read(long,int[],int,int)
-
read
public void read(long bOff, long[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
read
in classPointer
- Parameters:
bOff
- byte offset from pointer from which data is copiedbuf
-long
array into which data is copiedindex
- array index to which data is copiedlength
- number of elements from native pointer that must be copied- See Also:
Pointer.read(long,long[],int,int)
-
read
public void read(long bOff, float[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
read
in classPointer
- Parameters:
bOff
- byte offset from pointer from which data is copiedbuf
-float
array into which data is copiedindex
- array index to which data is copiedlength
- number of elements from native pointer that must be copied- See Also:
Pointer.read(long,float[],int,int)
-
read
public void read(long bOff, double[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.read
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
read
in classPointer
- Parameters:
bOff
- byte offset from pointer from which data is copiedbuf
-double
array into which data is copiedindex
- array index to which data is copiedlength
- number of elements from native pointer that must be copied- See Also:
Pointer.read(long,double[],int,int)
-
write
public void write(long bOff, byte[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
write
in classPointer
- Parameters:
bOff
- byte offset from pointer into which data is copiedbuf
-byte
array from which to copyindex
- array index from which to start copyinglength
- number of elements frombuf
that must be copied- See Also:
Pointer.write(long,byte[],int,int)
-
write
public void write(long bOff, short[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
write
in classPointer
- Parameters:
bOff
- byte offset from pointer into which data is copiedbuf
-short
array from which to copyindex
- array index from which to start copyinglength
- number of elements frombuf
that must be copied- See Also:
Pointer.write(long,short[],int,int)
-
write
public void write(long bOff, char[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
write
in classPointer
- Parameters:
bOff
- byte offset from pointer into which data is copiedbuf
-char
array from which to copyindex
- array index from which to start copyinglength
- number of elements frombuf
that must be copied- See Also:
Pointer.write(long,char[],int,int)
-
write
public void write(long bOff, int[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
write
in classPointer
- Parameters:
bOff
- byte offset from pointer into which data is copiedbuf
-int
array from which to copyindex
- array index from which to start copyinglength
- number of elements frombuf
that must be copied- See Also:
Pointer.write(long,int[],int,int)
-
write
public void write(long bOff, long[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
write
in classPointer
- Parameters:
bOff
- byte offset from pointer into which data is copiedbuf
-long
array from which to copyindex
- array index from which to start copyinglength
- number of elements frombuf
that must be copied- See Also:
Pointer.write(long,long[],int,int)
-
write
public void write(long bOff, float[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
write
in classPointer
- Parameters:
bOff
- byte offset from pointer into which data is copiedbuf
-float
array from which to copyindex
- array index from which to start copyinglength
- number of elements frombuf
that must be copied- See Also:
Pointer.write(long,float[],int,int)
-
write
public void write(long bOff, double[] buf, int index, int length)
Indirect the native pointer tomalloc
space, a laPointer.write
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
write
in classPointer
- Parameters:
bOff
- byte offset from pointer into which data is copiedbuf
-double
array from which to copyindex
- array index from which to start copyinglength
- number of elements frombuf
that must be copied- See Also:
Pointer.write(long,double[],int,int)
-
getByte
public byte getByte(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getByte
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
getByte
in classPointer
- Parameters:
offset
- offset from pointer to perform the indirection- Returns:
- the
byte
value being pointed to - See Also:
Pointer.getByte(long)
-
getChar
public char getChar(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getByte
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
getChar
in classPointer
- Parameters:
offset
- offset from pointer to perform the indirection- Returns:
- the
wchar_t
value being pointed to - See Also:
Pointer.getByte(long)
-
getShort
public short getShort(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getShort
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
getShort
in classPointer
- Parameters:
offset
- byte offset from pointer to perform the indirection- Returns:
- the
short
value being pointed to - See Also:
Pointer.getShort(long)
-
getInt
public int getInt(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getInt
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
getInt
in classPointer
- Parameters:
offset
- byte offset from pointer to perform the indirection- Returns:
- the
int
value being pointed to - See Also:
Pointer.getInt(long)
-
getLong
public long getLong(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getLong
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
getLong
in classPointer
- Parameters:
offset
- byte offset from pointer to perform the indirection- Returns:
- the
long
value being pointed to - See Also:
Pointer.getLong(long)
-
getFloat
public float getFloat(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getFloat
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
getFloat
in classPointer
- Parameters:
offset
- byte offset from pointer to perform the indirection- Returns:
- the
float
value being pointed to - See Also:
Pointer.getFloat(long)
-
getDouble
public double getDouble(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getDouble
. But this method performs a bounds check to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
getDouble
in classPointer
- Parameters:
offset
- byte offset from pointer to perform the indirection- Returns:
- the
double
value being pointed to - See Also:
Pointer.getDouble(long)
-
getPointer
public Pointer getPointer(long offset)
Indirect the native pointer tomalloc
space, a laPointer.getPointer
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
getPointer
in classPointer
- Parameters:
offset
- byte offset from pointer to perform the indirection- Returns:
- a
Pointer
equivalent of the pointer value being pointed to, ornull
if the pointer value isNULL
; - See Also:
Pointer.getPointer(long)
-
getByteBuffer
public ByteBuffer getByteBuffer(long offset, long length)
Get a ByteBuffer mapped to a portion of this memory. We keep a weak reference to all ByteBuffers provided so that this memory object is not GC'd while there are still implicit outstanding references to it (it'd be nice if we could attach our own reference to the ByteBuffer, but the VM generates the object so we have no control over it).- Overrides:
getByteBuffer
in classPointer
- Parameters:
offset
- byte offset from pointer to start the bufferlength
- Length of ByteBuffer- Returns:
- a direct ByteBuffer that accesses the memory being pointed to,
-
getString
public String getString(long offset, String encoding)
Description copied from class:Pointer
Copy native memory to a Java String using the requested encoding.
-
getWideString
public String getWideString(long offset)
Description copied from class:Pointer
Read a wide (const wchar_t *
) string from memory.- Overrides:
getWideString
in classPointer
-
setByte
public void setByte(long offset, byte value)
Indirect the native pointer tomalloc
space, a laPointer.setByte
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
setByte
in classPointer
- Parameters:
offset
- byte offset from pointer at whichvalue
must be setvalue
-byte
value to set- See Also:
Pointer.setByte(long, byte)
-
setChar
public void setChar(long offset, char value)
Indirect the native pointer tomalloc
space, a laPointer.setChar
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
setChar
in classPointer
- Parameters:
offset
- byte offset from pointer at whichvalue
must be setvalue
-char
value to set- See Also:
Pointer.setChar(long, char)
-
setShort
public void setShort(long offset, short value)
Indirect the native pointer tomalloc
space, a laPointer.setShort
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
setShort
in classPointer
- Parameters:
offset
- byte offset from pointer at whichvalue
must be setvalue
-short
value to set- See Also:
Pointer.setShort(long, short)
-
setInt
public void setInt(long offset, int value)
Indirect the native pointer tomalloc
space, a laPointer.setInt
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
setInt
in classPointer
- Parameters:
offset
- byte offset from pointer at whichvalue
must be setvalue
-int
value to set- See Also:
Pointer.setInt(long, int)
-
setLong
public void setLong(long offset, long value)
Indirect the native pointer tomalloc
space, a laPointer.setLong
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
setLong
in classPointer
- Parameters:
offset
- byte offset from pointer at whichvalue
must be setvalue
-long
value to set- See Also:
Pointer.setLong(long, long)
-
setFloat
public void setFloat(long offset, float value)
Indirect the native pointer tomalloc
space, a laPointer.setFloat
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
setFloat
in classPointer
- Parameters:
offset
- byte offset from pointer at whichvalue
must be setvalue
-float
value to set- See Also:
Pointer.setFloat(long, float)
-
setDouble
public void setDouble(long offset, double value)
Indirect the native pointer tomalloc
space, a laPointer.setDouble
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
setDouble
in classPointer
- Parameters:
offset
- byte offset from pointer at whichvalue
must be setvalue
-double
value to set- See Also:
Pointer.setDouble(long, double)
-
setPointer
public void setPointer(long offset, Pointer value)
Indirect the native pointer tomalloc
space, a laPointer.setPointer
. But this method performs a bounds checks to ensure that the indirection does not cause memory outside themalloc
ed space to be accessed.- Overrides:
setPointer
in classPointer
- Parameters:
offset
- byte offset from pointer at whichvalue
must be setvalue
-Pointer
holding the actual pointer value to set, which may benull
to indicate aNULL
pointer.- See Also:
Pointer.setPointer(long, com.sun.jna.Pointer)
-
setString
public void setString(long offset, String value, String encoding)
Description copied from class:Pointer
Copy stringvalue
to the location being pointed to, using the requested encoding.
-
setWideString
public void setWideString(long offset, String value)
Description copied from class:Pointer
Copy stringvalue
to the location being pointed to as a wide string (wchar_t*
).- Overrides:
setWideString
in classPointer
- Parameters:
offset
- byte offset from pointer at which characters invalue
must be setvalue
-java.lang.String
value to set
-
free
protected static void free(long p)
-
malloc
protected static long malloc(long size)
-
dump
public String dump()
Dumps the contents of this memory object.
-
-