LAMMP 4.2.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
safe_memory.h 文件参考
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+ safe_memory.h 的引用(Include)关系图:
+ 此图展示该文件直接或间接的被哪些文件引用了:

浏览源代码.

结构体

struct  mem_header
 Copyright (C) 2026 HJimmyK(Jericho Knox) 更多...
 

宏定义

#define ALIGNMENT   LAMMP_MAX_ALIGN
 
#define EXTRA_MEM_PATTERN   0xAA
 
#define MEM_GUARD   0xDEADBEEFUL
 
#define MEM_MAGIC   0xDEADBEEFDEADBEEFULL
 
#define SAFE_APPEND(...)
 

函数

static size_t align_up (size_t size)
 
static int check_extra_memory_overflow (mem_header *hdr, void *user_ptr, const char *check_func, int check_line)
 检查额外内存区域是否被修改。
 
static int check_memory_block_integrity (mem_header *hdr, void *user_ptr, const char *check_func, int check_line)
 检查内存块的完整性(包括头尾和额外内存区域)。
 
static void find_corruption_range (const char *data, unsigned char pattern, size_t len, int *first, int *last, int *count)
 找到指定模式的连续内存区域,并记录起始和结束位置。
 
static voidlmmp_alloc_debug (size_t size, const char *func, int line)
 调试版 malloc
 
static void lmmp_free_debug (void *ptr, const char *func, int line)
 调试版 free
 
static voidlmmp_realloc_debug (void *ptr, size_t new_size, const char *func, int line)
 调试版 realloc
 

结构体说明

◆ mem_header

struct mem_header

Copyright (C) 2026 HJimmyK(Jericho Knox)

This file is part of LAMMP.

LAMMP is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (LGPL) as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed WITHOUT ANY WARRANTY.

See https://www.gnu.org/licenses/.

在文件 safe_memory.h27 行定义.

+ mem_header 的协作图:
成员变量
size_t extra_size
const char * func
uint32_t guard
int line
uint64_t magic
size_t total_size
size_t user_size

宏定义说明

◆ ALIGNMENT

#define ALIGNMENT   LAMMP_MAX_ALIGN

在文件 safe_memory.h41 行定义.

◆ EXTRA_MEM_PATTERN

#define EXTRA_MEM_PATTERN   0xAA

在文件 safe_memory.h39 行定义.

◆ MEM_GUARD

#define MEM_GUARD   0xDEADBEEFUL

在文件 safe_memory.h38 行定义.

◆ MEM_MAGIC

#define MEM_MAGIC   0xDEADBEEFDEADBEEFULL

在文件 safe_memory.h37 行定义.

◆ SAFE_APPEND

#define SAFE_APPEND (   ...)
值:
do { \
if (n > 0) \
offset += n; \
} \
} while (0)
#define n

函数说明

◆ align_up()

static size_t align_up ( size_t  size)
inlinestatic

在文件 safe_memory.h43 行定义.

