SimpleKernel 1.17.0
Loading...
Searching...
No Matches
kstd_libcxx.cpp File Reference
#include <cpu_io.h>
#include <stdint.h>
#include <algorithm>
#include <atomic>
#include "kernel_log.hpp"
Include dependency graph for kstd_libcxx.cpp:

Go to the source code of this file.

Classes

struct  atexit_func_entry_t
 析构函数结构 More...
 
struct  GuardType
 

Typedefs

using function_t = void(*)()
 全局构造函数函数指针
 

Functions

auto __cxa_atexit (void(*destructor_func)(void *), void *obj_ptr, void *) -> int
 
auto __cxa_finalize (void *destructor_func) -> void
 
auto __cxa_pure_virtual () -> void
 
auto abort () -> void
 
void __assert_fail (const char *assertion, const char *file, unsigned int line, const char *function)
 libc assert() 失败时调用的底层接口
 
auto CppInit () -> void
 构造 c++ 全局对象
 
auto CppDeInit () -> void
 析构 c++ 全局对象
 
保证静态局部变量线程安全
Todo:
确保正确
auto __cxa_guard_acquire (GuardType *guard) -> int
 
auto __cxa_guard_release (GuardType *guard) -> void
 
auto __cxa_guard_abort (GuardType *guard) -> void
 

Variables

function_t __init_array_start
 全局构造函数函数指针起点地址
 
function_t __init_array_end
 全局构造函数函数指针终点地址
 
function_t __fini_array_start
 全局析构函数函数指针起点地址
 
function_t __fini_array_end
 全局析构函数函数指针终点地址
 
void * __dso_handle = nullptr
 动态共享对象标识,内核使用静态链接,此变量在内核中没有使用
 
static constexpr size_t kMaxAtExitFuncsCount = 128
 最大析构函数数量
 
static atexit_func_entry_t atexit_funcs [kMaxAtExitFuncsCount]
 析构函数数组
 
static size_t atexit_func_count = 0
 析构函数个数
 

Typedef Documentation

◆ function_t

using function_t = void (*)()

全局构造函数函数指针

Definition at line 14 of file kstd_libcxx.cpp.

Function Documentation

◆ __assert_fail()

void __assert_fail ( const char *  assertion,
const char *  file,
unsigned int  line,
const char *  function 
)

libc assert() 失败时调用的底层接口

Parameters
assertion断言表达式字符串
file源文件名
line行号
function函数名

Definition at line 199 of file kstd_libcxx.cpp.

201 {
202 klog::RawPut("\n[ASSERT FAILED] ");
203 klog::RawPut(file);
204 etl_putchar(':');
205 char buf_line[12];
206 auto* end = etl::format_to_n(buf_line, sizeof(buf_line) - 1, "{}", line);
207 *end = '\0';
208 klog::RawPut(" in ");
209 klog::RawPut(function);
210 klog::RawPut("\n Expression: ");
211 klog::RawPut(assertion);
212 etl_putchar('\n');
213 while (1) {
215 }
216}
auto etl_putchar(int c) -> void
早期控制台字符输出
void * end[]
内核结束
void Pause()
Definition cpu_io.h:20
__always_inline auto RawPut(const char *msg) -> void
绕过队列直接输出至串口(用于 panic 路径)
Here is the call graph for this function:

◆ __cxa_atexit()

auto __cxa_atexit ( void(*)(void *)  destructor_func,
void *  obj_ptr,
void *   
) -> int

注册在程序正常终止时调用的析构函数

Parameters
destructor_func指向要调用的析构函数的指针
obj_ptr传递给析构函数的参数
Returns
成功返回 0

Definition at line 51 of file kstd_libcxx.cpp.

52 {
54 return -1;
55 }
60 return 0;
61}
static size_t atexit_func_count
析构函数个数
static constexpr size_t kMaxAtExitFuncsCount
最大析构函数数量
static atexit_func_entry_t atexit_funcs[kMaxAtExitFuncsCount]
析构函数数组
void * dso_handle
void(* destructor_func)(void *)
void * obj_ptr

◆ __cxa_finalize()

auto __cxa_finalize ( void *  destructor_func) -> void

调用析构函数

Parameters
destructor_func要调用的析构函数指针,为 nullptr 时调用所有注册的析构函数。

Definition at line 68 of file kstd_libcxx.cpp.

68 {
69 if (destructor_func == nullptr) {
70 // 如果 destructor_func 为 nullptr,调用所有析构函数
71 for (size_t i = atexit_func_count; i > 0; --i) {
72 size_t idx = i - 1;
73 if (atexit_funcs[idx].destructor_func != nullptr) {
74 (*atexit_funcs[idx].destructor_func)(atexit_funcs[idx].obj_ptr);
75 }
76 }
77 } else {
78 // 不为空时只调用对应的析构函数
79 for (size_t i = atexit_func_count; i > 0; --i) {
80 size_t idx = i - 1;
81 if (reinterpret_cast<void*>(atexit_funcs[idx].destructor_func) ==
82 destructor_func) {
83 (*atexit_funcs[idx].destructor_func)(atexit_funcs[idx].obj_ptr);
84 atexit_funcs[idx].destructor_func = nullptr;
85 }
86 }
87 }
88}

