Browse Source

refactor: prohibit c++ global objects

greatbridf 2 năm trước cách đây
mục cha
commit
d5f54a40c5
3 tập tin đã thay đổi với 32 bổ sung13 xóa
  1. 11 0
      include/types/allocator.hpp
  2. 7 3
      src/kernel/event/event.cpp
  3. 14 10
      src/kernel_main.c

+ 11 - 0
include/types/allocator.hpp

@@ -1,4 +1,5 @@
 #pragma once
+#include <kernel/mem.h>
 #include <types/types.h>
 
 inline void* operator new(size_t, void* ptr)
@@ -7,6 +8,10 @@ inline void* operator new(size_t, void* ptr)
 }
 
 namespace types {
+
+template <typename Allocator>
+class allocator_traits;
+
 template <typename T>
 class kernel_allocator {
 public:
@@ -23,6 +28,12 @@ public:
     }
 };
 
+template <typename T, typename... Args>
+T* kernel_allocator_new(Args... args)
+{
+    return allocator_traits<kernel_allocator<T>>::allocate_and_construct(args...);
+}
+
 template <typename Allocator>
 class allocator_traits {
 public:

+ 7 - 3
src/kernel/event/event.cpp

@@ -1,16 +1,20 @@
-#include <kernel/tty.h>
 #include <asm/port_io.h>
 #include <kernel/event/event.h>
 #include <kernel/input/input_event.h>
 #include <kernel/stdio.h>
+#include <kernel/tty.h>
+#include <types/allocator.hpp>
 #include <types/list.hpp>
 
-static ::types::list<::input_event> _input_event_queue {};
+static ::types::list<::input_event>* _input_event_queue;
 
 namespace event {
 ::types::list<::input_event>& input_event_queue(void)
 {
-    return _input_event_queue;
+    if (!_input_event_queue) {
+        _input_event_queue = types::kernel_allocator_new<types::list<input_event>>();
+    }
+    return *_input_event_queue;
 }
 } // namespace event
 

+ 14 - 10
src/kernel_main.c

@@ -15,16 +15,6 @@
 #include <kernel/vga.h>
 #include <types/bitmap.h>
 
-typedef void (*constructor)(void);
-extern constructor start_ctors;
-extern constructor end_ctors;
-void call_constructors_for_cpp(void)
-{
-    for (constructor* ctor = &start_ctors; ctor != &end_ctors; ++ctor) {
-        (*ctor)();
-    }
-}
-
 #define KERNEL_MAIN_BUF_SIZE (128)
 
 struct tty* console = NULL;
@@ -57,6 +47,20 @@ static inline void halt_on_init_error(void)
         asm_hlt();
 }
 
+typedef void (*constructor)(void);
+extern constructor start_ctors;
+extern constructor end_ctors;
+void call_constructors_for_cpp(void)
+{
+    if (start_ctors != end_ctors) {
+        tty_print(console, "error: c++ global objects are now allowed");
+        halt_on_init_error();
+    }
+    // for (constructor* ctor = &start_ctors; ctor != &end_ctors; ++ctor) {
+    //     (*ctor)();
+    // }
+}
+
 uint8_t e820_mem_map[1024];
 uint32_t e820_mem_map_count;
 uint32_t e820_mem_map_entry_size;