43 {
44 return (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
45}
#define ALIGNMENT
Definition safe_memory.h:41

引用了 ALIGNMENT.

被这些函数引用 check_extra_memory_overflow(), lmmp_alloc_debug(), lmmp_free_debug() , 以及 lmmp_realloc_debug().

+ 这是这个函数的调用关系图:

◆ check_extra_memory_overflow()

static int check_extra_memory_overflow ( mem_header hdr,
void user_ptr,
const char check_func,
int  check_line 
)
inlinestatic

检查额外内存区域是否被修改。

在文件 safe_memory.h75 行定义.

75 {
76 if (!hdr || !user_ptr || hdr->extra_size == 0)
77 return 0;
78
79 size_t aligned_user_size = align_up(hdr->user_size);
80 char* extra_start = (char*)user_ptr + aligned_user_size;
81
82 int first, last, count;
84
85 if (count > 0) {
86 char error_buf[640];
87 int offset = 0;
88 const int buf_size = sizeof(error_buf);
89
90#define SAFE_APPEND(...) \
91 do { \
92 if (offset < buf_size) { \
93 int n = snprintf(error_buf + offset, (size_t)(buf_size - offset), __VA_ARGS__); \
94 if (n > 0) \
95 offset += n; \
96 } \
97 } while (0)
98
99 SAFE_APPEND("Memory overflow (extra memory corruption) detected!%s", "\n");
100 SAFE_APPEND("Memory header:%s", "\n");
101 SAFE_APPEND(" allocated at: [%s]:%d\n", hdr->func, hdr->line);
102 SAFE_APPEND(" checked at: [%s]:%d\n", check_func, check_line);
103 SAFE_APPEND(" user size: %zu bytes\n", hdr->user_size);
104 SAFE_APPEND(" extra size: %zu bytes (%.0f%% of user size)\n", hdr->extra_size,
106 SAFE_APPEND(" user ptr: %p\n", user_ptr);
107 SAFE_APPEND(" extra memory: %p to %p\n", (void*)extra_start, (void*)(extra_start + hdr->extra_size - 1));
108 SAFE_APPEND(" corrupted range: offset %d to %d (total %d bytes)\n", first, last, count);
109 SAFE_APPEND("Likely cause: Buffer overflow beyond the end of the memory.%s", "\n");
110
111 error_buf[buf_size - 1] = '\0';
113 return 1;
114 }
115 return 0;
116}
void lmmp_abort(lmmp_error_t type, const char *msg, const char *func, int line)
LAMMP 全局退出函数,内部错误或断言失败时调用,若设置了全局退出函数,则会调用该函数,否则会调用默认的退出函数。
Definition abort.c:102
#define LAMMP_MEMORY_MORE_ALLOC_TIMES
Definition lmmp.h:51
@ LAMMP_ERROR_OUT_OF_BOUNDS
Definition lmmp.h:217
#define SAFE_APPEND(...)
static void find_corruption_range(const char *data, unsigned char pattern, size_t len, int *first, int *last, int *count)
找到指定模式的连续内存区域,并记录起始和结束位置。
Definition safe_memory.h:50
#define EXTRA_MEM_PATTERN
Definition safe_memory.h:39
static size_t align_up(size_t size)
Definition safe_memory.h:43

引用了 align_up(), EXTRA_MEM_PATTERN, mem_header::extra_size, find_corruption_range(), mem_header::func, LAMMP_ERROR_OUT_OF_BOUNDS, LAMMP_MEMORY_MORE_ALLOC_TIMES, mem_header::line, lmmp_abort(), SAFE_APPEND , 以及 mem_header::user_size.

被这些函数引用 check_memory_block_integrity().

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ check_memory_block_integrity()

static int check_memory_block_integrity ( mem_header hdr,
void user_ptr,
const char check_func,
int  check_line 
)
inlinestatic

检查内存块的完整性(包括头尾和额外内存区域)。

在文件 safe_memory.h121 行定义.

121 {
122 if (!hdr || !user_ptr)
123 return 0;
124
125 if (hdr->magic != MEM_MAGIC || hdr->guard != MEM_GUARD) {
126 char error_buf[240];
128 "Memory header corruption detected!\n"
129 " Magic: 0x%016llx (expected 0x%016llx)\n"
130 " Guard: 0x%08lx (expected 0x%08lx)\n"
131 "Possible overflow or underflow or invalid pointer.",
132 hdr->magic, MEM_MAGIC, (unsigned long)hdr->guard, MEM_GUARD);
134 return 1;
135 }
136
138}
@ LAMMP_ERROR_MEMORY_FREE_FAILURE
Definition lmmp.h:216
static int check_extra_memory_overflow(mem_header *hdr, void *user_ptr, const char *check_func, int check_line)
检查额外内存区域是否被修改。
Definition safe_memory.h:75
#define MEM_MAGIC
Definition safe_memory.h:37
#define MEM_GUARD
Definition safe_memory.h:38

引用了 check_extra_memory_overflow(), mem_header::guard, LAMMP_ERROR_MEMORY_FREE_FAILURE, lmmp_abort(), mem_header::magic, MEM_GUARD , 以及 MEM_MAGIC.

被这些函数引用 lmmp_free_debug() , 以及 lmmp_realloc_debug().

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ find_corruption_range()

static void find_corruption_range ( const char data,
unsigned char  pattern,
size_t  len,
int *  first,
int *  last,
int *  count 
)
inlinestatic

找到指定模式的连续内存区域,并记录起始和结束位置。

在文件 safe_memory.h50 行定义.

57 {
58 *first = -1;
59 *last = -1;
60 *count = 0;
61
62 for (size_t i = 0; i < len; i++) {
63 if ((unsigned char)data[i] != pattern) {
64 (*count)++;
65 if (*first == -1)
66 *first = (int)i;
67 *last = (int)i;
68 }
69 }
70}

被这些函数引用 check_extra_memory_overflow().

+ 这是这个函数的调用关系图:

◆ lmmp_alloc_debug()

static void * lmmp_alloc_debug ( size_t  size,
const char func,
int  line 
)
inlinestatic

调试版 malloc

参数
size要分配的内存大小
func分配内存的函数名
line分配内存的行号
返回
分配的内存块的指针

在文件 safe_memory.h147 行定义.

147 {
148 if (size == 0)
149 return NULL;
150
151 size_t extra_size = (size * LAMMP_MEMORY_MORE_ALLOC_TIMES) / 10;
152 extra_size = align_up(extra_size);
153
154 size_t header_size = align_up(sizeof(mem_header));
155 size_t aligned_user_size = align_up(size);
156 size_t total_size = header_size + aligned_user_size + extra_size;
157
158 void* base = heap_alloc_func(total_size);
159 if (!base) {
160 char msg[128];
161 snprintf(msg, sizeof(msg), "Memory allocation failed (size: %zu bytes, extra: %zu bytes)", size, extra_size);
163 return NULL;
164 }
165
166 mem_header* hdr = (mem_header*)base;
167 void* user_ptr = (char*)base + header_size;
168 void* extra_mem = (char*)user_ptr + aligned_user_size;
169
171 hdr->user_size = size;
172 hdr->total_size = total_size;
173 hdr->extra_size = extra_size;
174 hdr->func = func;
175 hdr->line = line;
176 hdr->guard = MEM_GUARD;
177
178 memset(extra_mem, EXTRA_MEM_PATTERN, extra_size);
179
180 return user_ptr;
181}
@ LAMMP_ERROR_MEMORY_ALLOC_FAILURE
Definition lmmp.h:215
#define heap_alloc_func
Definition memory.c:35
uint64_t magic
Definition safe_memory.h:28
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition safe_memory.h:27

引用了 align_up(), EXTRA_MEM_PATTERN, heap_alloc_func, LAMMP_ERROR_MEMORY_ALLOC_FAILURE, LAMMP_MEMORY_MORE_ALLOC_TIMES, lmmp_abort(), mem_header::magic, MEM_GUARD, MEM_MAGIC , 以及 n.

被这些函数引用 lmmp_realloc_debug().

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ lmmp_free_debug()

static void lmmp_free_debug ( void ptr,
const char func,
int  line 
)
inlinestatic

调试版 free

参数
ptr指向要释放的内存块的指针
func分配内存的函数名
line分配内存的行号

在文件 safe_memory.h189 行定义.

189 {
190 if (!ptr)
191 return;
192
193 size_t header_size = align_up(sizeof(mem_header));
194 mem_header* hdr = (mem_header*)((char*)ptr - header_size);
195
196 check_memory_block_integrity(hdr, ptr, func, line);
197
199}
#define heap_free_func
Definition memory.c:36
static int check_memory_block_integrity(mem_header *hdr, void *user_ptr, const char *check_func, int check_line)
检查内存块的完整性(包括头尾和额外内存区域)。

引用了 align_up(), check_memory_block_integrity(), heap_free_func , 以及 n.

被这些函数引用 lmmp_realloc_debug().

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ lmmp_realloc_debug()

static void * lmmp_realloc_debug ( void ptr,
size_t  new_size,
const char func,
int  line 
)
inlinestatic

调试版 realloc

参数
ptr指向要重新分配的内存块的指针
new_size新的内存大小
func分配内存的函数名
line分配内存的行号
返回
新分配的内存块的指针

在文件 safe_memory.h209 行定义.

209 {
210 if (!ptr)
211 return lmmp_alloc_debug(new_size, func, line);
212 if (new_size == 0) {
213 lmmp_abort(LAMMP_ERROR_MEMORY_ALLOC_FAILURE, "Reallocating zero bytes is not allowed.", func, line);
214 return NULL;
215 }
216
217 size_t header_size = align_up(sizeof(mem_header));
218 mem_header* old_hdr = (mem_header*)((char*)ptr - header_size);
219
220 check_memory_block_integrity(old_hdr, ptr, func, line);
221
222 void* new_ptr = lmmp_alloc_debug(new_size, func, line);
223 if (!new_ptr)
224 return NULL;
225
226 size_t copy_size = (old_hdr->user_size < new_size) ? old_hdr->user_size : new_size;
227 memcpy(new_ptr, ptr, copy_size);
228
229 lmmp_free_debug(ptr, func, line);
230 return new_ptr;
231}
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

引用了 align_up(), check_memory_block_integrity(), LAMMP_ERROR_MEMORY_ALLOC_FAILURE, lmmp_abort(), lmmp_alloc_debug(), lmmp_free_debug() , 以及 n.

+ 函数调用图: