Top Description Inners Fields Constructors Methods
org.lwjgl.system

public final Class MemoryUtil

extends Object
Class Inheritance
Imports
org.lwjgl.*, org.lwjgl.system.MemoryManage.*, org.lwjgl.system.MemoryUtil.MemoryAllocationReport.*, javax.annotation.*, java.lang.reflect.*, java.nio.*, java.nio.charset.*, java.util.*, java.util.function.*

This class provides functionality for managing native memory.

All methods in this class will make use of sun.misc.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:

The codec implementations do no codepoint validation, for improved performance. Therefore, if malformed input or unmappable characters are expected, the JDK CharsetEncoder/CharsetDecoder classes should be used instead. Methods in bindings that accept/return CharSequence/String also support ByteBuffer, so custom codecs can be used if necessary.

See Also
Configuration#MEMORY_ALLOCATOR, Configuration#DEBUG_MEMORY_ALLOCATOR

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
public static interface
MemoryUtil.MemoryAllocationReport

The memory allocation report callback.

public static interface
MemoryUtil.MemoryAllocator

The interface implemented by the memory allocator used by the explicit memory management API (memAlloc, memFree, etc).

Field Summary

Modifier and TypeField and Description
private static final long
pack-priv static final ThreadLocal<byte[]>
pack-priv static final ThreadLocal<char[]>
pack-priv static final int
pack-priv static final Class<? extends ByteBuffer>
pack-priv static final Class<? extends CharBuffer>
pack-priv static final Class<? extends DoubleBuffer>
pack-priv static final Class<? extends FloatBuffer>
pack-priv static final Class<? extends IntBuffer>
pack-priv static final Class<? extends LongBuffer>
pack-priv static final Class<? extends ShortBuffer>
public static final int
CACHE_LINE_SIZE

The cache-line size, in bytes.

private static final long
private static final int
private static final long
private static final long
private static final int
private static final int
private static final long
pack-priv static final ByteOrder
public static final long
NULL

Alias for the null pointer address.

public static final int
PAGE_SIZE

The memory page size, in bytes.

private static final long
private static final long
private static final long
private static final long
private static final long
private static final long
private static final long
private static final long
pack-priv static final Unsafe
private static final Charset

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static long
address(int position, int elementShift, long address)

pack-priv static <T extends Buffer> T
duplicate(Class<? extends T> clazz, T source, long attachmentOffset)

pack-priv static int
encodeASCIIUnsafe(CharSequence text, boolean nullTerminated, long target)

pack-priv static int
encodeUTF16Unsafe(CharSequence text, boolean nullTerminated, long target)

pack-priv static int
encodeUTF8Safe(CharSequence text, boolean nullTerminated, long target, int maxLength)

pack-priv static int
encodeUTF8Unsafe(CharSequence text, boolean nullTerminated, long target)

private static long
private static long
getAllocationSize(int elements, int elementShift)

public static MemoryUtil.MemoryAllocator

Returns:

the MemoryAllocator instance
getAllocator
()

Returns the MemoryAllocator instance used internally by the explicit memory management API (memAlloc, memFree, etc).

public static MemoryUtil.MemoryAllocator

Returns:

the MemoryAllocator instance
getAllocator
(boolean
whether allocations will be tracked for memory leaks, if Configuration#DEBUG_MEMORY_ALLOCATOR is enabled.
tracked
)

Returns the MemoryAllocator instance used internally by the explicit memory management API (memAlloc, memFree, etc).

private static long
private static long
getFieldOffset(Class<?> containerType, Class<?> fieldType, LongPredicate predicate)

private static long
getFieldOffsetInt(Object container, int value)

private static long
private static long
private static long
private static long
private static Unsafe
public static long

Returns:

the memory address
memAddress
(ByteBuffer
the buffer
buffer
)

Returns the memory address at the current position of the specified buffer.

public static long

Returns:

the memory address
memAddress
(ByteBuffer
the buffer
buffer
,
int
the buffer position
position
)

Returns the memory address at the specified position of the specified buffer.

public static long
memAddress(ShortBuffer buffer)

ShortBuffer version of memAddress(ByteBuffer).

public static long
memAddress(ShortBuffer buffer, int position)

ShortBuffer version of memAddress(ByteBuffer, int).

public static long
memAddress(CharBuffer buffer)

CharBuffer version of memAddress(ByteBuffer).

public static long
memAddress(CharBuffer buffer, int position)

CharBuffer version of memAddress(ByteBuffer, int).

public static long
memAddress(IntBuffer buffer)

IntBuffer version of memAddress(ByteBuffer).

public static long
memAddress(IntBuffer buffer, int position)

IntBuffer version of memAddress(ByteBuffer, int).

public static long
memAddress(FloatBuffer buffer)

FloatBuffer version of memAddress(ByteBuffer).

public static long
memAddress(FloatBuffer buffer, int position)

FloatBuffer version of memAddress(ByteBuffer, int).

public static long
memAddress(LongBuffer buffer)

LongBuffer version of memAddress(ByteBuffer).

public static long
memAddress(LongBuffer buffer, int position)

LongBuffer version of memAddress(ByteBuffer, int).

public static long
memAddress(DoubleBuffer buffer)

DoubleBuffer version of memAddress(ByteBuffer).

public static long
memAddress(DoubleBuffer buffer, int position)

DoubleBuffer version of memAddress(ByteBuffer, int).

public static long
memAddress(Buffer buffer)

Polymorphic version of memAddress(ByteBuffer).

public static long
memAddress(CustomBuffer<?> buffer)

CustomBuffer version of memAddress(ByteBuffer).

public static long
memAddress(CustomBuffer<?> buffer, int position)

CustomBuffer version of memAddress(ByteBuffer, int).

public static long

Returns:

the memory address
memAddress0
(Buffer
the buffer
buffer
)

Returns the memory address of the specified buffer.

public static long
memAddressSafe(ByteBuffer buffer)

Null-safe version of memAddress(ByteBuffer).

public static long
memAddressSafe(ShortBuffer buffer)

ShortBuffer version of memAddressSafe(ByteBuffer).

public static long
public static long
public static long
memAddressSafe(FloatBuffer buffer)

FloatBuffer version of memAddressSafe(ByteBuffer).

public static long
public static long
memAddressSafe(DoubleBuffer buffer)

DoubleBuffer version of memAddressSafe(ByteBuffer).

public static long
public static ByteBuffer
memAlignedAlloc(int
the alignment. Must be a power of two value and a multiple of sizeof(void *).
alignment
,
int
the number of bytes to allocate. Must be a multiple of alignment.
size
)

The standard C aligned_alloc function.

public static void
memAlignedFree(ByteBuffer
the aligned block of memory to free
ptr
)

Frees a block of memory that was allocated with memAlignedAlloc.

public static ByteBuffer

Returns:

on success, a pointer to the memory block allocated by the function
memAlloc
(int
the size of the memory block to allocate, in bytes. If size is zero, the returned pointer shall not be dereferenced.
size
)

The standard C malloc function.

public static CLongBuffer
memAllocCLong(int
the number of C long values to allocate.
size
)

CLongBuffer version of memAlloc.

public static DoubleBuffer
memAllocDouble(int
the number of double values to allocate.
size
)

DoubleBuffer version of memAlloc.

public static FloatBuffer
memAllocFloat(int
the number of float values to allocate.
size
)

FloatBuffer version of memAlloc.

public static IntBuffer
memAllocInt(int
the number of int values to allocate.
size
)

IntBuffer version of memAlloc.

public static LongBuffer
memAllocLong(int
the number of long values to allocate.
size
)

LongBuffer version of memAlloc.

public static PointerBuffer
memAllocPointer(int
the number of pointer values to allocate.
size
)

PointerBuffer version of memAlloc.

public static ShortBuffer
memAllocShort(int
the number of short values to allocate.
size
)

ShortBuffer version of memAlloc.

public static ByteBuffer

Returns:

the encoded text. The returned buffer must be deallocated manually with memFree.
memASCII
(CharSequence
the text to encode
text
)

Returns a ByteBuffer containing the specified text ASCII encoded and null-terminated.

public static ByteBuffer

Returns:

the encoded text. The returned buffer must be deallocated manually with memFree.
memASCII
(CharSequence
the text to encode
text
,
boolean
if true, the text will be terminated with a '\0'.
nullTerminated
)

Returns a ByteBuffer containing the specified text ASCII encoded and optionally null-terminated.

public static int

Returns:

the number of bytes of the encoded string
memASCII
(CharSequence
the text to encode
text
,
boolean
if true, the text will be terminated with a '\0'
nullTerminated
,
ByteBuffer
the buffer where the encoded text should be stored
target
)

Encodes and optionally null-terminates the specified text using ASCII encoding.

public static int

Returns:

the number of bytes of the encoded string
memASCII
(CharSequence
the text to encode
text
,
boolean
if true, the text will be terminated with a '\0'.
nullTerminated
,
ByteBuffer target, int
the buffer position to which the string will be encoded
offset
)

Encodes and optionally null-terminates the specified text using ASCII encoding.

public static String

Returns:

the decoded String
memASCII
(long
the string memory address
address
)

Converts the null-terminated ASCII encoded string at the specified memory address to a String.

public static String

Returns:

the decoded String
memASCII
(long
the string memory address
address
,
int
the number of bytes to decode
length
)

Converts the ASCII encoded string at the specified memory address to a String.

public static String

Returns:

the decoded String
memASCII
(ByteBuffer
the ByteBuffer to decode
buffer
)

Decodes the bytes with index [position(), position()+remaining()) in buffer, as an ASCII string.

public static String

Returns:

the decoded String
memASCII
(ByteBuffer
the ByteBuffer to decode
buffer
,
int
the number of bytes to decode
length
)

Decodes the bytes with index [position(), position()+length) in buffer, as an ASCII string.

public static String

Returns:

the decoded String
memASCII
(ByteBuffer
the ByteBuffer to decode
buffer
,
int
the number of bytes to decode
length
,
int
the offset at which to start decoding.
offset
)

Decodes the bytes with index [offset, offset+length) in buffer, as an ASCII string.

