Class MemoryUtil
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.)
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe memory allocation report callback.static interfaceThe interface implemented by the memory allocator used by the explicit memory management API (memAlloc(int),memFree(java.nio.Buffer), etc). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe cache-line size, in bytes.static final longAlias for the null pointer address.static final intThe memory page size, in bytes. -
Method Summary
Modifier and TypeMethodDescriptionstatic MemoryUtil.MemoryAllocatorReturns theMemoryUtil.MemoryAllocatorinstance used internally by the explicit memory management API (memAlloc(int),memFree(java.nio.Buffer), etc).static MemoryUtil.MemoryAllocatorgetAllocator(boolean tracked) Returns theMemoryUtil.MemoryAllocatorinstance used internally by the explicit memory management API (memAlloc(int),memFree(java.nio.Buffer), etc).static longmemAddress(Buffer buffer) Polymorphic version ofmemAddress(ByteBuffer).static longmemAddress(ByteBuffer buffer) Returns the memory address at the current position of the specified buffer.static longmemAddress(ByteBuffer buffer, int position) Returns the memory address at the specified position of the specified buffer.static longmemAddress(CharBuffer buffer) CharBuffer version ofmemAddress(ByteBuffer).static longmemAddress(CharBuffer buffer, int position) CharBuffer version ofmemAddress(ByteBuffer, int).static longmemAddress(DoubleBuffer buffer) DoubleBuffer version ofmemAddress(ByteBuffer).static longmemAddress(DoubleBuffer buffer, int position) DoubleBuffer version ofmemAddress(ByteBuffer, int).static longmemAddress(FloatBuffer buffer) FloatBuffer version ofmemAddress(ByteBuffer).static longmemAddress(FloatBuffer buffer, int position) FloatBuffer version ofmemAddress(ByteBuffer, int).static longmemAddress(IntBuffer buffer) IntBuffer version ofmemAddress(ByteBuffer).static longmemAddress(IntBuffer buffer, int position) IntBuffer version ofmemAddress(ByteBuffer, int).static longmemAddress(LongBuffer buffer) LongBuffer version ofmemAddress(ByteBuffer).static longmemAddress(LongBuffer buffer, int position) LongBuffer version ofmemAddress(ByteBuffer, int).static longmemAddress(ShortBuffer buffer) ShortBuffer version ofmemAddress(ByteBuffer).static longmemAddress(ShortBuffer buffer, int position) ShortBuffer version ofmemAddress(ByteBuffer, int).static longmemAddress(CustomBuffer<?> buffer) CustomBuffer version ofmemAddress(ByteBuffer).static longmemAddress(CustomBuffer<?> buffer, int position) CustomBuffer version ofmemAddress(ByteBuffer, int).static longmemAddress0(Buffer buffer) Returns the memory address of the specified buffer.static longmemAddress0(ByteBuffer buffer) ByteBufferversion ofmemAddress0(Buffer).static longmemAddress0(CharBuffer buffer) CharBufferversion ofmemAddress0(Buffer).static longmemAddress0(DoubleBuffer buffer) DoubleBufferversion ofmemAddress0(Buffer).static longmemAddress0(FloatBuffer buffer) FloatBufferversion ofmemAddress0(Buffer).static longmemAddress0(IntBuffer buffer) IntBufferversion ofmemAddress0(Buffer).static longmemAddress0(LongBuffer buffer) LongBufferversion ofmemAddress0(Buffer).static longmemAddress0(ShortBuffer buffer) ShortBufferversion ofmemAddress0(Buffer).static longmemAddressSafe(@Nullable ByteBuffer buffer) Null-safe version ofmemAddress(ByteBuffer).static longmemAddressSafe(@Nullable CharBuffer buffer) CharBuffer version ofmemAddressSafe(ByteBuffer).static longmemAddressSafe(@Nullable DoubleBuffer buffer) DoubleBuffer version ofmemAddressSafe(ByteBuffer).static longmemAddressSafe(@Nullable FloatBuffer buffer) FloatBuffer version ofmemAddressSafe(ByteBuffer).static longmemAddressSafe(@Nullable IntBuffer buffer) IntBuffer version ofmemAddressSafe(ByteBuffer).static longmemAddressSafe(@Nullable LongBuffer buffer) LongBuffer version ofmemAddressSafe(ByteBuffer).static longmemAddressSafe(@Nullable ShortBuffer buffer) ShortBuffer version ofmemAddressSafe(ByteBuffer).static longmemAddressSafe(@Nullable Pointer pointer) Pointer version ofmemAddressSafe(ByteBuffer).static ByteBuffermemAlignedAlloc(int alignment, int size) The standard C aligned_alloc function.static voidmemAlignedFree(@Nullable ByteBuffer ptr) Frees a block of memory that was allocated withmemAlignedAlloc(int, int).static ByteBuffermemAlloc(int size) The standard C malloc function.static CLongBuffermemAllocCLong(int size) CLongBufferversion ofmemAlloc(int).static DoubleBuffermemAllocDouble(int size) DoubleBuffer version ofmemAlloc(int).static FloatBuffermemAllocFloat(int size) FloatBuffer version ofmemAlloc(int).static IntBuffermemAllocInt(int size) IntBuffer version ofmemAlloc(int).static LongBuffermemAllocLong(int size) LongBuffer version ofmemAlloc(int).static PointerBuffermemAllocPointer(int size) PointerBuffer version ofmemAlloc(int).static ShortBuffermemAllocShort(int size) ShortBuffer version ofmemAlloc(int).static StringmemASCII(long address) Converts the null-terminated ASCII encoded string at the specified memory address to aString.static StringmemASCII(long address, int length) Converts the ASCII encoded string at the specified memory address to aString.static ByteBuffermemASCII(CharSequence text) Returns a ByteBuffer containing the specified text ASCII encoded and null-terminated.static ByteBuffermemASCII(CharSequence text, boolean nullTerminated) Returns aByteBuffercontaining the specified text ASCII encoded and optionally null-terminated.static intmemASCII(CharSequence text, boolean nullTerminated, ByteBuffer target) Encodes and optionally null-terminates the specified text using ASCII encoding.static intmemASCII(CharSequence text, boolean nullTerminated, ByteBuffer target, int offset) Encodes and optionally null-terminates the specified text using ASCII encoding.static StringmemASCII(ByteBuffer buffer) Decodes the bytes with index[position(), position()+remaining()) inbuffer, as an ASCII string.static StringmemASCII(ByteBuffer buffer, int length) Decodes the bytes with index[position(), position()+length) inbuffer, as an ASCII string.static StringmemASCII(ByteBuffer buffer, int length, int offset) Decodes the bytes with index[offset, offset+length) inbuffer, as an ASCII string.static @Nullable StringmemASCIISafe(long address) static @Nullable StringmemASCIISafe(long address, int length) static @Nullable ByteBuffermemASCIISafe(@Nullable CharSequence text) static @Nullable ByteBuffermemASCIISafe(@Nullable CharSequence text, boolean nullTerminated) static @Nullable StringmemASCIISafe(@Nullable ByteBuffer buffer) static ByteBuffermemByteBuffer(long address, int capacity) Creates a new direct ByteBuffer that starts at the specified memory address and has the specified capacity.static ByteBuffermemByteBuffer(CharBuffer buffer) Creates aByteBufferinstance as a view of the specifiedCharBufferbetween its current position and limit.static ByteBuffermemByteBuffer(DoubleBuffer buffer) Creates aByteBufferinstance as a view of the specifiedDoubleBufferbetween its current position and limit.static ByteBuffermemByteBuffer(FloatBuffer buffer) Creates aByteBufferinstance as a view of the specifiedFloatBufferbetween its current position and limit.static ByteBuffermemByteBuffer(IntBuffer buffer) Creates aByteBufferinstance as a view of the specifiedIntBufferbetween its current position and limit.static ByteBuffermemByteBuffer(LongBuffer buffer) Creates aByteBufferinstance as a view of the specifiedLongBufferbetween its current position and limit.static ByteBuffermemByteBuffer(ShortBuffer buffer) Creates aByteBufferinstance as a view of the specifiedShortBufferbetween its current position and limit.static ByteBuffermemByteBuffer(CustomBuffer<?> buffer) Creates aByteBufferinstance as a view of the specifiedCustomBufferbetween its current position and limit.static <T extends Struct<T>>
ByteBuffermemByteBuffer(T value) Creates aByteBufferinstance as a view of the specifiedStruct.static ByteBuffermemByteBufferNT1(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 ByteBuffermemByteBufferNT1(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 ofmaxLengthbytes.static @Nullable ByteBuffermemByteBufferNT1Safe(long address) static @Nullable ByteBuffermemByteBufferNT1Safe(long address, int maxLength) static ByteBuffermemByteBufferNT2(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 ByteBuffermemByteBufferNT2(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 ofmaxLengthbytes.static @Nullable ByteBuffermemByteBufferNT2Safe(long address) static @Nullable ByteBuffermemByteBufferNT2Safe(long address, int maxLength) static @Nullable ByteBuffermemByteBufferSafe(long address, int capacity) static ByteBuffermemCalloc(int num) Alternative version ofmemCalloc(int, int).static ByteBuffermemCalloc(int num, int size) The standard C calloc function.static CLongBuffermemCallocCLong(int num) CLongBufferversion ofmemCalloc(int, int).static DoubleBuffermemCallocDouble(int num) DoubleBuffer version ofmemCalloc(int, int).static FloatBuffermemCallocFloat(int num) FloatBuffer version ofmemCalloc(int, int).static IntBuffermemCallocInt(int num) IntBuffer version ofmemCalloc(int, int).static LongBuffermemCallocLong(int num) LongBuffer version ofmemCalloc(int, int).static PointerBuffermemCallocPointer(int num) PointerBuffer version ofmemCalloc(int, int).static ShortBuffermemCallocShort(int num) ShortBuffer version ofmemCalloc(int, int).static CharBuffermemCharBuffer(long address, int capacity) Creates a new direct CharBuffer that starts at the specified memory address and has the specified capacity.static @Nullable CharBuffermemCharBufferSafe(long address, int capacity) static CLongBuffermemCLongBuffer(long address, int capacity) Creates a new directCLongBufferthat starts at the specified memory address and has the specified capacity.static @Nullable CLongBuffermemCLongBufferSafe(long address, int capacity) static voidmemCopy(long src, long dst, long bytes) Sets all bytes in a specified block of memory to a copy of another block.static voidmemCopy(ByteBuffer src, ByteBuffer dst) Sets all bytes in a specified block of memory to a copy of another block.static voidmemCopy(CharBuffer src, CharBuffer dst) Sets all bytes in a specified block of memory to a copy of another block.static voidmemCopy(DoubleBuffer src, DoubleBuffer dst) Sets all bytes in a specified block of memory to a copy of another block.static voidmemCopy(FloatBuffer src, FloatBuffer dst) Sets all bytes in a specified block of memory to a copy of another block.static voidSets all bytes in a specified block of memory to a copy of another block.static voidmemCopy(LongBuffer src, LongBuffer dst) Sets all bytes in a specified block of memory to a copy of another block.static voidmemCopy(ShortBuffer src, ShortBuffer dst) Sets all bytes in a specified block of memory to a copy of another block.static <T extends CustomBuffer<T>>
voidmemCopy(T src, T dst) Sets all bytes in a specified block of memory to a copy of another block.static DoubleBuffermemDoubleBuffer(long address, int capacity) Creates a new direct DoubleBuffer that starts at the specified memory address and has the specified capacity.static @Nullable DoubleBuffermemDoubleBufferSafe(long address, int capacity) static ByteBuffermemDuplicate(ByteBuffer buffer) Duplicates the specified buffer.static CharBuffermemDuplicate(CharBuffer buffer) Duplicates the specified buffer.static DoubleBuffermemDuplicate(DoubleBuffer buffer) Duplicates the specified buffer.static FloatBuffermemDuplicate(FloatBuffer buffer) Duplicates the specified buffer.static IntBuffermemDuplicate(IntBuffer buffer) Duplicates the specified buffer.static LongBuffermemDuplicate(LongBuffer buffer) Duplicates the specified buffer.static ShortBuffermemDuplicate(ShortBuffer buffer) Duplicates the specified buffer.static FloatBuffermemFloatBuffer(long address, int capacity) Creates a new direct FloatBuffer that starts at the specified memory address and has the specified capacity.static @Nullable FloatBuffermemFloatBufferSafe(long address, int capacity) static voidThe standard C free function.static voidmemFree(@Nullable ByteBuffer ptr) ByteBufferversion ofmemFree(Buffer).static voidmemFree(@Nullable CharBuffer ptr) CharBufferversion ofmemFree(Buffer).static voidmemFree(@Nullable DoubleBuffer ptr) DoubleBufferversion ofmemFree(Buffer).static voidmemFree(@Nullable FloatBuffer ptr) FloatBufferversion ofmemFree(Buffer).static voidIntBufferversion ofmemFree(Buffer).static voidmemFree(@Nullable LongBuffer ptr) LongBufferversion ofmemFree(Buffer).static voidmemFree(@Nullable ShortBuffer ptr) ShortBufferversion ofmemFree(Buffer).static voidmemFree(@Nullable CustomBuffer<?> ptr) CustomBufferversion ofmemFree(java.nio.Buffer).static longmemGetAddress(long ptr) static booleanmemGetBoolean(long ptr) static bytememGetByte(long ptr) static longmemGetCLong(long ptr) static doublememGetDouble(long ptr) static floatmemGetFloat(long ptr) static intmemGetInt(long ptr) static longmemGetLong(long ptr) static shortmemGetShort(long ptr) static <T> TmemGlobalRefToObject(long globalRef) Returns the object that the specified global reference points to.static IntBuffermemIntBuffer(long address, int capacity) Creates a new direct IntBuffer that starts at the specified memory address and has the specified capacity.static @Nullable IntBuffermemIntBufferSafe(long address, int capacity) static intmemLengthASCII(CharSequence value, boolean nullTerminated) Returns the number of bytes required to encode the specified text in the ASCII encoding.static intmemLengthNT1(ByteBuffer buffer) Calculates the length, in bytes, of the null-terminated string that starts at the current position of the specified buffer.static intmemLengthNT2(ByteBuffer buffer) Calculates the length, in bytes, of the null-terminated string that starts at the current position of the specified buffer.static intmemLengthUTF16(CharSequence value, boolean nullTerminated) Returns the number of bytes required to encode the specified text in the UTF-16 encoding.static intmemLengthUTF8(CharSequence value, boolean nullTerminated) Returns the number of bytes required to encode the specified text in the UTF-8 encoding.static LongBuffermemLongBuffer(long address, int capacity) Creates a new direct LongBuffer that starts at the specified memory address and has the specified capacity.static @Nullable LongBuffermemLongBufferSafe(long address, int capacity) static PointerBuffermemPointerBuffer(long address, int capacity) Creates a new PointerBuffer that starts at the specified memory address and has the specified capacity.static @Nullable PointerBuffermemPointerBufferSafe(long address, int capacity) static voidmemPutAddress(long ptr, long value) static voidmemPutByte(long ptr, byte value) static voidmemPutCLong(long ptr, long value) static voidmemPutDouble(long ptr, double value) static voidmemPutFloat(long ptr, float value) static voidmemPutInt(long ptr, int value) static voidmemPutLong(long ptr, long value) static voidmemPutShort(long ptr, short value) static ByteBuffermemRealloc(@Nullable ByteBuffer ptr, int size) The standard C realloc function.static DoubleBuffermemRealloc(@Nullable DoubleBuffer ptr, int size) DoubleBuffer version ofmemRealloc(java.nio.ByteBuffer, int).static FloatBuffermemRealloc(@Nullable FloatBuffer ptr, int size) FloatBuffer version ofmemRealloc(java.nio.ByteBuffer, int).static IntBuffermemRealloc(@Nullable IntBuffer ptr, int size) IntBuffer version ofmemRealloc(java.nio.ByteBuffer, int).static LongBuffermemRealloc(@Nullable LongBuffer ptr, int size) LongBuffer version ofmemRealloc(java.nio.ByteBuffer, int).static ShortBuffermemRealloc(@Nullable ShortBuffer ptr, int size) ShortBuffer version ofmemRealloc(java.nio.ByteBuffer, int).static CLongBuffermemRealloc(@Nullable CLongBuffer ptr, int size) CLongBufferversion ofmemRealloc(java.nio.ByteBuffer, int).static PointerBuffermemRealloc(@Nullable PointerBuffer ptr, int size) PointerBuffer version ofmemRealloc(java.nio.ByteBuffer, int).static voidReports all live allocations.static voidmemReport(MemoryUtil.MemoryAllocationReport report, MemoryUtil.MemoryAllocationReport.Aggregate groupByStackTrace, boolean groupByThread) Reports aggregates for the live allocations.static voidmemSet(long ptr, int value, long bytes) Sets all bytes in a specified block of memory to a fixed value (usually zero).static voidmemSet(ByteBuffer ptr, int value) Sets all bytes in a specified block of memory to a fixed value (usually zero).static voidmemSet(CharBuffer ptr, int value) Sets all bytes in a specified block of memory to a fixed value (usually zero).static voidmemSet(DoubleBuffer ptr, int value) Sets all bytes in a specified block of memory to a fixed value (usually zero).static voidmemSet(FloatBuffer ptr, int value) Sets all bytes in a specified block of memory to a fixed value (usually zero).static voidSets all bytes in a specified block of memory to a fixed value (usually zero).static voidmemSet(LongBuffer ptr, int value) Sets all bytes in a specified block of memory to a fixed value (usually zero).static voidmemSet(ShortBuffer ptr, int value) Sets all bytes in a specified block of memory to a fixed value (usually zero).static <T extends CustomBuffer<T>>
voidmemSet(T ptr, int value) Sets all bytes in a specified block of memory to a fixed value (usually zero).static ShortBuffermemShortBuffer(long address, int capacity) Creates a new direct ShortBuffer that starts at the specified memory address and has the specified capacity.static @Nullable ShortBuffermemShortBufferSafe(long address, int capacity) static ByteBuffermemSlice(ByteBuffer buffer) Slices the specified buffer.static ByteBuffermemSlice(ByteBuffer buffer, int offset, int capacity) Returns a slice of the specified buffer between(buffer.position() + offset)and(buffer.position() + offset + capacity).static CharBuffermemSlice(CharBuffer buffer) Slices the specified buffer.static CharBuffermemSlice(CharBuffer buffer, int offset, int capacity) Returns a slice of the specified buffer between(buffer.position() + offset)and(buffer.position() + offset + capacity).static DoubleBuffermemSlice(DoubleBuffer buffer) Slices the specified buffer.static DoubleBuffermemSlice(DoubleBuffer buffer, int offset, int capacity) Returns a slice of the specified buffer between(buffer.position() + offset)and(buffer.position() + offset + capacity).static FloatBuffermemSlice(FloatBuffer buffer) Slices the specified buffer.static FloatBuffermemSlice(FloatBuffer buffer, int offset, int capacity) Returns a slice of the specified buffer between(buffer.position() + offset)and(buffer.position() + offset + capacity).static IntBufferSlices the specified buffer.static IntBufferReturns a slice of the specified buffer between(buffer.position() + offset)and(buffer.position() + offset + capacity).static LongBuffermemSlice(LongBuffer buffer) Slices the specified buffer.static LongBuffermemSlice(LongBuffer buffer, int offset, int capacity) Returns a slice of the specified buffer between(buffer.position() + offset)and(buffer.position() + offset + capacity).static ShortBuffermemSlice(ShortBuffer buffer) Slices the specified buffer.static ShortBuffermemSlice(ShortBuffer buffer, int offset, int capacity) Returns a slice of the specified buffer between(buffer.position() + offset)and(buffer.position() + offset + capacity).static <T extends CustomBuffer<T>>
TmemSlice(T buffer, int offset, int capacity) Returns a slice of the specified buffer between(buffer.position() + offset)and(buffer.position() + offset + capacity).static StringmemUTF16(long address) Converts the null-terminated UTF-16 encoded string at the specified memory address to aString.static StringmemUTF16(long address, int length) Converts the UTF-16 encoded string at the specified memory address to aString.static ByteBuffermemUTF16(CharSequence text) Returns a ByteBuffer containing the specified text UTF-16 encoded and null-terminated.static ByteBuffermemUTF16(CharSequence text, boolean nullTerminated) Returns a ByteBuffer containing the specified text UTF-16 encoded and optionally null-terminated.static intmemUTF16(CharSequence text, boolean nullTerminated, ByteBuffer target) Encodes and optionally null-terminates the specified text using UTF-16 encoding.static intmemUTF16(CharSequence text, boolean nullTerminated, ByteBuffer target, int offset) Encodes and optionally null-terminates the specified text using UTF-16 encoding.static StringmemUTF16(ByteBuffer buffer) Decodes the bytes with index[position(), position()+remaining()) inbuffer, as a UTF-16 string.static StringmemUTF16(ByteBuffer buffer, int length) Decodes the bytes with index[position(), position()+(length*2)) inbuffer, as a UTF-16 string.static StringmemUTF16(ByteBuffer buffer, int length, int offset) Decodes the bytes with index[offset, offset+(length*2)) inbuffer, as a UTF-16 string.static @Nullable StringmemUTF16Safe(long address) static @Nullable StringmemUTF16Safe(long address, int length) static @Nullable ByteBuffermemUTF16Safe(@Nullable CharSequence text) static @Nullable ByteBuffermemUTF16Safe(@Nullable CharSequence text, boolean nullTerminated) static @Nullable StringmemUTF16Safe(@Nullable ByteBuffer buffer) static StringmemUTF8(long address) Converts the null-terminated UTF-8 encoded string at the specified memory address to aString.static StringmemUTF8(long address, int length) Converts the UTF-8 encoded string at the specified memory address to aString.static ByteBuffermemUTF8(CharSequence text) Returns a ByteBuffer containing the specified text UTF-8 encoded and null-terminated.static ByteBuffermemUTF8(CharSequence text, boolean nullTerminated) Returns a ByteBuffer containing the specified text UTF-8 encoded and optionally null-terminated.static intmemUTF8(CharSequence text, boolean nullTerminated, ByteBuffer target) Encodes and optionally null-terminates the specified text using UTF-8 encoding.static intmemUTF8(CharSequence text, boolean nullTerminated, ByteBuffer target, int offset) Encodes and optionally null-terminates the specified text using UTF-8 encoding.static StringmemUTF8(ByteBuffer buffer) Decodes the bytes with index[position(), position()+remaining()) inbuffer, as a UTF-8 string.static StringmemUTF8(ByteBuffer buffer, int length) Decodes the bytes with index[position(), position()+length) inbuffer, as a UTF-8 string.static StringmemUTF8(ByteBuffer buffer, int length, int offset) Decodes the bytes with index[offset, offset+length) inbuffer, as a UTF-8 string.static @Nullable StringmemUTF8Safe(long address) static @Nullable StringmemUTF8Safe(long address, int length) static @Nullable ByteBuffermemUTF8Safe(@Nullable CharSequence text) static @Nullable ByteBuffermemUTF8Safe(@Nullable CharSequence text, boolean nullTerminated) static @Nullable StringmemUTF8Safe(@Nullable ByteBuffer buffer) static longnmemAlignedAlloc(long alignment, long size) Unsafe version ofmemAlignedAlloc(int, int).static longnmemAlignedAllocChecked(long alignment, long size) Unsafe version ofmemAlignedAlloc(int, int)that checks the returned pointer.static voidnmemAlignedFree(long ptr) Unsafe version ofmemAlignedFree(java.nio.ByteBuffer).static longnmemAlloc(long size) Unsafe version ofmemAlloc(int).static longnmemAllocChecked(long size) Unsafe version ofmemAlloc(int)that checks the returned pointer.static longnmemCalloc(long num, long size) Unsafe version ofmemCalloc(int, int).static longnmemCallocChecked(long num, long size) Unsafe version ofmemCalloc(int, int)that checks the returned pointer.static voidnmemFree(long ptr) Unsafe version ofmemFree(java.nio.Buffer).static longnmemRealloc(long ptr, long size) Unsafe version ofmemRealloc(java.nio.ByteBuffer, int).static longnmemReallocChecked(long ptr, long size) Unsafe version ofmemRealloc(java.nio.ByteBuffer, int)that checks the returned pointer.
-
Field Details
-
NULL
public static final long NULLAlias for the null pointer address.- See Also:
-
PAGE_SIZE
public static final int PAGE_SIZEThe memory page size, in bytes. This value is always a power-of-two. -
CACHE_LINE_SIZE
public static final int CACHE_LINE_SIZEThe cache-line size, in bytes. This value is always a power-of-two.
-
-
Method Details
-
getAllocator
Returns theMemoryUtil.MemoryAllocatorinstance used internally by the explicit memory management API (memAlloc(int),memFree(java.nio.Buffer), etc).Allocations made through the returned instance will not be tracked for memory leaks, even if
Configuration.DEBUG_MEMORY_ALLOCATORis enabled. This can be useful forstatic finalallocations 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
memprefix to avoid pollution of auto-complete lists.- Returns:
- the
MemoryUtil.MemoryAllocatorinstance
-
getAllocator
Returns theMemoryUtil.MemoryAllocatorinstance used internally by the explicit memory management API (memAlloc(int),memFree(java.nio.Buffer), etc).- Parameters:
tracked- whether allocations will be tracked for memory leaks, ifConfiguration.DEBUG_MEMORY_ALLOCATORis enabled.- Returns:
- the
MemoryUtil.MemoryAllocatorinstance
-
nmemAlloc
public static long nmemAlloc(long size) -
nmemAllocChecked
public static long nmemAllocChecked(long size) Unsafe version ofmemAlloc(int)that checks the returned pointer.- Returns:
- a pointer to the memory block allocated by the function on success. This pointer will never be
NULL, even ifsizeis zero. - Throws:
OutOfMemoryError- if the function failed to allocate the requested block of memory
-
memAlloc
The standard C malloc function.Allocates a block of
sizebytes 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
memFree(java.nio.Buffer).- Parameters:
size- the size of the memory block to allocate, in bytes. Ifsizeis zero, the returned pointer shall not be dereferenced.- Returns:
- on success, a pointer to the memory block allocated by the function
- Throws:
OutOfMemoryError- if the function failed to allocate the requested block of memory
-
memAllocShort
ShortBuffer version ofmemAlloc(int).- Parameters:
size- the number of short values to allocate.
-
memAllocInt
IntBuffer version ofmemAlloc(int).- Parameters:
size- the number of int values to allocate.
-
memAllocFloat
FloatBuffer version ofmemAlloc(int).- Parameters:
size- the number of float values to allocate.
-
memAllocLong
LongBuffer version ofmemAlloc(int).- Parameters:
size- the number of long values to allocate.
-
memAllocCLong
CLongBufferversion ofmemAlloc(int).- Parameters:
size- the number of C long values to allocate.
-
memAllocDouble
DoubleBuffer version ofmemAlloc(int).- Parameters:
size- the number of double values to allocate.
-
memAllocPointer
PointerBuffer version ofmemAlloc(int).- Parameters:
size- the number of pointer values to allocate.
-
nmemFree
public static void nmemFree(long ptr) Unsafe version ofmemFree(java.nio.Buffer). -
memFree
The standard C free function.A block of memory previously allocated by a call to
memAlloc(int),memCalloc(int, int)ormemRealloc(java.nio.ByteBuffer, int)is deallocated, making it available again for further allocations.- Parameters:
ptr- pointer to a memory block previously allocated withmemAlloc(int),memCalloc(int, int)ormemRealloc(java.nio.ByteBuffer, int). Ifptrdoes not point to a block of memory allocated with the above functions, it causes undefined behavior. Ifptris aNULLpointer, the function does nothing.
-
memFree
ByteBufferversion ofmemFree(Buffer). -
memFree
ShortBufferversion ofmemFree(Buffer). -
memFree
CharBufferversion ofmemFree(Buffer). -
memFree
IntBufferversion ofmemFree(Buffer). -
memFree
LongBufferversion ofmemFree(Buffer). -
memFree
FloatBufferversion ofmemFree(Buffer). -
memFree
DoubleBufferversion ofmemFree(Buffer). -
memFree
CustomBufferversion ofmemFree(java.nio.Buffer). -
nmemCalloc
public static long nmemCalloc(long num, long size) Unsafe version ofmemCalloc(int, int). May returnNULLifnumorsizeare zero or the allocation failed. -
nmemCallocChecked
public static long nmemCallocChecked(long num, long size) Unsafe version ofmemCalloc(int, int)that checks the returned pointer.- Returns:
- a pointer to the memory block allocated by the function on success. This pointer will never be
NULL, even ifnumorsizeare zero. - Throws:
OutOfMemoryError- if the function failed to allocate the requested block of memory
-
memCalloc
The standard C calloc function.Allocates a block of memory for an array of
numelements, each of themsizebytes 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
memFree(java.nio.Buffer).- Parameters:
num- the number of elements to allocate.size- the size of each element. Ifsizeis 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
- Throws:
OutOfMemoryError- if the function failed to allocate the requested block of memory
-
memCalloc
Alternative version ofmemCalloc(int, int).- Parameters:
num- the number of bytes to allocate.
-
memCallocShort
ShortBuffer version ofmemCalloc(int, int).- Parameters:
num- the number of short values to allocate.
-
memCallocInt
IntBuffer version ofmemCalloc(int, int).- Parameters:
num- the number of int values to allocate.
-
memCallocFloat
FloatBuffer version ofmemCalloc(int, int).- Parameters:
num- the number of float values to allocate.
-
memCallocLong
LongBuffer version ofmemCalloc(int, int).- Parameters:
num- the number of long values to allocate.
-
memCallocCLong
CLongBufferversion ofmemCalloc(int, int).- Parameters:
num- the number of C long values to allocate.
-
memCallocDouble
DoubleBuffer version ofmemCalloc(int, int).- Parameters:
num- the number of double values to allocate.
-
memCallocPointer
PointerBuffer version ofmemCalloc(int, int).- Parameters:
num- the number of pointer values to allocate.
-
nmemRealloc
public static long nmemRealloc(long ptr, long size) Unsafe version ofmemRealloc(java.nio.ByteBuffer, int). May returnNULLifsizeis zero or the allocation failed. -
nmemReallocChecked
public static long nmemReallocChecked(long ptr, long size) Unsafe version ofmemRealloc(java.nio.ByteBuffer, int)that checks the returned pointer.- Returns:
- a pointer to the memory block reallocated by the function on success. This pointer will never be
NULL, even ifsizeis zero. - Throws:
OutOfMemoryError- if the function failed to allocate the requested block of memory
-
memRealloc
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.The memory address used is always the address at the start of
ptr, so the current position ofptrdoes 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, unlesssizeis 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 withmemAlloc(int),memCalloc(int, int)ormemRealloc(java.nio.ByteBuffer, int). Alternatively, this can be aNULLpointer, in which case a new block is allocated (as ifmemAlloc(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
ptror a new location - Throws:
OutOfMemoryError- if the function failed to allocate the requested block of memory. The memory block pointed to by argumentptris not deallocated (it is still valid, and with its contents unchanged).
-
memRealloc
ShortBuffer version ofmemRealloc(java.nio.ByteBuffer, int).- Parameters:
size- the number of short values to allocate.
-
memRealloc
IntBuffer version ofmemRealloc(java.nio.ByteBuffer, int).- Parameters:
size- the number of int values to allocate.
-
memRealloc
LongBuffer version ofmemRealloc(java.nio.ByteBuffer, int).- Parameters:
size- the number of long values to allocate.
-
memRealloc
CLongBufferversion ofmemRealloc(java.nio.ByteBuffer, int).- Parameters:
size- the number of C long values to allocate.
-
memRealloc
FloatBuffer version ofmemRealloc(java.nio.ByteBuffer, int).- Parameters:
size- the number of float values to allocate.
-
memRealloc
DoubleBuffer version ofmemRealloc(java.nio.ByteBuffer, int).- Parameters:
size- the number of double values to allocate.
-
memRealloc
PointerBuffer version ofmemRealloc(java.nio.ByteBuffer, int).- Parameters:
size- the number of pointer values to allocate.
-
nmemAlignedAlloc
public static long nmemAlignedAlloc(long alignment, long size) Unsafe version ofmemAlignedAlloc(int, int). May returnNULLifsizeis zero or the allocation failed. -
nmemAlignedAllocChecked
public static long nmemAlignedAllocChecked(long alignment, long size) Unsafe version ofmemAlignedAlloc(int, int)that checks the returned pointer.- Returns:
- a pointer to the memory block allocated by the function on success. This pointer will never be
NULL, even ifsizeis zero. - Throws:
OutOfMemoryError- if the function failed to allocate the requested block of memory
-
memAlignedAlloc
The standard C aligned_alloc function.Allocate
sizebytes 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 withmemAlignedFree(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.
-
nmemAlignedFree
public static void nmemAlignedFree(long ptr) Unsafe version ofmemAlignedFree(java.nio.ByteBuffer). -
memAlignedFree
Frees a block of memory that was allocated withmemAlignedAlloc(int, int). If ptr isNULL, no operation is performed.- Parameters:
ptr- the aligned block of memory to free
-
memReport
Reports all live allocations.This method can only be used if the
Configuration.DEBUG_MEMORY_ALLOCATORoption 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_ALLOCATORoption 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
Returns the memory address of the specified buffer. [INTERNAL USE ONLY]- Parameters:
buffer- the buffer- Returns:
- the memory address
-
memAddress0
ByteBufferversion ofmemAddress0(Buffer). -
memAddress0
ShortBufferversion ofmemAddress0(Buffer). -
memAddress0
CharBufferversion ofmemAddress0(Buffer). -
memAddress0
IntBufferversion ofmemAddress0(Buffer). -
memAddress0
LongBufferversion ofmemAddress0(Buffer). -
memAddress0
FloatBufferversion ofmemAddress0(Buffer). -
memAddress0
DoubleBufferversion ofmemAddress0(Buffer). -
memAddress
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
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:
-
memAddress
ShortBuffer version ofmemAddress(ByteBuffer). -
memAddress
ShortBuffer version ofmemAddress(ByteBuffer, int). -
memAddress
CharBuffer version ofmemAddress(ByteBuffer). -
memAddress
CharBuffer version ofmemAddress(ByteBuffer, int). -
memAddress
IntBuffer version ofmemAddress(ByteBuffer). -
memAddress
IntBuffer version ofmemAddress(ByteBuffer, int). -
memAddress
FloatBuffer version ofmemAddress(ByteBuffer). -
memAddress
FloatBuffer version ofmemAddress(ByteBuffer, int). -
memAddress
LongBuffer version ofmemAddress(ByteBuffer). -
memAddress
LongBuffer version ofmemAddress(ByteBuffer, int). -
memAddress
DoubleBuffer version ofmemAddress(ByteBuffer). -
memAddress
DoubleBuffer version ofmemAddress(ByteBuffer, int). -
memAddress
Polymorphic version ofmemAddress(ByteBuffer). -
memAddress
CustomBuffer version ofmemAddress(ByteBuffer). -
memAddress
CustomBuffer version ofmemAddress(ByteBuffer, int). -
memAddressSafe
Null-safe version ofmemAddress(ByteBuffer). ReturnsNULLif the specified buffer is null. -
memAddressSafe
ShortBuffer version ofmemAddressSafe(ByteBuffer). -
memAddressSafe
CharBuffer version ofmemAddressSafe(ByteBuffer). -
memAddressSafe
IntBuffer version ofmemAddressSafe(ByteBuffer). -
memAddressSafe
FloatBuffer version ofmemAddressSafe(ByteBuffer). -
memAddressSafe
LongBuffer version ofmemAddressSafe(ByteBuffer). -
memAddressSafe
DoubleBuffer version ofmemAddressSafe(ByteBuffer). -
memAddressSafe
Pointer version ofmemAddressSafe(ByteBuffer). -
memByteBuffer
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 nativeByteOrder.- Parameters:
address- the starting memory addresscapacity- the buffer capacity- Returns:
- the new ByteBuffer
-
memByteBufferSafe
-
memByteBuffer
Creates aByteBufferinstance as a view of the specifiedShortBufferbetween its current position and limit.This operation is the inverse of
ByteBuffer.asShortBuffer(). The returnedByteBufferinstance will be set to the nativeByteOrder.- Parameters:
buffer- the source buffer- Returns:
- the
ByteBufferview
-
memByteBuffer
Creates aByteBufferinstance as a view of the specifiedCharBufferbetween its current position and limit.This operation is the inverse of
ByteBuffer.asCharBuffer(). The returnedByteBufferinstance will be set to the nativeByteOrder.- Parameters:
buffer- the source buffer- Returns:
- the
ByteBufferview
-
memByteBuffer
Creates aByteBufferinstance as a view of the specifiedIntBufferbetween its current position and limit.This operation is the inverse of
ByteBuffer.asIntBuffer(). The returnedByteBufferinstance will be set to the nativeByteOrder.- Parameters:
buffer- the source buffer- Returns:
- the
ByteBufferview
-
memByteBuffer
Creates aByteBufferinstance as a view of the specifiedLongBufferbetween its current position and limit.This operation is the inverse of
ByteBuffer.asLongBuffer(). The returnedByteBufferinstance will be set to the nativeByteOrder.- Parameters:
buffer- the source buffer- Returns:
- the
ByteBufferview
-
memByteBuffer
Creates aByteBufferinstance as a view of the specifiedFloatBufferbetween its current position and limit.This operation is the inverse of
ByteBuffer.asFloatBuffer(). The returnedByteBufferinstance will be set to the nativeByteOrder.- Parameters:
buffer- the source buffer- Returns:
- the
ByteBufferview
-
memByteBuffer
Creates aByteBufferinstance as a view of the specifiedDoubleBufferbetween its current position and limit.This operation is the inverse of
ByteBuffer.asDoubleBuffer(). The returnedByteBufferinstance will be set to the nativeByteOrder.- Parameters:
buffer- the source buffer- Returns:
- the
ByteBufferview
-
memByteBuffer
Creates aByteBufferinstance as a view of the specifiedCustomBufferbetween its current position and limit.The returned
ByteBufferinstance will be set to the nativeByteOrder.- Parameters:
buffer- the source buffer- Returns:
- the
ByteBufferview
-
memByteBuffer
Creates aByteBufferinstance as a view of the specifiedStruct.The returned
ByteBufferinstance will be set to the nativeByteOrder.- Parameters:
value- the struct value- Returns:
- the
ByteBufferview
-
memShortBuffer
Creates a new direct ShortBuffer that starts at the specified memory address and has the specified capacity.The
addressspecified 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
-
memShortBufferSafe
-
memCharBuffer
Creates a new direct CharBuffer that starts at the specified memory address and has the specified capacity.The
addressspecified 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
-
memCharBufferSafe
-
memIntBuffer
Creates a new direct IntBuffer that starts at the specified memory address and has the specified capacity.The
addressspecified 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
-
memIntBufferSafe
-
memLongBuffer
Creates a new direct LongBuffer that starts at the specified memory address and has the specified capacity.The
addressspecified 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
-
memLongBufferSafe
-
memCLongBuffer
Creates a new directCLongBufferthat starts at the specified memory address and has the specified capacity.The
addressspecified 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
CLongBuffer
-
memCLongBufferSafe
-
memFloatBuffer
Creates a new direct FloatBuffer that starts at the specified memory address and has the specified capacity.The
addressspecified 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
-
memFloatBufferSafe
-
memDoubleBuffer
Creates a new direct DoubleBuffer that starts at the specified memory address and has the specified capacity.The
addressspecified 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
-
memDoubleBufferSafe
-
memPointerBuffer
Creates a new PointerBuffer that starts at the specified memory address and has the specified capacity.The
addressspecified 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
-
memPointerBufferSafe
-
memDuplicate
Duplicates the specified buffer. The returned buffer will have the sameByteOrderas the source buffer.This method should be preferred over
ByteBuffer.duplicate()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to duplicate- Returns:
- the duplicated buffer
-
memDuplicate
Duplicates the specified buffer.This method should be preferred over
ShortBuffer.duplicate()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to duplicate- Returns:
- the duplicated buffer
-
memDuplicate
Duplicates the specified buffer.This method should be preferred over
CharBuffer.duplicate()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to duplicate- Returns:
- the duplicated buffer
-
memDuplicate
Duplicates the specified buffer.This method should be preferred over
IntBuffer.duplicate()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to duplicate- Returns:
- the duplicated buffer
-
memDuplicate
Duplicates the specified buffer.This method should be preferred over
LongBuffer.duplicate()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to duplicate- Returns:
- the duplicated buffer
-
memDuplicate
Duplicates the specified buffer.This method should be preferred over
FloatBuffer.duplicate()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to duplicate- Returns:
- the duplicated buffer
-
memDuplicate
Duplicates the specified buffer.This method should be preferred over
DoubleBuffer.duplicate()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to duplicate- Returns:
- the duplicated buffer
-
memSlice
Slices the specified buffer. The returned buffer will have the sameByteOrderas the source buffer.This method should be preferred over
ByteBuffer.slice()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to slice- Returns:
- the sliced buffer
-
memSlice
Slices the specified buffer.This method should be preferred over
ShortBuffer.slice()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to slice- Returns:
- the sliced buffer
-
memSlice
Slices the specified buffer.This method should be preferred over
CharBuffer.slice()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to slice- Returns:
- the sliced buffer
-
memSlice
Slices the specified buffer.This method should be preferred over
IntBuffer.slice()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to slice- Returns:
- the sliced buffer
-
memSlice
Slices the specified buffer.This method should be preferred over
LongBuffer.slice()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to slice- Returns:
- the sliced buffer
-
memSlice
Slices the specified buffer.This method should be preferred over
FloatBuffer.slice()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to slice- Returns:
- the sliced buffer
-
memSlice
Slices the specified buffer.This method should be preferred over
DoubleBuffer.slice()because it has a much shorter call chain. Long call chains may fail to inline due to JVM limits, disabling important optimizations (e.g. scalar replacement via Escape Analysis).- Parameters:
buffer- the buffer to slice- Returns:
- the sliced buffer
-
memSlice
Returns a slice of the specified buffer between(buffer.position() + offset)and(buffer.position() + offset + capacity). The returned buffer will have the sameByteOrderas 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
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
-
memSlice
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
-
memSlice
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
-
memSlice
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
-
memSlice
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
-
memSlice
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
-
memSlice
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
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)
-
memSet
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)
-
memSet
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)
-
memSet
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)
-
memSet
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)
-
memSet
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)
-
memSet
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)
-
memSet
Sets all bytes in a specified block of memory to a fixed value (usually zero).- Type Parameters:
T- the buffer type- Parameters:
ptr- the starting memory addressvalue- the value to set (memSet will convert it to unsigned byte)
-
memSet
Sets all bytes in a specified block of memory to a fixed value (usually zero).- Type Parameters:
T- the struct type- Parameters:
ptr- the starting memory addressvalue- the value to set (memSet will convert it to unsigned byte)
-
memCopy
Sets all bytes in a specified block of memory to a copy of another block.- Parameters:
src- the source memory addressdst- the destination memory address
-
memCopy
Sets all bytes in a specified block of memory to a copy of another block.- Parameters:
src- the source memory addressdst- the destination memory address
-
memCopy
Sets all bytes in a specified block of memory to a copy of another block.- Parameters:
src- the source memory addressdst- the destination memory address
-
memCopy
Sets all bytes in a specified block of memory to a copy of another block.- Parameters:
src- the source memory addressdst- the destination memory address
-
memCopy
Sets all bytes in a specified block of memory to a copy of another block.- Parameters:
src- the source memory addressdst- the destination memory address
-
memCopy
Sets all bytes in a specified block of memory to a copy of another block.- Parameters:
src- the source memory addressdst- the destination memory address
-
memCopy
Sets all bytes in a specified block of memory to a copy of another block.- Parameters:
src- the source memory addressdst- the destination memory address
-
memCopy
Sets all bytes in a specified block of memory to a copy of another block.- Type Parameters:
T- the buffer type- Parameters:
src- the source memory addressdst- the destination memory address
-
memCopy
Sets all bytes of a struct to a copy of another struct.- Type Parameters:
T- the struct type- Parameters:
src- the source structdst- the destination struct
-
memSet
public static void memSet(long ptr, int value, long 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, long 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) -
memGetCLong
public static long memGetCLong(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) -
memPutCLong
public static void memPutCLong(long ptr, long 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
-
memASCII
Returns a ByteBuffer containing the specified text ASCII encoded and null-terminated.- Parameters:
text- the text to encode- Returns:
- the encoded text. The returned buffer must be deallocated manually with
memFree(java.nio.Buffer). - Throws:
BufferOverflowException- if more thanInteger.MAX_VALUEbytes are required to encode the text.
-
memASCIISafe
-
memASCII
Returns aByteBuffercontaining the specified text ASCII encoded and optionally null-terminated.- Parameters:
text- the text to encodenullTerminated- if true, the text will be terminated with a '\0'.- Returns:
- the encoded text. The returned buffer must be deallocated manually with
memFree(java.nio.Buffer). - Throws:
BufferOverflowException- if more thanInteger.MAX_VALUEbytes are required to encode the text.
-
memASCIISafe
public static @Nullable ByteBuffer memASCIISafe(@Nullable CharSequence text, boolean nullTerminated) -
memASCII
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.- Parameters:
text- the text to encodenullTerminated- if true, the text will be terminated with a '\0'target- the buffer where the encoded text should be stored- Returns:
- the number of bytes of the encoded string
- Throws:
BufferOverflowException- if more thantarget.remaining()bytes are required to encode the text.
-
memASCII
public static int memASCII(CharSequence text, boolean nullTerminated, ByteBuffer target, int offset) Encodes and optionally null-terminates the specified text using ASCII encoding. The encoded text is stored in the specifiedByteBufferat the specifiedpositionoffset. The current buffer position is not modified by this operation.- 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
- Throws:
BufferOverflowException- if more thantarget.capacity() - offsetbytes are required to encode the text.
-
memLengthASCII
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
- Throws:
BufferOverflowException- if more thanInteger.MAX_VALUEbytes are required to encode the text.
-
memUTF8
Returns a ByteBuffer containing the specified text UTF-8 encoded and null-terminated.- Parameters:
text- the text to encode- Returns:
- the encoded text. The returned buffer must be deallocated manually with
memFree(java.nio.Buffer). - Throws:
BufferOverflowException- if more thanInteger.MAX_VALUEbytes are required to encode the text.
-
memUTF8Safe
-
memUTF8
Returns a ByteBuffer containing the specified text UTF-8 encoded and optionally null-terminated.- Parameters:
text- the text to encodenullTerminated- if true, the text will be terminated with a '\0'.- Returns:
- the encoded text. The returned buffer must be deallocated manually with
memFree(java.nio.Buffer). - Throws:
BufferOverflowException- if more thanInteger.MAX_VALUEbytes are required to encode the text.
-
memUTF8Safe
-
memUTF8
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.- 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
- Throws:
BufferOverflowException- if more thantarget.remainingbytes are required to encode the text.
-
memUTF8
Encodes and optionally null-terminates the specified text using UTF-8 encoding. The encoded text is stored in the specifiedByteBuffer, at the specifiedpositionoffset. The current buffer position is not modified by this operation.- 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
- Throws:
BufferOverflowException- if more thantarget.capacity() - offsetbytes are required to encode the text.
-
memLengthUTF8
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
- Throws:
BufferOverflowException- if more thanInteger.MAX_VALUEbytes are required to encode the text.
-
memUTF16
Returns a ByteBuffer containing the specified text UTF-16 encoded and null-terminated.- Parameters:
text- the text to encode- Returns:
- the encoded text. The returned buffer must be deallocated manually with
memFree(java.nio.Buffer). - Throws:
BufferOverflowException- if more thanInteger.MAX_VALUEbytes are required to encode the text.
-
memUTF16Safe
-
memUTF16
Returns a ByteBuffer containing the specified text UTF-16 encoded and optionally null-terminated.- Parameters:
text- the text to encodenullTerminated- if true, the text will be terminated with a '\0'.- Returns:
- the encoded text. The returned buffer must be deallocated manually with
memFree(java.nio.Buffer). - Throws:
BufferOverflowException- if more thanInteger.MAX_VALUEbytes are required to encode the text.
-
memUTF16Safe
public static @Nullable ByteBuffer memUTF16Safe(@Nullable CharSequence text, boolean nullTerminated) -
memUTF16
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. Thetargetbuffer 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
- Throws:
BufferOverflowException- if more thantarget.remaining()bytes are required to encode the text.
-
memUTF16
public static int memUTF16(CharSequence text, boolean nullTerminated, ByteBuffer target, int offset) Encodes and optionally null-terminates the specified text using UTF-16 encoding. The encoded text is stored in the specifiedByteBufferat the specifiedpositionoffset. The current buffer position is not modified by this operation. Thetargetbuffer 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
- Throws:
BufferOverflowException- if more thantarget.capacity() - offsetbytes are required to encode the text.
-
memLengthUTF16
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
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
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
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
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 ofmaxLengthbytes. 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
-
memByteBufferNT1Safe
-
memByteBufferNT1Safe
-
memByteBufferNT2
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
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 ofmaxLengthbytes. 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
-
memByteBufferNT2Safe
-
memByteBufferNT2Safe
-
memASCII
Converts the null-terminated ASCII encoded string at the specified memory address to aString.- Parameters:
address- the string memory address- Returns:
- the decoded
String
-
memASCII
Converts the ASCII encoded string at the specified memory address to aString.- Parameters:
address- the string memory addresslength- the number of bytes to decode- Returns:
- the decoded
String
-
memASCII
Decodes the bytes with index[position(), position()+remaining()) inbuffer, as an ASCII string.The current
positionandlimitof the specifiedbufferare not affected by this operation.- Parameters:
buffer- theByteBufferto decode- Returns:
- the decoded
String
-
memASCIISafe
-
memASCIISafe
-
memASCIISafe
-
memASCII
Decodes the bytes with index[position(), position()+length) inbuffer, as an ASCII string.The current
positionandlimitof the specifiedbufferare not affected by this operation.- Parameters:
buffer- theByteBufferto decodelength- the number of bytes to decode- Returns:
- the decoded
String
-
memASCII
Decodes the bytes with index[offset, offset+length) inbuffer, as an ASCII string.The current
positionandlimitof the specifiedbufferare not affected by this operation.- Parameters:
buffer- theByteBufferto decodelength- the number of bytes to decodeoffset- the offset at which to start decoding.- Returns:
- the decoded
String
-
memUTF8
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
-
memUTF8
Converts the UTF-8 encoded string at the specified memory address to aString.- Parameters:
address- the string memory addresslength- the number of bytes to decode- Returns:
- the decoded
String
-
memUTF8
Decodes the bytes with index[position(), position()+remaining()) inbuffer, as a UTF-8 string.The current
positionandlimitof the specifiedbufferare not affected by this operation.- Parameters:
buffer- theByteBufferto decode- Returns:
- the decoded
String
-
memUTF8Safe
-
memUTF8Safe
-
memUTF8Safe
-
memUTF8
Decodes the bytes with index[position(), position()+length) inbuffer, as a UTF-8 string.The current
positionandlimitof the specifiedbufferare not affected by this operation.- Parameters:
buffer- theByteBufferto decodelength- the number of bytes to decode- Returns:
- the decoded
String
-
memUTF8
Decodes the bytes with index[offset, offset+length) inbuffer, as a UTF-8 string.The current
positionandlimitof the specifiedbufferare not affected by this operation.- Parameters:
buffer- theByteBufferto decodelength- the number of bytes to decodeoffset- the offset at which to start decoding.- Returns:
- the decoded
String
-
memUTF16
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
-
memUTF16
Converts the UTF-16 encoded string at the specified memory address to aString.- Parameters:
address- the string memory addresslength- the number of characters to decode- Returns:
- the decoded
String
-
memUTF16
Decodes the bytes with index[position(), position()+remaining()) inbuffer, as a UTF-16 string.The current
positionandlimitof the specifiedbufferare not affected by this operation.- Parameters:
buffer- theByteBufferto decode- Returns:
- the decoded
String
-
memUTF16Safe
-
memUTF16Safe
-
memUTF16Safe
-
memUTF16
Decodes the bytes with index[position(), position()+(length*2)) inbuffer, as a UTF-16 string.The current
positionandlimitof the specifiedbufferare not affected by this operation.- Parameters:
buffer- theByteBufferto decodelength- the number of characters to decode- Returns:
- the decoded
String
-
memUTF16
Decodes the bytes with index[offset, offset+(length*2)) inbuffer, as a UTF-16 string.The current
positionandlimitof the specifiedbufferare not affected by this operation.- Parameters:
buffer- theByteBufferto decodelength- the number of characters to decodeoffset- the offset at which to start decoding, in bytes.- Returns:
- the decoded
String
-