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
105#include "../../../include/lammp/impl/safe_memory.h"
106
108 if (cnt != 0) {
109 int new_cnt = cnt;
112 return cnt;
113 }
114 return heap_alloc_count;
115}
116
117void lmmp_leak_tracker(const char* func, int line) {
118 char msg[360] = {0};
119 int offset = 0;
120 const int max_len = sizeof(msg) - 1;
121 bool t = false;
122 if (heap_alloc_count != 0) {
123 offset +=
124 snprintf(msg + offset, max_len - offset, "Heap allocations not freed: %d block(s);\n", heap_alloc_count);
125 t = true;
126 }
129 "Default stack allocator is not empty. top: %p, begin: %p, end: %p;\n",
131 t = true;
132 }
135 "Default pool allocator is not empty. top: %p, begin: %p; remain: %zu bytes, capacity: %zu "
136 "bytes;\n",
139 t = true;
140 }
141 if (t) {
143 }
144}
145
146#if LAMMP_DEBUG_MEMORY_CHECK == 1
147void* lmmp_alloc(size_t size, const char* func, int line) {
148 void* ret = lmmp_alloc_debug(size, func, line);
149#if LAMMP_DEBUG_MEMORY_LEAK == 1
151#endif
152 return ret;
153}
154#else
155static inline void lmmp_memory_abort(size_t size, const char* func, int line) {
156 char msg[64];
157 snprintf(msg, sizeof(msg), "Memory allocation failed (size: %zu bytes)", size);
159}
160
161void* lmmp_alloc(size_t size) {
162#if LAMMP_DEBUG_PARAM_ASSERT_CHECK == 1
163 if (size == 0) {
164 lmmp_abort(LAMMP_ERROR_MEMORY_ALLOC_FAILURE, "Allocating zero bytes is not allowed.", __func__, __LINE__);
165 return NULL;
166 }
167#endif // LAMMP_DEBUG_PARAM_ASSERT_CHECK == 1
168 void* ret = heap_alloc_func(size);
169 if (ret == NULL)
171#if LAMMP_DEBUG_MEMORY_LEAK == 1
173#endif
174 return ret;
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) {
185#if LAMMP_DEBUG_PARAM_ASSERT_CHECK == 1
186 if (new_size == 0) {
187 lmmp_abort(LAMMP_ERROR_MEMORY_ALLOC_FAILURE, "Reallocating zero bytes is not allowed.", __func__, __LINE__);
188 return NULL;
189 }
190#endif // LAMMP_DEBUG_PARAM_ASSERT_CHECK == 1
192 if (ret == NULL) {
194 }
195 return ret;
196}
197#endif // LAMMP_DEBUG_MEMORY_CHECK == 1
198
199#if LAMMP_DEBUG_MEMORY_CHECK == 1
200void lmmp_free(void* ptr, const char* func, int line) {
201 lmmp_free_debug(ptr, func, line);
202#if LAMMP_DEBUG_MEMORY_LEAK == 1
203 if (ptr != NULL)
205#endif
206}
207#else
208void lmmp_free(void* ptr) {
209 heap_free_func(ptr);
210#if LAMMP_DEBUG_MEMORY_LEAK == 1
211 if (ptr != NULL)
213#endif
214}
215#endif
216
220
uint8_t mp_byte_t
Definition lmmp.h:75
void(* lmmp_heap_free_fn)(void *ptr)
Definition lmmp.h:172
#define LAMMP_MAX_ALIGN
Definition lmmp.h:84
lmmp_heap_alloc_fn alloc
Definition lmmp.h:176
void *(* lmmp_realloc_fn)(void *ptr, size_t size)
Definition lmmp.h:173
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:171
@ LAMMP_ERROR_MEMORY_ALLOC_FAILURE
Definition lmmp.h:221
@ LAMMP_ERROR_MEMORY_LEAK
Definition lmmp.h:224
#define lmmp_leak_tracker
Definition lmmp.h:367
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:161
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:208
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:217
static void lmmp_memory_abort(size_t size, const char *func, int line)
Definition memory.c:155
void lmmp_global_deinit(void)
(线程局部的)全局共享的动态分配的堆内存资源释放函数
Definition memory.c:221
_Thread_local lmmp_heap_allocator_t global_heap
Definition memory.c:29
int lmmp_alloc_count(int cnt)
堆内存分配计数器(线程局部)
Definition memory.c:107
#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