public static ByteBuffer
memASCIISafe(CharSequence text)

Like memASCII, but returns null if text is null.

public static ByteBuffer
memASCIISafe(CharSequence text, boolean nullTerminated)

Like memASCII, but returns null if text is null.

public static String
memASCIISafe(long address)

Like memASCII, but returns null if address is NULL.

public static String
memASCIISafe(long address, int length)

Like memASCII, but returns null if address is NULL.

public static String
memASCIISafe(ByteBuffer buffer)

Like memASCII, but returns null if buffer is null.

public static ByteBuffer

Returns:

the new ByteBuffer
memByteBuffer
(long
the starting memory address
address
,
int
the buffer capacity
capacity
)

Creates a new direct ByteBuffer that starts at the specified memory address and has the specified capacity.

public static ByteBuffer

Returns:

the ByteBuffer view
memByteBuffer
(ShortBuffer
the source buffer
buffer
)

Creates a ByteBuffer instance as a view of the specified ShortBuffer between its current position and limit.

public static ByteBuffer

Returns:

the ByteBuffer view
memByteBuffer
(CharBuffer
the source buffer
buffer
)

Creates a ByteBuffer instance as a view of the specified CharBuffer between its current position and limit.

public static ByteBuffer

Returns:

the ByteBuffer view
memByteBuffer
(IntBuffer
the source buffer
buffer
)

Creates a ByteBuffer instance as a view of the specified IntBuffer between its current position and limit.

public static ByteBuffer

Returns:

the ByteBuffer view
memByteBuffer
(LongBuffer
the source buffer
buffer
)

Creates a ByteBuffer instance as a view of the specified LongBuffer between its current position and limit.

public static ByteBuffer

Returns:

the ByteBuffer view
memByteBuffer
(FloatBuffer
the source buffer
buffer
)

Creates a ByteBuffer instance as a view of the specified FloatBuffer between its current position and limit.

public static ByteBuffer

Returns:

the ByteBuffer view
memByteBuffer
(DoubleBuffer
the source buffer
buffer
)

Creates a ByteBuffer instance as a view of the specified DoubleBuffer between its current position and limit.

public static ByteBuffer

Returns:

the ByteBuffer view
memByteBuffer
(CustomBuffer<?>
the source buffer
buffer
)

Creates a ByteBuffer instance as a view of the specified CustomBuffer between its current position and limit.

public static <T extends Struct<T>> ByteBuffer

Returns:

the ByteBuffer view
memByteBuffer
(T
the struct value
value
)

Creates a ByteBuffer instance as a view of the specified Struct.

public static ByteBuffer

Returns:

the new ByteBuffer
memByteBufferNT1
(long
the starting memory address
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.

public static ByteBuffer

Returns:

the new ByteBuffer
memByteBufferNT1
(long
the starting memory address
address
,
int
the maximum string length, in bytes
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 of maxLength bytes.

public static ByteBuffer
memByteBufferNT1Safe(long address)

Like memByteBufferNT1, but returns null if address is NULL.

public static ByteBuffer
memByteBufferNT1Safe(long address, int maxLength)

Like memByteBufferNT1, but returns null if address is NULL.

public static ByteBuffer

Returns:

the new ByteBuffer
memByteBufferNT2
(long
the starting memory address
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.

public static ByteBuffer

Returns:

the new ByteBuffer
memByteBufferNT2
(long
the starting memory address
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 of maxLength bytes.

public static ByteBuffer
memByteBufferNT2Safe(long address)

Like memByteBufferNT2, but returns null if address is NULL.

public static ByteBuffer
memByteBufferNT2Safe(long address, int maxLength)

Like memByteBufferNT2, but returns null if address is NULL.

public static ByteBuffer
memByteBufferSafe(long address, int capacity)

Like memByteBuffer, but returns null if address is NULL.

public static ByteBuffer

Returns:

on success, a pointer to the memory block allocated by the function
memCalloc
(int
the number of elements to allocate.
num
,
int
the size of each element. If size 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.
size
)

The standard C calloc function.

public static ByteBuffer
memCalloc(int
the number of bytes to allocate.
num
)

Alternative version of memCalloc.

public static CLongBuffer
memCallocCLong(int
the number of C long values to allocate.
num
)

CLongBuffer version of memCalloc.

public static DoubleBuffer
memCallocDouble(int
the number of double values to allocate.
num
)

DoubleBuffer version of memCalloc.

public static FloatBuffer
memCallocFloat(int
the number of float values to allocate.
num
)

FloatBuffer version of memCalloc.

public static IntBuffer
memCallocInt(int
the number of int values to allocate.
num
)

IntBuffer version of memCalloc.

public static LongBuffer
memCallocLong(int
the number of long values to allocate.
num
)

LongBuffer version of memCalloc.

public static PointerBuffer
memCallocPointer(int
the number of pointer values to allocate.
num
)

PointerBuffer version of memCalloc.

public static ShortBuffer
memCallocShort(int
the number of short values to allocate.
num
)

ShortBuffer version of memCalloc.

public static CharBuffer

Returns:

the new CharBuffer
memCharBuffer
(long
the starting memory address
address
,
int
the buffer capacity
capacity
)

Creates a new direct CharBuffer that starts at the specified memory address and has the specified capacity.

public static CharBuffer
memCharBufferSafe(long address, int capacity)

Like memCharBuffer, but returns null if address is NULL.

public static CLongBuffer

Returns:

the new CLongBuffer
memCLongBuffer
(long
the starting memory address
address
,
int
the buffer capacity
capacity
)

Creates a new direct CLongBuffer that starts at the specified memory address and has the specified capacity.

public static CLongBuffer
memCLongBufferSafe(long address, int capacity)

Like memCLongBuffer, but returns null if address is NULL.

public static void
memCopy(ByteBuffer
the source memory address
src
,
ByteBuffer
the destination memory address
dst
)

Sets all bytes in a specified block of memory to a copy of another block.

public static void
memCopy(ShortBuffer
the source memory address
src
,
ShortBuffer
the destination memory address
dst
)

Sets all bytes in a specified block of memory to a copy of another block.

public static void
memCopy(CharBuffer
the source memory address
src
,
CharBuffer
the destination memory address
dst
)

Sets all bytes in a specified block of memory to a copy of another block.

public static void
memCopy(IntBuffer
the source memory address
src
,
IntBuffer
the destination memory address
dst
)

Sets all bytes in a specified block of memory to a copy of another block.

public static void
memCopy(LongBuffer
the source memory address
src
,
LongBuffer
the destination memory address
dst
)

Sets all bytes in a specified block of memory to a copy of another block.

public static void
memCopy(FloatBuffer
the source memory address
src
,
FloatBuffer
the destination memory address
dst
)

Sets all bytes in a specified block of memory to a copy of another block.

public static void
memCopy(DoubleBuffer
the source memory address
src
,
DoubleBuffer
the destination memory address
dst
)

Sets all bytes in a specified block of memory to a copy of another block.

public static <
the buffer type
T extends CustomBuffer<T>
>
void
memCopy(T
the source memory address
src
,
T
the destination memory address
dst
)

Sets all bytes in a specified block of memory to a copy of another block.

public static <
the struct type
T extends Struct<T>
>
void
memCopy(T
the source struct
src
,
T
the destination struct
dst
)

Sets all bytes of a struct to a copy of another struct.

public static void
memCopy(long
the source memory address
src
,
long
the destination memory address
dst
,
long
the number of bytes to copy
bytes
)

Sets all bytes in a specified block of memory to a copy of another block.

pack-priv static void
memCopyAligned32(int src, int dst, int bytes)

pack-priv static void
memCopyAligned64(long src, long dst, int bytes)

public static DoubleBuffer

Returns:

the new DoubleBuffer
memDoubleBuffer
(long
the starting memory address
address
,
int
the buffer capacity
capacity
)

Creates a new direct DoubleBuffer that starts at the specified memory address and has the specified capacity.

public static DoubleBuffer
memDoubleBufferSafe(long address, int capacity)

Like memDoubleBuffer, but returns null if address is NULL.

public static ByteBuffer

Returns:

the duplicated buffer
memDuplicate
(ByteBuffer
the buffer to duplicate
buffer
)

Duplicates the specified buffer.

public static ShortBuffer

Returns:

the duplicated buffer
memDuplicate
(ShortBuffer
the buffer to duplicate
buffer
)

Duplicates the specified buffer.

public static CharBuffer

Returns:

the duplicated buffer
memDuplicate
(CharBuffer
the buffer to duplicate
buffer
)

Duplicates the specified buffer.

public static IntBuffer

Returns:

the duplicated buffer
memDuplicate
(IntBuffer
the buffer to duplicate
buffer
)

Duplicates the specified buffer.

public static LongBuffer

Returns:

the duplicated buffer
memDuplicate
(LongBuffer
the buffer to duplicate
buffer
)

Duplicates the specified buffer.

public static FloatBuffer

Returns:

the duplicated buffer
memDuplicate
(FloatBuffer
the buffer to duplicate
buffer
)

Duplicates the specified buffer.

public static DoubleBuffer

Returns:

the duplicated buffer
memDuplicate
(DoubleBuffer
the buffer to duplicate
buffer
)

Duplicates the specified buffer.

public static FloatBuffer

Returns:

the new FloatBuffer
memFloatBuffer
(long
the starting memory address
address
,
int
the buffer capacity
capacity
)

Creates a new direct FloatBuffer that starts at the specified memory address and has the specified capacity.

public static FloatBuffer
memFloatBufferSafe(long address, int capacity)

Like memFloatBuffer, but returns null if address is NULL.

public static void
memFree(Buffer
pointer to a memory block previously allocated with memAlloc, memCalloc or memRealloc. If ptr does not point to a block of memory allocated with the above functions, it causes undefined behavior. If ptr is a NULL pointer, the function does nothing.
ptr
)

The standard C free function.

public static void
memFree(CustomBuffer<?> ptr)

CustomBuffer version of memFree.

public static long
memGetAddress(long ptr)

public static boolean
memGetBoolean(long ptr)

public static byte
memGetByte(long ptr)

public static long
memGetCLong(long ptr)

public static double
memGetDouble(long ptr)

public static float
memGetFloat(long ptr)

public static int
memGetInt(long ptr)

public static long
memGetLong(long ptr)

public static short
memGetShort(long ptr)

public static native <
the object type
T
>
T

Returns:

the object pointed to by globalRef
memGlobalRefToObject
(long
the global reference
globalRef
)

Returns the object that the specified global reference points to.

public static IntBuffer

Returns:

the new IntBuffer
memIntBuffer
(long
the starting memory address
address
,
int
the buffer capacity
capacity
)

Creates a new direct IntBuffer that starts at the specified memory address and has the specified capacity.

public static IntBuffer
memIntBufferSafe(long address, int capacity)

Like memIntBuffer, but returns null if address is NULL.

public static int

Returns:

the number of bytes
memLengthASCII
(CharSequence
the text to encode
value
,
boolean
if true, add the number of bytes required for null-termination
nullTerminated
)

Returns the number of bytes required to encode the specified text in the ASCII encoding.

private static int
memLengthNT1(long address, int maxLength)

public static int

Returns:

the string length, in bytes
memLengthNT1
(ByteBuffer
the buffer containing the null-terminated string
buffer
)

Calculates the length, in bytes, of the null-terminated string that starts at the current position of the specified buffer.

private static int
memLengthNT2(long address, int maxLength)

public static int

Returns:

the string length, in bytes
memLengthNT2
(ByteBuffer
the buffer containing the null-terminated string
buffer
)

Calculates the length, in bytes, of the null-terminated string that starts at the current position of the specified buffer.

public static int

Returns:

the number of bytes
memLengthUTF16
(CharSequence
the text to encode
value
,
boolean
if true, add the number of bytes required for null-termination
nullTerminated
)

Returns the number of bytes required to encode the specified text in the UTF-16 encoding.

public static int

Returns:

the number of bytes
memLengthUTF8
(CharSequence
the text to encode
value
,
boolean
if true, add the number of bytes required for null-termination
nullTerminated
)

Returns the number of bytes required to encode the specified text in the UTF-8 encoding.

public static LongBuffer

Returns:

the new LongBuffer
memLongBuffer
(long
the starting memory address
address
,
int
the buffer capacity
capacity
)

Creates a new direct LongBuffer that starts at the specified memory address and has the specified capacity.

public static LongBuffer
memLongBufferSafe(long address, int capacity)

Like memLongBuffer, but returns null if address is NULL.

public static PointerBuffer

Returns:

the new PointerBuffer
memPointerBuffer
(long
the starting memory address
address
,
int
the buffer capacity
capacity
)

Creates a new PointerBuffer that starts at the specified memory address and has the specified capacity.

public static PointerBuffer
memPointerBufferSafe(long address, int capacity)

Like memPointerBuffer, but returns null if address is NULL.

public static void
memPutAddress(long ptr, long value)

public static void
memPutByte(long ptr, byte value)

public static void
memPutCLong(long ptr, long value)

public static void
memPutDouble(long ptr, double value)

public static void
memPutFloat(long ptr, float value)

public static void
memPutInt(long ptr, int value)

public static void
memPutLong(long ptr, long value)

public static void
memPutShort(long ptr, short value)

public static ByteBuffer

Returns:

a pointer to the reallocated memory block, which may be either the same as ptr or a new location
memRealloc
(ByteBuffer
a pointer to a memory block previously allocated with memAlloc, memCalloc or memRealloc. Alternatively, this can be a NULL pointer, in which case a new block is allocated (as if memAlloc was called).
ptr
,
int
the new size for the memory block, in bytes.
size
)

The standard C realloc function.

public static ShortBuffer
memRealloc(ShortBuffer ptr, int
the number of short values to allocate.
size
)

ShortBuffer version of memRealloc.

public static IntBuffer
memRealloc(IntBuffer ptr, int
the number of int values to allocate.
size
)

IntBuffer version of memRealloc.

public static LongBuffer
memRealloc(LongBuffer ptr, int
the number of long values to allocate.
size
)

LongBuffer version of memRealloc.

public static CLongBuffer
memRealloc(CLongBuffer ptr, int
the number of C long values to allocate.
size
)

CLongBuffer version of memRealloc.

public static FloatBuffer
memRealloc(FloatBuffer ptr, int
the number of float values to allocate.
size
)

FloatBuffer version of memRealloc.

public static DoubleBuffer
memRealloc(DoubleBuffer ptr, int
the number of double values to allocate.
size
)

DoubleBuffer version of memRealloc.

public static PointerBuffer
memRealloc(PointerBuffer ptr, int
the number of pointer values to allocate.
size
)

PointerBuffer version of memRealloc.

public static void
memReport(MemoryUtil.MemoryAllocationReport
the report callback
report
)

Reports all live allocations.

public static void
memReport(MemoryUtil.MemoryAllocationReport
the report callback
report
,
MemoryUtil.MemoryAllocationReport.Aggregate
how to aggregate the reported allocations
groupByStackTrace
,
boolean
if the reported allocations should be grouped by thread
groupByThread
)

Reports aggregates for the live allocations.

public static void
memSet(ByteBuffer
the starting memory address
ptr
,
int
the value to set (memSet will convert it to unsigned byte)
value
)

Sets all bytes in a specified block of memory to a fixed value (usually zero).

public static void
memSet(ShortBuffer
the starting memory address
ptr
,
int
the value to set (memSet will convert it to unsigned byte)
value
)

Sets all bytes in a specified block of memory to a fixed value (usually zero).

public static void
memSet(CharBuffer
the starting memory address
ptr
,
int
the value to set (memSet will convert it to unsigned byte)
value
)

Sets all bytes in a specified block of memory to a fixed value (usually zero).

public static void
memSet(IntBuffer
the starting memory address
ptr
,
int
the value to set (memSet will convert it to unsigned byte)
value
)

Sets all bytes in a specified block of memory to a fixed value (usually zero).

public static void
memSet(LongBuffer
the starting memory address
ptr
,
int
the value to set (memSet will convert it to unsigned byte)
value
)

Sets all bytes in a specified block of memory to a fixed value (usually zero).

public static void
memSet(FloatBuffer
the starting memory address
ptr
,
int
the value to set (memSet will convert it to unsigned byte)
value
)

Sets all bytes in a specified block of memory to a fixed value (usually zero).

public static void
memSet(DoubleBuffer
the starting memory address
ptr
,
int
the value to set (memSet will convert it to unsigned byte)
value
)

Sets all bytes in a specified block of memory to a fixed value (usually zero).

public static <
the buffer type
T extends CustomBuffer<T>
>
void
memSet(T
the starting memory address
ptr
,
int
the value to set (memSet will convert it to unsigned byte)
value
)

Sets all bytes in a specified block of memory to a fixed value (usually zero).

public static <
the struct type
T extends Struct<T>
>
void
memSet(T
the starting memory address
ptr
,
int
the value to set (memSet will convert it to unsigned byte)
value
)

Sets all bytes in a specified block of memory to a fixed value (usually zero).

public static void
memSet(long
the starting memory address
ptr
,
int
the value to set (memSet will convert it to unsigned byte)
value
,
long
the number of bytes to set
bytes
)

Sets all bytes in a specified block of memory to a fixed value (usually zero).

private static void
memSet32(int ptr, int value, int bytes)

private static void
memSet64(long ptr, int value, int bytes)

public static ShortBuffer

Returns:

the new ShortBuffer
memShortBuffer
(long
the starting memory address
address
,
int
the buffer capacity
capacity
)

Creates a new direct ShortBuffer that starts at the specified memory address and has the specified capacity.

public static ShortBuffer
memShortBufferSafe(long address, int capacity)

Like memShortBuffer, but returns null if address is NULL.

public static ByteBuffer

Returns:

the sliced buffer
memSlice
(ByteBuffer
the buffer to slice
buffer
)

Slices the specified buffer.

public static ShortBuffer

Returns:

the sliced buffer
memSlice
(ShortBuffer
the buffer to slice
buffer
)

Slices the specified buffer.

public static CharBuffer

Returns:

the sliced buffer
memSlice
(CharBuffer
the buffer to slice
buffer
)

Slices the specified buffer.

public static IntBuffer

Returns:

the sliced buffer
memSlice
(IntBuffer
the buffer to slice
buffer
)

Slices the specified buffer.

public static LongBuffer

Returns:

the sliced buffer
memSlice
(LongBuffer
the buffer to slice
buffer
)

Slices the specified buffer.

public static FloatBuffer

Returns:

the sliced buffer
memSlice
(FloatBuffer
the buffer to slice
buffer
)

Slices the specified buffer.

public static DoubleBuffer

Returns:

the sliced buffer
memSlice
(DoubleBuffer
the buffer to slice
buffer
)

Slices the specified buffer.

public static ByteBuffer

Returns:

the sliced buffer
memSlice
(ByteBuffer
the buffer to slice
buffer
,
int
the slice offset, it must be ≤ buffer.remaining()
offset
,
int
the slice length, it must be ≤ buffer.capacity() - (buffer.position() + offset)
capacity
)

Returns a slice of the specified buffer between (buffer.position() + offset) and (buffer.position() + offset + capacity).

public static ShortBuffer

Returns:

the sliced buffer
memSlice
(ShortBuffer
the buffer to slice
buffer
,
int
the slice offset, it must be ≤ buffer.remaining()
offset
,
int
the slice length, it must be ≤ buffer.capacity() - (buffer.position() + offset)
capacity
)

Returns a slice of the specified buffer between (buffer.position() + offset) and (buffer.position() + offset + capacity).

public static CharBuffer

Returns:

the sliced buffer
memSlice
(CharBuffer
the buffer to slice
buffer
,
int
the slice offset, it must be ≤ buffer.remaining()
offset
,
int
the slice length, it must be ≤ buffer.capacity() - (buffer.position() + offset)
capacity
)

Returns a slice of the specified buffer between (buffer.position() + offset) and (buffer.position() + offset + capacity).

public static IntBuffer

Returns:

the sliced buffer
memSlice
(IntBuffer
the buffer to slice
buffer
,
int
the slice offset, it must be ≤ buffer.remaining()
offset
,
int
the slice length, it must be ≤ buffer.capacity() - (buffer.position() + offset)
capacity
)

Returns a slice of the specified buffer between (buffer.position() + offset) and (buffer.position() + offset + capacity).

public static LongBuffer

Returns:

the sliced buffer
memSlice
(LongBuffer
the buffer to slice
buffer
,
int
the slice offset, it must be ≤ buffer.remaining()
offset
,
int
the slice length, it must be ≤ buffer.capacity() - (buffer.position() + offset)
capacity
)

Returns a slice of the specified buffer between (buffer.position() + offset) and (buffer.position() + offset + capacity).

public static FloatBuffer

Returns:

the sliced buffer
memSlice
(FloatBuffer
the buffer to slice
buffer
,
int
the slice offset, it must be ≤ buffer.remaining()
offset
,
int
the slice length, it must be ≤ buffer.capacity() - (buffer.position() + offset)
capacity
)

Returns a slice of the specified buffer between (buffer.position() + offset) and (buffer.position() + offset + capacity).

public static DoubleBuffer

Returns:

the sliced buffer
memSlice
(DoubleBuffer
the buffer to slice
buffer
,
int
the slice offset, it must be ≤ buffer.remaining()
offset
,
int
the slice length, it must be ≤ buffer.capacity() - (buffer.position() + offset)
capacity
)

Returns a slice of the specified buffer between (buffer.position() + offset) and (buffer.position() + offset + capacity).

public static <T extends CustomBuffer<T>> T

Returns:

the sliced buffer
memSlice
(T
the buffer to slice
buffer
,
int
the slice offset, it must be ≤ buffer.remaining()
offset
,
int
the slice length, it must be ≤ buffer.capacity() - (buffer.position() + offset)
capacity
)

Returns a slice of the specified buffer between (buffer.position() + offset) and (buffer.position() + offset + capacity).

public static ByteBuffer

Returns:

the encoded text. The returned buffer must be deallocated manually with memFree.
memUTF16
(CharSequence
the text to encode
text
)

Returns a ByteBuffer containing the specified text UTF-16 encoded and null-terminated.

public static ByteBuffer

Returns:

the encoded text. The returned buffer must be deallocated manually with memFree.
memUTF16
(CharSequence
the text to encode
text
,
boolean
if true, the text will be terminated with a '\0'.
nullTerminated
)

Returns a ByteBuffer containing the specified text UTF-16 encoded and optionally null-terminated.

public static int

Returns:

the number of bytes of the encoded string
memUTF16
(CharSequence
the text to encode
text
,
boolean
if true, the text will be terminated with a '\0'.
nullTerminated
,
ByteBuffer
the buffer in which to store the encoded text
target
)

Encodes and optionally null-terminates the specified text using UTF-16 encoding.

public static int

Returns:

the number of bytes of the encoded string
memUTF16
(CharSequence
the text to encode
text
,
boolean
if true, the text will be terminated with a '\0'.
nullTerminated
,
ByteBuffer
the buffer in which to store the encoded text
target
,
int
the buffer position to which the string will be encoded
offset
)

Encodes and optionally null-terminates the specified text using UTF-16 encoding.

public static String

Returns:

the decoded String
memUTF16
(long
the string memory address
address
)

Converts the null-terminated UTF-16 encoded string at the specified memory address to a String.

public static String

Returns:

the decoded String
memUTF16
(long
the string memory address
address
,
int
the number of characters to decode
length
)

Converts the UTF-16 encoded string at the specified memory address to a String.

public static String

Returns:

the decoded String
memUTF16
(ByteBuffer
the ByteBuffer to decode
buffer
)

Decodes the bytes with index [position(), position()+remaining()) in buffer, as a UTF-16 string.

public static String

Returns:

the decoded String
memUTF16
(ByteBuffer
the ByteBuffer to decode
buffer
,
int
the number of characters to decode
length
)

Decodes the bytes with index [position(), position()+(length*2)) in buffer, as a UTF-16 string.

public static String

Returns:

the decoded String
memUTF16
(ByteBuffer
the ByteBuffer to decode
buffer
,
int
the number of characters to decode
length
,
int
the offset at which to start decoding, in bytes.
offset
)

Decodes the bytes with index [offset, offset+(length*2)) in buffer, as a UTF-16 string.

public static ByteBuffer
memUTF16Safe(CharSequence text)

Like memUTF16, but returns null if text is null.

public static ByteBuffer
memUTF16Safe(CharSequence text, boolean nullTerminated)

Like memUTF16, but returns null if text is null.

public static String
memUTF16Safe(long address)

Like memUTF16, but returns null if address is NULL.

public static String
memUTF16Safe(long address, int length)

Like memUTF16, but returns null if address is NULL.

public static String
memUTF16Safe(ByteBuffer buffer)

Like memUTF16, but returns null if buffer is null.

public static ByteBuffer

Returns:

the encoded text. The returned buffer must be deallocated manually with memFree.
memUTF8
(CharSequence
the text to encode
text
)

Returns a ByteBuffer containing the specified text UTF-8 encoded and null-terminated.

public static ByteBuffer

Returns:

the encoded text. The returned buffer must be deallocated manually with memFree.
memUTF8
(CharSequence
the text to encode
text
,
boolean
if true, the text will be terminated with a '\0'.
nullTerminated
)

Returns a ByteBuffer containing the specified text UTF-8 encoded and optionally null-terminated.

public static int

Returns:

the number of bytes of the encoded string
memUTF8
(CharSequence
the text to encode
text
,
boolean
if true, the text will be terminated with a '\0'.
nullTerminated
,
ByteBuffer
the buffer in which to store the encoded text
target
)

Encodes and optionally null-terminates the specified text using UTF-8 encoding.

public static int

Returns:

the number of bytes of the encoded string
memUTF8
(CharSequence
the text to encode
text
,
boolean
if true, the text will be terminated with a '\0'.
nullTerminated
,
ByteBuffer
the buffer in which to store the encoded text
target
,
int
the buffer position to which the string will be encoded
offset
)

Encodes and optionally null-terminates the specified text using UTF-8 encoding.

public static String

Returns:

the decoded String
memUTF8
(long
the string memory address
address
)

Converts the null-terminated UTF-8 encoded string at the specified memory address to a String.

public static String

Returns:

the decoded String
memUTF8
(long
the string memory address
address
,
int
the number of bytes to decode
length
)

Converts the UTF-8 encoded string at the specified memory address to a String.

public static String

Returns:

the decoded String
memUTF8
(ByteBuffer
the ByteBuffer to decode
buffer
)

Decodes the bytes with index [position(), position()+remaining()) in buffer, as a UTF-8 string.

public static String

Returns:

the decoded String
memUTF8
(ByteBuffer
the ByteBuffer to decode
buffer
,
int
the number of bytes to decode
length
)

Decodes the bytes with index [position(), position()+length) in buffer, as a UTF-8 string.

public static String

Returns:

the decoded String
memUTF8
(ByteBuffer
the ByteBuffer to decode
buffer
,
int
the number of bytes to decode
length
,
int
the offset at which to start decoding.
offset
)

Decodes the bytes with index [offset, offset+length) in buffer, as a UTF-8 string.

public static ByteBuffer
memUTF8Safe(CharSequence text)

Like memUTF8, but returns null if text is null.

public static ByteBuffer
memUTF8Safe(CharSequence text, boolean nullTerminated)

Like memUTF8, but returns null if text is null.

public static String
memUTF8Safe(long address)

Like memUTF8, but returns null if address is NULL.

public static String
memUTF8Safe(long address, int length)

Like memUTF8, but returns null if address is NULL.

public static String
memUTF8Safe(ByteBuffer buffer)

Like memUTF8, but returns null if buffer is null.

public static long
nmemAlignedAlloc(long alignment, long size)

Unsafe version of memAlignedAlloc.

public static long

Returns:

a pointer to the memory block allocated by the function on success. This pointer will never be NULL, even if size is zero.
nmemAlignedAllocChecked
(long alignment, long size)

Unsafe version of memAlignedAlloc that checks the returned pointer.

public static void
nmemAlignedFree(long ptr)

Unsafe version of memAlignedFree.

public static long
nmemAlloc(long size)

Unsafe version of memAlloc.

public static long

Returns:

a pointer to the memory block allocated by the function on success. This pointer will never be NULL, even if size is zero.
nmemAllocChecked
(long size)

Unsafe version of memAlloc that checks the returned pointer.

public static long
nmemCalloc(long num, long size)

Unsafe version of memCalloc.

public static long

Returns:

a pointer to the memory block allocated by the function on success. This pointer will never be NULL, even if num or size are zero.
nmemCallocChecked
(long num, long size)

Unsafe version of memCalloc that checks the returned pointer.

public static void
nmemFree(long ptr)

Unsafe version of memFree.

public static long
nmemRealloc(long ptr, long size)

Unsafe version of memRealloc.

public static long

Returns:

a pointer to the memory block reallocated by the function on success. This pointer will never be NULL, even if size is zero.
nmemReallocChecked
(long ptr, long size)

Unsafe version of memRealloc that checks the returned pointer.

private static <T extends Buffer> T
realloc(T old_p, T new_p, int size)

pack-priv static ByteBuffer
slice(ByteBuffer source, long address, int capacity)

pack-priv static <T extends Buffer> T
slice(Class<? extends T> clazz, T source, long address, int capacity, long attachmentOffset)

private static int
strlen32NT1(long address, int maxLength)

private static int
strlen32NT2(long address, int maxLength)

private static int
strlen64NT1(long address, int maxLength)

private static int
strlen64NT2(long address, int maxLength)

pack-priv static <T extends Buffer> T
wrap(Class<? extends T> clazz, long address, int capacity)

private static int
write16(long target, int offset, char value)

private static int
write8(long target, int offset, int value)

private static int
write8Safe(long target, int offset, int maxLength, int value)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait