LAMMP 4.1.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
abort.c
浏览该文件的文档.
1/*
2 * LAMMP - Copyright (C) 2025-2026 HJimmyK(Jericho Knox)
3 * This file is part of lammp, under the GNU LGPL v2 license.
4 * See LICENSE in the project root for the full license text.
5 */
6
7#include "../../include/lammp/lmmp.h"
8#include <stdatomic.h>
9#include <stdio.h>
10#include <stdlib.h>
11
12static atomic_uintptr_t lmmp_abort_func;
13
15 uintptr_t old = atomic_exchange(&lmmp_abort_func, (uintptr_t)func);
16 return (lmmp_abort_fn)old;
17}
18
19static const char* type_to_str(lmmp_error_t type) {
20 switch (type) {
22 return "ASSERT_FAILURE";
24 return "DEBUG_ASSERT_FAILURE";
26 return "PARAM_ASSERT_FAILURE";
28 return "MEMORY_ALLOC_FAILURE";
30 return "MEMORY_FREE_FAILURE";
32 return "OUT_OF_BOUNDS";
34 return "MEMORY_LEAK";
36 return "UNEXPECTED_ERROR";
37 default:
38 return "UNKNOWN_TYPE";
39 }
40}
41
42void lmmp_abort(lmmp_error_t type, const char* msg, const char* func, int line) {
43 uintptr_t curr = atomic_load(&lmmp_abort_func);
45 if (fn != NULL) {
46 fn(type, msg, func, line);
47 } else {
48 fprintf(stderr, "LAMMP abort at [%s]:%d\n", func, line);
49 fprintf(stderr, "Abort type: %s, abort msg: \n%s\n", type_to_str(type), msg);
50 fflush(stderr);
51 abort();
52 }
53}
static const char * type_to_str(lmmp_error_t type)
Definition abort.c:19
lmmp_abort_fn lmmp_set_abort_fn(lmmp_abort_fn func)
设置 LAMMP 全局退出函数(所有线程均生效)
Definition abort.c:14
void lmmp_abort(lmmp_error_t type, const char *msg, const char *func, int line)
LAMMP 全局退出函数,内部错误或断言失败时调用,若设置了全局退出函数,则会调用该函数,否则会调用默认的退出函数。
Definition abort.c:42
static atomic_uintptr_t lmmp_abort_func
Definition abort.c:12
void(* lmmp_abort_fn)(lmmp_error_t type, const char *msg, const char *func, int line)
LAMMP 全局退出函数指针类型
Definition lmmp.h:150
lmmp_error_t
Definition lmmp.h:132
@ LAMMP_ERROR_MEMORY_ALLOC_FAILURE
Definition lmmp.h:136
@ LAMMP_ERROR_DEBUG_ASSERT_FAILURE
Definition lmmp.h:134
@ LAMMP_ERROR_UNEXPECTED_ERROR
Definition lmmp.h:140
@ LAMMP_ERROR_MEMORY_LEAK
Definition lmmp.h:139
@ LAMMP_ERROR_ASSERT_FAILURE
Definition lmmp.h:133
@ LAMMP_ERROR_PARAM_ASSERT_FAILURE
Definition lmmp.h:135
@ LAMMP_ERROR_MEMORY_FREE_FAILURE
Definition lmmp.h:137
@ LAMMP_ERROR_OUT_OF_BOUNDS
Definition lmmp.h:138