LAMMP 4.1.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
tmp_alloc.h
浏览该文件的文档.
1/*
2 * [LAMMP]
3 * Copyright (C) [2025-2026] [HJimmyK(Jericho Knox)]
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#ifndef __LAMMP_TMP_ALLOC_H__
20#define __LAMMP_TMP_ALLOC_H__
21
22#include "../lmmp.h"
23#include <stdio.h>
24
28
29/**
30 * @brief 临时堆内存分配函数
31 * @param pmarker 标记
32 * @param size 要分配的内存字节数
33 */
34void* lmmp_temp_heap_alloc_(void** pmarker, size_t size);
35
36/**
37 * @brief 临时堆内存释放函数
38 * @param marker 要释放的临时内存标记
39 */
40void lmmp_temp_heap_free_(void* marker);
41
42static inline void* lmmp_temp_stack_alloc_(void** pmarker, size_t size) {
43 /*
44 * On the first call, *pmarker is a null pointer.
45 * We will use *pmarker to record the stack frame at this time.
46 * When allocating memory subsequently, we will not modify *pmarker.
47 * Until all stack memory is finally released, we will move to the initial stack position at once,
48 * which is the position recorded by *pmarker.
49 */
50 void* p = lmmp_stack_top;
51 size_t offset = LMMP_ROUND_UP_MULTIPLE(size, LAMMP_MAX_ALIGN);
52 void* new_top = (void*)((mp_byte_t*)p + offset);
53#if LAMMP_DEBUG_STACK_OVERFLOW_CHECK == 1
54 if (new_top > lmmp_stack_end) {
55 char msg[128];
56 snprintf(msg, sizeof(msg), "Stack overflow (trying to allocate: %zu bytes, stack remaining: %zu bytes)", offset,
58 lmmp_abort(LAMMP_ERROR_MEMORY_ALLOC_FAILURE, msg, __func__, __LINE__);
59 }
60#endif
61 lmmp_stack_top = new_top;
62 if (*pmarker == NULL)
63 *pmarker = p;
64 return p;
65}
66
67static inline void lmmp_temp_stack_free_(void* marker) {
68 lmmp_stack_top = marker;
69}
70
71// 临时内存标记声明:用于跟踪临时内存分配
72#define TEMP_DECL \
73 void *lmmp_temp_alloc_marker_ = NULL, *lmmp_temp_stack_marker_ = NULL
74
75#define TEMP_B_DECL void* lmmp_temp_alloc_marker_ = NULL
76#define TEMP_S_DECL void* lmmp_temp_stack_marker_ = NULL
77
78#define TEMP_SALLOC_THRESHOLD 0x7f00 // 小内存分配阈值(小于等于该值的内存分配在栈上)
79
80// 栈内存分配:使用lmmp_temp_stack_alloc_在栈上分配n字节内存(小内存)
81#define TEMP_SALLOC(n) lmmp_temp_stack_alloc_(&lmmp_temp_stack_marker_, (n))
82// 堆内存分配:使用lmmp_temp_heap_alloc_在堆上分配n字节内存(大内存)
83#define TEMP_BALLOC(n) lmmp_temp_heap_alloc_(&lmmp_temp_alloc_marker_, (n))
84// 临时内存分配:小内存用栈,大内存用堆
85#define TEMP_TALLOC(n) ((n) <= TEMP_SALLOC_THRESHOLD ? TEMP_SALLOC(n) : TEMP_BALLOC(n))
86// 类型化栈内存分配:分配n个type类型的栈内存
87#define SALLOC_TYPE(n, type) ((type*)TEMP_SALLOC((n) * sizeof(type)))
88// 类型化堆内存分配:分配n个type类型的堆内存
89#define BALLOC_TYPE(n, type) ((type*)TEMP_BALLOC((n) * sizeof(type)))
90// 类型化临时内存分配:智能选择栈/堆分配n个type类型内存
91#define TALLOC_TYPE(n, type) ((type*)TEMP_TALLOC((n) * sizeof(type)))
92// 临时内存释放:释放所有通过TEMP_XALLOC系列函数分配的临时内存
93#define TEMP_FREE \
94 do { \
95 if (lmmp_temp_alloc_marker_) \
96 lmmp_temp_heap_free_(lmmp_temp_alloc_marker_); \
97 if (lmmp_temp_stack_marker_) \
98 lmmp_temp_stack_free_(lmmp_temp_stack_marker_); \
99 } while (0)
100#define TEMP_B_FREE \
101 do { \
102 if (lmmp_temp_alloc_marker_) \
103 lmmp_temp_heap_free_(lmmp_temp_alloc_marker_); \
104 } while (0)
105#define TEMP_S_FREE \
106 do { \
107 if (lmmp_temp_stack_marker_) \
108 lmmp_temp_stack_free_(lmmp_temp_stack_marker_); \
109 } while (0)
110
111// 类型化内存分配:分配n个type类型的内存(堆)
112#define ALLOC_TYPE(n, type) ((type*)lmmp_alloc((size_t)(n) * sizeof(type)))
113// 类型化内存重分配:将p指向的内存重分配为new_size个type类型
114#define REALLOC_TYPE(p, new_size, type) ((type*)lmmp_realloc((p), (new_size) * sizeof(type)))
115
116#endif /* __LAMMP_TMP_ALLOC_H__ */
uint8_t mp_byte_t
Definition lmmp.h:210
#define LAMMP_MAX_ALIGN
Definition lmmp.h:219
void lmmp_abort(lmmp_error_t type, const char *msg, const char *func, int line)
LAMMP 全局退出函数,内部错误或断言失败时调用,若设置了全局退出函数,则会调用该函数,否则会调用默认的退出函数。
Definition abort.c:42
#define LAMMP_THREAD_LOCAL
Definition lmmp.h:237
@ LAMMP_ERROR_MEMORY_ALLOC_FAILURE
Definition lmmp.h:136
#define LMMP_ROUND_UP_MULTIPLE(a, m)
Definition lmmp.h:361
void lmmp_temp_heap_free_(void *marker)
临时堆内存释放函数
Definition memory.c:104
void * lmmp_temp_heap_alloc_(void **pmarker, size_t size)
临时堆内存分配函数
Definition memory.c:91
_Thread_local void * lmmp_stack_begin
Definition memory.c:32
_Thread_local void * lmmp_stack_top
Definition memory.c:34
_Thread_local void * lmmp_stack_end
Definition memory.c:33
static void * lmmp_temp_stack_alloc_(void **pmarker, size_t size)
Definition tmp_alloc.h:42
static void lmmp_temp_stack_free_(void *marker)
Definition tmp_alloc.h:67