50 #if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__)
51 #define ARCH_ALIGNMENT (8U)
54 #elif defined(__i386__) || defined(_M_IX86) || defined(__386__)
55 #define ARCH_ALIGNMENT (4U)
58 #elif defined(__aarch64__) || defined(_M_ARM64) || defined(__arm64__)
59 #define ARCH_ALIGNMENT (8U)
62 #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_6__) || defined(__arm__) \
64 #define ARCH_ALIGNMENT (4U)
67 #elif defined(__riscv) && (__riscv_xlen == 64)
68 #define ARCH_ALIGNMENT (8U)
70 #elif defined(__riscv) && (__riscv_xlen == 32)
71 #define ARCH_ALIGNMENT (4U)
74 #elif defined(__powerpc64__) || defined(__PPC64__)
75 #define ARCH_ALIGNMENT (8U)
77 #elif defined(__powerpc__) || defined(__PPC__)
78 #define ARCH_ALIGNMENT (4U)
81 #elif defined(__s390x__) || defined(__s390__)
82 #define ARCH_ALIGNMENT (8U)
85 #elif defined(__mips64) || defined(__mips64el__)
86 #define ARCH_ALIGNMENT (8U)
88 #elif defined(__mips__)
89 #define ARCH_ALIGNMENT (4U)
92 #elif defined(__loongarch__)
93 #define ARCH_ALIGNMENT (8U)
96 #elif defined(__AVR__)
97 #define ARCH_ALIGNMENT (2U)
99 #elif defined(__avr32__)
100 #define ARCH_ALIGNMENT (4U)
103 #elif defined(__MICROBLAZE__)
104 #define ARCH_ALIGNMENT (4U)
107 #elif defined(__sh__)
108 #define ARCH_ALIGNMENT (4U)
111 #elif defined(__arc__)
112 #define ARCH_ALIGNMENT (4U)
115 #elif defined(__bfin__)
116 #define ARCH_ALIGNMENT (4U)
119 #elif defined(__EMSCRIPTEN__) || defined(__wasm__)
120 #define ARCH_ALIGNMENT (4U)
123 #elif defined(__sparc__) || defined(__sparc)
124 #define ARCH_ALIGNMENT (4U)
126 #elif defined(__sparcv9__)
127 #define ARCH_ALIGNMENT (8U)
130 #elif defined(__ia64__)
131 #define ARCH_ALIGNMENT (8U)
134 #elif defined(__alpha__)
135 #define ARCH_ALIGNMENT (8U)
138 #elif defined(__TI_PRU__) || defined(__PRU__)
139 #define ARCH_ALIGNMENT (4U)
141 #elif defined(__hppa__)
142 #define ARCH_ALIGNMENT (4U)
145 #elif defined(__zarch__)
146 #define ARCH_ALIGNMENT (8U)
149 #elif defined(__cell_spu__)
150 #define ARCH_ALIGNMENT (16U)
153 #elif defined(__NVPTX__) || defined(__PTX__)
154 #define ARCH_ALIGNMENT (8U)
157 #elif defined(__bpf__)
158 #define ARCH_ALIGNMENT (8U)
161 #elif defined(__e2k__)
162 #define ARCH_ALIGNMENT (8U)
165 #elif defined(__CRIS__)
166 #define ARCH_ALIGNMENT (4U)
169 #elif defined(__tilegx__)
170 #define ARCH_ALIGNMENT (8U)
174 #error "[ERROR] Unknow Architecture: check 'ARCH_ALIGNMENT'"
192 #if defined(__GNUC__) || defined(__clang__)
193 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
195 __attribute__((scalar_storage_order("big-endian"), \
196 aligned(ARCH_ALIGNMENT)))
197 #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
199 __attribute__((scalar_storage_order("little-endian"), \
200 aligned(ARCH_ALIGNMENT)))
202 #error "[ERROR] Unknow Endianness: check '__BYTE_ORDER__'"
222 #ifdef BUILDING_LIBMEMALLOC
223 #define __LIBMEMALLOC_API __declspec(dllexport)
225 #define __LIBMEMALLOC_API __declspec(dllimport)
228 #if defined(__GNUC__) || defined(__clang__)
229 #define __LIBMEMALLOC_API __attribute__((visibility("default")))
231 #define __LIBMEMALLOC_API
248#if defined(__GNUC__) || defined(__clang__)
249 #define __LIBMEMALLOC_MALLOC \
250 __attribute__((malloc, alloc_size(2), warn_unused_result))
252 #define __LIBMEMALLOC_MALLOC
267#if defined(__GNUC__) || defined(__clang__)
268 #define __LIBMEMALLOC_REALLOC \
269 __attribute__((malloc, alloc_size(3), warn_unused_result))
271 #define __LIBMEMALLOC_REALLOC
284#define PTR_ERR(x) ((void *)((intptr_t)(x)))
295#define ALIGN(x) (((x) + (ARCH_ALIGNMENT - 1U)) & ~(ARCH_ALIGNMENT - 1U))
306#define GC_INTERVAL_MS (uint16_t)(100U)
325typedef enum AllocationStrategy
363 const char *var_name;
369 struct BlockHeader *next;
370 struct BlockHeader *prev;
372 struct BlockHeader *fl_next;
418 struct MmapBlock *next;
446 pthread_t main_thread;
451 uint16_t gc_thread_started;
453 uint32_t gc_interval_ms;
455 pthread_cond_t gc_cond;
456 pthread_mutex_t gc_lock;
486 size_t metadata_size;
488 uintptr_t *stack_top;
489 uintptr_t *stack_bottom;
491 size_t num_size_classes;
597 const char *
const var)
616 const char *
const var)
635 const char *
const var)
655 const char *
const var,
676 const char *
const var,
698 const size_t new_size,
699 const char *
const var,
719 const char *
const var);
int MEM_allocatorInit(mem_allocator_t *const allocator)
Initializes the memory allocator and its internal structures.
Definition libmemalloc.c:1587
allocation_strategy_t
Defines allocation strategies for memory management.
Definition libmemalloc.h:323
int MEM_allocFree(mem_allocator_t *const allocator, void *const ptr, const char *const var)
Releases allocated memory back to the heap.
Definition libmemalloc.c:3778
void * MEM_allocCalloc(mem_allocator_t *const allocator, const size_t size, const char *const var, const allocation_strategy_t strategy)
Allocates and zero‐initializes memory using the specified strategy.
Definition libmemalloc.c:3705
void * MEM_allocMallocBestFit(mem_allocator_t *const allocator, const size_t size, const char *const var)
Allocates memory using the BEST_FIT strategy.
Definition libmemalloc.c:3609
void * MEM_allocMallocFirstFit(mem_allocator_t *const allocator, const size_t size, const char *const var)
Allocates memory using the FIRST_FIT strategy.
Definition libmemalloc.c:3577
#define __ALIGN
Defines structure alignment and enforces the target byte order.
Definition libmemalloc.h:203
void * MEM_allocMallocNextFit(mem_allocator_t *const allocator, const size_t size, const char *const var)
Allocates memory using the NEXT_FIT strategy.
Definition libmemalloc.c:3640
#define __LIBMEMALLOC_REALLOC
Annotates reallocator functions that may return a pointer to resized memory.
Definition libmemalloc.h:269
#define __LIBMEMALLOC_API
Defines the default visibility attribute for exported symbols.
Definition libmemalloc.h:229
int MEM_disableGc(mem_allocator_t *const allocator)
Stop the garbage collector thread and perform a final collection.
Definition libmemalloc.c:3826
void * MEM_memset(void *const source, const int value, const size_t size)
Fills a memory block with a specified byte value using optimized operations.
Definition libmemalloc.c:1034
int MEM_enableGc(mem_allocator_t *const allocator)
Start or signal the garbage collector thread.
Definition libmemalloc.c:3806
void * MEM_memcpy(void *const dest, const void *src, const size_t size)
Copies a memory block between buffers using optimized operations.
Definition libmemalloc.c:1117
#define __LIBMEMALLOC_MALLOC
Annotates allocator functions that return newly allocated memory.
Definition libmemalloc.h:250
void * MEM_allocMalloc(mem_allocator_t *const allocator, const size_t size, const char *const var, const allocation_strategy_t strategy)
Allocates memory using the specified strategy.
Definition libmemalloc.c:3672
void * MEM_allocRealloc(mem_allocator_t *const allocator, void *const ptr, const size_t new_size, const char *const var, const allocation_strategy_t strategy)
Reallocates memory with safety checks using the specified strategy.
Definition libmemalloc.c:3739
@ NEXT_FIT
Definition libmemalloc.h:325
@ FIRST_FIT
Definition libmemalloc.h:324
@ BEST_FIT
Definition libmemalloc.h:326
Orchestrates the background mark-and-sweep garbage collector.
Definition libmemalloc.h:441
pthread_t gc_thread
Definition libmemalloc.h:442
pthread_cond_t gc_cond
Definition libmemalloc.h:452
bool gc_running
Definition libmemalloc.h:445
bool gc_exit
Definition libmemalloc.h:446
pthread_mutex_t gc_lock
Definition libmemalloc.h:453
pthread_t main_thread
Definition libmemalloc.h:443
uint32_t gc_interval_ms
Definition libmemalloc.h:450
uint16_t gc_thread_started
Definition libmemalloc.h:448
Manages dynamic memory allocation.
Definition libmemalloc.h:479
block_header_t * last_allocated
Definition libmemalloc.h:491
gc_thread_t gc_thread
Definition libmemalloc.h:497
mmap_t * mmap_list
Definition libmemalloc.h:495
size_t metadata_size
Definition libmemalloc.h:483
size_t num_size_classes
Definition libmemalloc.h:488
block_header_t ** free_lists
Definition libmemalloc.h:492
uintptr_t * stack_top
Definition libmemalloc.h:485
uintptr_t * stack_bottom
Definition libmemalloc.h:486
uint8_t * heap_end
Definition libmemalloc.h:481
size_t num_arenas
Definition libmemalloc.h:489
mem_arena_t * arenas
Definition libmemalloc.h:494
uint8_t * heap_start
Definition libmemalloc.h:480
Represents a memory arena with its own free lists.
Definition libmemalloc.h:390
size_t num_bins
Definition libmemalloc.h:391
block_header_t * top_chunk
Definition libmemalloc.h:394
block_header_t ** bins
Definition libmemalloc.h:393
Tracks memory-mapped regions for large allocations.
Definition libmemalloc.h:411
void * addr
Definition libmemalloc.h:412
size_t size
Definition libmemalloc.h:413
struct MmapBlock * next
Definition libmemalloc.h:415