Skip to content

Commit

Permalink
pref(libcxx): update cpp ctor and dtor
Browse files Browse the repository at this point in the history
Signed-off-by: Zone.N <[email protected]>
  • Loading branch information
MRNIU committed Jun 3, 2024
1 parent 41397dd commit 7c47a7c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/kernel/include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
* @param argc 由 bootloader 传递的参数,在不同架构有不同的含义
* @param argv 由 bootloader 传递的参数,在不同架构有不同的含义
*/
extern "C" void _start(uint32_t argc, uint8_t* argv);
extern "C" [[maybe_unused]] [[noreturn]] void _start(uint32_t argc,
uint8_t* argv);

/**
* @brief 内核入口
Expand Down
7 changes: 6 additions & 1 deletion src/kernel/libcxx/include/libcxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
#include <cstdint>

/**
* @brief 初始化 c++ 全局对象
* @brief 构造 c++ 全局对象
*/
void CppInit();

/**
* @brief 析构 c++ 全局对象
*/
void CppDeInit();

/**
* @brief 入口
* @param argc 参数个数
Expand Down
20 changes: 14 additions & 6 deletions src/kernel/libcxx/libcxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@

#include "libcxx.h"

typedef void (*ctor_t)(void);
#include <algorithm>

typedef void (*function_t)(void);
// 在 link.ld 中定义
extern "C" ctor_t __init_array_start[];
extern "C" ctor_t __init_array_end[];
extern "C" function_t __init_array_start;
extern "C" function_t __init_array_end;
extern "C" function_t __fini_array_start;
extern "C" function_t __fini_array_end;

void CppInit(void) {
for (auto ctor = __init_array_start; ctor < __init_array_end; ctor++) {
(*ctor)();
}
std::for_each(&__init_array_start, &__init_array_end,
[](function_t func) { (func)(); });
}

void CppDeInit(void) {
std::for_each(&__fini_array_start, &__fini_array_end,
[](function_t func) { (func)(); });
}

uint32_t LibCxxInit(uint32_t argc, uint8_t* argv) {
Expand Down
12 changes: 7 additions & 5 deletions src/kernel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@
#include "kernel.h"
#include "libcxx.h"

[[maybe_unused]] void _start(uint32_t argc, uint8_t* argv) {
void _start(uint32_t argc, uint8_t* argv) {
CppInit();
main(argc, argv);
CppDeInit();

// 进入死循环
while (1) {
;
}
}

uint32_t main(uint32_t argc, uint8_t* argv) {
Expand All @@ -31,9 +37,5 @@ uint32_t main(uint32_t argc, uint8_t* argv) {
printf("Hello printf\n");
std::cout << "Hello ostream" << std::endl;

// 进入死循环
while (1) {
;
}
return 0;
}
11 changes: 9 additions & 2 deletions test/system_test/gnu_efi_test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
#include "cstdio"
#include "kernel.h"

extern "C" void _start(int argc, char **argv) { main(argc, argv); }
void _start(uint32_t argc, uint8_t *argv) {
main(argc, argv);

// 进入死循环
while (1) {
;
}
}

extern "C" void _putchar(char character) {
auto serial = Cpu::Serial(Cpu::kCom1);
Expand All @@ -47,7 +54,7 @@ static void Fillrect(uint8_t *vram, uint8_t r, uint8_t g, unsigned char b,
}
}

int main(int argc, char **argv) {
uint32_t main(uint32_t argc, uint8_t *argv) {
(void)argc;
(void)argv;

Expand Down

0 comments on commit 7c47a7c

Please sign in to comment.