feat: upgrade rpmalloc to v2.0.1

Signed-off-by: sisungo <[email protected]>
This commit is contained in:
sisungo committed 2026-11-18 06:12:34 +00:00
1 parent cc7da21f74
commit 16c5d70c9a
3 files changed
+3098 -3103

No files matched your search

+12 -13
View File
@@ -1,12 +1,7 @@
#include "rpmalloc.h" #include "rpmalloc.h"
#if (defined(__GNUC__) && __GNUC__ >= 9)
#pragma GCC diagnostic ignored "-Wattributes" // or we get warnings that nodiscard is ignored on a forward #define hidden_alias(fun) __attribute__((alias(#fun), used, visibility("hidden")));
#define hidden_alias(fun) __attribute__((alias(#fun), used, visibility("hidden"), copy(fun))); #define public_alias(fun) __attribute__((alias(#fun), used, visibility("default"), copy(fun)));
#define public_alias(fun) __attribute__((alias(#fun), used, visibility("default"), copy(fun)));
#else
#define hidden_alias(fun) __attribute__((alias(#fun), used, visibility("hidden")));
#define public_alias(fun) __attribute__((alias(#fun), used, visibility("default"), copy(fun)));
#endif
void* __libc_malloc_impl(size_t) hidden_alias(rpmalloc) void* __libc_malloc_impl(size_t) hidden_alias(rpmalloc)
void* __libc_realloc(void*, size_t) hidden_alias(rprealloc) void* __libc_realloc(void*, size_t) hidden_alias(rprealloc)
@@ -18,8 +13,8 @@ hidden void __malloc_atfork(int who) {}
hidden void __malloc_donate(char *start, char *end) {} hidden void __malloc_donate(char *start, char *end) {}
hidden void __malloc_process_init() { hidden void __malloc_process_init() {
rpmalloc_set_main_thread(); global_main_thread_id = get_thread_id();
if (rpmalloc_initialize() < 0) { if (rpmalloc_initialize(0) < 0) {
abort(); abort();
} }
} }
@@ -27,7 +22,7 @@ hidden void __malloc_thread_init() {
rpmalloc_thread_initialize(); rpmalloc_thread_initialize();
} }
hidden void __malloc_thread_finalize() { hidden void __malloc_thread_finalize() {
rpmalloc_thread_finalize(1); rpmalloc_thread_finalize();
} }
#include "pthread_impl.h" #include "pthread_impl.h"
@@ -42,6 +37,10 @@ static inline uintptr_t get_thread_id(void) {
} }
static void set_thread_heap(heap_t* heap) { static void set_thread_heap(heap_t* heap) {
__pthread_self()->theap = (void *)heap; if (heap && (heap->id != 0)) {
if (heap) heap->owner_thread = get_thread_id(); rpmalloc_assert(heap->id != 0, "Default heap being used");
heap->owner_thread = get_thread_id();
}
__pthread_self()->theap = (void*)heap;
} }
File diff suppressed because it is too large. Load diff
+209 -153
View File
@@ -1,117 +1,128 @@
/* rpmalloc.h - Memory allocator - Public Domain - 2016 Mattias Jansson /* rpmalloc.h - Memory allocator - 2016-2024 Mattias Jansson
* *
* This library provides a cross-platform lock free thread caching malloc implementation in C11. * SPDX-FileCopyrightText: 2016-2024 Mattias Jansson
* The latest source code is always available at * SPDX-License-Identifier: Unlicense OR MIT
*
* This library provides a cross-platform lock free thread caching malloc
* implementation in C11. The latest source code is always available at
* *
* https://github.com/mjansson/rpmalloc * https://github.com/mjansson/rpmalloc
* *
* This library is put in the public domain; you can redistribute it and/or modify it without any restrictions. * This library is put in the public domain; you can redistribute it and/or
* modify it without any restrictions. Or, if you choose, you can use it under
* the MIT license.
* *
*/ */
#pragma once #pragma once
//! rpmalloc version. RPMALLOC_VERSION is a human-readable string and may carry a pre-release
// suffix such as "-rc1". RPMALLOC_VERSION_NUMBER is a monotonic integer for comparisons,
// computed as major*10000 + minor*100 + patch (pre-release suffixes are not encoded).
#define RPMALLOC_VERSION "2.0.1"
#define RPMALLOC_VERSION_MAJOR 2
#define RPMALLOC_VERSION_MINOR 0
#define RPMALLOC_VERSION_PATCH 1
#define RPMALLOC_VERSION_NUMBER \
(RPMALLOC_VERSION_MAJOR * 10000 + RPMALLOC_VERSION_MINOR * 100 + RPMALLOC_VERSION_PATCH)
#include <stddef.h> #include <stddef.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#define RPMALLOC_CACHE_LINE_SIZE 64
#if defined(__clang__) || defined(__GNUC__) #if defined(__clang__) || defined(__GNUC__)
# define RPMALLOC_EXPORT static #define RPMALLOC_EXPORT static
# define RPMALLOC_ALLOCATOR #define RPMALLOC_RESTRICT __restrict
# if (defined(__clang_major__) && (__clang_major__ < 4)) || (defined(__GNUC__) && defined(ENABLE_PRELOAD) && ENABLE_PRELOAD) #define RPMALLOC_ALLOCATOR
# define RPMALLOC_ATTRIB_MALLOC #define RPMALLOC_CACHE_ALIGNED __attribute__((aligned(RPMALLOC_CACHE_LINE_SIZE)))
# define RPMALLOC_ATTRIB_ALLOC_SIZE(size) #if (defined(__clang_major__) && (__clang_major__ < 4)) || (!defined(__clang_major__) && defined(__GNUC__))
# define RPMALLOC_ATTRIB_ALLOC_SIZE2(count, size) #define RPMALLOC_ATTRIB_MALLOC
# else #define RPMALLOC_ATTRIB_ALLOC_SIZE(size)
# define RPMALLOC_ATTRIB_MALLOC __attribute__((__malloc__)) #define RPMALLOC_ATTRIB_ALLOC_SIZE2(count, size)
# define RPMALLOC_ATTRIB_ALLOC_SIZE(size) __attribute__((alloc_size(size)))
# define RPMALLOC_ATTRIB_ALLOC_SIZE2(count, size) __attribute__((alloc_size(count, size)))
# endif
# define RPMALLOC_CDECL
#elif defined(_MSC_VER)
# define RPMALLOC_EXPORT
# define RPMALLOC_ALLOCATOR __declspec(allocator) __declspec(restrict)
# define RPMALLOC_ATTRIB_MALLOC
# define RPMALLOC_ATTRIB_ALLOC_SIZE(size)
# define RPMALLOC_ATTRIB_ALLOC_SIZE2(count,size)
# define RPMALLOC_CDECL __cdecl
#else #else
# define RPMALLOC_EXPORT #define RPMALLOC_ATTRIB_MALLOC __attribute__((__malloc__))
# define RPMALLOC_ALLOCATOR #define RPMALLOC_ATTRIB_ALLOC_SIZE(size) __attribute__((alloc_size(size)))
# define RPMALLOC_ATTRIB_MALLOC #define RPMALLOC_ATTRIB_ALLOC_SIZE2(count, size) __attribute__((alloc_size(count, size)))
# define RPMALLOC_ATTRIB_ALLOC_SIZE(size) #endif
# define RPMALLOC_ATTRIB_ALLOC_SIZE2(count,size) #define RPMALLOC_CDECL
# define RPMALLOC_CDECL #elif defined(_MSC_VER)
#define RPMALLOC_EXPORT
#define RPMALLOC_RESTRICT __declspec(restrict)
#define RPMALLOC_ALLOCATOR __declspec(allocator) __declspec(restrict)
#define RPMALLOC_CACHE_ALIGNED __declspec(align(RPMALLOC_CACHE_LINE_SIZE))
#define RPMALLOC_ATTRIB_MALLOC
#define RPMALLOC_ATTRIB_ALLOC_SIZE(size)
#define RPMALLOC_ATTRIB_ALLOC_SIZE2(count, size)
#define RPMALLOC_CDECL __cdecl
#else
#define RPMALLOC_EXPORT
#define RPMALLOC_ALLOCATOR
#define RPMALLOC_ATTRIB_MALLOC
#define RPMALLOC_ATTRIB_ALLOC_SIZE(size)
#define RPMALLOC_ATTRIB_ALLOC_SIZE2(count, size)
#define RPMALLOC_CDECL
#endif #endif
//! Define RPMALLOC_CONFIGURABLE to enable configuring sizes. Will introduce #define RPMALLOC_MAX_ALIGNMENT (256 * 1024)
// a very small overhead due to some size calculations not being compile time constants
#ifndef RPMALLOC_CONFIGURABLE
#define RPMALLOC_CONFIGURABLE 0
#endif
//! Define RPMALLOC_FIRST_CLASS_HEAPS to enable heap based API (rpmalloc_heap_* functions). //! Define RPMALLOC_FIRST_CLASS_HEAPS to non-zero to enable heap based API (rpmalloc_heap_* functions).
// Will introduce a very small overhead to track fully allocated spans in heaps
#ifndef RPMALLOC_FIRST_CLASS_HEAPS #ifndef RPMALLOC_FIRST_CLASS_HEAPS
#define RPMALLOC_FIRST_CLASS_HEAPS 0 #define RPMALLOC_FIRST_CLASS_HEAPS 0
#endif #endif
//! Define RPMALLOC_HEAP_STATISTICS to non-zero to enable first class heap statistics gathering.
#ifndef RPMALLOC_HEAP_STATISTICS
#define RPMALLOC_HEAP_STATISTICS 0
#endif
//! Flag to rpaligned_realloc to not preserve content in reallocation //! Flag to rpaligned_realloc to not preserve content in reallocation
#define RPMALLOC_NO_PRESERVE 1 #define RPMALLOC_NO_PRESERVE 1
//! Flag to rpaligned_realloc to fail and return null pointer if grow cannot be done in-place, //! Flag to rpaligned_realloc to 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 // in which case the original pointer is still valid (just like a call to realloc which failes to allocate
// a new block). // a new block).
#define RPMALLOC_GROW_OR_FAIL 2 #define RPMALLOC_GROW_OR_FAIL 2
typedef struct rpmalloc_global_statistics_t { typedef struct rpmalloc_global_statistics_t {
//! Current amount of virtual memory mapped, all of which might not have been committed (only if ENABLE_STATISTICS=1) //! Current amount of virtual memory mapped, all of which might not have been committed (only if
//! ENABLE_STATISTICS=1)
size_t mapped; size_t mapped;
//! Peak amount of virtual memory mapped, all of which might not have been committed (only if ENABLE_STATISTICS=1) //! Peak amount of virtual memory mapped, all of which might not have been committed (only if ENABLE_STATISTICS=1)
size_t mapped_peak; size_t mapped_peak;
//! Current amount of memory in global caches for small and medium sizes (<32KiB) //! Running counter of total amount of memory committed (only if ENABLE_STATISTICS=1)
size_t cached; size_t committed;
//! Current amount of memory allocated in huge allocations, i.e larger than LARGE_SIZE_LIMIT which is 2MiB by default (only if ENABLE_STATISTICS=1) //! Running counter of total amount of memory decommitted (only if ENABLE_STATISTICS=1)
size_t decommitted;
//! Current amount of virtual memory active and committed (only if ENABLE_STATISTICS=1)
size_t active;
//! Peak amount of virtual memory active and committed (only if ENABLE_STATISTICS=1)
size_t active_peak;
//! Current amount of memory allocated in huge block allocations, i.e blocks above the
//! largest size class (only if ENABLE_STATISTICS=1)
size_t huge_alloc; size_t huge_alloc;
//! Peak amount of memory allocated in huge allocations, i.e larger than LARGE_SIZE_LIMIT which is 2MiB by default (only if ENABLE_STATISTICS=1) //! Peak amount of memory allocated in huge block allocations, i.e blocks above the
//! largest size class (only if ENABLE_STATISTICS=1)
size_t huge_alloc_peak; size_t huge_alloc_peak;
//! Total amount of memory mapped since initialization (only if ENABLE_STATISTICS=1) //! Current heap count (only if ENABLE_STATISTICS=1)
size_t mapped_total; size_t heap_count;
//! Total amount of memory unmapped since initialization (only if ENABLE_STATISTICS=1)
size_t unmapped_total;
} rpmalloc_global_statistics_t; } rpmalloc_global_statistics_t;
typedef struct rpmalloc_thread_statistics_t { typedef struct rpmalloc_thread_statistics_t {
//! Current number of bytes available in thread size class caches for small and medium sizes (<32KiB) //! Current number of bytes available in thread size class caches (only if ENABLE_STATISTICS=1)
size_t sizecache; size_t sizecache;
//! Current number of bytes available in thread span caches for small and medium sizes (<32KiB) //! Current number of bytes available in thread page caches (only if ENABLE_STATISTICS=1)
size_t spancache; size_t spancache;
//! Total number of bytes transitioned from thread cache to global cache (only if ENABLE_STATISTICS=1) //! Per page type span statistics, indexed by page type (small, medium-small, medium-large,
size_t thread_to_global; //! large, huge)
//! Total number of bytes transitioned from global cache to thread cache (only if ENABLE_STATISTICS=1)
size_t global_to_thread;
//! Per span count statistics (only if ENABLE_STATISTICS=1)
struct { struct {
//! Currently used number of spans //! Currently mapped number of spans of this page type
size_t current; size_t current;
//! High water mark of spans used //! Number of raw memory map calls for this page type resulting in actual OS mmap calls
size_t peak; //! (only if ENABLE_STATISTICS=1)
//! Number of spans transitioned to global cache
size_t to_global;
//! Number of spans transitioned from global cache
size_t from_global;
//! Number of spans transitioned to thread cache
size_t to_cache;
//! Number of spans transitioned from thread cache
size_t from_cache;
//! Number of spans transitioned to reserved state
size_t to_reserved;
//! Number of spans transitioned from reserved state
size_t from_reserved;
//! Number of raw memory map calls (not hitting the reserve spans but resulting in actual OS mmap calls)
size_t map_calls; size_t map_calls;
} span_use[64]; } span_use[5];
//! Per size class statistics (only if ENABLE_STATISTICS=1) //! Per size class statistics (only if ENABLE_STATISTICS=1)
struct { struct {
//! Current number of allocations //! Current number of allocations
@@ -122,62 +133,41 @@ typedef struct rpmalloc_thread_statistics_t {
size_t alloc_total; size_t alloc_total;
//! Total number of frees //! Total number of frees
size_t free_total; size_t free_total;
//! Number of spans transitioned to cache
size_t spans_to_cache;
//! Number of spans transitioned from cache
size_t spans_from_cache;
//! Number of spans transitioned from reserved state
size_t spans_from_reserved;
//! Number of raw memory map calls (not hitting the reserve spans but resulting in actual OS mmap calls)
size_t map_calls;
} size_use[128]; } size_use[128];
} rpmalloc_thread_statistics_t; } rpmalloc_thread_statistics_t;
typedef struct rpmalloc_config_t { typedef struct rpmalloc_interface_t {
//! Map memory pages for the given number of bytes. The returned address MUST be //! Map memory pages for the given number of bytes. The returned address MUST be aligned to the given alignment,
// aligned to the rpmalloc span size, which will always be a power of two. //! which will always be either 0 or the span size. The function can store an alignment offset in the offset
// Optionally the function can store an alignment offset in the offset variable //! variable in case it performs alignment and the returned pointer is offset from the actual start of the memory
// in case it performs alignment and the returned pointer is offset from the //! region due to this alignment. This alignment offset will be passed to the memory unmap function. The mapped size
// actual start of the memory region due to this alignment. The alignment offset //! can be stored in the mapped_size variable, which will also be passed to the memory unmap function as the release
// will be passed to the memory unmap function. The alignment offset MUST NOT be //! parameter once the entire mapped region is ready to be released. If you set a memory_map function, you must also
// larger than 65535 (storable in an uint16_t), if it is you must use natural //! set a memory_unmap function or else the default implementation will be used for both. This function must be
// alignment to shift it into 16 bits. If you set a memory_map function, you //! thread safe, it can be called by multiple threads simultaneously.
// must also set a memory_unmap function or else the default implementation will void* (*memory_map)(size_t size, size_t alignment, size_t* offset, size_t* mapped_size);
// be used for both. This function must be thread safe, it can be called by //! Commit a range of memory pages. Return non-zero if the operation failed and the address range could not be committed.
// multiple threads simultaneously. int (*memory_commit)(void* address, size_t size);
void* (*memory_map)(size_t size, size_t* offset); //! Decommit a range of memory pages. Return non-zero if the operation failed and the address range could not be decommitted.
//! Unmap the memory pages starting at address and spanning the given number of bytes. int (*memory_decommit)(void* address, size_t size);
// If release is set to non-zero, the unmap is for an entire span range as returned by //! Unmap the memory pages starting at address and spanning the given number of bytes. If you set a memory_unmap
// a previous call to memory_map and that the entire range should be released. The //! function, you must also set a memory_map function or else the default implementation will be used for both. This
// release argument holds the size of the entire span range. If release is set to 0, //! function must be thread safe, it can be called by multiple threads simultaneously.
// the unmap is a partial decommit of a subset of the mapped memory range. void (*memory_unmap)(void* address, size_t offset, size_t mapped_size);
// If you set a memory_unmap function, you must also set a memory_map function or //! Called when a call to map memory pages fails (out of memory). If this callback is not set or returns zero the
// else the default implementation will be used for both. This function must be thread //! library will return a null pointer in the allocation call. If this callback returns non-zero the map call will
// safe, it can be called by multiple threads simultaneously. //! be retried. The argument passed is the number of bytes that was requested in the map call. Only used if the
void (*memory_unmap)(void* address, size_t size, size_t offset, size_t release); //! default system memory map function is used (memory_map callback is not set).
//! Called when an assert fails, if asserts are enabled. Will use the standard assert()
// if this is not set.
void (*error_callback)(const char* message);
//! Called when a call to map memory pages fails (out of memory). If this callback is
// not set or returns zero the library will return a null pointer in the allocation
// call. If this callback returns non-zero the map call will be retried. The argument
// passed is the number of bytes that was requested in the map call. Only used if
// the default system memory map function is used (memory_map callback is not set).
int (*map_fail_callback)(size_t size); int (*map_fail_callback)(size_t size);
//! Called when an assert fails, if asserts are enabled. Will use the standard assert() if this is not set.
void (*error_callback)(const char* message);
} rpmalloc_interface_t;
typedef struct rpmalloc_config_t {
//! Size of memory pages. The page size MUST be a power of two. All memory mapping //! Size of memory pages. The page size MUST be a power of two. All memory mapping
// requests to memory_map will be made with size set to a multiple of the page size. // requests to memory_map will be made with size set to a multiple of the page size.
// Used if RPMALLOC_CONFIGURABLE is defined to 1, otherwise system page size is used. // Set to 0 to use the OS default page size.
size_t page_size; size_t page_size;
//! Size of a span of memory blocks. MUST be a power of two, and in [4096,262144]
// range (unless 0 - set to 0 to use the default span size). Used if RPMALLOC_CONFIGURABLE
// is defined to 1.
size_t span_size;
//! Number of spans to map at each request to map new virtual memory blocks. This can
// be used to minimize the system call overhead at the cost of virtual memory address
// space. The extra mapped pages will not be written until actually used, so physical
// committed memory should not be affected in the default implementation. Will be
// aligned to a multiple of spans that match memory page size in case of huge pages.
size_t span_map_count;
//! Enable use of large/huge pages. If this flag is set to non-zero and page size is //! Enable use of large/huge pages. If this flag is set to non-zero and page size is
// zero, the allocator will try to enable huge pages and auto detect the configuration. // zero, the allocator will try to enable huge pages and auto detect the configuration.
// If this is set to non-zero and page_size is also non-zero, the allocator will // If this is set to non-zero and page_size is also non-zero, the allocator will
@@ -186,16 +176,45 @@ typedef struct rpmalloc_config_t {
// For Windows, see https://docs.microsoft.com/en-us/windows/desktop/memory/large-page-support // For Windows, see https://docs.microsoft.com/en-us/windows/desktop/memory/large-page-support
// For Linux, see https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt // For Linux, see https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt
int enable_huge_pages; int enable_huge_pages;
int unused; //! Enable use of transparent huge pages, advising the kernel to back large memory
// mappings with huge pages without requiring a preallocated huge page pool. Unlike
// enable_huge_pages this does not affect page size or accounting, and unused memory
// ranges can still be decommitted. Ignored if enable_huge_pages is in effect or if
// the platform has no transparent huge page support (currently implemented for
// Linux and Android only). After initialization the config value reflects if
// transparent huge pages are actually used.
int enable_thp;
//! Disable decommitting unused pages when allocator determines the memory pressure
// is low and there is enough active pages cached. If set to 1, keep all pages committed.
int disable_decommit;
//! Allocated pages names for systems supporting it to be able to distinguish among anonymous regions.
const char* page_name;
//! Allocated huge pages names for systems supporting it to be able to distinguish among anonymous regions.
const char* huge_page_name;
//! Unmap all memory on finalize if set to 1. Normally you can let the OS unmap all pages
// when process exits, but if using rpmalloc in a dynamic library you might want to unmap
// all pages when the dynamic library unloads to avoid process memory leaks and bloat.
// NOTE: this is ignored when rpmalloc is built with ENABLE_OVERRIDE, because the standard
// library override makes rpmalloc the backing store for the C runtime's own allocations
// (for example per-thread TLS). Returning all mappings to the OS while the process keeps
// running would unmap memory the runtime still uses. Only honored without the override.
int unmap_on_finalize;
#if defined(__linux__) || defined(__ANDROID__)
///! Allows to disable the Transparent Huge Page feature on Linux on a process basis,
/// rather than enabling/disabling system-wise (done via /sys/kernel/mm/transparent_hugepage/enabled).
/// It can possibly improve performance and reduced allocation overhead in some contexts, albeit
/// THP is usually enabled by default.
int disable_thp;
#endif
} rpmalloc_config_t; } rpmalloc_config_t;
//! Initialize allocator with default configuration //! Initialize allocator
RPMALLOC_EXPORT int RPMALLOC_EXPORT int
rpmalloc_initialize(void); rpmalloc_initialize(rpmalloc_interface_t* memory_interface);
//! Initialize allocator with given configuration //! Initialize allocator
RPMALLOC_EXPORT int RPMALLOC_EXPORT int
rpmalloc_initialize_config(const rpmalloc_config_t* config); rpmalloc_initialize_config(rpmalloc_interface_t* memory_interface, rpmalloc_config_t* config);
//! Get allocator configuration //! Get allocator configuration
RPMALLOC_EXPORT const rpmalloc_config_t* RPMALLOC_EXPORT const rpmalloc_config_t*
@@ -211,7 +230,7 @@ rpmalloc_thread_initialize(void);
//! Finalize allocator for calling thread //! Finalize allocator for calling thread
RPMALLOC_EXPORT void RPMALLOC_EXPORT void
rpmalloc_thread_finalize(int release_caches); rpmalloc_thread_finalize(void);
//! Perform deferred deallocations pending for the calling thread heap //! Perform deferred deallocations pending for the calling thread heap
RPMALLOC_EXPORT void RPMALLOC_EXPORT void
@@ -237,9 +256,9 @@ rpmalloc_dump_statistics(void* file);
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void* RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpmalloc(size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(1); rpmalloc(size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(1);
//! Free the given memory block //! Allocate a zero initialized memory block of at least the given size
RPMALLOC_EXPORT void RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpfree(void* ptr); rpzalloc(size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(1);
//! Allocate a memory block of at least the given size and zero initialize it //! Allocate a memory block of at least the given size and zero initialize it
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void* RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
@@ -252,43 +271,53 @@ rprealloc(void* ptr, size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_S
//! Reallocate the given block to at least the given size and alignment, //! Reallocate the given block to at least the given size and alignment,
// with optional control flags (see RPMALLOC_NO_PRESERVE). // with optional control flags (see RPMALLOC_NO_PRESERVE).
// Alignment must be a power of two and a multiple of sizeof(void*), // 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 // and should ideally be less than memory page size.
// internals is that this must also be strictly less than the span size (default 64KiB)
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void* RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpaligned_realloc(void* ptr, size_t alignment, size_t size, size_t oldsize, unsigned int flags) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(3); rpaligned_realloc(void* ptr, size_t alignment, size_t size, size_t oldsize, unsigned int flags) RPMALLOC_ATTRIB_MALLOC
RPMALLOC_ATTRIB_ALLOC_SIZE(3);
//! Allocate a memory block of at least the given size and alignment. //! Allocate a memory block of at least the given size and alignment.
// Alignment must be a power of two and a multiple of sizeof(void*), // 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 // and should ideally be less than memory page size.
// internals is that this must also be strictly less than the span size (default 64KiB)
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void* RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpaligned_alloc(size_t alignment, size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(2); rpaligned_alloc(size_t alignment, size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(2);
//! Allocate 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.
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpaligned_zalloc(size_t alignment, size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(2);
//! Allocate a memory block of at least the given size and alignment, and zero initialize it. //! Allocate a memory block of at least the given size and alignment, and zero initialize it.
// Alignment must be a power of two and a multiple of sizeof(void*), // 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 // and should ideally be less than memory page size.
// internals is that this must also be strictly less than the span size (default 64KiB)
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void* RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpaligned_calloc(size_t alignment, size_t num, size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE2(2, 3); rpaligned_calloc(size_t alignment, size_t num, size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE2(2, 3);
//! Allocate a memory block of at least the given size and alignment. //! Allocate a memory block of at least the given size and alignment.
// Alignment must be a power of two and a multiple of sizeof(void*), // 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 // and should ideally be less than memory page size.
// internals is that this must also be strictly less than the span size (default 64KiB)
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void* RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpmemalign(size_t alignment, size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(2); rpmemalign(size_t alignment, size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(2);
//! Allocate a memory block of at least the given size and alignment. //! Allocate a memory block of at least the given size and alignment.
// Alignment must be a power of two and a multiple of sizeof(void*), // 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 // and should ideally be less than memory page size.
// internals is that this must also be strictly less than the span size (default 64KiB)
RPMALLOC_EXPORT int RPMALLOC_EXPORT int
rpposix_memalign(void** memptr, size_t alignment, size_t size); rpposix_memalign(void** memptr, size_t alignment, size_t size);
//! Free the given memory block
RPMALLOC_EXPORT void
rpfree(void* ptr);
//! Query the usable size of the given memory block (from given pointer to the end of block) //! Query the usable size of the given memory block (from given pointer to the end of block)
RPMALLOC_EXPORT size_t RPMALLOC_EXPORT size_t
rpmalloc_usable_size(void* ptr); rpmalloc_usable_size(void* ptr);
//! Dummy empty function for forcing linker symbol inclusion
RPMALLOC_EXPORT void
rpmalloc_linker_reference(void);
#if RPMALLOC_FIRST_CLASS_HEAPS #if RPMALLOC_FIRST_CLASS_HEAPS
//! Heap type //! Heap type
@@ -300,7 +329,8 @@ typedef struct heap_t rpmalloc_heap_t;
RPMALLOC_EXPORT rpmalloc_heap_t* RPMALLOC_EXPORT rpmalloc_heap_t*
rpmalloc_heap_acquire(void); rpmalloc_heap_acquire(void);
//! Release a heap (does NOT free the memory allocated by the heap, use rpmalloc_heap_free_all before destroying the heap). //! Release a heap (does NOT free the memory allocated by the heap, use rpmalloc_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. // Releasing a heap will enable it to be reused by other threads. Safe to pass a null pointer.
RPMALLOC_EXPORT void RPMALLOC_EXPORT void
rpmalloc_heap_release(rpmalloc_heap_t* heap); rpmalloc_heap_release(rpmalloc_heap_t* heap);
@@ -311,34 +341,43 @@ rpmalloc_heap_alloc(rpmalloc_heap_t* heap, size_t size) RPMALLOC_ATTRIB_MALLOC R
//! Allocate a memory block of at least the given size using the given heap. The returned //! Allocate 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*), // 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 // and should ideally be less than memory page size.
// internals is that this must also be strictly less than the span size (default 64KiB).
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void* RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpmalloc_heap_aligned_alloc(rpmalloc_heap_t* heap, size_t alignment, size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(3); rpmalloc_heap_aligned_alloc(rpmalloc_heap_t* heap, size_t alignment, size_t size) RPMALLOC_ATTRIB_MALLOC
RPMALLOC_ATTRIB_ALLOC_SIZE(3);
//! Allocate a zero initialized 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.
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpmalloc_heap_aligned_zalloc(rpmalloc_heap_t* heap, size_t alignment, size_t size) RPMALLOC_ATTRIB_MALLOC
RPMALLOC_ATTRIB_ALLOC_SIZE(3);
//! Allocate a memory block of at least the given size using the given heap and zero initialize it. //! Allocate a memory block of at least the given size using the given heap and zero initialize it.
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void* RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpmalloc_heap_calloc(rpmalloc_heap_t* heap, size_t num, size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE2(2, 3); rpmalloc_heap_calloc(rpmalloc_heap_t* heap, size_t num, size_t size) RPMALLOC_ATTRIB_MALLOC
RPMALLOC_ATTRIB_ALLOC_SIZE2(2, 3);
//! Allocate a memory block of at least the given size using the given heap and zero initialize it. The returned //! Allocate a memory block of at least the given size using the given heap and zero initialize 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*), // block will have the requested alignment. Alignment must either be zero, or a power of two and a multiple of
// and should ideally be less than memory page size. A caveat of rpmalloc // sizeof(void*), and should ideally be less than memory page size.
// internals is that this must also be strictly less than the span size (default 64KiB).
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void* RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpmalloc_heap_aligned_calloc(rpmalloc_heap_t* heap, size_t alignment, size_t num, size_t size) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE2(2, 3); rpmalloc_heap_aligned_calloc(rpmalloc_heap_t* heap, size_t alignment, size_t num, size_t size) RPMALLOC_ATTRIB_MALLOC
RPMALLOC_ATTRIB_ALLOC_SIZE2(3, 4);
//! Reallocate the given block to at least the given size. The memory block MUST be allocated //! Reallocate the given block to at least the given size. The memory block MUST be allocated
// by the same heap given to this function. // by the same heap given to this function.
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void* RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpmalloc_heap_realloc(rpmalloc_heap_t* heap, void* ptr, size_t size, unsigned int flags) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(3); rpmalloc_heap_realloc(rpmalloc_heap_t* heap, void* ptr, size_t size, unsigned int flags) RPMALLOC_ATTRIB_MALLOC
RPMALLOC_ATTRIB_ALLOC_SIZE(3);
//! Reallocate the given block to at least the given size. The memory block MUST be allocated //! Reallocate 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. // 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 // 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 // less than memory page size.
// the span size (default 64KiB).
RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void* RPMALLOC_EXPORT RPMALLOC_ALLOCATOR void*
rpmalloc_heap_aligned_realloc(rpmalloc_heap_t* heap, void* ptr, size_t alignment, size_t size, unsigned int flags) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(4); rpmalloc_heap_aligned_realloc(rpmalloc_heap_t* heap, void* ptr, size_t alignment, size_t size,
unsigned int flags) RPMALLOC_ATTRIB_MALLOC RPMALLOC_ATTRIB_ALLOC_SIZE(4);
//! Free the given memory block from the given heap. The memory block MUST be allocated //! Free the given memory block from the given heap. The memory block MUST be allocated
// by the same heap given to this function. // by the same heap given to this function.
@@ -349,12 +388,29 @@ rpmalloc_heap_free(rpmalloc_heap_t* heap, void* ptr);
RPMALLOC_EXPORT void RPMALLOC_EXPORT void
rpmalloc_heap_free_all(rpmalloc_heap_t* heap); rpmalloc_heap_free_all(rpmalloc_heap_t* heap);
struct rpmalloc_heap_statistics_t {
// Number of bytes allocated
size_t allocated_size;
// Number of bytes committed
size_t committed_size;
// Number of bytes mapped
size_t mapped_size;
};
//! Get heap statistics (if enabled in build)
RPMALLOC_EXPORT struct rpmalloc_heap_statistics_t
rpmalloc_heap_statistics(rpmalloc_heap_t* heap);
//! Set the given heap as the current heap for the calling thread. A heap MUST only be current heap //! Set 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 // 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. // current heap for the calling thread is released to be reused by other threads.
RPMALLOC_EXPORT void RPMALLOC_EXPORT void
rpmalloc_heap_thread_set_current(rpmalloc_heap_t* heap); rpmalloc_heap_thread_set_current(rpmalloc_heap_t* heap);
//! Returns which heap the given pointer is allocated on
RPMALLOC_EXPORT rpmalloc_heap_t*
rpmalloc_get_heap_for_ptr(void* ptr);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus