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
|
static 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 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 . |
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 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,
String encoding)
Copy string
value to the location being pointed to, using
the requested encoding. |
void |
setWideString(long offset,
String value)
Copy string
value 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 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, dump, equals, getByteArray, getCharArray, getDoubleArray, getFloatArray, getIntArray, getLongArray, getNativeLong, getPointerArray, getPointerArray, getShortArray, getString, getString, getStringArray, getStringArray, getStringArray, getStringArray, getStringArray, getStringArray, getValue, getWideStringArray, getWideStringArray, hashCode, indexOf, nativeValue, nativeValue, read, setMemory, setNativeLong, setString, setString, setString, setValue, write
public Memory(long size)
malloc
.size
- number of bytes of space to allocateprotected Memory()
public static void purge()
public static void disposeAll()
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 valid()
public long size()
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, String encoding)
Pointer
public String getWideString(long offset)
Pointer
const wchar_t *
) string from memory.getWideString
in class Pointer
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, String encoding)
Pointer
value
to the location being pointed to, using
the requested encoding.public void setWideString(long offset, String value)
Pointer
value
to the location being pointed to as a
wide string (wchar_t*
).setWideString
in class Pointer
offset
- byte offset from pointer at which characters in
value
must be setvalue
- java.lang.String
value to setprotected static void free(long p)
protected static long malloc(long size)
public String dump()