libmemalloc  v4.0.00
Modern Memory Allocator
Loading...
Searching...
No Matches
libmemalloc.h
Go to the documentation of this file.
1
18#pragma once
19
20/*< C++ Compatibility >*/
21#ifdef __cplusplus
22extern "C"
23{
24#endif
25
30/*< Dependencies >*/
31#include <pthread.h>
32#include <stdatomic.h>
33#include <stdbool.h>
34#include <stdint.h>
35#include <string.h>
36
48#ifndef ARCH_ALIGNMENT
49 /* x86_64 processor [64-bit] - Vendor: AMD/Intel */
50 #if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__)
51 #define ARCH_ALIGNMENT (8U)
52
53 /* i386 processor [32-bit] - Vendor: Intel */
54 #elif defined(__i386__) || defined(_M_IX86) || defined(__386__)
55 #define ARCH_ALIGNMENT (4U)
56
57 /* aarch64 processor [64-bit] - Vendor: ARM */
58 #elif defined(__aarch64__) || defined(_M_ARM64) || defined(__arm64__)
59 #define ARCH_ALIGNMENT (8U)
60
61 /* arm processor [32-bit] - Vendor: ARM (v7/v6) */
62 #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_6__) || defined(__arm__) \
63 || defined(_M_ARM)
64 #define ARCH_ALIGNMENT (4U)
65
66 /* riscv processor [64-bit] - Vendor: RISC-V */
67 #elif defined(__riscv) && (__riscv_xlen == 64)
68 #define ARCH_ALIGNMENT (8U)
69 /* riscv processor [32-bit] - Vendor: RISC-V */
70 #elif defined(__riscv) && (__riscv_xlen == 32)
71 #define ARCH_ALIGNMENT (4U)
72
73 /* powerpc processor [64-bit] - Vendor: IBM */
74 #elif defined(__powerpc64__) || defined(__PPC64__)
75 #define ARCH_ALIGNMENT (8U)
76 /* powerpc processor [32-bit] - Vendor: IBM */
77 #elif defined(__powerpc__) || defined(__PPC__)
78 #define ARCH_ALIGNMENT (4U)
79
80 /* s390x processor [64-bit] - Vendor: IBM */
81 #elif defined(__s390x__) || defined(__s390__)
82 #define ARCH_ALIGNMENT (8U)
83
84 /* mips processor [64-bit] - Vendor: MIPS */
85 #elif defined(__mips64) || defined(__mips64el__)
86 #define ARCH_ALIGNMENT (8U)
87 /* mips processor [32-bit] - Vendor: MIPS */
88 #elif defined(__mips__)
89 #define ARCH_ALIGNMENT (4U)
90
91 /* loongarch processor [64-bit] - Vendor: Loongson */
92 #elif defined(__loongarch__)
93 #define ARCH_ALIGNMENT (8U)
94
95 /* avr processor [8-bit] - Vendor: Microchip */
96 #elif defined(__AVR__)
97 #define ARCH_ALIGNMENT (2U)
98 /* avr32 processor [32-bit] - Vendor: Microchip */
99 #elif defined(__avr32__)
100 #define ARCH_ALIGNMENT (4U)
101
102 /* microblaze processor [32-bit] - Vendor: Xilinx */
103 #elif defined(__MICROBLAZE__)
104 #define ARCH_ALIGNMENT (4U)
105
106 /* sh processor [32-bit] - Vendor: Renesas */
107 #elif defined(__sh__)
108 #define ARCH_ALIGNMENT (4U)
109
110 /* arc processor [32-bit] - Vendor: Synopsys */
111 #elif defined(__arc__)
112 #define ARCH_ALIGNMENT (4U)
113
114 /* bfin processor [32-bit] - Vendor: Analog Devices */
115 #elif defined(__bfin__)
116 #define ARCH_ALIGNMENT (4U)
117
118 /* wasm processor [32-bit] - Vendor: WebAssembly/Emscripten */
119 #elif defined(__EMSCRIPTEN__) || defined(__wasm__)
120 #define ARCH_ALIGNMENT (4U)
121
122 /* sparc processor [32-bit] - Vendor: Oracle */
123 #elif defined(__sparc__) || defined(__sparc)
124 #define ARCH_ALIGNMENT (4U)
125 /* sparc processor [64-bit] - Vendor: Oracle (V9) */
126 #elif defined(__sparcv9__)
127 #define ARCH_ALIGNMENT (8U)
128
129 /* ia64 processor [64-bit] - Vendor: Intel Itanium */
130 #elif defined(__ia64__)
131 #define ARCH_ALIGNMENT (8U)
132
133 /* alpha processor [64-bit] - Vendor: DEC */
134 #elif defined(__alpha__)
135 #define ARCH_ALIGNMENT (8U)
136
137 /* pru processor [32-bit] - Vendor: TI */
138 #elif defined(__TI_PRU__) || defined(__PRU__)
139 #define ARCH_ALIGNMENT (4U)
140 /* PA-RISC [32-bit] – Vendor: HP PA-RISC */
141 #elif defined(__hppa__)
142 #define ARCH_ALIGNMENT (4U)
143
144 /* z/Arch [64-bit] – Vendor: IBM Z */
145 #elif defined(__zarch__)
146 #define ARCH_ALIGNMENT (8U)
147
148 /* SPU [32-bit] – Vendor: Cell Broadband */
149 #elif defined(__cell_spu__)
150 #define ARCH_ALIGNMENT (16U) /* SPU natural aligns to 16 bytes */
151
152 /* NVPTX [64-bit] – Vendor: NVIDIA PTX */
153 #elif defined(__NVPTX__) || defined(__PTX__)
154 #define ARCH_ALIGNMENT (8U)
155
156 /* eBPF [64-bit] – in-kernel VM */
157 #elif defined(__bpf__)
158 #define ARCH_ALIGNMENT (8U)
159
160 /* Elbrus E2K [64-bit] – Vendor: MCST */
161 #elif defined(__e2k__)
162 #define ARCH_ALIGNMENT (8U)
163
164 /* CRIS [32-bit] – Vendor: Axis Communications */
165 #elif defined(__CRIS__)
166 #define ARCH_ALIGNMENT (4U)
167
168 /* TILEGX [64-bit] – Vendor: Tilera */
169 #elif defined(__tilegx__)
170 #define ARCH_ALIGNMENT (8U)
171
172 /* Generic fallback */
173 #else
174 #error "[ERROR] Unknow Architecture: check 'ARCH_ALIGNMENT'"
175 #endif
176#endif
177
191#ifndef __ALIGN
192 #if defined(__GNUC__) || defined(__clang__)
193 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
194 #define __ALIGN \
195 __attribute__((scalar_storage_order("big-endian"), \
196 aligned(ARCH_ALIGNMENT)))
197 #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
198 #define __ALIGN \
199 __attribute__((scalar_storage_order("little-endian"), \
200 aligned(ARCH_ALIGNMENT)))
201 #else
202 #error "[ERROR] Unknow Endianness: check '__BYTE_ORDER__'"
203 #endif
204 #else
205 #define __ALIGN
206 #endif
207#endif
208
221#if defined(_WIN32)
222 #ifdef BUILDING_LIBMEMALLOC
223 #define __LIBMEMALLOC_API __declspec(dllexport)
224 #else
225 #define __LIBMEMALLOC_API __declspec(dllimport)
226 #endif
227#else
228 #if defined(__GNUC__) || defined(__clang__)
229 #define __LIBMEMALLOC_API __attribute__((visibility("default")))
230 #else
231 #define __LIBMEMALLOC_API
232 #endif
233#endif
234
248#if defined(__GNUC__) || defined(__clang__)
249 #define __LIBMEMALLOC_MALLOC \
250 __attribute__((malloc, alloc_size(2), warn_unused_result))
251#else
252 #define __LIBMEMALLOC_MALLOC
253#endif
254
267#if defined(__GNUC__) || defined(__clang__)
268 #define __LIBMEMALLOC_REALLOC \
269 __attribute__((malloc, alloc_size(3), warn_unused_result))
270#else
271 #define __LIBMEMALLOC_REALLOC
272#endif
273
284#define PTR_ERR(x) ((void *)((intptr_t)(x)))
285
295#define ALIGN(x) (((x) + (ARCH_ALIGNMENT - 1U)) & ~(ARCH_ALIGNMENT - 1U))
296
306#define GC_INTERVAL_MS (uint16_t)(100U)
307
325typedef enum AllocationStrategy
326{
327 FIRST_FIT = (uint8_t)(0u),
328 NEXT_FIT = (uint8_t)(1u),
329 BEST_FIT = (uint8_t)(2u)
331
355typedef struct __ALIGN BlockHeader
356{
357 uintptr_t magic;
358 size_t size;
360 uint32_t free;
361 uint32_t marked;
363 const char *var_name;
364 const char *file;
365 uint64_t line;
367 uintptr_t canary;
369 struct BlockHeader *next;
370 struct BlockHeader *prev;
372 struct BlockHeader *fl_next;
373 struct BlockHeader
374 *fl_prev;
376
392typedef struct __ALIGN MemArena
394 size_t num_bins;
396 block_header_t **bins;
397 block_header_t *top_chunk;
399
413typedef struct __ALIGN MmapBlock
414{
415 void *addr;
416 size_t size;
418 struct MmapBlock *next;
419} mmap_t;
420
443typedef struct __ALIGN GcThread
444{
445 pthread_t gc_thread;
446 pthread_t main_thread;
448 bool gc_running;
449 bool gc_exit;
451 uint16_t gc_thread_started;
453 uint32_t gc_interval_ms;
455 pthread_cond_t gc_cond;
456 pthread_mutex_t gc_lock;
458
481typedef struct __ALIGN MemoryAllocator
482{
483 uint8_t *heap_start;
484 uint8_t *heap_end;
486 size_t metadata_size;
488 uintptr_t *stack_top;
489 uintptr_t *stack_bottom;
491 size_t num_size_classes;
492 size_t num_arenas;
494 block_header_t *last_allocated;
495 block_header_t **free_lists;
497 mem_arena_t *arenas;
498 mmap_t *mmap_list;
500 gc_thread_t gc_thread;
502
527__LIBMEMALLOC_API void *MEM_memset(void *const source,
528 const int value,
529 const size_t size);
530
550__LIBMEMALLOC_API void *MEM_memcpy(void *const dest,
551 const void *src,
552 const size_t size);
553
575
595 *const allocator,
596 const size_t size,
597 const char *const var)
599
615 const size_t size,
616 const char *const var)
618
634 const size_t size,
635 const char *const var)
637
654 const size_t size,
655 const char *const var,
656 const allocation_strategy_t strategy)
658
675 const size_t size,
676 const char *const var,
677 const allocation_strategy_t strategy)
679
697 void *const ptr,
698 const size_t new_size,
699 const char *const var,
700 const allocation_strategy_t strategy)
702
718 void *const ptr,
719 const char *const var);
720
737
750
751/*< C++ Compatibility >*/
752#ifdef __cplusplus
753}
754#endif
755
758/*< end of header file >*/
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
Represents the header for a memory block.
Definition libmemalloc.h:353
size_t size
Definition libmemalloc.h:355
struct BlockHeader * fl_prev
Definition libmemalloc.h:370
uint32_t free
Definition libmemalloc.h:357
struct BlockHeader * fl_next
Definition libmemalloc.h:369
uintptr_t canary
Definition libmemalloc.h:364
struct BlockHeader * prev
Definition libmemalloc.h:367
uint32_t marked
Definition libmemalloc.h:358
const char * file
Definition libmemalloc.h:361
uintptr_t magic
Definition libmemalloc.h:354
const char * var_name
Definition libmemalloc.h:360
struct BlockHeader * next
Definition libmemalloc.h:366
uint64_t line
Definition libmemalloc.h:362
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