◆ __cxa_guard_abort()

auto __cxa_guard_abort ( GuardType guard) -> void

如果在初始化过程中出现异常或其他错误,调用此函数以释放锁而不标记变量为已初始化

Parameters
guard

Definition at line 167 of file kstd_libcxx.cpp.

167 {
168 // 清除所有位
169 guard->guard.store(0, std::memory_order_release);
170}
std::atomic< uint64_t > guard
原子守护变量:bit 0 = is_initialized, bit 8 = is_in_use

◆ __cxa_guard_acquire()

auto __cxa_guard_acquire ( GuardType guard) -> int

检测静态局部变量是否已经初始化

Parameters
guard锁,一个 64 位变量
Returns
未初始化返回非零值,已初始化返回 0
Note
使用紧凑的 CAS 循环,compare_exchange_weak 失败时会自动更新 expected

Definition at line 127 of file kstd_libcxx.cpp.

127 {
128 uint64_t expected = 0;
129
130 // 紧凑的 CAS 循环:尝试将 0 -> kInUseMask
131 // compare_exchange_weak 失败时会将当前值写入 expected
132 while (!guard->guard.compare_exchange_weak(expected, GuardType::kInUseMask,
133 std::memory_order_acq_rel,
134 std::memory_order_acquire)) {
135 // CAS 失败,检查失败原因
136 if ((expected & GuardType::kInitializedMask) != 0U) {
137 // 已被其他核心初始化完成
138 return 0;
139 }
140
141 // 其他核心正在初始化中,等待
143
144 // 重置 expected 以便下次 CAS 尝试
145 // 如果当前值仍有 kInUseMask,CAS 会再次失败并更新 expected
146 expected = 0;
147 }
148
149 // CAS 成功,当前核心负责初始化
150 return 1;
151}
static constexpr uint64_t kInUseMask
static constexpr uint64_t kInitializedMask
Here is the call graph for this function:

◆ __cxa_guard_release()

auto __cxa_guard_release ( GuardType guard) -> void

用于检测静态局部变量是否已经初始化,并设置锁

Parameters
guard锁,一个 64 位变量
Returns
未初始化返回非零值并设置锁,已初始化返回 0

Definition at line 158 of file kstd_libcxx.cpp.

158 {
159 // 设置 initialized 位,清除 in_use 位
160 guard->guard.store(GuardType::kInitializedMask, std::memory_order_release);
161}

◆ __cxa_pure_virtual()

auto __cxa_pure_virtual ( ) -> void

纯虚函数调用处理

Definition at line 177 of file kstd_libcxx.cpp.

177 {
178 while (true) {
180 }
181}
Here is the call graph for this function:

◆ abort()

auto abort ( ) -> void

终止程序

Definition at line 186 of file kstd_libcxx.cpp.

186 {
187 while (true) {
189 }
190}
Here is the call graph for this function:

◆ CppDeInit()

auto CppDeInit ( ) -> void

析构 c++ 全局对象

c++ 全局对象析构

Definition at line 230 of file kstd_libcxx.cpp.

230 {
231 // 调用析构函数
232 std::for_each(&__fini_array_start, &__fini_array_end,
233 [](function_t func) { (func)(); });
234}
function_t __fini_array_end
全局析构函数函数指针终点地址
void(*)() function_t
全局构造函数函数指针
function_t __fini_array_start
全局析构函数函数指针起点地址

◆ CppInit()

auto CppInit ( ) -> void

构造 c++ 全局对象

c++ 全局对象构造

Definition at line 221 of file kstd_libcxx.cpp.

221 {
222 // 调用构造函数
223 std::for_each(&__init_array_start, &__init_array_end,
224 [](function_t func) { (func)(); });
225}
function_t __init_array_start
全局构造函数函数指针起点地址
function_t __init_array_end
全局构造函数函数指针终点地址
Here is the caller graph for this function:

Variable Documentation

◆ __dso_handle

void* __dso_handle = nullptr

动态共享对象标识,内核使用静态链接,此变量在内核中没有使用

Definition at line 25 of file kstd_libcxx.cpp.

◆ __fini_array_end

function_t __fini_array_end

全局析构函数函数指针终点地址

Definition at line 23 of file kstd_libcxx.cpp.

◆ __fini_array_start

function_t __fini_array_start

全局析构函数函数指针起点地址

Definition at line 21 of file kstd_libcxx.cpp.

◆ __init_array_end

function_t __init_array_end

全局构造函数函数指针终点地址

Definition at line 19 of file kstd_libcxx.cpp.

◆ __init_array_start

function_t __init_array_start

全局构造函数函数指针起点地址

Definition at line 17 of file kstd_libcxx.cpp.

◆ atexit_func_count

size_t atexit_func_count = 0
static

析构函数个数

Definition at line 43 of file kstd_libcxx.cpp.

◆ atexit_funcs

析构函数数组

Definition at line 41 of file kstd_libcxx.cpp.

◆ kMaxAtExitFuncsCount

constexpr size_t kMaxAtExitFuncsCount = 128
staticconstexpr

最大析构函数数量

Definition at line 28 of file kstd_libcxx.cpp.