Class RPmalloc
- java.lang.Object
-
- org.lwjgl.system.rpmalloc.RPmalloc
-
public class RPmalloc extends java.lang.ObjectNative bindings to the rpmalloc library. rpmalloc is a public domain cross platform lock free thread caching 16-byte aligned memory allocator implemented in C.You are required to call these functions from your own code in order to initialize and finalize the allocator in your process and threads:
malloc_initialize: Call at process start to initialize the allocatormalloc_finalize: Call at process exit to finalize the allocatormalloc_thread_initialize: Call at each thread start to initialize the thread local data for the allocatormalloc_thread_finalize: Call at each thread exit to finalize and release thread cache back to global cache
Then simply use the
malloc/freeand the other malloc style replacement functions. Remember all allocations are 16-byte aligned, so no need to call the explicitmemalign/aligned_alloc/posix_memalignfunctions unless you need greater alignment, they are simply wrappers to make it easier to replace in existing code.The rpmalloc build in LWJGL is configured with
RPMALLOC_CONFIGURABLE=1andENABLE_STATISTICS=0.
-
-
Field Summary
Fields Modifier and Type Field Description static intRPMALLOC_GROW_OR_FAILFlag toaligned_reallocto fail and return null pointer if grow cannot be done in-place, in which case the original pointer is still valid (just like a call to realloc which failes to allocate a new block).static intRPMALLOC_NO_PRESERVEFlag toaligned_reallocto not preserve content in reallocation.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static longnrpaligned_alloc(long alignment, long size)Unsafe version of:aligned_allocstatic longnrpaligned_calloc(long alignment, long num, long size)Unsafe version of:aligned_callocstatic longnrpaligned_realloc(long ptr, long alignment, long size, long oldsize, int flags)Unsafe version of:aligned_reallocstatic longnrpcalloc(long num, long size)Unsafe version of:callocstatic voidnrpfree(long ptr)Unsafe version of:freestatic longnrpmalloc(long size)Unsafe version of:mallocstatic longnrpmalloc_config()Unsafe version of:malloc_configstatic voidnrpmalloc_global_statistics(long stats)Unsafe version of:malloc_global_statisticsstatic longnrpmalloc_heap_aligned_alloc(long heap, long alignment, long size)Unsafe version of:malloc_heap_aligned_allocstatic longnrpmalloc_heap_aligned_calloc(long heap, long alignment, long num, long size)Unsafe version of:malloc_heap_aligned_callocstatic longnrpmalloc_heap_aligned_realloc(long heap, long ptr, long alignment, long size, int flags)Unsafe version of:malloc_heap_aligned_reallocstatic longnrpmalloc_heap_alloc(long heap, long size)Unsafe version of:malloc_heap_allocstatic longnrpmalloc_heap_calloc(long heap, long num, long size)Unsafe version of:malloc_heap_callocstatic voidnrpmalloc_heap_free(long heap, long ptr)Unsafe version of:malloc_heap_freestatic voidnrpmalloc_heap_free_all(long heap)Unsafe version of:malloc_heap_free_allstatic longnrpmalloc_heap_realloc(long heap, long ptr, long size, int flags)Unsafe version of:malloc_heap_reallocstatic intnrpmalloc_initialize()Unsafe version of:malloc_initializestatic intnrpmalloc_initialize_config(long config)Unsafe version of:malloc_initialize_configstatic intnrpmalloc_is_thread_initialized()Unsafe version of:malloc_is_thread_initializedstatic voidnrpmalloc_thread_finalize(int release_caches)Unsafe version of:malloc_thread_finalizestatic voidnrpmalloc_thread_statistics(long stats)Unsafe version of:malloc_thread_statisticsstatic longnrpmalloc_usable_size(long ptr)Unsafe version of:malloc_usable_sizestatic longnrpmemalign(long alignment, long size)Unsafe version of:memalignstatic intnrpposix_memalign(long memptr, long alignment, long size)Unsafe version of:posix_memalignstatic longnrprealloc(long ptr, long size)Unsafe version of:reallocstatic java.nio.ByteBufferrpaligned_alloc(long alignment, long size)Allocates a memory block of at least the given size and alignment.static java.nio.ByteBufferrpaligned_calloc(long alignment, long num, long size)Allocates a memory block of at least the given size and alignment, and zero initializes it.static java.nio.ByteBufferrpaligned_realloc(java.nio.ByteBuffer ptr, long alignment, long size, int flags)Reallocates the given block to at least the given size and alignment, with optional control flags (seeMALLOC_NO_PRESERVE).static java.nio.ByteBufferrpcalloc(long num, long size)Allocates a memory block of at least the given size and zero initializes it.static voidrpfree(java.nio.ByteBuffer ptr)Frees the given memory block.static voidrpfree(java.nio.DoubleBuffer ptr)Frees the given memory block.static voidrpfree(java.nio.FloatBuffer ptr)Frees the given memory block.static voidrpfree(java.nio.IntBuffer ptr)Frees the given memory block.static voidrpfree(java.nio.LongBuffer ptr)Frees the given memory block.static voidrpfree(java.nio.ShortBuffer ptr)Frees the given memory block.static voidrpfree(org.lwjgl.PointerBuffer ptr)Frees the given memory block.static java.nio.ByteBufferrpmalloc(long size)Allocates a memory block of at least the given size.static RPMallocConfigrpmalloc_config()Gets allocator configuration.static voidrpmalloc_finalize()Finalizes allocator.static voidrpmalloc_global_statistics(RPmallocGlobalStatistics stats)Gets global statistics.static longrpmalloc_heap_acquire()Acquires a new heap.static java.nio.ByteBufferrpmalloc_heap_aligned_alloc(long heap, long alignment, long size)Allocates a memory block of at least the given size using the given heap.static java.nio.ByteBufferrpmalloc_heap_aligned_calloc(long heap, long alignment, long num, long size)Allocates a memory block of at least the given size using the given heap and zero initializes it.static java.nio.ByteBufferrpmalloc_heap_aligned_realloc(long heap, java.nio.ByteBuffer ptr, long alignment, long size, int flags)Reallocates the given block to at least the given size.static java.nio.ByteBufferrpmalloc_heap_alloc(long heap, long size)Allocates a memory block of at least the given size using the given heap.static java.nio.ByteBufferrpmalloc_heap_calloc(long heap, long num, long size)Allocates a memory block of at least the given size using the given heap and zero initializes it.static voidrpmalloc_heap_free(long heap, java.nio.ByteBuffer ptr)Frees the given memory block from the given heap.static voidrpmalloc_heap_free(long heap, java.nio.DoubleBuffer ptr)Frees the given memory block from the given heap.static voidrpmalloc_heap_free(long heap, java.nio.FloatBuffer ptr)Frees the given memory block from the given heap.static voidrpmalloc_heap_free(long heap, java.nio.IntBuffer ptr)Frees the given memory block from the given heap.static voidrpmalloc_heap_free(long heap, java.nio.LongBuffer ptr)Frees the given memory block from the given heap.static voidrpmalloc_heap_free(long heap, java.nio.ShortBuffer ptr)Frees the given memory block from the given heap.static voidrpmalloc_heap_free(long heap, org.lwjgl.PointerBuffer ptr)Frees the given memory block from the given heap.static voidrpmalloc_heap_free_all(long heap)Frees all memory allocated by the heap.static java.nio.ByteBufferrpmalloc_heap_realloc(long heap, java.nio.ByteBuffer ptr, long size, int flags)Reallocates the given block to at least the given size.static voidrpmalloc_heap_release(long heap)Releases a heap (does NOT free the memory allocated by the heap, usemalloc_heap_free_allbefore destroying the heap).static voidrpmalloc_heap_thread_set_current(long heap)Sets the given heap as the current heap for the calling thread.static booleanrpmalloc_initialize()Initializes allocator with default configuration.static booleanrpmalloc_initialize_config(RPMallocConfig config)Initializes allocator with given configuration.static booleanrpmalloc_is_thread_initialized()Query if allocator is initialized for calling thread.static voidrpmalloc_thread_collect()Performs deferred deallocations pending for the calling thread heap.static voidrpmalloc_thread_finalize(boolean release_caches)Finalizes allocator for calling thread.static voidrpmalloc_thread_initialize()Initializes allocator for calling thread.static voidrpmalloc_thread_statistics(RPmallocThreadStatistics stats)Gets per-thread statistics.static longrpmalloc_usable_size(java.nio.ByteBuffer ptr)Queries the usable size of the given memory block (from given pointer to the end of block).static longrpmalloc_usable_size(java.nio.DoubleBuffer ptr)Queries the usable size of the given memory block (from given pointer to the end of block).static longrpmalloc_usable_size(java.nio.FloatBuffer ptr)Queries the usable size of the given memory block (from given pointer to the end of block).static longrpmalloc_usable_size(java.nio.IntBuffer ptr)Queries the usable size of the given memory block (from given pointer to the end of block).static longrpmalloc_usable_size(java.nio.LongBuffer ptr)Queries the usable size of the given memory block (from given pointer to the end of block).static longrpmalloc_usable_size(java.nio.ShortBuffer ptr)Queries the usable size of the given memory block (from given pointer to the end of block).static longrpmalloc_usable_size(org.lwjgl.PointerBuffer ptr)Queries the usable size of the given memory block (from given pointer to the end of block).static java.nio.ByteBufferrpmemalign(long alignment, long size)Allocates a memory block of at least the given size and alignment.static intrpposix_memalign(org.lwjgl.PointerBuffer memptr, long alignment, long size)Allocates a memory block of at least the given size and alignment.static java.nio.ByteBufferrprealloc(java.nio.ByteBuffer ptr, long size)Reallocates the given block to at least the given size.
-
-
-
Field Detail
-
RPMALLOC_NO_PRESERVE
Flag toaligned_reallocto not preserve content in reallocation.
-
RPMALLOC_GROW_OR_FAIL
Flag toaligned_reallocto fail and return null pointer if grow cannot be done in-place, in which case the original pointer is still valid (just like a call to realloc which failes to allocate a new block).
-
-
Method Detail
-
nrpmalloc_initialize
public static int nrpmalloc_initialize()
Unsafe version of:malloc_initialize
-
rpmalloc_initialize
public static boolean rpmalloc_initialize()
Initializes allocator with default configuration.
-
nrpmalloc_initialize_config
public static int nrpmalloc_initialize_config(long config)
Unsafe version of:malloc_initialize_config
-
rpmalloc_initialize_config
public static boolean rpmalloc_initialize_config(@Nullable RPMallocConfig config)Initializes allocator with given configuration.- Parameters:
config- an optionalRPMallocConfiginstance
-
nrpmalloc_config
public static long nrpmalloc_config()
Unsafe version of:malloc_config
-
rpmalloc_config
public static RPMallocConfig rpmalloc_config()
Gets allocator configuration.
-
rpmalloc_finalize
public static void rpmalloc_finalize()
Finalizes allocator.
-
rpmalloc_thread_initialize
public static void rpmalloc_thread_initialize()
Initializes allocator for calling thread.
-
nrpmalloc_thread_finalize
public static void nrpmalloc_thread_finalize(int release_caches)
Unsafe version of:malloc_thread_finalize
-
rpmalloc_thread_finalize
public static void rpmalloc_thread_finalize(boolean release_caches)
Finalizes allocator for calling thread.- Parameters:
release_caches- pass non-zero to release thread caches to global cache
-
rpmalloc_thread_collect
public static void rpmalloc_thread_collect()
Performs deferred deallocations pending for the calling thread heap.
-
nrpmalloc_is_thread_initialized
public static int nrpmalloc_is_thread_initialized()
Unsafe version of:malloc_is_thread_initialized
-
rpmalloc_is_thread_initialized
public static boolean rpmalloc_is_thread_initialized()
Query if allocator is initialized for calling thread.
-
nrpmalloc_thread_statistics
public static void nrpmalloc_thread_statistics(long stats)
Unsafe version of:malloc_thread_statistics
-
rpmalloc_thread_statistics
public static void rpmalloc_thread_statistics(RPmallocThreadStatistics stats)
Gets per-thread statistics.
-
nrpmalloc_global_statistics
public static void nrpmalloc_global_statistics(long stats)
Unsafe version of:malloc_global_statistics
-
rpmalloc_global_statistics
public static void rpmalloc_global_statistics(RPmallocGlobalStatistics stats)
Gets global statistics.
-
nrpmalloc
public static long nrpmalloc(long size)
Unsafe version of:malloc
-
rpmalloc
@Nullable public static java.nio.ByteBuffer rpmalloc(long size)
Allocates a memory block of at least the given size.
-
nrpfree
public static void nrpfree(long ptr)
Unsafe version of:free
-
rpfree
public static void rpfree(@Nullable java.nio.ByteBuffer ptr) public static void rpfree(@Nullable java.nio.ShortBuffer ptr) public static void rpfree(@Nullable java.nio.IntBuffer ptr) public static void rpfree(@Nullable java.nio.LongBuffer ptr) public static void rpfree(@Nullable java.nio.FloatBuffer ptr) public static void rpfree(@Nullable java.nio.DoubleBuffer ptr) public static void rpfree(@Nullable org.lwjgl.PointerBuffer ptr)Frees the given memory block.
-
nrpcalloc
public static long nrpcalloc(long num, long size)Unsafe version of:calloc
-
rpcalloc
@Nullable public static java.nio.ByteBuffer rpcalloc(long num, long size)Allocates a memory block of at least the given size and zero initializes it.
-
nrprealloc
public static long nrprealloc(long ptr, long size)Unsafe version of:realloc
-
rprealloc
@Nullable public static java.nio.ByteBuffer rprealloc(@Nullable java.nio.ByteBuffer ptr, long size)Reallocates the given block to at least the given size.
-
nrpaligned_realloc
public static long nrpaligned_realloc(long ptr, long alignment, long size, long oldsize, int flags)Unsafe version of:aligned_realloc
-
rpaligned_realloc
@Nullable public static java.nio.ByteBuffer rpaligned_realloc(@Nullable java.nio.ByteBuffer ptr, long alignment, long size, int flags)Reallocates the given block to at least the given size and alignment, with optional control flags (seeMALLOC_NO_PRESERVE).Alignment must be a power of two and a multiple of
sizeof(void*), and should ideally be less than memory page size. A caveat of rpmalloc internals is that this must also be strictly less than the span size (default64KiB).- Parameters:
flags-MALLOC_NO_PRESERVEMALLOC_GROW_OR_FAIL
-
nrpaligned_alloc
public static long nrpaligned_alloc(long alignment, long size)Unsafe version of:aligned_alloc
-
rpaligned_alloc
@Nullable public static java.nio.ByteBuffer rpaligned_alloc(long alignment, long size)Allocates a memory block of at least the given size and alignment.Alignment must be a power of two and a multiple of
sizeof(void*), and should ideally be less than memory page size. A caveat of rpmalloc internals is that this must also be strictly less than the span size (default64KiB).
-
nrpaligned_calloc
public static long nrpaligned_calloc(long alignment, long num, long size)Unsafe version of:aligned_calloc
-
rpaligned_calloc
@Nullable public static java.nio.ByteBuffer rpaligned_calloc(long alignment, long num, long size)Allocates a memory block of at least the given size and alignment, and zero initializes it.Alignment must be a power of two and a multiple of
sizeof(void*), and should ideally be less than memory page size. A caveat of rpmalloc internals is that this must also be strictly less than the span size (default64KiB).
-
nrpmemalign
public static long nrpmemalign(long alignment, long size)Unsafe version of:memalign
-
rpmemalign
@Nullable public static java.nio.ByteBuffer rpmemalign(long alignment, long size)Allocates a memory block of at least the given size and alignment.Alignment must be a power of two and a multiple of
sizeof(void*), and should ideally be less than memory page size. A caveat of rpmalloc internals is that this must also be strictly less than the span size (default64KiB).
-
nrpposix_memalign
public static int nrpposix_memalign(long memptr, long alignment, long size)Unsafe version of:posix_memalign
-
rpposix_memalign
public static int rpposix_memalign(org.lwjgl.PointerBuffer memptr, long alignment, long size)Allocates a memory block of at least the given size and alignment.Alignment must be a power of two and a multiple of
sizeof(void*), and should ideally be less than memory page size. A caveat of rpmalloc internals is that this must also be strictly less than the span size (default64KiB).
-
nrpmalloc_usable_size
public static long nrpmalloc_usable_size(long ptr)
Unsafe version of:malloc_usable_size
-
rpmalloc_usable_size
public static long rpmalloc_usable_size(java.nio.ByteBuffer ptr) public static long rpmalloc_usable_size(java.nio.ShortBuffer ptr) public static long rpmalloc_usable_size(java.nio.IntBuffer ptr) public static long rpmalloc_usable_size(java.nio.LongBuffer ptr) public static long rpmalloc_usable_size(java.nio.FloatBuffer ptr) public static long rpmalloc_usable_size(java.nio.DoubleBuffer ptr) public static long rpmalloc_usable_size(org.lwjgl.PointerBuffer ptr)
Queries the usable size of the given memory block (from given pointer to the end of block).
-
rpmalloc_heap_acquire
public static long rpmalloc_heap_acquire()
Acquires a new heap.Will reuse existing released heaps or allocate memory for a new heap if none available. Heap API is implemented with the strict assumption that only one single thread will call heap functions for a given heap at any given time, no functions are thread safe.
-
rpmalloc_heap_release
public static void rpmalloc_heap_release(long heap)
Releases a heap (does NOT free the memory allocated by the heap, usemalloc_heap_free_allbefore destroying the heap).Releasing a heap will enable it to be reused by other threads. Safe to pass a null pointer.
-
nrpmalloc_heap_alloc
public static long nrpmalloc_heap_alloc(long heap, long size)Unsafe version of:malloc_heap_alloc
-
rpmalloc_heap_alloc
@Nullable public static java.nio.ByteBuffer rpmalloc_heap_alloc(long heap, long size)Allocates a memory block of at least the given size using the given heap.
-
nrpmalloc_heap_aligned_alloc
public static long nrpmalloc_heap_aligned_alloc(long heap, long alignment, long size)Unsafe version of:malloc_heap_aligned_alloc
-
rpmalloc_heap_aligned_alloc
@Nullable public static java.nio.ByteBuffer rpmalloc_heap_aligned_alloc(long heap, long alignment, long size)Allocates a memory block of at least the given size using the given heap.The returned block will have the requested alignment. Alignment must be a power of two and a multiple of
sizeof(void*), and should ideally be less than memory page size. A caveat of rpmalloc internals is that this must also be strictly less than the span size (default64KiB).
-
nrpmalloc_heap_calloc
public static long nrpmalloc_heap_calloc(long heap, long num, long size)Unsafe version of:malloc_heap_calloc
-
rpmalloc_heap_calloc
@Nullable public static java.nio.ByteBuffer rpmalloc_heap_calloc(long heap, long num, long size)Allocates a memory block of at least the given size using the given heap and zero initializes it.
-
nrpmalloc_heap_aligned_calloc
public static long nrpmalloc_heap_aligned_calloc(long heap, long alignment, long num, long size)Unsafe version of:malloc_heap_aligned_calloc
-
rpmalloc_heap_aligned_calloc
@Nullable public static java.nio.ByteBuffer rpmalloc_heap_aligned_calloc(long heap, long alignment, long num, long size)Allocates a memory block of at least the given size using the given heap and zero initializes it.The returned block will have the requested alignment. Alignment must either be zero, or a power of two and a multiple of
sizeof(void*), and should ideally be less than memory page size. A caveat of rpmalloc internals is that this must also be strictly less than the span size (default64KiB).- Parameters:
num- '
-
nrpmalloc_heap_realloc
public static long nrpmalloc_heap_realloc(long heap, long ptr, long size, int flags)Unsafe version of:malloc_heap_realloc
-
rpmalloc_heap_realloc
@Nullable public static java.nio.ByteBuffer rpmalloc_heap_realloc(long heap, @Nullable java.nio.ByteBuffer ptr, long size, int flags)Reallocates the given block to at least the given size.The memory block MUST be allocated by the same heap given to this function.
- Parameters:
flags-MALLOC_NO_PRESERVEMALLOC_GROW_OR_FAIL
-
nrpmalloc_heap_aligned_realloc
public static long nrpmalloc_heap_aligned_realloc(long heap, long ptr, long alignment, long size, int flags)Unsafe version of:malloc_heap_aligned_realloc
-
rpmalloc_heap_aligned_realloc
@Nullable public static java.nio.ByteBuffer rpmalloc_heap_aligned_realloc(long heap, @Nullable java.nio.ByteBuffer ptr, long alignment, long size, int flags)Reallocates the given block to at least the given size.The memory block MUST be allocated by the same heap given to this function. The returned block will have the requested alignment. Alignment must be either zero, or a power of two and a multiple of
sizeof(void*), and should ideally be less than memory page size. A caveat of rpmalloc internals is that this must also be strictly less than the span size (default64KiB).- Parameters:
flags-MALLOC_NO_PRESERVEMALLOC_GROW_OR_FAIL
-
nrpmalloc_heap_free
public static void nrpmalloc_heap_free(long heap, long ptr)Unsafe version of:malloc_heap_free
-
rpmalloc_heap_free
public static void rpmalloc_heap_free(long heap, @Nullable java.nio.ByteBuffer ptr) public static void rpmalloc_heap_free(long heap, @Nullable java.nio.ShortBuffer ptr) public static void rpmalloc_heap_free(long heap, @Nullable java.nio.IntBuffer ptr) public static void rpmalloc_heap_free(long heap, @Nullable java.nio.LongBuffer ptr) public static void rpmalloc_heap_free(long heap, @Nullable java.nio.FloatBuffer ptr) public static void rpmalloc_heap_free(long heap, @Nullable java.nio.DoubleBuffer ptr) public static void rpmalloc_heap_free(long heap, @Nullable org.lwjgl.PointerBuffer ptr)Frees the given memory block from the given heap.The memory block MUST be allocated by the same heap given to this function.
-
nrpmalloc_heap_free_all
public static void nrpmalloc_heap_free_all(long heap)
Unsafe version of:malloc_heap_free_all
-
rpmalloc_heap_free_all
public static void rpmalloc_heap_free_all(long heap)
Frees all memory allocated by the heap.
-
rpmalloc_heap_thread_set_current
public static void rpmalloc_heap_thread_set_current(long heap)
Sets the given heap as the current heap for the calling thread.A heap MUST only be current heap for a single thread, a heap can never be shared between multiple threads. The previous current heap for the calling thread is released to be reused by other threads.
-
-