Class RPmalloc
- java.lang.Object
-
- org.lwjgl.system.rpmalloc.RPmalloc
-
public class RPmalloc extends java.lang.Object
Native 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
/free
and the other malloc style replacement functions. Remember all allocations are 16-byte aligned, so no need to call the explicitmemalign
/aligned_alloc
/posix_memalign
functions 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=1
andENABLE_STATISTICS=0
.
-
-
Field Summary
Fields Modifier and Type Field Description static int
RPMALLOC_NO_PRESERVE
Flag toaligned_realloc
to not preserve content in reallocation.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
nrpaligned_alloc(long alignment, long size)
Unsafe version of:aligned_alloc
static long
nrpaligned_realloc(long ptr, long alignment, long size, long oldsize, int flags)
Unsafe version of:aligned_realloc
static long
nrpcalloc(long num, long size)
Unsafe version of:calloc
static void
nrpfree(long ptr)
Unsafe version of:free
static long
nrpmalloc(long size)
Unsafe version of:malloc
static long
nrpmalloc_config()
Unsafe version of:malloc_config
static void
nrpmalloc_global_statistics(long stats)
Unsafe version of:malloc_global_statistics
static int
nrpmalloc_initialize()
Unsafe version of:malloc_initialize
static int
nrpmalloc_initialize_config(long config)
Unsafe version of:malloc_initialize_config
static int
nrpmalloc_is_thread_initialized()
Unsafe version of:malloc_is_thread_initialized
static void
nrpmalloc_thread_statistics(long stats)
Unsafe version of:malloc_thread_statistics
static long
nrpmalloc_usable_size(long ptr)
Unsafe version of:malloc_usable_size
static long
nrpmemalign(long alignment, long size)
Unsafe version of:memalign
static int
nrpposix_memalign(long memptr, long alignment, long size)
Unsafe version of:posix_memalign
static long
nrprealloc(long ptr, long size)
Unsafe version of:realloc
static java.nio.ByteBuffer
rpaligned_alloc(long alignment, long size)
Allocates a memory block of at least the given size and alignment.static java.nio.ByteBuffer
rpaligned_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.ByteBuffer
rpcalloc(long num, long size)
Allocates a memory block of at least the given size and zero initializes it.static void
rpfree(java.nio.ByteBuffer ptr)
Frees the given memory block.static void
rpfree(java.nio.DoubleBuffer ptr)
Frees the given memory block.static void
rpfree(java.nio.FloatBuffer ptr)
Frees the given memory block.static void
rpfree(java.nio.IntBuffer ptr)
Frees the given memory block.static void
rpfree(java.nio.LongBuffer ptr)
Frees the given memory block.static void
rpfree(java.nio.ShortBuffer ptr)
Frees the given memory block.static void
rpfree(org.lwjgl.PointerBuffer ptr)
Frees the given memory block.static java.nio.ByteBuffer
rpmalloc(long size)
Allocates a memory block of at least the given size.static RPMallocConfig
rpmalloc_config()
Gets allocator configuration.static void
rpmalloc_finalize()
Finalizes allocator.static void
rpmalloc_global_statistics(RPmallocGlobalStatistics stats)
Gets global statistics.static boolean
rpmalloc_initialize()
Initializes allocator with default configuration.static boolean
rpmalloc_initialize_config(RPMallocConfig config)
Initializes allocator with given configuration.static boolean
rpmalloc_is_thread_initialized()
Query if allocator is initialized for calling thread.static void
rpmalloc_thread_collect()
Performs deferred deallocations pending for the calling thread heap.static void
rpmalloc_thread_finalize()
Finalizes allocator for calling thread.static void
rpmalloc_thread_initialize()
Initializes allocator for calling thread.static void
rpmalloc_thread_statistics(RPmallocThreadStatistics stats)
Gets per-thread statistics.static long
rpmalloc_usable_size(java.nio.ByteBuffer ptr)
Queries the usable size of the given memory block (from given pointer to the end of block).static long
rpmalloc_usable_size(java.nio.DoubleBuffer ptr)
Queries the usable size of the given memory block (from given pointer to the end of block).static long
rpmalloc_usable_size(java.nio.FloatBuffer ptr)
Queries the usable size of the given memory block (from given pointer to the end of block).static long
rpmalloc_usable_size(java.nio.IntBuffer ptr)
Queries the usable size of the given memory block (from given pointer to the end of block).static long
rpmalloc_usable_size(java.nio.LongBuffer ptr)
Queries the usable size of the given memory block (from given pointer to the end of block).static long
rpmalloc_usable_size(java.nio.ShortBuffer ptr)
Queries the usable size of the given memory block (from given pointer to the end of block).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).static java.nio.ByteBuffer
rpmemalign(long alignment, long size)
Allocates a memory block of at least the given size and alignment.static int
rpposix_memalign(org.lwjgl.PointerBuffer memptr, long alignment, long size)
Allocates a memory block of at least the given size and alignment.static java.nio.ByteBuffer
rprealloc(java.nio.ByteBuffer ptr, long size)
Reallocates the given block to at least the given size.
-
-
-
Field Detail
-
RPMALLOC_NO_PRESERVE
Flag toaligned_realloc
to not preserve content in reallocation.
-
-
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 optionalRPMallocConfig
instance
-
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.
-
rpmalloc_thread_finalize
public static void rpmalloc_thread_finalize()
Finalizes allocator for calling thread.
-
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
).
-
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
).
-
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).
-
-