com.sun.jna

Class Memory

public class Memory extends Pointer

A 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 Memory 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

Field Summary
protected longsize
Constructor Summary
Memory(long size)
Allocate space in the native heap via a call to C's malloc.
protected Memory()
Method Summary
Memoryalign(int byteBoundary)
Provide a view onto this structure with the given alignment.
protected voidboundsCheck(long off, long sz)
Check that indirection won't cause us to write outside the malloc'ed space.
voidclear()
Zero the full extent of this memory region.
protected voidfinalize()
protected static voidfree(long ptr)
Call the real native free
bytegetByte(long offset)
Indirect the native pointer to malloc space, a la Pointer.getByte.
ByteBuffergetByteBuffer(long offset, long length)
Get a ByteBuffer mapped to a portion of this memory.
chargetChar(long offset)
Indirect the native pointer to malloc space, a la Pointer.getByte.
doublegetDouble(long offset)
Indirect the native pointer to malloc space, a la Pointer.getDouble.
floatgetFloat(long offset)
Indirect the native pointer to malloc space, a la Pointer.getFloat.
intgetInt(long offset)
Indirect the native pointer to malloc space, a la Pointer.getInt.
longgetLong(long offset)
Indirect the native pointer to malloc space, a la Pointer.getLong.
PointergetPointer(long offset)
Indirect the native pointer to malloc space, a la Pointer.getPointer.
shortgetShort(long offset)
Indirect the native pointer to malloc space, a la Pointer.getShort.
longgetSize()
StringgetString(long offset, boolean wide)
Indirect the native pointer to malloc space, a la Pointer.getString.
booleanisValid()
Returns false if the memory has been freed.
protected static longmalloc(long size)
Call the real native malloc
static voidpurge()
Force cleanup of memory that has associated NIO Buffers which have been GC'd.
voidread(long bOff, byte[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read.
voidread(long bOff, short[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read.
voidread(long bOff, char[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read.
voidread(long bOff, int[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read.
voidread(long bOff, long[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read.
voidread(long bOff, float[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read.
voidread(long bOff, double[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read.
voidsetByte(long offset, byte value)
Indirect the native pointer to malloc space, a la Pointer.setByte.
voidsetChar(long offset, char value)
Indirect the native pointer to malloc space, a la Pointer.setChar.
voidsetDouble(long offset, double value)
Indirect the native pointer to malloc space, a la Pointer.setDouble.
voidsetFloat(long offset, float value)
Indirect the native pointer to malloc space, a la Pointer.setFloat.
voidsetInt(long offset, int value)
Indirect the native pointer to malloc space, a la Pointer.setInt.
voidsetLong(long offset, long value)
Indirect the native pointer to malloc space, a la Pointer.setLong.
voidsetPointer(long offset, Pointer value)
Indirect the native pointer to malloc space, a la Pointer.setPointer.
voidsetShort(long offset, short value)
Indirect the native pointer to malloc space, a la Pointer.setShort.
voidsetString(long offset, String value, boolean wide)
Indirect the native pointer to malloc space, a la Pointer.setString.
Pointershare(long offset)
Provide a view onto this structure from the given offset.
Pointershare(long offset, long sz)
Provide a view onto this structure from the given offset.
StringtoString()
voidwrite(long bOff, byte[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write.
voidwrite(long bOff, short[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write.
voidwrite(long bOff, char[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write.
voidwrite(long bOff, int[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write.
voidwrite(long bOff, long[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write.
voidwrite(long bOff, float[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write.
voidwrite(long bOff, double[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write.

Field Detail

size

protected long size

Constructor Detail

Memory

public Memory(long size)
Allocate space in the native heap via a call to C's malloc.

Parameters: size number of bytes of space to allocate

Memory

protected Memory()

Method Detail

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.

boundsCheck

protected void boundsCheck(long off, long sz)
Check that indirection won't cause us to write outside the malloc'ed space.

clear

public void clear()
Zero the full extent of this memory region.

finalize

protected void finalize()

free

protected static void free(long ptr)
Call the real native free

getByte

public byte getByte(long offset)
Indirect the native pointer to malloc space, a la Pointer.getByte. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

getByteBuffer

public ByteBuffer getByteBuffer(long offset, long length)
Get a ByteBuffer mapped to a portion of this memory.

Parameters: offset byte offset from pointer to start the buffer length Length of ByteBuffer

Returns: a direct ByteBuffer that accesses the memory being pointed to,

getChar

public char getChar(long offset)
Indirect the native pointer to malloc space, a la Pointer.getByte. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

getDouble

public double getDouble(long offset)
Indirect the native pointer to malloc space, a la Pointer.getDouble. But this method performs a bounds check to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

getFloat

public float getFloat(long offset)
Indirect the native pointer to malloc space, a la Pointer.getFloat. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

getInt

public int getInt(long offset)
Indirect the native pointer to malloc space, a la Pointer.getInt. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

getLong

public long getLong(long offset)
Indirect the native pointer to malloc space, a la Pointer.getLong. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

getPointer

public Pointer getPointer(long offset)
Indirect the native pointer to malloc space, a la Pointer.getPointer. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

getShort

public short getShort(long offset)
Indirect the native pointer to malloc space, a la Pointer.getShort. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

getSize

public long getSize()

getString

public String getString(long offset, boolean wide)
Indirect the native pointer to malloc space, a la Pointer.getString. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

isValid

public boolean isValid()
Returns false if the memory has been freed.

malloc

protected static long malloc(long size)
Call the real native malloc

purge

public static void purge()
Force cleanup of memory that has associated NIO Buffers which have been GC'd.

read

public void read(long bOff, byte[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,byte[],int,int)

read

public void read(long bOff, short[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,short[],int,int)

read

public void read(long bOff, char[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,char[],int,int)

read

public void read(long bOff, int[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,int[],int,int)

read

public void read(long bOff, long[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,long[],int,int)

read

public void read(long bOff, float[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,float[],int,int)

read

public void read(long bOff, double[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.read. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,double[],int,int)

setByte

public void setByte(long offset, byte value)
Indirect the native pointer to malloc space, a la Pointer.setByte. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

setChar

public void setChar(long offset, char value)
Indirect the native pointer to malloc space, a la Pointer.setChar. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

setDouble

public void setDouble(long offset, double value)
Indirect the native pointer to malloc space, a la Pointer.setDouble. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

setFloat

public void setFloat(long offset, float value)
Indirect the native pointer to malloc space, a la Pointer.setFloat. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

setInt

public void setInt(long offset, int value)
Indirect the native pointer to malloc space, a la Pointer.setInt. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

setLong

public void setLong(long offset, long value)
Indirect the native pointer to malloc space, a la Pointer.setLong. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

setPointer

public void setPointer(long offset, Pointer value)
Indirect the native pointer to malloc space, a la Pointer.setPointer. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

setShort

public void setShort(long offset, short value)
Indirect the native pointer to malloc space, a la Pointer.setShort. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

setString

public void setString(long offset, String value, boolean wide)
Indirect the native pointer to malloc space, a la Pointer.setString. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: Pointer

share

public Pointer share(long offset)
Provide a view onto this structure from the given offset. The returned Pointer will have the same size as the original, reduced by the offset.

Throws: IndexOutOfBoundsException if the requested memory is outside the allocated bounds.

share

public Pointer share(long offset, long sz)
Provide a view onto this structure from the given offset.

Throws: IndexOutOfBoundsException if the requested memory is outside the allocated bounds.

toString

public String toString()

write

public void write(long bOff, byte[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,byte[],int,int)

write

public void write(long bOff, short[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,short[],int,int)

write

public void write(long bOff, char[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,char[],int,int)

write

public void write(long bOff, int[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,int[],int,int)

write

public void write(long bOff, long[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,long[],int,int)

write

public void write(long bOff, float[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,float[],int,int)

write

public void write(long bOff, double[] buf, int index, int length)
Indirect the native pointer to malloc space, a la Pointer.write. But this method performs a bounds checks to ensure that the indirection does not cause memory outside the malloced space to be accessed.

See Also: (long,double[],int,int)

Copyright © 2007-2009 Timothy Wall. All Rights Reserved.