LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
tmp_alloc.h
浏览该文件的文档.
1/**
2 * Copyright (C) 2026 HJimmyK (Jericho Knox)
3 *
4 * This file is part of LMMP.
5 *
6 * LMMP is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <https://www.gnu.org/licenses/>.
19 *
20 * SPDX-License-Identifier: LGPL-3.0-or-later
21 */
22
23#ifndef __LMMP_TMP_ALLOC_H__
24#define __LMMP_TMP_ALLOC_H__
25
26#include "lmmp/lmmp.h"
27#include <stdio.h>
28
29typedef struct {
30 void* stack_begin; // 栈内存起始地址
31 void* stack_end; // 栈内存结束地址
32 void* stack_top; // 栈内存当前指针位置
33 void* pool_begin; // 缓冲池起始地址
34 void* pool_top; // 缓冲池当前指针位置
35 size_t remain; // 缓冲池剩余内存字节数
36 size_t capacity; // 缓冲池容量(初始化后不变)
38
41
42typedef struct {
43 void* stack_marker; // 栈内存标记
44 void* pool_marker; // 缓冲池标记
45 void* heap_marker; // 堆内存标记
47
48static inline void* lmmp_temp_heap_alloc_(lmmp_alloc_marker* pmarker, size_t size) {
49 /*
50 * pmarker->heap_marker is a head pointer to a linked list of allocated memory blocks.
51 * Each allocated block has a header of size HSIZE, which is used to store the
52 * next pointer of the block. The actual data starts at (mp_byte_t*)p + offset.
53 */
54 // 在经过缓冲池后,size已经对齐至LMMP_MAX_ALIGN
55 // size = LMMP_ROUND_UP_MULTIPLE(size, LMMP_MAX_ALIGN);
56#define HSIZE sizeof(void*)
57 const size_t offset = LMMP_ROUND_UP_MULTIPLE(HSIZE, LMMP_MAX_ALIGN);
58#undef HSIZE
59 void* p = global_heap.alloc(size + offset);
60 /* 默认分配器为裸 malloc,失败返回 NULL;OOM 须无条件中止(对齐
61 lmmp_alloc 契约),否则下面的链表头写入即空指针崩溃 */
62 if (p == NULL) {
63#if LMMP_DEBUG_MEMORY_CHECK == 1
64 char msg[128];
65 snprintf(msg, sizeof(msg), "Memory allocation failure (trying to allocate: %zu bytes)", size);
66 lmmp_abort(LMMP_ERROR_MEMORY_ALLOC_FAILURE, msg, __func__, __LINE__);
67#else
68 lmmp_abort(LMMP_ERROR_MEMORY_ALLOC_FAILURE, "Memory allocation failure", __func__, __LINE__);
69#endif
70 }
71 *(void**)p = pmarker->heap_marker;
72 pmarker->heap_marker = p;
73 return (mp_byte_t*)p + offset;
74}
75
76static inline void lmmp_temp_heap_free_(lmmp_alloc_marker* pmarker) {
77 /*
78 * Free all allocated memory blocks in the linked list pointed to by pmarker->heap_marker.
79 */
80 while (pmarker->heap_marker != NULL) {
81 void* next = *(void**)(pmarker->heap_marker);
83 pmarker->heap_marker = next;
84 }
85}
86
87static inline void* lmmp_temp_stack_alloc_(lmmp_alloc_marker* pmarker, size_t size) {
88 /*
89 * On the first call, pmarker->stack_marker is a null pointer.
90 * We will use pmarker->stack_marker to record the stack frame at this time.
91 * When allocating memory subsequently, we will not modify pmarker->stack_marker.
92 * Until all stack memory is finally released, we will move to the initial stack position at once,
93 * which is the position recorded by pmarker->stack_marker.
94 */
96 size_t offset = LMMP_ROUND_UP_MULTIPLE(size, LMMP_MAX_ALIGN);
97 mp_byte_t* new_top = p + offset;
98 /* 越界检查无条件保留:越过后将静默破坏相邻堆,宁可中止 */
99 if (new_top > (mp_byte_t*)(lmmp_tmpmem_ctx.stack_end)) {
100#if LMMP_DEBUG_STACK_OVERFLOW_CHECK == 1
101 char msg[128];
102 snprintf(msg, sizeof(msg), "Stack overflow (trying to allocate: %zu bytes, stack remaining: %zu bytes)", offset,
104 lmmp_abort(LMMP_ERROR_MEMORY_ALLOC_FAILURE, msg, __func__, __LINE__);
105#else
106 lmmp_abort(LMMP_ERROR_MEMORY_ALLOC_FAILURE, "Stack overflow", __func__, __LINE__);
107#endif
108 }
109 lmmp_tmpmem_ctx.stack_top = new_top;
110 if (pmarker->stack_marker == NULL)
111 pmarker->stack_marker = p;
112 return p;
113}
114
115static inline void lmmp_temp_stack_free_(lmmp_alloc_marker* pmarker) {
116 // pmarker->stack_marker is not NULL
118}
119
120static inline void* lmmp_temp_pool_alloc_(lmmp_alloc_marker* pmarker, size_t size) {
122 size_t offset = LMMP_ROUND_UP_MULTIPLE(size, LMMP_MAX_ALIGN);
123 size_t remaining = lmmp_tmpmem_ctx.remain;
124 if (remaining < 2 * offset) {
125 // 保证pool留有一个offset的空间,使得递归后子问题也尽可能分配到pool中,避免递归时子问题分配到堆上
126 return lmmp_temp_heap_alloc_(pmarker, offset);
127 } else {
128 mp_byte_t* new_top = p + offset;
129 lmmp_tmpmem_ctx.remain -= offset;
130 lmmp_tmpmem_ctx.pool_top = new_top;
131 if (pmarker->pool_marker == NULL)
132 pmarker->pool_marker = p;
133 return p;
134 }
135}
136
137static inline void lmmp_temp_pool_free_(lmmp_alloc_marker* pmarker) {
138 // pmarker->pool_marker is not NULL
139 size_t freed = (mp_byte_t*)(lmmp_tmpmem_ctx.pool_top) - (mp_byte_t*)(pmarker->pool_marker);
140 lmmp_tmpmem_ctx.remain += freed;
142}
143
144// 临时内存标记声明:用于跟踪临时内存分配
145#define TEMP_DECL \
146 lmmp_alloc_marker __lmmp_marker_ = {NULL, NULL, NULL}; \
147 int __lmmp_temp_decl_guard_ = 0 // if TEMP_DECL without TEMP_FREE, compiler will warn this
148
149#define TEMP_B_DECL \
150 lmmp_alloc_marker __lmmp_marker_ = {NULL, NULL, NULL}; \
151 int __lmmp_temp_b_decl_guard_ = 0 // if TEMP_B_DECL without TEMP_B_FREE, compiler will warn this
152
153#define TEMP_S_DECL \
154 lmmp_alloc_marker __lmmp_marker_ = {NULL, NULL, NULL}; \
155 int __lmmp_temp_s_decl_guard_ = 0 // if TEMP_S_DECL without TEMP_S_FREE, compiler will warn this
156
157/*
158不可将此值设置的过大,如想设置此值更大,请将内部栈大小相应增大
159*/
160#define TEMP_SALLOC_THRESHOLD 0x7f00 // 小内存分配阈值(小于等于该值的内存可能分配在内部栈上)
161
162// 栈内存分配:使用lmmp_temp_stack_alloc_在栈上分配n字节内存(小内存)
163#define TEMP_SALLOC(n) lmmp_temp_stack_alloc_(&__lmmp_marker_, (n))
164// 缓冲池分配:使用lmmp_temp_pool_alloc_优先在pool上分配n字节内存(大内存)
165#define TEMP_BALLOC(n) lmmp_temp_pool_alloc_(&__lmmp_marker_, (n))
166// 临时内存分配:小内存用栈,大内存用堆
167#define TEMP_TALLOC(n) ((n) <= TEMP_SALLOC_THRESHOLD ? TEMP_SALLOC(n) : TEMP_BALLOC(n))
168// 类型化栈内存分配:分配n个type类型的栈内存
169#define SALLOC_TYPE(n, type) ((type*)TEMP_SALLOC((n) * sizeof(type)))
170// 类型化堆内存分配:分配n个type类型的堆内存
171#define BALLOC_TYPE(n, type) ((type*)TEMP_BALLOC((n) * sizeof(type)))
172// 类型化临时内存分配:智能选择栈/堆分配n个type类型内存
173#define TALLOC_TYPE(n, type) ((type*)TEMP_TALLOC((n) * sizeof(type)))
174// 临时内存释放:释放所有通过TEMP_XALLOC系列函数分配的临时内存
175#define TEMP_FREE \
176 do { \
177 (void)__lmmp_temp_decl_guard_; \
178 lmmp_temp_heap_free_(&__lmmp_marker_); \
179 if (__lmmp_marker_.stack_marker != NULL) \
180 lmmp_temp_stack_free_(&__lmmp_marker_); \
181 if (__lmmp_marker_.pool_marker != NULL) \
182 lmmp_temp_pool_free_(&__lmmp_marker_); \
183 } while (0)
184#define TEMP_B_FREE \
185 do { \
186 (void)__lmmp_temp_b_decl_guard_; \
187 lmmp_temp_heap_free_(&__lmmp_marker_); \
188 if (__lmmp_marker_.pool_marker != NULL) \
189 lmmp_temp_pool_free_(&__lmmp_marker_); \
190 } while (0)
191#define TEMP_S_FREE \
192 do { \
193 (void)__lmmp_temp_s_decl_guard_; \
194 if (__lmmp_marker_.stack_marker != NULL) \
195 lmmp_temp_stack_free_(&__lmmp_marker_); \
196 } while (0)
197
198// 类型化内存分配:分配n个type类型的内存(堆)
199#define ALLOC_TYPE(n, type) ((type*)lmmp_alloc((size_t)(n) * sizeof(type)))
200// 类型化内存重分配:将p指向的内存重分配为new_size个type类型
201#define REALLOC_TYPE(p, new_size, type) ((type*)lmmp_realloc((p), (new_size) * sizeof(type)))
202
203#endif /* __LMMP_TMP_ALLOC_H__ */
uint8_t mp_byte_t
定义 lmmp.h:91
#define LMMP_MAX_ALIGN
定义 lmmp.h:100
lmmp_heap_alloc_fn alloc
定义 lmmp.h:240
void lmmp_abort(lmmp_error_t type, const char *msg, const char *func, int line)
LMMP 全局退出函数,内部错误或断言失败时调用,若设置了全局退出函数,则会调用该函数,否则会调用默认的退出函数。
lmmp_heap_free_fn free
定义 lmmp.h:241
#define LMMP_THREAD_LOCAL
定义 lmmp.h:114
@ LMMP_ERROR_MEMORY_ALLOC_FAILURE
定义 lmmp.h:285
#define LMMP_ROUND_UP_MULTIPLE(a, m)
将a向上取整到m的倍数
定义 lmmp.h:471
static void * lmmp_temp_heap_alloc_(lmmp_alloc_marker *pmarker, size_t size)
__thread lmmp_heap_allocator_t global_heap
static void * lmmp_temp_stack_alloc_(lmmp_alloc_marker *pmarker, size_t size)
__thread lmmp_memory_ctx lmmp_tmpmem_ctx
static void lmmp_temp_stack_free_(lmmp_alloc_marker *pmarker)
static void lmmp_temp_heap_free_(lmmp_alloc_marker *pmarker)
static void * lmmp_temp_pool_alloc_(lmmp_alloc_marker *pmarker, size_t size)
static void lmmp_temp_pool_free_(lmmp_alloc_marker *pmarker)
#define HSIZE
Copyright (C) 2026 HJimmyK (Jericho Knox)