Class 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:

    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 explicit memalign/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 and ENABLE_STATISTICS=0.

    • 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 optional RPMallocConfig 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.
      • 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.
      • 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 (see MALLOC_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 (default 64KiB).

        Parameters:
        flags - MALLOC_NO_PRESERVE MALLOC_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 (default 64KiB).

      • 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 (default 64KiB).

      • 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 (default 64KiB).

      • 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 (default 64KiB).

      • 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, use malloc_heap_free_all before 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 (default 64KiB).

      • 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 (default 64KiB).

        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_PRESERVE MALLOC_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 (default 64KiB).

        Parameters:
        flags - MALLOC_NO_PRESERVE MALLOC_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.