Class MemoryUtil
- java.lang.Object
-
- org.lwjgl.system.MemoryUtil
-
public final class MemoryUtil extends java.lang.Object
This class provides functionality for managing native memory.All methods in this class will make use of
Unsafe
if it's available, for performance. If Unsafe is not available, the fallback implementations make use of reflection and, in the worst-case, JNI.Method names in this class are prefixed with
mem
to avoid ambiguities when used with static imports.Text encoding/decoding
Three codecs are available, each with a different postfix:- UTF16 - Direct mapping of 2 bytes to Java char and vice versa
- UTF8 - custom UTF-8 codec without intermediate allocations
- ASCII - Not the original 7bit ASCII, but any character set with a single byte encoding (ISO 8859-1, Windows-1252, etc.)
Methods in bindings that accept/return
CharSequence
/String
also supportByteBuffer
, so custom codecs can be used if necessary.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static interface
MemoryUtil.MemoryAllocationReport
The memory allocation report callback.static interface
MemoryUtil.MemoryAllocator
The interface implemented by the memory allocator used by the explicit memory management API (MemoryUtil.memAlloc(int)
,MemoryUtil.memFree(java.nio.Buffer)
, etc).
-
Field Summary
Fields Modifier and Type Field and Description static int
CACHE_LINE_SIZE
The cache-line size, in bytes.static long
NULL
Alias for the null pointer address.static int
PAGE_SIZE
The memory page size, in bytes.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static MemoryUtil.MemoryAllocator
getAllocator()
Returns theMemoryUtil.MemoryAllocator
instance used internally by the explicit memory management API (MemoryUtil.memAlloc(int)
,MemoryUtil.memFree(java.nio.Buffer)
, etc).static long
memAddress(java.nio.ByteBuffer buffer)
Returns the memory address at the current position of the specified buffer.static long
memAddress(java.nio.ByteBuffer buffer, int position)
Returns the memory address at the specified position of the specified buffer.static long
memAddress(java.nio.CharBuffer buffer)
CharBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.static long
memAddress(java.nio.CharBuffer buffer, int position)
CharBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.static long
memAddress(CustomBuffer<?> buffer)
CustomBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.static long
memAddress(CustomBuffer<?> buffer, int position)
CustomBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.static long
memAddress(java.nio.DoubleBuffer buffer)
DoubleBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.static long
memAddress(java.nio.DoubleBuffer buffer, int position)
DoubleBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.static long
memAddress(java.nio.FloatBuffer buffer)
FloatBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.static long
memAddress(java.nio.FloatBuffer buffer, int position)
FloatBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.static long
memAddress(java.nio.IntBuffer buffer)
IntBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.static long
memAddress(java.nio.IntBuffer buffer, int position)
IntBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.static long
memAddress(java.nio.LongBuffer buffer)
LongBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.static long
memAddress(java.nio.LongBuffer buffer, int position)
LongBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.static long
memAddress(java.nio.ShortBuffer buffer)
ShortBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.static long
memAddress(java.nio.ShortBuffer buffer, int position)
ShortBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.static long
memAddress0(java.nio.Buffer buffer)
Returns the memory address of the specified buffer.static long
memAddress0(CustomBuffer<?> buffer)
CustomBuffer version ofMemoryUtil.memAddress0(Buffer)
.static long
memAddress0Safe(java.nio.Buffer buffer)
Null-safe version ofMemoryUtil.memAddress0(Buffer)
.static long
memAddress0Safe(CustomBuffer<?> buffer)
CustomBuffer version ofMemoryUtil.memAddress0Safe(Buffer)
.static long
memAddressSafe(java.nio.ByteBuffer buffer)
Null-safe version ofMemoryUtil.memAddress(ByteBuffer)
.static long
memAddressSafe(java.nio.ByteBuffer buffer, int position)
Null-safe version ofMemoryUtil.memAddress(ByteBuffer, int)
.static long
memAddressSafe(java.nio.CharBuffer buffer)
CharBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.static long
memAddressSafe(java.nio.CharBuffer buffer, int position)
CharBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.static long
memAddressSafe(CustomBuffer<?> buffer, int position)
CustomBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.static long
memAddressSafe(java.nio.DoubleBuffer buffer)
DoubleBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.static long
memAddressSafe(java.nio.DoubleBuffer buffer, int position)
DoubleBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.static long
memAddressSafe(java.nio.FloatBuffer buffer)
FloatBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.static long
memAddressSafe(java.nio.FloatBuffer buffer, int position)
FloatBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.static long
memAddressSafe(java.nio.IntBuffer buffer)
IntBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.static long
memAddressSafe(java.nio.IntBuffer buffer, int position)
IntBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.static long
memAddressSafe(java.nio.LongBuffer buffer)
LongBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.static long
memAddressSafe(java.nio.LongBuffer buffer, int position)
LongBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.static long
memAddressSafe(Pointer pointer)
Pointer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.static long
memAddressSafe(java.nio.ShortBuffer buffer)
ShortBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.static long
memAddressSafe(java.nio.ShortBuffer buffer, int position)
ShortBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.static java.nio.ByteBuffer
memAlignedAlloc(int alignment, int size)
The standard C aligned_alloc function.static void
memAlignedFree(java.nio.ByteBuffer ptr)
Frees a block of memory that was allocated withMemoryUtil.memAlignedAlloc(int, int)
.static java.nio.ByteBuffer
memAlloc(int size)
The standard C malloc function.static java.nio.DoubleBuffer
memAllocDouble(int size)
DoubleBuffer version ofMemoryUtil.memAlloc(int)
.static java.nio.FloatBuffer
memAllocFloat(int size)
FloatBuffer version ofMemoryUtil.memAlloc(int)
.static java.nio.IntBuffer
memAllocInt(int size)
IntBuffer version ofMemoryUtil.memAlloc(int)
.static java.nio.LongBuffer
memAllocLong(int size)
LongBuffer version ofMemoryUtil.memAlloc(int)
.static PointerBuffer
memAllocPointer(int size)
PointerBuffer version ofMemoryUtil.memAlloc(int)
.static java.nio.ShortBuffer
memAllocShort(int size)
ShortBuffer version ofMemoryUtil.memAlloc(int)
.static java.lang.String
memASCII(java.nio.ByteBuffer buffer)
Decodes the bytes with index[position(), position()+remaining()
) inbuffer
, as an ASCII string.static java.lang.String
memASCII(java.nio.ByteBuffer buffer, int length)
Decodes the bytes with index[position(), position()+length
) inbuffer
, as an ASCII string.static java.lang.String
memASCII(java.nio.ByteBuffer buffer, int length, int offset)
Decodes the bytes with index[offset, offset+length
) inbuffer
, as an ASCII string.static java.nio.ByteBuffer
memASCII(java.lang.CharSequence text)
Returns a ByteBuffer containing the specified text ASCII encoded and null-terminated.static java.nio.ByteBuffer
memASCII(java.lang.CharSequence text, boolean nullTerminated)
Returns a ByteBuffer containing the specified text ASCII encoded and optionally null-terminated.static int
memASCII(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target)
Encodes and optionally null-terminates the specified text using ASCII encoding.static int
memASCII(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target, int offset)
Encodes and optionally null-terminates the specified text using ASCII encoding.static java.lang.String
memASCII(long address)
Converts the null-terminated ASCII encoded string at the specified memory address to aString
.static java.nio.ByteBuffer
memByteBuffer(long address, int capacity)
Creates a new direct ByteBuffer that starts at the specified memory address and has the specified capacity.static java.nio.ByteBuffer
memByteBufferNT1(long address)
Creates a new direct ByteBuffer that starts at the specified memory address and has capacity equal to the null-terminated string starting at that address.static java.nio.ByteBuffer
memByteBufferNT1(long address, int maxLength)
Creates a new direct ByteBuffer that starts at the specified memory address and has capacity equal to the null-terminated string starting at that address, up to a maximum ofmaxLength
bytes.static java.nio.ByteBuffer
memByteBufferNT2(long address)
Creates a new direct ByteBuffer that starts at the specified memory address and has capacity equal to the null-terminated string starting at that address.static java.nio.ByteBuffer
memByteBufferNT2(long address, int maxLength)
Creates a new direct ByteBuffer that starts at the specified memory address and has capacity equal to the null-terminated string starting at that address, up to a maximum ofmaxLength
bytes.static java.nio.ByteBuffer
memCalloc(int num)
Alternative version ofMemoryUtil.memCalloc(int, int)
.static java.nio.ByteBuffer
memCalloc(int num, int size)
The standard C calloc function.static java.nio.DoubleBuffer
memCallocDouble(int num)
DoubleBuffer version ofMemoryUtil.memCalloc(int, int)
.static java.nio.FloatBuffer
memCallocFloat(int num)
FloatBuffer version ofMemoryUtil.memCalloc(int, int)
.static java.nio.IntBuffer
memCallocInt(int num)
IntBuffer version ofMemoryUtil.memCalloc(int, int)
.static java.nio.LongBuffer
memCallocLong(int num)
LongBuffer version ofMemoryUtil.memCalloc(int, int)
.static PointerBuffer
memCallocPointer(int num)
PointerBuffer version ofMemoryUtil.memCalloc(int, int)
.static java.nio.ShortBuffer
memCallocShort(int num)
ShortBuffer version ofMemoryUtil.memCalloc(int, int)
.static java.nio.CharBuffer
memCharBuffer(long address, int capacity)
Creates a new direct CharBuffer that starts at the specified memory address and has the specified capacity.static void
memCopy(long src, long dst, int bytes)
Sets all bytes in a specified block of memory to a copy of another block.static void
memDeleteGlobalRef(long globalRef)
Deletes a global reference.static void
memDeleteWeakGlobalRef(long globalRef)
Deletes a weak global reference.static java.nio.DoubleBuffer
memDoubleBuffer(long address, int capacity)
Creates a new direct DoubleBuffer that starts at the specified memory address and has the specified capacity.static java.nio.FloatBuffer
memFloatBuffer(long address, int capacity)
Creates a new direct FloatBuffer that starts at the specified memory address and has the specified capacity.static void
memFree(java.nio.Buffer ptr)
The standard C free function.static void
memFree(PointerBuffer ptr)
PointerBuffer version ofMemoryUtil.memFree(java.nio.Buffer)
.static long
memGetAddress(long ptr)
static boolean
memGetBoolean(long ptr)
static byte
memGetByte(long ptr)
static double
memGetDouble(long ptr)
static float
memGetFloat(long ptr)
static int
memGetInt(long ptr)
static long
memGetLong(long ptr)
static short
memGetShort(long ptr)
static <T> T
memGlobalRefToObject(long globalRef)
Returns the object that the specified global reference points to.static java.nio.IntBuffer
memIntBuffer(long address, int capacity)
Creates a new direct IntBuffer that starts at the specified memory address and has the specified capacity.static int
memLengthASCII(java.lang.CharSequence value, boolean nullTerminated)
Returns the number of bytes required to encode the specified text in the ASCII encoding.static int
memLengthNT1(java.nio.ByteBuffer buffer)
Calculates the length, in bytes, of the null-terminated string that starts at the current position of the specified buffer.static int
memLengthNT2(java.nio.ByteBuffer buffer)
Calculates the length, in bytes, of the null-terminated string that starts at the current position of the specified buffer.static int
memLengthUTF16(java.lang.CharSequence value, boolean nullTerminated)
Returns the number of bytes required to encode the specified text in the UTF-16 encoding.static int
memLengthUTF8(java.lang.CharSequence value, boolean nullTerminated)
Returns the number of bytes required to encode the specified text in the UTF-8 encoding.static java.nio.LongBuffer
memLongBuffer(long address, int capacity)
Creates a new direct LongBuffer that starts at the specified memory address and has the specified capacity.static long
memNewGlobalRef(java.lang.Object obj)
Creates a new global reference to the specified object.static long
memNewWeakGlobalRef(java.lang.Object obj)
Creates a new weak global reference to the specified object.static PointerBuffer
memPointerBuffer(long address, int capacity)
Creates a new PointerBuffer that starts at the specified memory address and has the specified capacity.static void
memPutAddress(long ptr, long value)
static void
memPutByte(long ptr, byte value)
static void
memPutDouble(long ptr, double value)
static void
memPutFloat(long ptr, float value)
static void
memPutInt(long ptr, int value)
static void
memPutLong(long ptr, long value)
static void
memPutShort(long ptr, short value)
static java.nio.ByteBuffer
memRealloc(java.nio.ByteBuffer ptr, int size)
The standard C realloc function.static java.nio.DoubleBuffer
memRealloc(java.nio.DoubleBuffer ptr, int size)
DoubleBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.static java.nio.FloatBuffer
memRealloc(java.nio.FloatBuffer ptr, int size)
FloatBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.static java.nio.IntBuffer
memRealloc(java.nio.IntBuffer ptr, int size)
IntBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.static java.nio.LongBuffer
memRealloc(java.nio.LongBuffer ptr, int size)
LongBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.static PointerBuffer
memRealloc(PointerBuffer ptr, int size)
PointerBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.static java.nio.ShortBuffer
memRealloc(java.nio.ShortBuffer ptr, int size)
ShortBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.static void
memReport(MemoryUtil.MemoryAllocationReport report)
Reports all live allocations.static void
memReport(MemoryUtil.MemoryAllocationReport report, MemoryUtil.MemoryAllocationReport.Aggregate groupByStackTrace, boolean groupByThread)
Reports aggregates for the live allocations.static void
memSet(long ptr, int value, int bytes)
Sets all bytes in a specified block of memory to a fixed value (usually zero).static java.nio.ByteBuffer
memSetupBuffer(java.nio.ByteBuffer buffer, long address, int capacity)
This method is an alternative toMemoryUtil.memByteBuffer(long, int)
that allows the reuse of an existing direct ByteBuffer instance.static java.nio.CharBuffer
memSetupBuffer(java.nio.CharBuffer buffer, long address, int capacity)
CharBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
static java.nio.DoubleBuffer
memSetupBuffer(java.nio.DoubleBuffer buffer, long address, int capacity)
DoubleBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
static java.nio.FloatBuffer
memSetupBuffer(java.nio.FloatBuffer buffer, long address, int capacity)
FloatBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
static java.nio.IntBuffer
memSetupBuffer(java.nio.IntBuffer buffer, long address, int capacity)
IntBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
static java.nio.LongBuffer
memSetupBuffer(java.nio.LongBuffer buffer, long address, int capacity)
LongBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
static PointerBuffer
memSetupBuffer(PointerBuffer buffer, long address, int capacity)
PointerBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
static java.nio.ShortBuffer
memSetupBuffer(java.nio.ShortBuffer buffer, long address, int capacity)
ShortBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
static java.nio.ShortBuffer
memShortBuffer(long address, int capacity)
Creates a new direct ShortBuffer that starts at the specified memory address and has the specified capacity.static java.nio.ByteBuffer
memSlice(java.nio.ByteBuffer buffer)
Slices the specified buffer.static java.nio.ByteBuffer
memSlice(java.nio.ByteBuffer buffer, int offset, int capacity)
Returns a slice of the specified buffer between(buffer.position() + offset)
and(buffer.position() + offset + capacity)
.static java.nio.DoubleBuffer
memSlice(java.nio.DoubleBuffer buffer, int offset, int capacity)
Returns a slice of the specified buffer between(buffer.position() + offset)
and(buffer.position() + offset + capacity)
.static java.nio.FloatBuffer
memSlice(java.nio.FloatBuffer buffer, int offset, int capacity)
Returns a slice of the specified buffer between(buffer.position() + offset)
and(buffer.position() + offset + capacity)
.static java.nio.IntBuffer
memSlice(java.nio.IntBuffer buffer, int offset, int capacity)
Returns a slice of the specified buffer between(buffer.position() + offset)
and(buffer.position() + offset + capacity)
.static java.nio.LongBuffer
memSlice(java.nio.LongBuffer buffer, int offset, int capacity)
Returns a slice of the specified buffer between(buffer.position() + offset)
and(buffer.position() + offset + capacity)
.static PointerBuffer
memSlice(PointerBuffer buffer, int offset, int capacity)
Returns a slice of the specified buffer between(buffer.position() + offset)
and(buffer.position() + offset + capacity)
.static java.nio.ShortBuffer
memSlice(java.nio.ShortBuffer buffer, int offset, int capacity)
Returns a slice of the specified buffer between(buffer.position() + offset)
and(buffer.position() + offset + capacity)
.static java.lang.String
memUTF16(java.nio.ByteBuffer buffer)
Decodes the bytes with index[position(), position()+remaining()
) inbuffer
, as a UTF-16 string.static java.lang.String
memUTF16(java.nio.ByteBuffer buffer, int length)
Decodes the bytes with index[position(), position()+(length*2)
) inbuffer
, as a UTF-16 string.static java.lang.String
memUTF16(java.nio.ByteBuffer buffer, int length, int offset)
Decodes the bytes with index[offset, offset+(length*2)
) inbuffer
, as a UTF-16 string.static java.nio.ByteBuffer
memUTF16(java.lang.CharSequence text)
Returns a ByteBuffer containing the specified text UTF-16 encoded and null-terminated.static java.nio.ByteBuffer
memUTF16(java.lang.CharSequence text, boolean nullTerminated)
Returns a ByteBuffer containing the specified text UTF-16 encoded and optionally null-terminated.static int
memUTF16(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target)
Encodes and optionally null-terminates the specified text using UTF-16 encoding.static int
memUTF16(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target, int offset)
Encodes and optionally null-terminates the specified text using UTF-16 encoding.static java.lang.String
memUTF16(long address)
Converts the null-terminated UTF-16 encoded string at the specified memory address to aString
.static java.lang.String
memUTF8(java.nio.ByteBuffer buffer)
Decodes the bytes with index[position(), position()+remaining()
) inbuffer
, as a UTF-8 string.static java.lang.String
memUTF8(java.nio.ByteBuffer buffer, int length)
Decodes the bytes with index[position(), position()+length
) inbuffer
, as a UTF-8 string.static java.lang.String
memUTF8(java.nio.ByteBuffer buffer, int length, int offset)
Decodes the bytes with index[offset, offset+length
) inbuffer
, as a UTF-8 string.static java.nio.ByteBuffer
memUTF8(java.lang.CharSequence text)
Returns a ByteBuffer containing the specified text UTF-8 encoded and null-terminated.static java.nio.ByteBuffer
memUTF8(java.lang.CharSequence text, boolean nullTerminated)
Returns a ByteBuffer containing the specified text UTF-8 encoded and optionally null-terminated.static int
memUTF8(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target)
Encodes and optionally null-terminates the specified text using UTF-8 encoding.static int
memUTF8(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target, int offset)
Encodes and optionally null-terminates the specified text using UTF-8 encoding.static java.lang.String
memUTF8(long address)
Converts the null-terminated UTF-8 encoded string at the specified memory address to aString
.
-
-
-
Field Detail
-
NULL
Alias for the null pointer address.
-
PAGE_SIZE
The memory page size, in bytes. This value is always a power-of-two.
-
CACHE_LINE_SIZE
The cache-line size, in bytes. This value is always a power-of-two.
-
-
Method Detail
-
getAllocator
public static MemoryUtil.MemoryAllocator getAllocator()
Returns theMemoryUtil.MemoryAllocator
instance used internally by the explicit memory management API (MemoryUtil.memAlloc(int)
,MemoryUtil.memFree(java.nio.Buffer)
, etc).Allocations made through the returned instance will not be tracked for memory leaks, even if
Configuration.DEBUG_MEMORY_ALLOCATOR
is enabled. This can be useful forstatic final
allocations that live throughout the application's lifetime and will never be freed until the process is terminated. Normally such allocations would be reported as memory leaks by the debug allocator.The expectation is that this method will rarely be used, so it does not have the
mem
prefix to avoid pollution of auto-complete lists.- Returns:
- the
MemoryUtil.MemoryAllocator
instance
-
memAlloc
public static java.nio.ByteBuffer memAlloc(int size)
The standard C malloc function.Allocates a block of
size
bytes of memory, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.Memory allocated with this method must be freed with
MemoryUtil.memFree(java.nio.Buffer)
.- Parameters:
size
- the size of the memory block to allocate, in bytes. Ifsize
is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced.- Returns:
- on success, a pointer to the memory block allocated by the function. If the function failed to allocate the requested block of memory, a
MemoryUtil.NULL
pointer is returned.
-
memAllocShort
public static java.nio.ShortBuffer memAllocShort(int size)
ShortBuffer version ofMemoryUtil.memAlloc(int)
.- Parameters:
size
- the number of short values to allocate.
-
memAllocInt
public static java.nio.IntBuffer memAllocInt(int size)
IntBuffer version ofMemoryUtil.memAlloc(int)
.- Parameters:
size
- the number of int values to allocate.
-
memAllocFloat
public static java.nio.FloatBuffer memAllocFloat(int size)
FloatBuffer version ofMemoryUtil.memAlloc(int)
.- Parameters:
size
- the number of float values to allocate.
-
memAllocLong
public static java.nio.LongBuffer memAllocLong(int size)
LongBuffer version ofMemoryUtil.memAlloc(int)
.- Parameters:
size
- the number of long values to allocate.
-
memAllocDouble
public static java.nio.DoubleBuffer memAllocDouble(int size)
DoubleBuffer version ofMemoryUtil.memAlloc(int)
.- Parameters:
size
- the number of double values to allocate.
-
memAllocPointer
public static PointerBuffer memAllocPointer(int size)
PointerBuffer version ofMemoryUtil.memAlloc(int)
.- Parameters:
size
- the number of pointer values to allocate.
-
memFree
public static void memFree(java.nio.Buffer ptr)
The standard C free function.A block of memory previously allocated by a call to
MemoryUtil.memAlloc(int)
,MemoryUtil.memCalloc(int, int)
orMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
is deallocated, making it available again for further allocations.- Parameters:
ptr
- pointer to a memory block previously allocated withMemoryUtil.memAlloc(int)
,MemoryUtil.memCalloc(int, int)
orMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
. Ifptr
does not point to a block of memory allocated with the above functions, it causes undefined behavior. Ifptr
is aMemoryUtil.NULL
pointer, the function does nothing.
-
memFree
public static void memFree(PointerBuffer ptr)
PointerBuffer version ofMemoryUtil.memFree(java.nio.Buffer)
.
-
memCalloc
public static java.nio.ByteBuffer memCalloc(int num, int size)
The standard C calloc function.Allocates a block of memory for an array of
num
elements, each of themsize
bytes long, and initializes all its bits to zero. The effective result is the allocation of a zero-initialized memory block of(num*size)
bytes.Memory allocated with this method must be freed with
MemoryUtil.memFree(java.nio.Buffer)
.- Parameters:
num
- the number of elements to allocate.size
- the size of each element. Ifsize
is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced.- Returns:
- on success, a pointer to the memory block allocated by the function. If the function failed to allocate the requested block of memory, a
MemoryUtil.NULL
pointer is returned.
-
memCalloc
public static java.nio.ByteBuffer memCalloc(int num)
Alternative version ofMemoryUtil.memCalloc(int, int)
.- Parameters:
num
- the number of bytes to allocate.
-
memCallocShort
public static java.nio.ShortBuffer memCallocShort(int num)
ShortBuffer version ofMemoryUtil.memCalloc(int, int)
.- Parameters:
num
- the number of short values to allocate.
-
memCallocInt
public static java.nio.IntBuffer memCallocInt(int num)
IntBuffer version ofMemoryUtil.memCalloc(int, int)
.- Parameters:
num
- the number of int values to allocate.
-
memCallocFloat
public static java.nio.FloatBuffer memCallocFloat(int num)
FloatBuffer version ofMemoryUtil.memCalloc(int, int)
.- Parameters:
num
- the number of float values to allocate.
-
memCallocLong
public static java.nio.LongBuffer memCallocLong(int num)
LongBuffer version ofMemoryUtil.memCalloc(int, int)
.- Parameters:
num
- the number of long values to allocate.
-
memCallocDouble
public static java.nio.DoubleBuffer memCallocDouble(int num)
DoubleBuffer version ofMemoryUtil.memCalloc(int, int)
.- Parameters:
num
- the number of double values to allocate.
-
memCallocPointer
public static PointerBuffer memCallocPointer(int num)
PointerBuffer version ofMemoryUtil.memCalloc(int, int)
.- Parameters:
num
- the number of pointer values to allocate.
-
memRealloc
public static java.nio.ByteBuffer memRealloc(java.nio.ByteBuffer ptr, int size)
The standard C realloc function.Changes the size of the memory block pointed to by
ptr
. The function may move the memory block to a new location (whose address is returned by the function). The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved to a new location. If the new size is larger, the value of the newly allocated portion is indeterminate.In case that
ptr
is aMemoryUtil.NULL
pointer, the function behaves likeMemoryUtil.memAlloc(int)
, assigning a new block of size bytes and returning a pointer to its beginning.The memory address used is always the address at the start of
ptr
, so the current position ofptr
does not need to be set to 0 for this function to work. The current position is preserved, even if the memory block is moved to a new location, unlesssize
is less than the current position in which case position will be equal to capacity. The limit is set to the capacity, and the mark is discarded.- Parameters:
ptr
- a pointer to a memory block previously allocated withMemoryUtil.memAlloc(int)
,MemoryUtil.memCalloc(int, int)
orMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
. Alternatively, this can be aMemoryUtil.NULL
pointer, in which case a new block is allocated (as ifMemoryUtil.memAlloc(int)
was called).size
- the new size for the memory block, in bytes.- Returns:
- a pointer to the reallocated memory block, which may be either the same as
ptr
or a new location. If the function fails to allocate the requested block of memory, aMemoryUtil.NULL
pointer is returned, and the memory block pointed to by argumentptr
is not deallocated (it is still valid, and with its contents unchanged).
-
memRealloc
public static java.nio.ShortBuffer memRealloc(java.nio.ShortBuffer ptr, int size)
ShortBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.- Parameters:
size
- the number of short values to allocate.
-
memRealloc
public static java.nio.IntBuffer memRealloc(java.nio.IntBuffer ptr, int size)
IntBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.- Parameters:
size
- the number of int values to allocate.
-
memRealloc
public static java.nio.FloatBuffer memRealloc(java.nio.FloatBuffer ptr, int size)
FloatBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.- Parameters:
size
- the number of float values to allocate.
-
memRealloc
public static java.nio.LongBuffer memRealloc(java.nio.LongBuffer ptr, int size)
LongBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.- Parameters:
size
- the number of long values to allocate.
-
memRealloc
public static java.nio.DoubleBuffer memRealloc(java.nio.DoubleBuffer ptr, int size)
DoubleBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.- Parameters:
size
- the number of double values to allocate.
-
memRealloc
public static PointerBuffer memRealloc(PointerBuffer ptr, int size)
PointerBuffer version ofMemoryUtil.memRealloc(java.nio.ByteBuffer, int)
.- Parameters:
size
- the number of pointer values to allocate.
-
memAlignedAlloc
public static java.nio.ByteBuffer memAlignedAlloc(int alignment, int size)
The standard C aligned_alloc function.Allocate
size
bytes of uninitialized storage whose alignment is specified byalignment
. The size parameter must be an integral multiple of alignment. Memory allocated with memAlignedAlloc() must be freed withMemoryUtil.memAlignedFree(java.nio.ByteBuffer)
.- Parameters:
alignment
- the alignment. Must be a power of two value and a multiple ofsizeof(void *)
.size
- the number of bytes to allocate. Must be a multiple ofalignment
.
-
memAlignedFree
public static void memAlignedFree(java.nio.ByteBuffer ptr)
Frees a block of memory that was allocated withMemoryUtil.memAlignedAlloc(int, int)
. If ptr isNULL
, no operation is performed.- Parameters:
ptr
- the aligned block of memory to free
-
memReport
public static void memReport(MemoryUtil.MemoryAllocationReport report)
Reports all live allocations.This method can only be used if the
Configuration.DEBUG_MEMORY_ALLOCATOR
option has been set to true.- Parameters:
report
- the report callback
-
memReport
public static void memReport(MemoryUtil.MemoryAllocationReport report, MemoryUtil.MemoryAllocationReport.Aggregate groupByStackTrace, boolean groupByThread)
Reports aggregates for the live allocations.This method can only be used if the
Configuration.DEBUG_MEMORY_ALLOCATOR
option has been set to true.- Parameters:
report
- the report callbackgroupByStackTrace
- how to aggregate the reported allocationsgroupByThread
- if the reported allocations should be grouped by thread
-
memAddress0
public static long memAddress0(java.nio.Buffer buffer)
Returns the memory address of the specified buffer. [INTERNAL USE ONLY]- Parameters:
buffer
- the buffer- Returns:
- the memory address
-
memAddress0Safe
public static long memAddress0Safe(java.nio.Buffer buffer)
Null-safe version ofMemoryUtil.memAddress0(Buffer)
. ReturnsMemoryUtil.NULL
if the specified buffer is null.
-
memAddress0
public static long memAddress0(CustomBuffer<?> buffer)
CustomBuffer version ofMemoryUtil.memAddress0(Buffer)
.
-
memAddress0Safe
public static long memAddress0Safe(CustomBuffer<?> buffer)
CustomBuffer version ofMemoryUtil.memAddress0Safe(Buffer)
.
-
memAddress
public static long memAddress(java.nio.ByteBuffer buffer)
Returns the memory address at the current position of the specified buffer. This is effectively a pointer value that can be used in native function calls.- Parameters:
buffer
- the buffer- Returns:
- the memory address
-
memAddress
public static long memAddress(java.nio.ByteBuffer buffer, int position)
Returns the memory address at the specified position of the specified buffer.- Parameters:
buffer
- the bufferposition
- the buffer position- Returns:
- the memory address
- See Also:
MemoryUtil.memAddress(ByteBuffer)
-
memAddress
public static long memAddress(java.nio.ShortBuffer buffer)
ShortBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.
-
memAddress
public static long memAddress(java.nio.ShortBuffer buffer, int position)
ShortBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.
-
memAddress
public static long memAddress(java.nio.CharBuffer buffer)
CharBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.
-
memAddress
public static long memAddress(java.nio.CharBuffer buffer, int position)
CharBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.
-
memAddress
public static long memAddress(java.nio.IntBuffer buffer)
IntBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.
-
memAddress
public static long memAddress(java.nio.IntBuffer buffer, int position)
IntBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.
-
memAddress
public static long memAddress(java.nio.FloatBuffer buffer)
FloatBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.
-
memAddress
public static long memAddress(java.nio.FloatBuffer buffer, int position)
FloatBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.
-
memAddress
public static long memAddress(java.nio.LongBuffer buffer)
LongBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.
-
memAddress
public static long memAddress(java.nio.LongBuffer buffer, int position)
LongBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.
-
memAddress
public static long memAddress(java.nio.DoubleBuffer buffer)
DoubleBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.
-
memAddress
public static long memAddress(java.nio.DoubleBuffer buffer, int position)
DoubleBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.
-
memAddress
public static long memAddress(CustomBuffer<?> buffer)
CustomBuffer version ofMemoryUtil.memAddress(ByteBuffer)
.
-
memAddress
public static long memAddress(CustomBuffer<?> buffer, int position)
CustomBuffer version ofMemoryUtil.memAddress(ByteBuffer, int)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.ByteBuffer buffer)
Null-safe version ofMemoryUtil.memAddress(ByteBuffer)
. ReturnsMemoryUtil.NULL
if the specified buffer is null.
-
memAddressSafe
public static long memAddressSafe(java.nio.ByteBuffer buffer, int position)
Null-safe version ofMemoryUtil.memAddress(ByteBuffer, int)
. ReturnsMemoryUtil.NULL
if the specified buffer is null.
-
memAddressSafe
public static long memAddressSafe(java.nio.ShortBuffer buffer)
ShortBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.ShortBuffer buffer, int position)
ShortBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.CharBuffer buffer)
CharBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.CharBuffer buffer, int position)
CharBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.IntBuffer buffer)
IntBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.IntBuffer buffer, int position)
IntBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.FloatBuffer buffer)
FloatBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.FloatBuffer buffer, int position)
FloatBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.LongBuffer buffer)
LongBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.LongBuffer buffer, int position)
LongBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.DoubleBuffer buffer)
DoubleBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.
-
memAddressSafe
public static long memAddressSafe(java.nio.DoubleBuffer buffer, int position)
DoubleBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.
-
memAddressSafe
public static long memAddressSafe(CustomBuffer<?> buffer, int position)
CustomBuffer version ofMemoryUtil.memAddressSafe(ByteBuffer, int)
.
-
memAddressSafe
public static long memAddressSafe(Pointer pointer)
Pointer version ofMemoryUtil.memAddressSafe(ByteBuffer)
.
-
memByteBuffer
public static java.nio.ByteBuffer memByteBuffer(long address, int capacity)
Creates a new direct ByteBuffer that starts at the specified memory address and has the specified capacity. The returned ByteBuffer instance will be set to the native ByteOrder.- Parameters:
address
- the starting memory addresscapacity
- the buffer capacity- Returns:
- the new ByteBuffer
-
memShortBuffer
public static java.nio.ShortBuffer memShortBuffer(long address, int capacity)
Creates a new direct ShortBuffer that starts at the specified memory address and has the specified capacity.The
address
specified must be aligned to 2 bytes. If not, usememByteBuffer(address, capacity * 2).asShortBuffer()
.- Parameters:
address
- the starting memory addresscapacity
- the buffer capacity- Returns:
- the new ShortBuffer
-
memCharBuffer
public static java.nio.CharBuffer memCharBuffer(long address, int capacity)
Creates a new direct CharBuffer that starts at the specified memory address and has the specified capacity.The
address
specified must be aligned to 2 bytes. If not, usememByteBuffer(address, capacity * 2).asCharBuffer()
.- Parameters:
address
- the starting memory addresscapacity
- the buffer capacity- Returns:
- the new CharBuffer
-
memIntBuffer
public static java.nio.IntBuffer memIntBuffer(long address, int capacity)
Creates a new direct IntBuffer that starts at the specified memory address and has the specified capacity.The
address
specified must be aligned to 4 bytes. If not, usememByteBuffer(address, capacity * 4).asIntBuffer()
.- Parameters:
address
- the starting memory addresscapacity
- the buffer capacity- Returns:
- the new IntBuffer
-
memLongBuffer
public static java.nio.LongBuffer memLongBuffer(long address, int capacity)
Creates a new direct LongBuffer that starts at the specified memory address and has the specified capacity.The
address
specified must be aligned to 8 bytes. If not, usememByteBuffer(address, capacity * 8).asLongBuffer()
.- Parameters:
address
- the starting memory addresscapacity
- the buffer capacity- Returns:
- the new LongBuffer
-
memFloatBuffer
public static java.nio.FloatBuffer memFloatBuffer(long address, int capacity)
Creates a new direct FloatBuffer that starts at the specified memory address and has the specified capacity.The
address
specified must be aligned to 4 bytes. If not, usememByteBuffer(address, capacity * 4).asFloatBuffer()
.- Parameters:
address
- the starting memory addresscapacity
- the buffer capacity- Returns:
- the new FloatBuffer
-
memDoubleBuffer
public static java.nio.DoubleBuffer memDoubleBuffer(long address, int capacity)
Creates a new direct DoubleBuffer that starts at the specified memory address and has the specified capacity.The
address
specified must be aligned to 8 bytes. If not, usememByteBuffer(address, capacity * 8).asDoubleBuffer()
.- Parameters:
address
- the starting memory addresscapacity
- the buffer capacity- Returns:
- the new DoubleBuffer
-
memPointerBuffer
public static PointerBuffer memPointerBuffer(long address, int capacity)
Creates a new PointerBuffer that starts at the specified memory address and has the specified capacity.The
address
specified must be aligned to the pointer size. If not, usePointerBuffer.create(memByteBuffer(address, capacity * POINTER_SIZE))
.- Parameters:
address
- the starting memory addresscapacity
- the buffer capacity- Returns:
- the new PointerBuffer
-
memSetupBuffer
public static java.nio.ByteBuffer memSetupBuffer(java.nio.ByteBuffer buffer, long address, int capacity)
This method is an alternative toMemoryUtil.memByteBuffer(long, int)
that allows the reuse of an existing direct ByteBuffer instance. It modifies that instance so that it starts at the specified memory address and has the specified capacity. The instance passed to this method should not be an instance created usingByteBuffer.allocateDirect(int)
. Using such an instance will cause an exception to be thrown. Other instances are allowed and their parent reference will be cleared before this method returns.ByteBuffer instance modification might not be possible. In that case this method behaves exactly like
MemoryUtil.memByteBuffer(long, int)
, so the returned instance should always replace the input one.- Parameters:
buffer
- the ByteBuffer to modifyaddress
- the starting memory addresscapacity
- the buffer capacity- Returns:
- the modified ByteBuffer
-
memSetupBuffer
public static java.nio.ShortBuffer memSetupBuffer(java.nio.ShortBuffer buffer, long address, int capacity)
ShortBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
-
memSetupBuffer
public static java.nio.CharBuffer memSetupBuffer(java.nio.CharBuffer buffer, long address, int capacity)
CharBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
-
memSetupBuffer
public static java.nio.IntBuffer memSetupBuffer(java.nio.IntBuffer buffer, long address, int capacity)
IntBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
-
memSetupBuffer
public static java.nio.LongBuffer memSetupBuffer(java.nio.LongBuffer buffer, long address, int capacity)
LongBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
-
memSetupBuffer
public static java.nio.FloatBuffer memSetupBuffer(java.nio.FloatBuffer buffer, long address, int capacity)
FloatBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
-
memSetupBuffer
public static java.nio.DoubleBuffer memSetupBuffer(java.nio.DoubleBuffer buffer, long address, int capacity)
DoubleBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
-
memSetupBuffer
public static PointerBuffer memSetupBuffer(PointerBuffer buffer, long address, int capacity)
PointerBuffer version of:MemoryUtil.memSetupBuffer(java.nio.ByteBuffer, long, int)
-
memSlice
public static java.nio.ByteBuffer memSlice(java.nio.ByteBuffer buffer)
Slices the specified buffer. The returned buffer will have the sameByteOrder
as the source buffer.- Parameters:
buffer
- the buffer to slice- Returns:
- the sliced buffer
- See Also:
ByteBuffer.slice()
-
memSlice
public static java.nio.ByteBuffer memSlice(java.nio.ByteBuffer buffer, int offset, int capacity)
Returns a slice of the specified buffer between(buffer.position() + offset)
and(buffer.position() + offset + capacity)
. The returned buffer will have the sameByteOrder
as the original buffer.The position and limit of the original buffer are preserved after a call to this method.
- Parameters:
buffer
- the buffer to sliceoffset
- the slice offset, it must be ≤buffer.remaining()
capacity
- the slice length, it must be ≤buffer.capacity() - (buffer.position() + offset)
- Returns:
- the sliced buffer
-
memSlice
public static java.nio.ShortBuffer memSlice(java.nio.ShortBuffer buffer, int offset, int capacity) public static java.nio.IntBuffer memSlice(java.nio.IntBuffer buffer, int offset, int capacity) public static java.nio.LongBuffer memSlice(java.nio.LongBuffer buffer, int offset, int capacity) public static java.nio.FloatBuffer memSlice(java.nio.FloatBuffer buffer, int offset, int capacity) public static java.nio.DoubleBuffer memSlice(java.nio.DoubleBuffer buffer, int offset, int capacity) public static PointerBuffer memSlice(PointerBuffer buffer, int offset, int capacity)
Returns a slice of the specified buffer between(buffer.position() + offset)
and(buffer.position() + offset + capacity)
.The position and limit of the original buffer are preserved after a call to this method.
- Parameters:
buffer
- the buffer to sliceoffset
- the slice offset, it must be ≤buffer.remaining()
capacity
- the slice length, it must be ≤buffer.capacity() - (buffer.position() + offset)
- Returns:
- the sliced buffer
-
memSet
public static void memSet(long ptr, int value, int bytes)
Sets all bytes in a specified block of memory to a fixed value (usually zero).- Parameters:
ptr
- the starting memory addressvalue
- the value to set (memSet will convert it to unsigned byte)bytes
- the number of bytes to set
-
memCopy
public static void memCopy(long src, long dst, int bytes)
Sets all bytes in a specified block of memory to a copy of another block.- Parameters:
src
- the source memory addressdst
- the destination memory addressbytes
- the number of bytes to copy
-
memGetBoolean
public static boolean memGetBoolean(long ptr)
-
memGetByte
public static byte memGetByte(long ptr)
-
memGetShort
public static short memGetShort(long ptr)
-
memGetInt
public static int memGetInt(long ptr)
-
memGetLong
public static long memGetLong(long ptr)
-
memGetFloat
public static float memGetFloat(long ptr)
-
memGetDouble
public static double memGetDouble(long ptr)
-
memGetAddress
public static long memGetAddress(long ptr)
-
memPutByte
public static void memPutByte(long ptr, byte value)
-
memPutShort
public static void memPutShort(long ptr, short value)
-
memPutInt
public static void memPutInt(long ptr, int value)
-
memPutLong
public static void memPutLong(long ptr, long value)
-
memPutFloat
public static void memPutFloat(long ptr, float value)
-
memPutDouble
public static void memPutDouble(long ptr, double value)
-
memPutAddress
public static void memPutAddress(long ptr, long value)
-
memGlobalRefToObject
public static <T> T memGlobalRefToObject(long globalRef)
Returns the object that the specified global reference points to.- Type Parameters:
T
- the object type- Parameters:
globalRef
- the global reference- Returns:
- the object pointed to by
globalRef
-
memNewGlobalRef
public static long memNewGlobalRef(java.lang.Object obj)
Creates a new global reference to the specified object. This method is a simple wrapper over the JNINewGlobalRef
function.- Parameters:
obj
- the object- Returns:
- the global reference memory address
-
memDeleteGlobalRef
public static void memDeleteGlobalRef(long globalRef)
Deletes a global reference. This method is a simple wrapper over the JNIDeleteGlobalRef
function.- Parameters:
globalRef
- the memory address of the global reference to delete
-
memNewWeakGlobalRef
public static long memNewWeakGlobalRef(java.lang.Object obj)
Creates a new weak global reference to the specified object. This method is a simple wrapper over the JNINewWeakGlobalRef
function.- Parameters:
obj
- the object- Returns:
- the weak global reference memory address
-
memDeleteWeakGlobalRef
public static void memDeleteWeakGlobalRef(long globalRef)
Deletes a weak global reference. This method is a simple wrapper over the JNIDeleteWeakGlobalRef
function.- Parameters:
globalRef
- the memory address of the weak global reference to delete
-
memASCII
public static java.nio.ByteBuffer memASCII(java.lang.CharSequence text)
Returns a ByteBuffer containing the specified text ASCII encoded and null-terminated. If text is null, null is returned.- Parameters:
text
- the text to encode- Returns:
- the encoded text or null
-
memASCII
public static java.nio.ByteBuffer memASCII(java.lang.CharSequence text, boolean nullTerminated)
Returns a ByteBuffer containing the specified text ASCII encoded and optionally null-terminated. If text is null, null is returned.- Parameters:
text
- the text to encodenullTerminated
- if true, the text will be terminated with a '\0'.- Returns:
- the encoded text or null
-
memASCII
public static int memASCII(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target)
Encodes and optionally null-terminates the specified text using ASCII encoding. The encoded text is stored in the specifiedByteBuffer
, at the current buffer position. The current buffer position is not modified by this operation. Thetarget
buffer is assumed to have enough remaining space to store the encoded text.- Parameters:
text
- the text to encodenullTerminated
- if true, the text will be terminated with a '\0'.- Returns:
- the number of bytes of the encoded string
-
memASCII
public static int memASCII(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target, int offset)
Encodes and optionally null-terminates the specified text using ASCII encoding. The encoded text is stored in the specifiedByteBuffer
at the specifiedposition
offset. The current buffer position is not modified by this operation. Thetarget
buffer is assumed to have enough remaining space to store the encoded text.- Parameters:
text
- the text to encodenullTerminated
- if true, the text will be terminated with a '\0'.offset
- the buffer position to which the string will be encoded- Returns:
- the number of bytes of the encoded string
-
memLengthASCII
public static int memLengthASCII(java.lang.CharSequence value, boolean nullTerminated)
Returns the number of bytes required to encode the specified text in the ASCII encoding.- Parameters:
value
- the text to encodenullTerminated
- if true, add the number of bytes required for null-termination- Returns:
- the number of bytes
-
memUTF8
public static java.nio.ByteBuffer memUTF8(java.lang.CharSequence text)
Returns a ByteBuffer containing the specified text UTF-8 encoded and null-terminated. If text is null, null is returned.- Parameters:
text
- the text to encode- Returns:
- the encoded text or null
-
memUTF8
public static java.nio.ByteBuffer memUTF8(java.lang.CharSequence text, boolean nullTerminated)
Returns a ByteBuffer containing the specified text UTF-8 encoded and optionally null-terminated. If text is null, null is returned.- Parameters:
text
- the text to encodenullTerminated
- if true, the text will be terminated with a '\0'.- Returns:
- the encoded text or null
-
memUTF8
public static int memUTF8(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target)
Encodes and optionally null-terminates the specified text using UTF-8 encoding. The encoded text is stored in the specifiedByteBuffer
, at the current buffer position. The current buffer position is not modified by this operation. Thetarget
buffer is assumed to have enough remaining space to store the encoded text. The specified text is assumed to be a valid UTF-16 string.- Parameters:
text
- the text to encodenullTerminated
- if true, the text will be terminated with a '\0'.target
- the buffer in which to store the encoded text- Returns:
- the number of bytes of the encoded string
-
memUTF8
public static int memUTF8(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target, int offset)
Encodes and optionally null-terminates the specified text using UTF-8 encoding. The encoded text is stored in the specifiedByteBuffer
, at the specifiedposition
offset. The current buffer position is not modified by this operation. Thetarget
buffer is assumed to have enough remaining space to store the encoded text. The specified text is assumed to be a valid UTF-16 string.- Parameters:
text
- the text to encodenullTerminated
- if true, the text will be terminated with a '\0'.target
- the buffer in which to store the encoded textoffset
- the buffer position to which the string will be encoded- Returns:
- the number of bytes of the encoded string
-
memLengthUTF8
public static int memLengthUTF8(java.lang.CharSequence value, boolean nullTerminated)
Returns the number of bytes required to encode the specified text in the UTF-8 encoding.- Parameters:
value
- the text to encodenullTerminated
- if true, add the number of bytes required for null-termination- Returns:
- the number of bytes
-
memUTF16
public static java.nio.ByteBuffer memUTF16(java.lang.CharSequence text)
Returns a ByteBuffer containing the specified text UTF-16 encoded and null-terminated. If text is null, null is returned.- Parameters:
text
- the text to encode- Returns:
- the encoded text
-
memUTF16
public static java.nio.ByteBuffer memUTF16(java.lang.CharSequence text, boolean nullTerminated)
Returns a ByteBuffer containing the specified text UTF-16 encoded and optionally null-terminated. If text is null, null is returned.- Parameters:
text
- the text to encodenullTerminated
- if true, the text will be terminated with a '\0'.- Returns:
- the encoded text
-
memUTF16
public static int memUTF16(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target)
Encodes and optionally null-terminates the specified text using UTF-16 encoding. The encoded text is stored in the specifiedByteBuffer
, at the current buffer position. The current buffer position is not modified by this operation. Thetarget
buffer is assumed to have enough remaining space to store the encoded text.- Parameters:
text
- the text to encodenullTerminated
- if true, the text will be terminated with a '\0'.target
- the buffer in which to store the encoded text- Returns:
- the number of bytes of the encoded string
-
memUTF16
public static int memUTF16(java.lang.CharSequence text, boolean nullTerminated, java.nio.ByteBuffer target, int offset)
Encodes and optionally null-terminates the specified text using UTF-16 encoding. The encoded text is stored in the specifiedByteBuffer
at the specifiedposition
offset. The current buffer position is not modified by this operation. Thetarget
buffer is assumed to have enough remaining space to store the encoded text.- Parameters:
text
- the text to encodenullTerminated
- if true, the text will be terminated with a '\0'.target
- the buffer in which to store the encoded textoffset
- the buffer position to which the string will be encoded- Returns:
- the number of bytes of the encoded string
-
memLengthUTF16
public static int memLengthUTF16(java.lang.CharSequence value, boolean nullTerminated)
Returns the number of bytes required to encode the specified text in the UTF-16 encoding.- Parameters:
value
- the text to encodenullTerminated
- if true, add the number of bytes required for null-termination- Returns:
- the number of bytes
-
memLengthNT1
public static int memLengthNT1(java.nio.ByteBuffer buffer)
Calculates the length, in bytes, of the null-terminated string that starts at the current position of the specified buffer. A single \0 character will terminate the string. The returned length will NOT include the \0 byte.This method is useful for reading ASCII and UTF8 encoded text.
- Parameters:
buffer
- the buffer containing the null-terminated string- Returns:
- the string length, in bytes
-
memLengthNT2
public static int memLengthNT2(java.nio.ByteBuffer buffer)
Calculates the length, in bytes, of the null-terminated string that starts at the current position of the specified buffer. Two \0 characters will terminate the string. The returned buffer will NOT include the \0 bytes.This method is useful for reading UTF16 encoded text.
- Parameters:
buffer
- the buffer containing the null-terminated string- Returns:
- the string length, in bytes
-
memByteBufferNT1
public static java.nio.ByteBuffer memByteBufferNT1(long address)
Creates a new direct ByteBuffer that starts at the specified memory address and has capacity equal to the null-terminated string starting at that address. A single \0 character will terminate the string. The returned buffer will NOT include the \0 byte.This method is useful for reading ASCII and UTF8 encoded text.
- Parameters:
address
- the starting memory address- Returns:
- the new ByteBuffer
-
memByteBufferNT1
public static java.nio.ByteBuffer memByteBufferNT1(long address, int maxLength)
Creates a new direct ByteBuffer that starts at the specified memory address and has capacity equal to the null-terminated string starting at that address, up to a maximum ofmaxLength
bytes. A single \0 character will terminate the string. The returned buffer will NOT include the \0 byte.This method is useful for reading ASCII and UTF8 encoded text.
- Parameters:
address
- the starting memory addressmaxLength
- the maximum string length, in bytes- Returns:
- the new ByteBuffer
-
memByteBufferNT2
public static java.nio.ByteBuffer memByteBufferNT2(long address)
Creates a new direct ByteBuffer that starts at the specified memory address and has capacity equal to the null-terminated string starting at that address. Two \0 characters will terminate the string. The returned buffer will NOT include the \0 bytes.This method is useful for reading UTF16 encoded text.
- Parameters:
address
- the starting memory address- Returns:
- the new ByteBuffer
-
memByteBufferNT2
public static java.nio.ByteBuffer memByteBufferNT2(long address, int maxLength)
Creates a new direct ByteBuffer that starts at the specified memory address and has capacity equal to the null-terminated string starting at that address, up to a maximum ofmaxLength
bytes. Two \0 characters will terminate the string. The returned buffer will NOT include the \0 bytes.This method is useful for reading UTF16 encoded text.
- Parameters:
address
- the starting memory address- Returns:
- the new ByteBuffer
-
memASCII
public static java.lang.String memASCII(long address)
Converts the null-terminated ASCII encoded string at the specified memory address to aString
.- Parameters:
address
- the string memory address- Returns:
- the decoded
String
or null if the specifiedaddress
is null
-
memASCII
public static java.lang.String memASCII(java.nio.ByteBuffer buffer)
Decodes the bytes with index[position(), position()+remaining()
) inbuffer
, as an ASCII string.The current
position
andlimit
of the specifiedbuffer
are not affected by this operation.- Parameters:
buffer
- theByteBuffer
to decode, or null- Returns:
- the decoded
String
or null if the specifiedbuffer
is null
-
memASCII
public static java.lang.String memASCII(java.nio.ByteBuffer buffer, int length)
Decodes the bytes with index[position(), position()+length
) inbuffer
, as an ASCII string.The current
position
andlimit
of the specifiedbuffer
are not affected by this operation.- Parameters:
buffer
- theByteBuffer
to decodelength
- the number of bytes to decode- Returns:
- the decoded
String
-
memASCII
public static java.lang.String memASCII(java.nio.ByteBuffer buffer, int length, int offset)
Decodes the bytes with index[offset, offset+length
) inbuffer
, as an ASCII string.The current
position
andlimit
of the specifiedbuffer
are not affected by this operation.- Parameters:
buffer
- theByteBuffer
to decodelength
- the number of bytes to decodeoffset
- the offset at which to start decoding.- Returns:
- the decoded
String
-
memUTF8
public static java.lang.String memUTF8(long address)
Converts the null-terminated UTF-8 encoded string at the specified memory address to aString
.- Parameters:
address
- the string memory address- Returns:
- the decoded
String
or null if the specifiedaddress
is null
-
memUTF8
public static java.lang.String memUTF8(java.nio.ByteBuffer buffer)
Decodes the bytes with index[position(), position()+remaining()
) inbuffer
, as a UTF-8 string.The current
position
andlimit
of the specifiedbuffer
are not affected by this operation.- Parameters:
buffer
- theByteBuffer
to decode, or null- Returns:
- the decoded
String
or null if the specifiedbuffer
is null
-
memUTF8
public static java.lang.String memUTF8(java.nio.ByteBuffer buffer, int length)
Decodes the bytes with index[position(), position()+length
) inbuffer
, as a UTF-8 string.The current
position
andlimit
of the specifiedbuffer
are not affected by this operation.- Parameters:
buffer
- theByteBuffer
to decodelength
- the number of bytes to decode- Returns:
- the decoded
String
-
memUTF8
public static java.lang.String memUTF8(java.nio.ByteBuffer buffer, int length, int offset)
Decodes the bytes with index[offset, offset+length
) inbuffer
, as a UTF-8 string.The current
position
andlimit
of the specifiedbuffer
are not affected by this operation.- Parameters:
buffer
- theByteBuffer
to decodelength
- the number of bytes to decodeoffset
- the offset at which to start decoding.- Returns:
- the decoded
String
-
memUTF16
public static java.lang.String memUTF16(long address)
Converts the null-terminated UTF-16 encoded string at the specified memory address to aString
.- Parameters:
address
- the string memory address- Returns:
- the decoded
String
or null if the specifiedaddress
is null
-
memUTF16
public static java.lang.String memUTF16(java.nio.ByteBuffer buffer)
Decodes the bytes with index[position(), position()+remaining()
) inbuffer
, as a UTF-16 string.The current
position
andlimit
of the specifiedbuffer
are not affected by this operation.- Parameters:
buffer
- theByteBuffer
to decode, or null- Returns:
- the decoded
String
or null if the specified is null
-
memUTF16
public static java.lang.String memUTF16(java.nio.ByteBuffer buffer, int length)
Decodes the bytes with index[position(), position()+(length*2)
) inbuffer
, as a UTF-16 string.The current
position
andlimit
of the specifiedbuffer
are not affected by this operation.>- Parameters:
buffer
- theByteBuffer
to decodelength
- the number of characters to decode- Returns:
- the decoded
String
-
memUTF16
public static java.lang.String memUTF16(java.nio.ByteBuffer buffer, int length, int offset)
Decodes the bytes with index[offset, offset+(length*2)
) inbuffer
, as a UTF-16 string.The current
position
andlimit
of the specifiedbuffer
are not affected by this operation.- Parameters:
buffer
- theByteBuffer
to decodelength
- the number of characters to decodeoffset
- the offset at which to start decoding, in bytes.- Returns:
- the decoded
String
-
-