Top Description Inners Fields Constructors Methods
com.sun.jna

public Class Memory

extends Pointer
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
com.sun.jna.Memory.SharedMemory, com.sun.jna.NativeString.StringMemory, com.sun.jna.StringArray, com.sun.jna.Structure.AutoAllocated, com.sun.jna.Function.NativeMappedArray, com.sun.jna.Function.PointerArray
Imports
java.io.Closeable, java.lang.ref.Reference, .WeakReference, java.nio.ByteBuffer, java.util.ArrayList, .Collection, .Map, java.util.concurrent.ConcurrentHashMap, com.sun.jna.internal.Cleaner

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);
Authors
Sheng Liang, originator, Todd Fast, suitability modifications, Timothy Wall
See Also
Pointer

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private class
Memory.SharedMemory

Provide a view into the original memory.

Field Summary

Modifier and TypeField and Description
private static final Map<Long, Reference<Memory>>
allocatedMemory

Keep track of all allocated memory so we can dispose of it before unloading.

private static final WeakMemoryHolder
private final Cleaner.Cleanable
protected long
Inherited from com.sun.jna.Pointer:
NULLpeer

Constructor Summary

AccessConstructor and Description
public
Memory(long
number of bytes of space to allocate
size
)

Allocate space in the native heap via a call to C's malloc.

protected

Method Summary

Modifier and TypeMethod and Description
public Memory
align(int
Align memory to this number of bytes; should be a power of two.
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.

public void
clear()

Zero the full extent of this memory region.

public void
close()

Implements java.io.Closeable.close.

Free the native memory and set peer to zero
protected void
dispose()

Deprecated
public static void
disposeAll()

Dispose of all allocated memory.

public String
dump()

Dumps the contents of this memory object.

protected static void
free(long p)

public byte
getByte(long
offset from pointer to perform the indirection
offset
)

Overrides com.sun.jna.Pointer.getByte.

Indirect the native pointer to malloc space, a la Pointer.getByte.
public ByteBuffer

Returns:

a direct ByteBuffer that accesses the memory being pointed to,
getByteBuffer
(long
byte offset from pointer to start the buffer
offset
,
long
Length of ByteBuffer
length
)

Overrides com.sun.jna.Pointer.getByteBuffer.

Get a ByteBuffer mapped to a portion of this memory.
public char
getChar(long
offset from pointer to perform the indirection
offset
)

Overrides com.sun.jna.Pointer.getChar.

Indirect the native pointer to malloc space, a la Pointer.getByte.
public double
getDouble(long
byte offset from pointer to perform the indirection
offset
)

Overrides com.sun.jna.Pointer.getDouble.

Indirect the native pointer to malloc space, a la Pointer.getDouble.
public float
getFloat(long
byte offset from pointer to perform the indirection
offset
)

Overrides com.sun.jna.Pointer.getFloat.

Indirect the native pointer to malloc space, a la Pointer.getFloat.
public int
getInt(long
byte offset from pointer to perform the indirection
offset
)

Overrides com.sun.jna.Pointer.getInt.

Indirect the native pointer to malloc space, a la Pointer.getInt.
public long
getLong(long
byte offset from pointer to perform the indirection
offset
)

Overrides com.sun.jna.Pointer.getLong.

Indirect the native pointer to malloc space, a la Pointer.getLong.
public Pointer
getPointer(long
byte offset from pointer to perform the indirection
offset
)

Overrides com.sun.jna.Pointer.getPointer.

Indirect the native pointer to malloc space, a la Pointer.getPointer.
public short
getShort(long
byte offset from pointer to perform the indirection
offset
)

Overrides com.sun.jna.Pointer.getShort.

Indirect the native pointer to malloc space, a la Pointer.getShort.
public String
getString(long
byte offset from pointer to obtain the native string
offset
,
String
the desired encoding
encoding
)

Overrides com.sun.jna.Pointer.getString.

Copy native memory to a Java String using the requested encoding.
public String
getWideString(long offset)

Overrides com.sun.jna.Pointer.getWideString.

Read a wide (const wchar_t *) string from memory.
protected static long
malloc(long size)

public static void
purge()

Force cleanup of memory that has associated NIO Buffers which have been GC'd.

public void
read(long
byte offset from pointer from which data is copied
bOff
,
byte[]
byte array into which data is copied
buf
,
int
array index to which data is copied
index
,
int
number of elements from native pointer that must be copied
length
)

Overrides com.sun.jna.Pointer.read.

Indirect the native pointer to malloc space, a la Pointer.read.
public void
read(long
byte offset from pointer from which data is copied
bOff
,
short[]
short array into which data is copied
buf
,
int
array index to which data is copied
index
,
int
number of elements from native pointer that must be copied
length
)

Overrides com.sun.jna.Pointer.read.

Indirect the native pointer to malloc space, a la Pointer.read.
public void
read(long
byte offset from pointer from which data is copied
bOff
,
char[]
char array into which data is copied
buf
,
int
array index to which data is copied
index
,
int
number of elements from native pointer that must be copied
length
)

Overrides com.sun.jna.Pointer.read.

Indirect the native pointer to malloc space, a la Pointer.read.
public void
read(long
byte offset from pointer from which data is copied
bOff
,
int[]
int array into which data is copied
buf
,
int
array index to which data is copied
index
,
int
number of elements from native pointer that must be copied
length
)

Overrides com.sun.jna.Pointer.read.

Indirect the native pointer to malloc space, a la Pointer.read.
public void
read(long
byte offset from pointer from which data is copied
bOff
,
long[]
long array into which data is copied
buf
,
int
array index to which data is copied
index
,
int
number of elements from native pointer that must be copied
length
)

Overrides com.sun.jna.Pointer.read.

Indirect the native pointer to malloc space, a la Pointer.read.
public void
read(long
byte offset from pointer from which data is copied
bOff
,
float[]
float array into which data is copied
buf
,
int
array index to which data is copied
index
,
int
number of elements from native pointer that must be copied
length
)

Overrides com.sun.jna.Pointer.read.

Indirect the native pointer to malloc space, a la Pointer.read.
public void
read(long
byte offset from pointer from which data is copied
bOff
,
double[]
double array into which data is copied
buf
,
int
array index to which data is copied
index
,
int
number of elements from native pointer that must be copied
length
)

Overrides com.sun.jna.Pointer.read.

Indirect the native pointer to malloc space, a la Pointer.read.
public void
read(long
byte offset from pointer from which data is copied
bOff
,
Pointer[]
Pointer array into which data is copied
buf
,
int
array index to which data is copied
index
,
int
number of elements from native pointer that must be copied
length
)

Overrides com.sun.jna.Pointer.read.

Indirect the native pointer to malloc space, a la Pointer.read.
public void
setByte(long
byte offset from pointer at which value must be set
offset
,
byte
byte value to set
value
)

Overrides com.sun.jna.Pointer.setByte.

Indirect the native pointer to malloc space, a la Pointer.setByte.
public void
setChar(long
byte offset from pointer at which value must be set
offset
,
char
char value to set
value
)

Overrides com.sun.jna.Pointer.setChar.

Indirect the native pointer to malloc space, a la Pointer.setChar.
public void
setDouble(long
byte offset from pointer at which value must be set
offset
,
double
double value to set
value
)

Overrides com.sun.jna.Pointer.setDouble.

Indirect the native pointer to malloc space, a la Pointer.setDouble.
public void
setFloat(long
byte offset from pointer at which value must be set
offset
,
float
float value to set
value
)

Overrides com.sun.jna.Pointer.setFloat.

Indirect the native pointer to malloc space, a la Pointer.setFloat.
public void
setInt(long
byte offset from pointer at which value must be set
offset
,
int
int value to set
value
)

Overrides com.sun.jna.Pointer.setInt.

Indirect the native pointer to malloc space, a la Pointer.setInt.
public void
setLong(long
byte offset from pointer at which value must be set
offset
,
long
long value to set
value
)

Overrides com.sun.jna.Pointer.setLong.

Indirect the native pointer to malloc space, a la Pointer.setLong.
public void
setPointer(long
byte offset from pointer at which value must be set
offset
,
Pointer
Pointer holding the actual pointer value to set, which may be null to indicate a NULL pointer.
value
)

Overrides com.sun.jna.Pointer.setPointer.

Indirect the native pointer to malloc space, a la Pointer.setPointer.
public void
setShort(long
byte offset from pointer at which value must be set
offset
,
short
short value to set
value
)

Overrides com.sun.jna.Pointer.setShort.

Indirect the native pointer to malloc space, a la Pointer.setShort.
public void
setString(long
byte offset from pointer at which characters in value must be set
offset
,
String
java.lang.String value to set
value
,
String
desired encoding
encoding
)

Overrides com.sun.jna.Pointer.setString.

Copy string value to the location being pointed to, using the requested encoding.
public void
setWideString(long
byte offset from pointer at which characters in value must be set
offset
,
String
java.lang.String value to set
value
)

Overrides com.sun.jna.Pointer.setWideString.

Copy string value to the location being pointed to as a wide string (wchar_t*).
public Pointer
share(long offset)

Overrides com.sun.jna.Pointer.share.

Provide a view of this memory using the given offset as the base address.
public Pointer
share(long offset, long sz)

Overrides com.sun.jna.Pointer.share.

Provide a view of this memory using the given offset as the base address, bounds-limited with the given size.
private Pointer

Returns:

target if target does not point into the region covered by this memory object, a newly SharedMemory object, if the pointer points to memory backed by this Memory object.
shareReferenceIfInBounds
(Pointer
Pointer to check
target
)

Check whether the supplied Pointer object points into the memory region backed by this memory object.

public long
size()

public String
toString()

Overrides com.sun.jna.Pointer.toString.

Returns a string representation of the object.
public boolean
valid()

Returns false if the memory has been freed.

public void
write(long
byte offset from pointer into which data is copied
bOff
,
byte[]
byte array from which to copy
buf
,
int
array index from which to start copying
index
,
int
number of elements from buf that must be copied
length
)

Overrides com.sun.jna.Pointer.write.

Indirect the native pointer to malloc space, a la Pointer.write.
public void
write(long
byte offset from pointer into which data is copied
bOff
,
short[]
short array from which to copy
buf
,
int
array index from which to start copying
index
,
int
number of elements from buf that must be copied
length
)

Overrides com.sun.jna.Pointer.write.

Indirect the native pointer to malloc space, a la Pointer.write.
public void
write(long
byte offset from pointer into which data is copied
bOff
,
char[]
char array from which to copy
buf
,
int
array index from which to start copying
index
,
int
number of elements from buf that must be copied
length
)

Overrides com.sun.jna.Pointer.write.

Indirect the native pointer to malloc space, a la Pointer.write.
public void
write(long
byte offset from pointer into which data is copied
bOff
,
int[]
int array from which to copy
buf
,
int
array index from which to start copying
index
,
int
number of elements from buf that must be copied
length
)

Overrides com.sun.jna.Pointer.write.

Indirect the native pointer to malloc space, a la Pointer.write.
public void
write(long
byte offset from pointer into which data is copied
bOff
,
long[]
long array from which to copy
buf
,
int
array index from which to start copying
index
,
int
number of elements from buf that must be copied
length
)

Overrides com.sun.jna.Pointer.write.

Indirect the native pointer to malloc space, a la Pointer.write.
public void
write(long
byte offset from pointer into which data is copied
bOff
,
float[]
float array from which to copy
buf
,
int
array index from which to start copying
index
,
int
number of elements from buf that must be copied
length
)

Overrides com.sun.jna.Pointer.write.

Indirect the native pointer to malloc space, a la Pointer.write.
public void
write(long
byte offset from pointer into which data is copied
bOff
,
double[]
double array from which to copy
buf
,
int
array index from which to start copying
index
,
int
number of elements from buf that must be copied
length
)

Overrides com.sun.jna.Pointer.write.

Indirect the native pointer to malloc space, a la Pointer.write.
public void
write(long
byte offset from pointer into which data is copied
bOff
,
Pointer[]
Pointer array from which to copy
buf
,
int
array index from which to start copying
index
,
int
number of elements from buf that must be copied
length
)

Overrides com.sun.jna.Pointer.write.

Indirect the native pointer to malloc space, a la Pointer.write.
Inherited from com.sun.jna.Pointer:
clearcreateConstantcreateConstantdumpequalsgetByteArraygetCharArraygetDoubleArraygetFloatArraygetIntArraygetLongArraygetNativeLonggetPointerArraygetPointerArraygetShortArraygetStringgetStringArraygetStringArraygetStringArraygetStringArraygetValuegetWideStringArraygetWideStringArrayhashCodeindexOfnativeValuenativeValuesetMemorysetNativeLongsetStringsetStringsetValue