LAMMP 4.2.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
memory.c
浏览该文件的文档.
1/**
2 * Copyright (C) 2026 HJimmyK(Jericho Knox)
3 *
4 * This file is part of LAMMP.
5 *
6 * LAMMP is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License (LGPL) as published
8 * by the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed WITHOUT ANY WARRANTY.
12 *
13 * See <https://www.gnu.org/licenses/>.
14 */
15
16#include "../../../include/lammp/impl/mparam.h"
17#include "../../../include/lammp/impl/prime_table.h"
18#include "../../../include/lammp/impl/tmp_alloc.h"
19#include "../../../include/lammp/lmmpn.h"
20
21#undef lmmp_alloc
22#undef lmmp_realloc
23#undef lmmp_free
24#undef lmmp_stack_alloc
25#undef lmmp_stack_free
26#undef lmmp_leak_tracker
27#define HSIZE sizeof(void*)
28
34
35#define heap_alloc_func global_heap.alloc
36#define heap_free_func global_heap.free
37#define realloc_func global_heap.realloc
38
40
43 .stack_end = NULL,
44 .stack_top = NULL,
45 .pool_begin = NULL,
46 .pool_top = NULL,
47 .remain = 0,
48 .capacity = 0,
49};
50
68
69int lmmp_stack_init(size_t size) {
71 return -1;
72 } else {
75 lmmp_abort(LAMMP_ERROR_MEMORY_ALLOC_FAILURE, "Failed to allocate stack memory", __func__, __LINE__);
76 }
79 if (size > LAMMP_MAX_ALIGN) {
80 // 只有当size大于最大对齐单位时,才分配缓冲池
83 heap_free_func(lmmp_tmpmem_ctx.stack_begin); // Free old stack memory
84 lmmp_abort(LAMMP_ERROR_MEMORY_ALLOC_FAILURE, "Failed to allocate pool memory", __func__, __LINE__);
85 }
89 }
90 return 0;
91 }
92}
93
95 if (heap == NULL)
96 return;
98#if LAMMP_DEBUG_MEMORY_LEAK == 1
99 lmmp_leak_tracker(__func__, __LINE__); // Check for memory leaks before setting new allocator
100#endif
101 global_heap = *heap;
103}
104
105static inline void lmmp_chech_memory(size_t size, const char* func, int line) {
106 char msg[64];
107 snprintf(msg, sizeof(msg), "Memory allocation failed (size: %zu bytes)", size);
109}
110
111#include "../../../include/lammp/impl/safe_memory.h"
112
114 if (cnt != 0) {
115 int new_cnt = cnt;
118 return cnt;
119 }
120 return heap_alloc_count;
121}
122
123void lmmp_leak_tracker(const char* func, int line) {
124 char msg[360] = {0};
125 int offset = 0;
126 const int max_len = sizeof(msg) - 1;
127 bool t = false;
128 if (heap_alloc_count != 0) {
129 offset +=
130 snprintf(msg + offset, max_len - offset, "Heap allocations not freed: %d block(s);\n", heap_alloc_count);
131 t = true;
132 }
135 "Default stack allocator is not empty. top: %p, begin: %p, end: %p;\n",
137 t = true;
138 }
141 "Default pool allocator is not empty. top: %p, begin: %p; remain: %zu bytes, capacity: %zu "
142 "bytes;\n",
145 t = true;
146 }
147 if (t) {
149 }
150}
151
152#if LAMMP_DEBUG_MEMORY_CHECK == 1
153void* lmmp_alloc(size_t size, const char* func, int line) {
154 if (size) {
155 void* ret = lmmp_alloc_debug(size, func, line);
156#if LAMMP_DEBUG_MEMORY_LEAK == 1
158#endif
159 return ret;
160 }
161 return NULL;
162}
163#else
164void* lmmp_alloc(size_t size) {
165 if (size) {
166 void* ret = heap_alloc_func(size);
167 if (ret == NULL)
169#if LAMMP_DEBUG_MEMORY_LEAK == 1
171#endif
172 return ret;
173 }
174 return NULL;
175}
176#endif
177
178#if LAMMP_DEBUG_MEMORY_CHECK == 1
179void* lmmp_realloc(void* oldptr, size_t new_size, const char* func, int line) {
180 void* ret = lmmp_realloc_debug(oldptr, new_size, func, line);
181 return ret;
182}
183#else
184void* lmmp_realloc(void* oldptr, size_t new_size) {
186 if (ret == NULL)
188 return ret;
189}
190#endif
191
192#if LAMMP_DEBUG_MEMORY_CHECK == 1
193void lmmp_free(void* ptr, const char* func, int line) {
194 if (ptr) {
195 lmmp_free_debug(ptr, func, line);
196#if LAMMP_DEBUG_MEMORY_LEAK == 1
198#endif
199 }
200}
201#else
202void lmmp_free(void* ptr) {
203 if (ptr) {
204 heap_free_func(ptr);
205#if LAMMP_DEBUG_MEMORY_LEAK == 1
207#endif
208 }
209}
210#endif
211
215
uint8_t mp_byte_t
Definition lmmp.h:75
void(* lmmp_heap_free_fn)(void *ptr)
Definition lmmp.h:166
#define LAMMP_MAX_ALIGN
Definition lmmp.h:84
lmmp_heap_alloc_fn alloc
Definition lmmp.h:170
void *(* lmmp_realloc_fn)(void *ptr, size_t size)
Definition lmmp.h:167
void lmmp_abort(lmmp_error_t type, const char *msg, const char *func, int line)
LAMMP 全局退出函数,内部错误或断言失败时调用,若设置了全局退出函数,则会调用该函数,否则会调用默认的退出函数。
Definition abort.c:102
#define LAMMP_THREAD_LOCAL
Definition lmmp.h:102
void *(* lmmp_heap_alloc_fn)(size_t size)
Definition lmmp.h:165
@ LAMMP_ERROR_MEMORY_ALLOC_FAILURE
Definition lmmp.h:215
@ LAMMP_ERROR_MEMORY_LEAK
Definition lmmp.h:218
#define lmmp_leak_tracker
Definition lmmp.h:345
void lmmp_set_heap_allocator(const lmmp_heap_allocator_t *heap)
设置 LAMMP 全局堆内存分配函数
Definition memory.c:94
#define heap_alloc_func
Definition memory.c:35
_Thread_local lmmp_memory_ctx lmmp_tmpmem_ctx
Definition memory.c:41
void * lmmp_alloc(size_t size)
内存分配函数(调用lmmp_heap_alloc_fn)
Definition memory.c:164
int lmmp_stack_deinit(void)
LAMMP 全局栈释放函数(通常不需要手动调用)
Definition memory.c:51
int lmmp_stack_init(size_t size)
LAMMP 全局栈初始化函数(通常不需要手动调用)
Definition memory.c:69
void lmmp_free(void *ptr)
内存释放函数(调用lmmp_heap_free_fn)
Definition memory.c:202
void * lmmp_realloc(void *oldptr, size_t new_size)
内存重分配函数(调用lmmp_realloc_fn)
Definition memory.c:184
#define realloc_func
Definition memory.c:37
static _Thread_local int heap_alloc_count
Definition memory.c:39
#define heap_free_func
Definition memory.c:36
void lmmp_global_init(void)
全局初始化函数(线程局部的)
Definition memory.c:212
void lmmp_global_deinit(void)
(线程局部的)全局共享的动态分配的堆内存资源释放函数
Definition memory.c:216
_Thread_local lmmp_heap_allocator_t global_heap
Definition memory.c:29
static void lmmp_chech_memory(size_t size, const char *func, int line)
Definition memory.c:105
int lmmp_alloc_count(int cnt)
堆内存分配计数器(线程局部)
Definition memory.c:113
#define LAMMP_DEFAULT_STACK_SIZE
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition mparam.h:20
#define LAMMP_POOL_SIZE
Definition mparam.h:23
#define t
#define n
void lmmp_prime_int_table_free_(void)
释放全局素数表
static void * lmmp_realloc_debug(void *ptr, size_t new_size, const char *func, int line)
调试版 realloc
static void * lmmp_alloc_debug(size_t size, const char *func, int line)
调试版 malloc
static void lmmp_free_debug(void *ptr, const char *func, int line)
调试版 free
void * stack_top
Definition tmp_alloc.h:25
size_t capacity
Definition tmp_alloc.h:29
void * pool_begin
Definition tmp_alloc.h:26
void * stack_end
Definition tmp_alloc.h:24
void * stack_begin
Definition tmp_alloc.h:23
void * pool_top
Definition tmp_alloc.h:27
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition tmp_alloc.h:22