public class Memory extends Pointer
Pointer
to memory obtained from the native heap via a
call to malloc
.
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.
Pointer
Modifier and Type | Field and Description |
---|---|
protected long |
size |
Modifier | Constructor and Description |
---|---|
protected |
Memory() |
|
Memory(long size)
Allocate space in the native heap via a call to C's
malloc . |
Modifier and Type | Method and 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 zero
|
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 to
malloc space, a la
Pointer.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 to
malloc space, a la
Pointer.getByte . |
double |
getDouble(long offset)
Indirect the native pointer to
malloc space, a la
Pointer.getDouble . |
float |
getFloat(long offset)
Indirect the native pointer to
malloc space, a la
Pointer.getFloat . |
int |
getInt(long offset)
Indirect the native pointer to
malloc space, a la
Pointer.getInt . |
long |
getLong(long offset)
Indirect the native pointer to
malloc space, a la
Pointer.getLong . |
Pointer |
getPointer(long offset)
Indirect the native pointer to
malloc space, a la
Pointer.getPointer . |
short |
getShort(long offset)
Indirect the native pointer to
malloc space, a la
Pointer.getShort . |
long |
getSize()
Deprecated.
use
size() instead. |
String |
getString(long offset,
boolean wide)
Indirect the native pointer to
malloc space, a la
Pointer.getString . |
boolean |
isValid()
Deprecated.
use
valid() instead. |
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 to
malloc space, a la
Pointer.read . |
void |
read(long bOff,
char[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.read . |
void |
read(long bOff,
double[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.read . |
void |
read(long bOff,
float[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.read . |
void |
read(long bOff,
int[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.read . |
void |
read(long bOff,
long[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.read . |
void |
read(long bOff,
short[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.read . |
void |
setByte(long offset,
byte value)
Indirect the native pointer to
malloc space, a la
Pointer.setByte . |
void |
setChar(long offset,
char value)
Indirect the native pointer to
malloc space, a la
Pointer.setChar . |
void |
setDouble(long offset,
double value)
Indirect the native pointer to
malloc space, a la
Pointer.setDouble . |
void |
setFloat(long offset,
float value)
Indirect the native pointer to
malloc space, a la
Pointer.setFloat . |
void |
setInt(long offset,
int value)
Indirect the native pointer to
malloc space, a la
Pointer.setInt . |
void |
setLong(long offset,
long value)
Indirect the native pointer to
malloc space, a la
Pointer.setLong . |
void |
setPointer(long offset,
Pointer value)
Indirect the native pointer to
malloc space, a la
Pointer.setPointer . |
void |
setShort(long offset,
short value)
Indirect the native pointer to
malloc space, a la
Pointer.setShort . |
void |
setString(long offset,
String value,
boolean wide)
Indirect the native pointer to
malloc space, a la
Pointer.setString . |
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 to
malloc space, a la
Pointer.write . |
void |
write(long bOff,
char[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.write . |
void |
write(long bOff,
double[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.write . |
void |
write(long bOff,
float[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.write . |
void |
write(long bOff,
int[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.write . |
void |
write(long bOff,
long[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.write . |
void |
write(long bOff,
short[] buf,
int index,
int length)
Indirect the native pointer to
malloc space, a la
Pointer.write . |
clear, createConstant, createConstant, equals, getByteArray, getCharArray, getDoubleArray, getFloatArray, getIntArray, getLongArray, getNativeLong, getPointerArray, getPointerArray, getShortArray, getString, getStringArray, getStringArray, getStringArray, getStringArray, getValue, hashCode, indexOf, nativeValue, nativeValue, read, setMemory, setNativeLong, setString, setValue, write
public Memory(long size)
malloc
.size
- number of bytes of space to allocateprotected Memory()
public static void purge()
public Pointer share(long offset)
Pointer
will have a size equal to that of the original
minus the offset.share
in class Pointer
IndexOutOfBoundsException
- if the requested memory is outside
the allocated bounds.public Pointer share(long offset, long sz)
Memory
object to avoid GC as long as the shared
memory is referenced.share
in class Pointer
IndexOutOfBoundsException
- if the requested memory is outside
the allocated bounds.public Memory align(int byteBoundary)
byteBoundary
- Align memory to this number of bytes; should be a
power of two.IndexOutOfBoundsException
- if the requested alignment can
not be met.IllegalArgumentException
- if the requested alignment is not
a positive power of two.protected void finalize()
protected void dispose()
public void clear()
public boolean isValid()
valid()
instead.public boolean valid()
public long size()
public long getSize()
size()
instead.protected void boundsCheck(long off, long sz)
public void read(long bOff, byte[] buf, int index, int length)
malloc
space, a la
Pointer.read
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.read
in class Pointer
bOff
- byte offset from pointer into which data is copiedbuf
- byte
array into which data is copiedindex
- array index from which to start copyinglength
- number of elements from native pointer that must be copiedPointer.read(long,byte[],int,int)
public void read(long bOff, short[] buf, int index, int length)
malloc
space, a la
Pointer.read
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.read
in class Pointer
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 copiedPointer.read(long,short[],int,int)
public void read(long bOff, char[] buf, int index, int length)
malloc
space, a la
Pointer.read
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.read
in class Pointer
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 copiedPointer.read(long,char[],int,int)
public void read(long bOff, int[] buf, int index, int length)
malloc
space, a la
Pointer.read
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.read
in class Pointer
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 copiedPointer.read(long,int[],int,int)
public void read(long bOff, long[] buf, int index, int length)
malloc
space, a la
Pointer.read
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.read
in class Pointer
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 copiedPointer.read(long,long[],int,int)
public void read(long bOff, float[] buf, int index, int length)
malloc
space, a la
Pointer.read
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.read
in class Pointer
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 copiedPointer.read(long,float[],int,int)
public void read(long bOff, double[] buf, int index, int length)
malloc
space, a la
Pointer.read
. But this method performs a bounds checks to
ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.read
in class Pointer
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 copiedPointer.read(long,double[],int,int)
public void write(long bOff, byte[] buf, int index, int length)
malloc
space, a la
Pointer.write
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.write
in class Pointer
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 from buf
that must be
copiedPointer.write(long,byte[],int,int)
public void write(long bOff, short[] buf, int index, int length)
malloc
space, a la
Pointer.write
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.write
in class Pointer
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 from buf
that must be
copiedPointer.write(long,short[],int,int)
public void write(long bOff, char[] buf, int index, int length)
malloc
space, a la
Pointer.write
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.write
in class Pointer
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 from buf
that must be
copiedPointer.write(long,char[],int,int)
public void write(long bOff, int[] buf, int index, int length)
malloc
space, a la
Pointer.write
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.write
in class Pointer
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 from buf
that must be
copiedPointer.write(long,int[],int,int)
public void write(long bOff, long[] buf, int index, int length)
malloc
space, a la
Pointer.write
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.write
in class Pointer
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 from buf
that must be
copiedPointer.write(long,long[],int,int)
public void write(long bOff, float[] buf, int index, int length)
malloc
space, a la
Pointer.write
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.write
in class Pointer
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 from buf
that must be
copiedPointer.write(long,float[],int,int)
public void write(long bOff, double[] buf, int index, int length)
malloc
space, a la
Pointer.write
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.write
in class Pointer
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 from buf
that must be
copiedPointer.write(long,double[],int,int)
public byte getByte(long offset)
malloc
space, a la
Pointer.getByte
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.getByte
in class Pointer
offset
- offset from pointer to perform the indirectionbyte
value being pointed toPointer.getByte(long)
public char getChar(long offset)
malloc
space, a la
Pointer.getByte
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.getChar
in class Pointer
offset
- offset from pointer to perform the indirectionwchar_t
value being pointed toPointer.getByte(long)
public short getShort(long offset)
malloc
space, a la
Pointer.getShort
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.getShort
in class Pointer
offset
- byte offset from pointer to perform the indirectionshort
value being pointed toPointer.getShort(long)
public int getInt(long offset)
malloc
space, a la
Pointer.getInt
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.getInt
in class Pointer
offset
- byte offset from pointer to perform the indirectionint
value being pointed toPointer.getInt(long)
public long getLong(long offset)
malloc
space, a la
Pointer.getLong
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.getLong
in class Pointer
offset
- byte offset from pointer to perform the indirectionlong
value being pointed toPointer.getLong(long)
public float getFloat(long offset)
malloc
space, a la
Pointer.getFloat
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.getFloat
in class Pointer
offset
- byte offset from pointer to perform the indirectionfloat
value being pointed toPointer.getFloat(long)
public double getDouble(long offset)
malloc
space, a la
Pointer.getDouble
. But this method performs a
bounds check to ensure that the indirection does not cause memory
outside the malloc
ed space to be accessed.getDouble
in class Pointer
offset
- byte offset from pointer to perform the indirectiondouble
value being pointed toPointer.getDouble(long)
public Pointer getPointer(long offset)
malloc
space, a la
Pointer.getPointer
. But this method performs
a bounds checks to ensure that the indirection does not cause memory
outside the malloc
ed space to be accessed.getPointer
in class Pointer
offset
- byte offset from pointer to perform the indirectionPointer
equivalent of the pointer value
being pointed to, or null
if the pointer value is
NULL
;Pointer.getPointer(long)
public ByteBuffer getByteBuffer(long offset, long length)
getByteBuffer
in class Pointer
offset
- byte offset from pointer to start the bufferlength
- Length of ByteBufferpublic String getString(long offset, boolean wide)
malloc
space, a la
Pointer.getString
. But this method performs a
bounds checks to ensure that the indirection does not cause memory
outside the malloc
ed space to be accessed.getString
in class Pointer
offset
- byte offset from pointer to obtain the native string
v * @param wide whether to convert from a wide or standard C stringString
value being pointed toPointer.getString(long, boolean)
public void setByte(long offset, byte value)
malloc
space, a la
Pointer.setByte
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.setByte
in class Pointer
offset
- byte offset from pointer at which value
must be setvalue
- byte
value to setPointer.setByte(long, byte)
public void setChar(long offset, char value)
malloc
space, a la
Pointer.setChar
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.setChar
in class Pointer
offset
- byte offset from pointer at which value
must be setvalue
- char
value to setPointer.setChar(long, char)
public void setShort(long offset, short value)
malloc
space, a la
Pointer.setShort
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.setShort
in class Pointer
offset
- byte offset from pointer at which value
must be setvalue
- short
value to setPointer.setShort(long, short)
public void setInt(long offset, int value)
malloc
space, a la
Pointer.setInt
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.setInt
in class Pointer
offset
- byte offset from pointer at which value
must be setvalue
- int
value to setPointer.setInt(long, int)
public void setLong(long offset, long value)
malloc
space, a la
Pointer.setLong
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.setLong
in class Pointer
offset
- byte offset from pointer at which value
must be setvalue
- long
value to setPointer.setLong(long, long)
public void setFloat(long offset, float value)
malloc
space, a la
Pointer.setFloat
. But this method performs a bounds
checks to ensure that the indirection does not cause memory outside the
malloc
ed space to be accessed.setFloat
in class Pointer
offset
- byte offset from pointer at which value
must be setvalue
- float
value to setPointer.setFloat(long, float)
public void setDouble(long offset, double value)
malloc
space, a la
Pointer.setDouble
. But this method performs a
bounds checks to ensure that the indirection does not cause memory
outside the malloc
ed space to be accessed.setDouble
in class Pointer
offset
- byte offset from pointer at which value
must be setvalue
- double
value to setPointer.setDouble(long, double)
public void setPointer(long offset, Pointer value)
malloc
space, a la
Pointer.setPointer
. But this method performs
a bounds checks to ensure that the indirection does not cause memory
outside the malloc
ed space to be accessed.setPointer
in class Pointer
offset
- byte offset from pointer at which value
must be setvalue
- Pointer
holding the actual pointer value to
set, which may be null
to indicate a NULL
pointer.Pointer.setPointer(long, com.sun.jna.Pointer)
public void setString(long offset, String value, boolean wide)
malloc
space, a la
Pointer.setString
. But this method performs a
bounds checks to ensure that the indirection does not cause memory
outside the malloc
ed space to be accessed.setString
in class Pointer
offset
- byte offset from pointer at which characters in
value
must be setvalue
- java.lang.String
value to setwide
- whether to write the native string as an array of
wchar_t
. If false, writes as a NUL-terminated array of
char
using the default platform encoding.Pointer.setString(long,String,boolean)
protected static void free(long p)
protected static long malloc(long size)