30#undef lmmp_stack_alloc
32#undef lmmp_leak_tracker
33#define HSIZE sizeof(void*)
41#define heap_alloc_func global_heap.alloc
42#define heap_free_func global_heap.free
43#define realloc_func global_heap.realloc
104#if LMMP_DEBUG_MEMORY_LEAK == 1
111#if LMMP_DEBUG_MEMORY_CHECK == 1
122#define MEM_MAGIC 0xDEADBEEFDEADBEEFULL
123#define MEM_GUARD 0xDEADBEEFUL
124#define EXTRA_MEM_PATTERN 0xAA
126#define ALIGNMENT LMMP_MAX_ALIGN
128static inline size_t align_up(
size_t size) {
129 return (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
135static inline void find_corruption_range(
137 unsigned char pattern,
147 for (
size_t i = 0; i < len; i++) {
148 if ((
unsigned char)data[i] != pattern) {
160static inline int check_extra_memory_overflow(mem_header* hdr,
void* user_ptr,
const char* check_func,
int check_line) {
161 if (!hdr || !user_ptr || hdr->extra_size == 0)
164 size_t aligned_user_size = align_up(hdr->user_size);
165 char* extra_start = (
char*)user_ptr + aligned_user_size;
167 int first, last, count;
168 find_corruption_range(extra_start, EXTRA_MEM_PATTERN, hdr->extra_size, &first, &last, &count);
173 const int buf_size =
sizeof(error_buf);
175#define SAFE_APPEND(...) \
177 if (offset < buf_size) { \
178 int n = snprintf(error_buf + offset, (size_t)(buf_size - offset), __VA_ARGS__); \
184 SAFE_APPEND(
"Memory overflow (extra memory corruption) detected!%s",
"\n");
185 SAFE_APPEND(
"Memory header:%s",
"\n");
186 SAFE_APPEND(
" allocated at: [%s]:%d\n", hdr->func, hdr->line);
187 SAFE_APPEND(
" checked at: [%s]:%d\n", check_func, check_line);
188 SAFE_APPEND(
" user size: %zu bytes\n", hdr->user_size);
189 SAFE_APPEND(
" extra size: %zu bytes (%.0f%% of user size)\n", hdr->extra_size,
190 LMMP_MEMORY_MORE_ALLOC_TIMES * 10.0);
191 SAFE_APPEND(
" user ptr: %p\n", user_ptr);
192 SAFE_APPEND(
" extra memory: %p to %p\n", (
void*)extra_start, (
void*)(extra_start + hdr->extra_size - 1));
193 SAFE_APPEND(
" corrupted range: offset %d to %d (total %d bytes)\n", first, last, count);
194 SAFE_APPEND(
"Likely cause: Buffer overflow beyond the end of the memory.%s",
"\n");
196 error_buf[buf_size - 1] =
'\0';
206static inline int check_memory_block_integrity(mem_header* hdr,
void* user_ptr,
const char* check_func,
int check_line) {
207 if (!hdr || !user_ptr)
210 if (hdr->magic != MEM_MAGIC || hdr->guard != MEM_GUARD) {
212 snprintf(error_buf,
sizeof(error_buf),
213 "Memory header corruption detected!\n"
214 " Magic: 0x%016llx (expected 0x%016llx)\n"
215 " Guard: 0x%08lx (expected 0x%08lx)\n"
216 "Possible overflow or underflow or invalid pointer.",
217 hdr->magic, MEM_MAGIC, (
unsigned long)hdr->guard, MEM_GUARD);
222 return check_extra_memory_overflow(hdr, user_ptr, check_func, check_line);
232static inline void* lmmp_alloc_debug(
size_t size,
const char* func,
int line) {
238 size_t extra_size = (size * LMMP_MEMORY_MORE_ALLOC_TIMES) / 10;
239 extra_size = align_up(extra_size);
241 size_t header_size = align_up(
sizeof(mem_header));
242 size_t aligned_user_size = align_up(size);
243 size_t total_size = header_size + aligned_user_size + extra_size;
248 snprintf(msg,
sizeof(msg),
"Memory allocation failed (size: %zu bytes, extra: %zu bytes)", size, extra_size);
253 mem_header* hdr = (mem_header*)base;
254 void* user_ptr = (
char*)base + header_size;
255 void* extra_mem = (
char*)user_ptr + aligned_user_size;
257 hdr->magic = MEM_MAGIC;
258 hdr->user_size = size;
259 hdr->total_size = total_size;
260 hdr->extra_size = extra_size;
263 hdr->guard = MEM_GUARD;
265 memset(extra_mem, EXTRA_MEM_PATTERN, extra_size);
276static inline void lmmp_free_debug(
void* ptr,
const char* func,
int line) {
280 size_t header_size = align_up(
sizeof(mem_header));
281 mem_header* hdr = (mem_header*)((
char*)ptr - header_size);
283 check_memory_block_integrity(hdr, ptr, func, line);
296static inline void* lmmp_realloc_debug(
void* ptr,
size_t new_size,
const char* func,
int line) {
298 return lmmp_alloc_debug(new_size, func, line);
304 size_t header_size = align_up(
sizeof(mem_header));
305 mem_header* old_hdr = (mem_header*)((
char*)ptr - header_size);
307 check_memory_block_integrity(old_hdr, ptr, func, line);
309 void* new_ptr = lmmp_alloc_debug(new_size, func, line);
313 size_t copy_size = (old_hdr->user_size < new_size) ? old_hdr->user_size : new_size;
314 memcpy(new_ptr, ptr, copy_size);
316 lmmp_free_debug(ptr, func, line);
324#undef EXTRA_MEM_PATTERN
341 const int max_len =
sizeof(msg) - 1;
345 snprintf(msg + offset, max_len - offset,
"Heap allocations not freed: %d block(s);\n",
heap_alloc_count);
349 offset += snprintf(msg + offset, max_len - offset,
350 "Default stack allocator is not empty. top: %p, begin: %p, end: %p;\n",
355 offset += snprintf(msg + offset, max_len - offset,
356 "Default pool allocator is not empty. top: %p, begin: %p; remain: %zu bytes, capacity: %zu "
367#if LMMP_DEBUG_MEMORY_CHECK == 1
368void*
lmmp_alloc(
size_t size,
const char* func,
int line) {
369 void* ret = lmmp_alloc_debug(size, func, line);
370#if LMMP_DEBUG_MEMORY_LEAK == 1
378 snprintf(msg,
sizeof(msg),
"Memory allocation failed (size: %zu bytes)", size);
383#if LMMP_DEBUG_PARAM_ASSERT_CHECK == 1
392#if LMMP_DEBUG_MEMORY_LEAK == 1
399#if LMMP_DEBUG_MEMORY_CHECK == 1
400void*
lmmp_realloc(
void* oldptr,
size_t new_size,
const char* func,
int line) {
401 void* ret = lmmp_realloc_debug(oldptr, new_size, func, line);
402#if LMMP_DEBUG_MEMORY_LEAK == 1
410#if LMMP_DEBUG_PARAM_ASSERT_CHECK == 1
420#if LMMP_DEBUG_MEMORY_LEAK == 1
428#if LMMP_DEBUG_MEMORY_CHECK == 1
429void lmmp_free(
void* ptr,
const char* func,
int line) {
430 lmmp_free_debug(ptr, func, line);
431#if LMMP_DEBUG_MEMORY_LEAK == 1
439#if LMMP_DEBUG_MEMORY_LEAK == 1
#define LMMP_DEFAULT_STACK_SIZE
Copyright (C) 2026 HJimmyK (Jericho Knox)
void * lmmp_realloc(void *ptr, size_t size)
内存重分配函数(调用lmmp_realloc_fn)
void lmmp_set_heap_allocator(const lmmp_heap_allocator_t *heap)
设置 LMMP 全局堆内存分配函数
void *(*) lmmp_heap_alloc_fn(size_t size)
void(*) lmmp_heap_free_fn(void *ptr)
void * lmmp_alloc(size_t size)
内存分配函数(调用lmmp_heap_alloc_fn)
void lmmp_abort(lmmp_error_t type, const char *msg, const char *func, int line)
LMMP 全局退出函数,内部错误或断言失败时调用,若设置了全局退出函数,则会调用该函数,否则会调用默认的退出函数。
int lmmp_stack_deinit(void)
LMMP 全局栈释放函数(通常不需要手动调用)
int lmmp_stack_init(size_t size)
LMMP 全局栈初始化函数(通常不需要手动调用)
void lmmp_free(void *ptr)
内存释放函数(调用lmmp_heap_free_fn)
void lmmp_global_init(void)
全局初始化函数(线程局部的)
void lmmp_global_deinit(void)
全局释放函数(线程局部的)
#define LMMP_THREAD_LOCAL
void *(*) lmmp_realloc_fn(void *ptr, size_t size)
@ LMMP_ERROR_MEMORY_ALLOC_FAILURE
@ LMMP_ERROR_MEMORY_FREE_FAILURE
@ LMMP_ERROR_OUT_OF_BOUNDS
#define lmmp_leak_tracker
int lmmp_alloc_count(int cnt)
堆内存分配计数器(线程局部)
static __thread int heap_alloc_count
static void lmmp_memory_abort(size_t size, const char *func, int line)
__thread lmmp_heap_allocator_t global_heap
__thread lmmp_memory_ctx lmmp_tmpmem_ctx
Copyright (C) 2026 HJimmyK (Jericho Knox)