Class Pointer
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
BooleanPointer,BoolPointer,BytePointer,CharPointer,CLongPointer,DoublePointer,FloatPointer,FunctionPointer,IntPointer,LongPointer,PointerPointer,ShortPointer,SizeTPointer
void*, which can point to any
struct, class, or union. All Pointer classes get parsed
by Generator to produce proper wrapping JNI code, but this base class also
provides functionality to access native array elements as well as utility methods
and classes to let users benefit not only from from garbage collection, but also the
try-with-resources statement, since it implements the AutoCloseable interface.
It is also possible to use a PointerScope to keep track of a group of Pointer objects,
and have them deallocated in a transparent but deterministic manner.
For examples of subclasses, please refer to the following:
- Author:
- Samuel Audet
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classAPointer.Deallocatorthat calls, during garbage collection, a method with signaturestatic void deallocate()from the Pointer object passed to the constructor and that accepts it as argument.protected static interfaceThe interface to implement to produce a Deallocator usable by Pointer.protected static classAPointer.Deallocatorthat calls, during garbage collection, a native function.protected static interface -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected longThe native address of this Pointer, which can be an array.protected longThe number of elements contained in this native array, or 0 if unknown.protected longThe index of the first element that should not be accessed, or 0 if unknown.protected longThe index of the element of a native array that should be accessed. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongaddress()Returnsaddress.asBuffer()Same asasByteBuffer(), but can be overridden to return subclasses of Buffer.static longReturns the amount of physical memory that is free according to the operating system, or 0 if unknown.static Pointercalloc(long n, long size) longcapacity()Returnscapacity.<P extends Pointer>
Pcapacity(long capacity) Sets the capacity and returns this.voidclose()CallsreleaseReference().voidCallsdeallocate(true).voiddeallocate(boolean deallocate) Explicitly manages native memory without waiting after the garbage collector.static voidClears, deallocates, and removes all garbage collected objects from thereferenceQueue.protected Pointer.DeallocatorReturnsdeallocator.protected <P extends Pointer>
Pdeallocator(Pointer.Deallocator deallocator) Sets the deallocator and returns this.booleanChecks for equality with argument.<P extends Pointer>
Pfill(int b) Calls in effectmemset(address + position, b, length), wherelength = sizeof() * (limit - position).static StringformatBytes(long bytes) Truncates and formats the number of bytes to a human readable string ending with "T", "G", "M", or "K" (as multiples of 1024).static voidprotected static longReturns the starting address of the memory region referenced by the given directBuffer.<P extends Pointer>
PReturnsgetPointer(0).<P extends Pointer>
PgetPointer(long i) ReturnsgetPointer(getClass(), i).<P extends Pointer>
PgetPointer(Class<P> type) ReturnsgetPointer(type, 0).<P extends Pointer>
PgetPointer(Class<P> type, long i) inthashCode()Returns(int)address.static voidCallsdeallocatorThread.interrupt().booleanisNull()Returnsaddress == 0.static booleanReturnsp == null || p.address == 0.longlimit()Returnslimit.<P extends Pointer>
Plimit(long limit) Sets the limit and returns this.static Pointermalloc(long size) static longmaxBytes()ReturnsmaxBytes, the maximum amount of memory allowed to be tracked.static longReturnsmaxPhysicalBytes, the maximum amount of physical memory that should be used.static Pointerstatic intstatic Pointerstatic Pointerstatic Pointerprotected <P extends Pointer>
PoffsetAddress(long i) static intReturnsLoader.offsetof(type, member).intReturnsLoader.offsetof(getClass(), member)or -1 on error.static longparseBytes(String string, long relativeMultiple) Returns the amount of bytes for a number possibly ending with "%", "t", "g", m", or "k" (as multiples of 1024).static longReturns the amount of non-shared physical memory currently used by the whole process, or 0 if unknown.static longphysicalBytesInaccurate(long maxSize) May return a value larger thanphysicalBytes()but less thanmaxSizeto save processing time.longposition()Returnsposition.<P extends Pointer>
Pposition(long position) Sets the position and returns this.<P extends Pointer>
PCalls in effectmemcpy(this.address + this.position, p.address + p.position, length), wherelength = sizeof(p) * (p.limit - p.position).static PointerintReturnsPointer.ReferenceCounter.count()or -1 if no deallocator has been set.booleanCallsPointer.ReferenceCounter.release(), decrementing the reference count by 1, in turn deallocating this Pointer when the count drops to 0.<P extends Pointer>
PCallsPointer.ReferenceCounter.retain(), incrementing the reference count by 1.voidsetNull()Setsaddressto 0.intsizeof()Returns 1 for Pointer or BytePointer elseLoader.sizeof(getClass())or -1 on error.static intReturnsLoader.sizeof(type).toString()static longReturnstotalBytes(), current amount of memory tracked by deallocators.static longReturnstotalCount(), current number of pointers tracked by deallocators.static longReturns the amount of physical memory installed according to the operating system, or 0 if unknown.protected static <P extends Pointer>
PwithDeallocator(P p) A utility method to register easily aPointer.CustomDeallocatorwith a Pointer.<P extends Pointer>
Pzero()Returnsfill(0).
-
Field Details
-
address
protected long addressThe native address of this Pointer, which can be an array. -
position
protected long positionThe index of the element of a native array that should be accessed. -
limit
protected long limitThe index of the first element that should not be accessed, or 0 if unknown. -
capacity
protected long capacityThe number of elements contained in this native array, or 0 if unknown.
-
-
Constructor Details
-
Pointer
public Pointer()Default constructor that does nothing. -
Pointer
Copies the address, position, limit, and capacity of another Pointer. Also keeps a reference to it to prevent its memory from getting deallocated.This copy constructor basically acts as a
reinterpret_cast, at least on plain old data (POD)struct, so we need to be careful with it.- Parameters:
p- the other Pointer to reference
-
Pointer
Copies the address, position, limit, and capacity of a direct NIOBuffer. Also keeps a reference to it to prevent its memory from getting deallocated.- Parameters:
b- the Buffer object to reference
-
-
Method Details
-
offsetAddress
-
withDeallocator
A utility method to register easily aPointer.CustomDeallocatorwith a Pointer.- Parameters:
p- the Pointer with which to register the deallocator- Returns:
- the Pointer
-
formatBytes
Truncates and formats the number of bytes to a human readable string ending with "T", "G", "M", or "K" (as multiples of 1024). -
parseBytes
Returns the amount of bytes for a number possibly ending with "%", "t", "g", m", or "k" (as multiples of 1024). May also be suffixed with an optional "b", where all letters are case-insensitive.- Parameters:
string- to parserelativeMultiple- to use in the case of "%", which is divided by 100- Returns:
- the amount of bytes
- Throws:
NumberFormatException
-
interruptDeallocatorThread
public static void interruptDeallocatorThread()CallsdeallocatorThread.interrupt(). -
deallocateReferences
public static void deallocateReferences()Clears, deallocates, and removes all garbage collected objects from thereferenceQueue. -
maxBytes
public static long maxBytes()ReturnsmaxBytes, the maximum amount of memory allowed to be tracked. -
totalBytes
public static long totalBytes()ReturnstotalBytes(), current amount of memory tracked by deallocators. -
totalCount
public static long totalCount()ReturnstotalCount(), current number of pointers tracked by deallocators. -
maxPhysicalBytes
public static long maxPhysicalBytes()ReturnsmaxPhysicalBytes, the maximum amount of physical memory that should be used. -
physicalBytes
Returns the amount of non-shared physical memory currently used by the whole process, or 0 if unknown. Also known as "anonymous resident set size" (Linux, Mac OS X, etc) or "private working set size" (Windows). -
physicalBytesInaccurate
May return a value larger thanphysicalBytes()but less thanmaxSizeto save processing time. -
totalPhysicalBytes
Returns the amount of physical memory installed according to the operating system, or 0 if unknown. It should not be possible forphysicalBytes()to go over this value. -
availablePhysicalBytes
Returns the amount of physical memory that is free according to the operating system, or 0 if unknown. -
getDirectBufferAddress
@Name("JavaCPP_getDirectBufferAddress") protected static long getDirectBufferAddress(@Raw(withEnv=true) Buffer b) Returns the starting address of the memory region referenced by the given directBuffer. An alternative to allocating a new Pointer object if all you need is the memory address. -
isNull
Returnsp == null || p.address == 0. -
isNull
public boolean isNull()Returnsaddress == 0. -
setNull
public void setNull()Setsaddressto 0. -
address
public long address()Returnsaddress. -
position
public long position()Returnsposition. -
position
Sets the position and returns this. That makes thearray.position(i)statement sort of equivalent to thearray[i]statement in C++.- Parameters:
position- the new position- Returns:
- this
-
limit
public long limit()Returnslimit. -
limit
Sets the limit and returns this. Used to limit the size of an operation on this object.- Parameters:
limit- the new limit- Returns:
- this
-
capacity
public long capacity()Returnscapacity. -
capacity
Sets the capacity and returns this. Should not be called more than once after allocation.- Parameters:
capacity- the new capacity- Returns:
- this
-
deallocator
Returnsdeallocator. -
deallocator
Sets the deallocator and returns this. Also clears current deallocator if notnull. That is, it deallocates previously allocated memory. Should not be called more than once after allocation.- Parameters:
deallocator- the new deallocator- Returns:
- this
-
close
public void close()CallsreleaseReference().- Specified by:
closein interfaceAutoCloseable
-
deallocate
public void deallocate()Callsdeallocate(true). -
deallocate
public void deallocate(boolean deallocate) Explicitly manages native memory without waiting after the garbage collector. Has no effect if no deallocator was previously set withdeallocator(Deallocator).- Parameters:
deallocate- if true, deallocates, else does not, but disables garbage collection
-
retainReference
CallsPointer.ReferenceCounter.retain(), incrementing the reference count by 1. Has no effect if no deallocator was previously set withdeallocator(Deallocator).- Returns:
- this
-
releaseReference
public boolean releaseReference()CallsPointer.ReferenceCounter.release(), decrementing the reference count by 1, in turn deallocating this Pointer when the count drops to 0. Has no effect if no deallocator was previously set withdeallocator(Deallocator).- Returns:
- true when the count drops to 0 and deallocation has occurred
-
referenceCount
public int referenceCount()ReturnsPointer.ReferenceCounter.count()or -1 if no deallocator has been set. -
offsetof
ReturnsLoader.offsetof(type, member). -
offsetof
ReturnsLoader.offsetof(getClass(), member)or -1 on error. -
sizeof
ReturnsLoader.sizeof(type). -
sizeof
public int sizeof()Returns 1 for Pointer or BytePointer elseLoader.sizeof(getClass())or -1 on error. -
asByteBuffer
Creates a newByteBuffercovering the memory space between thepositionandlimitof this Pointer. The way the methods were designed allows constructs such asthis.position(13).limit(42).asByteBuffer().The new buffer's position will be equal to
position, its capacity and its limit will be equal tolimit, and its mark will be undefined. In the event thatlimit <= 0, the capacity and limit will be set toposition + 1. The new buffer will be direct and mutable.NOTE: ByteBuffer objects can only support a capacity of size
Integer.MAX_VALUE. Certain combinations of position and limit can be greater than the supported capacity. For example, the following code snippet initializes a BytePointer and attempts to make a ByteBuffer from the final element:
In order to access this memory location using a ByteBuffer, you must first offset this pointer's address. See the example below:final long bigSize = Integer.MAX_VALUE + 1L; try (BytePointer pointer = new BytePointer(bigSize)) { pointer.fill(1); pointer.put(pointer.capacity() - 1L, (byte)100); ByteBuffer buffer = pointer.position(Integer.MAX_VALUE) .asByteBuffer(); buffer.position(); // 2147483647 buffer.capacity(); // 2147483647 (error: off by 1) buffer.remaining(); // 0 (error: off by 1) }final long bigSize = Integer.MAX_VALUE + 1L; try (BytePointer pointer = new BytePointer(bigSize)) { pointer.fill(1); pointer.put(pointer.capacity() - 1L, (byte)100); ByteBuffer buffer = pointer.getPointer(Integer.MAX_VALUE) .asByteBuffer(); buffer.position(); // 0 buffer.capacity(); // 1 buffer.remaining(); // 1 }- Returns:
- the direct NIO
ByteBuffercreated - See Also:
-
asBuffer
Same asasByteBuffer(), but can be overridden to return subclasses of Buffer.- Returns:
asByteBuffer()- See Also:
-
malloc
-
calloc
-
realloc
-
free
-
memchr
-
memcmp
-
memcpy
-
memmove
-
memset
-
getPointer
ReturnsgetPointer(0). -
getPointer
ReturnsgetPointer(getClass(), i). -
getPointer
ReturnsgetPointer(type, 0). -
getPointer
-
put
Calls in effectmemcpy(this.address + this.position, p.address + p.position, length), wherelength = sizeof(p) * (p.limit - p.position). If limit == 0, it uses position + 1 instead. The way the methods were designed allows constructs such asthis.position(0).put(p.position(13).limit(42)).- Parameters:
p- the Pointer from which to copy memory- Returns:
- this
-
fill
Calls in effectmemset(address + position, b, length), wherelength = sizeof() * (limit - position). If limit == 0, it uses position + 1 instead. The way the methods were designed allows constructs such asthis.position(0).limit(13).fill(42);- Parameters:
b- the byte value to fill the memory with- Returns:
- this
-
zero
Returnsfill(0). -
equals
Checks for equality with argument. Defines obj to be equal if(obj == null && this.address == 0) || (obj.address == this.address && obj.position == this.position), and the classes are the same, unless one of them in Pointer itself. -
hashCode
public int hashCode()Returns(int)address. -
toString
-