Browse Source

feat(gbstdlibc++): add gbstdlibc++ and utility

greatbridf 1 year ago
parent
commit
7472cbe839

+ 2 - 1
CMakeLists.txt

@@ -26,6 +26,7 @@ if (NOT DEFINED FDISK_BIN)
 endif()
 
 add_subdirectory(gblibc)
+add_subdirectory(gblibstdc++)
 add_subdirectory(user-space-program)
 
 set(BOOTLOADER_SOURCES src/boot.s
@@ -95,7 +96,7 @@ set(KERNEL_MAIN_SOURCES src/fs/fat.cpp
                         )
 
 add_executable(kernel.out ${KERNEL_MAIN_SOURCES} ${BOOTLOADER_SOURCES})
-target_link_libraries(kernel.out gblibc)
+target_link_libraries(kernel.out gblibc gblibstdc++)
 target_include_directories(kernel.out PRIVATE ${PROJECT_SOURCE_DIR}/include)
 target_link_options(kernel.out PRIVATE
     -T ${CMAKE_SOURCE_DIR}/src/kernel.ld -melf_i386 -lgblibc -L${CMAKE_BINARY_DIR}/gblibc)

+ 10 - 0
gblibstdc++/CMakeLists.txt

@@ -0,0 +1,10 @@
+cmake_minimum_required(VERSION 3.15)
+project(gblibstdc++)
+
+set(CMAKE_CXX_STANDARD 20)
+
+add_library(gblibstdc++ STATIC src/stdc++.cpp)
+
+file(GLOB_RECURSE GBSTDLIBCPP_PUBLIC_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include)
+target_include_directories(gblibstdc++ PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
+set_target_properties(gblibstdc++ PROPERTIES PUBLIC_HEADER "${GBSTDLIBCPP_PUBLIC_HEADERS}")

+ 18 - 0
gblibstdc++/include/type_traits

@@ -0,0 +1,18 @@
+#ifndef __GBLIBCPP_TYPE_TRAITS__
+#define __GBLIBCPP_TYPE_TRAITS__
+
+namespace std {
+
+template <typename T>
+struct remove_reference { using type = T; };
+template <typename T>
+struct remove_reference<T&> { using type = T; };
+template <typename T>
+struct remove_reference<T&&> { using type = T; };
+
+template <typename T>
+using remove_reference_t = typename remove_reference<T>::type;
+
+} // namespace std
+
+#endif

+ 35 - 0
gblibstdc++/include/utility

@@ -0,0 +1,35 @@
+#ifndef __GBLIBCPP_UTILITY__
+#define __GBLIBCPP_UTILITY__
+
+#include <type_traits>
+
+namespace std {
+
+template <typename T>
+constexpr remove_reference_t<T>&& move(T&& val)
+{
+    return static_cast<remove_reference_t<T>&&>(val);
+}
+
+template <typename T>
+constexpr T&& forward(remove_reference_t<T>& val)
+{
+    return static_cast<T&&>(val);
+}
+template <typename T>
+constexpr T&& forward(remove_reference_t<T>&& val)
+{
+    return static_cast<T&&>(val);
+}
+
+template <typename T, typename U = T>
+constexpr T exchange(T& dst, U&& val)
+{
+    T tmp = move(dst);
+    dst = forward<U>(val);
+    return tmp;
+}
+
+} // namespace std
+
+#endif

+ 0 - 0
gblibstdc++/src/stdc++.cpp


+ 3 - 1
include/kernel/mm.hpp

@@ -1,5 +1,7 @@
 #pragma once
 
+#include <utility>
+
 #include <kernel/mem.h>
 #include <kernel/vfs.hpp>
 #include <stdint.h>
@@ -175,7 +177,7 @@ public:
     }
     mm_list(const mm_list& v);
     constexpr mm_list(mm_list&& v)
-        : m_areas(::types::move(v.m_areas))
+        : m_areas(std::move(v.m_areas))
         , m_pd(v.m_pd)
     {
         v.m_pd = 0;

+ 5 - 5
include/kernel/process.hpp

@@ -114,7 +114,7 @@ public:
     constexpr thdlist& operator=(thdlist&& obj) = delete;
 
     constexpr thdlist(thdlist&& obj, process* new_parent)
-        : thds { types::move(obj.thds) }
+        : thds { std::move(obj.thds) }
     {
         for (auto& thd : thds)
             thd.owner = new_parent;
@@ -128,7 +128,7 @@ public:
     template <typename... Args>
     constexpr thread& Emplace(Args&&... args)
     {
-        return *thds.emplace_back(types::forward<Args>(args)...);
+        return *thds.emplace_back(std::forward<Args>(args)...);
     }
 
     constexpr size_t size(void) const
@@ -197,7 +197,7 @@ public:
         constexpr filearr& operator=(filearr&&) = delete;
         constexpr filearr(void) = default;
         constexpr filearr(filearr&& val)
-            : arr { types::move(val.arr) }
+            : arr { std::move(val.arr) }
         {
         }
 
@@ -401,10 +401,10 @@ public:
     template <typename... Args>
     iterator_type emplace(Args&&... args)
     {
-        process _proc(types::forward<Args>(args)...);
+        process _proc(std::forward<Args>(args)...);
         auto pid = _proc.pid;
         auto ppid = _proc.ppid;
-        auto iter = m_procs.insert(types::make_pair(pid, types::move(_proc)));
+        auto iter = m_procs.insert(types::make_pair(pid, std::move(_proc)));
 
         auto children = m_child_idx.find(ppid);
         if (!children) {

+ 1 - 1
include/kernel/signal.hpp

@@ -33,7 +33,7 @@ public:
     }
 
     constexpr signal_list(signal_list&& val)
-        : m_list(types::move(val.m_list))
+        : m_list(std::move(val.m_list))
         , m_mask(val.m_mask)
     {
     }

+ 5 - 4
include/types/allocator.hpp

@@ -1,4 +1,5 @@
 #pragma once
+#include <utility>
 #include <assert.h>
 #include <stdint.h>
 #include <types/cplusplus.hpp>
@@ -205,13 +206,13 @@ public:
 template <template <typename _T> class Allocator, typename T, typename... Args>
 constexpr T* _new(Args&&... args)
 {
-    return allocator_traits<Allocator<T>>::allocate_and_construct(forward<Args>(args)...);
+    return allocator_traits<Allocator<T>>::allocate_and_construct(std::forward<Args>(args)...);
 }
 
 template <template <typename _T> class Allocator, typename T, typename... Args>
 constexpr T* pnew(T* = nullptr, Args&&... args)
 {
-    return _new<Allocator, T, Args...>(forward<Args>(args)...);
+    return _new<Allocator, T, Args...>(std::forward<Args>(args)...);
 }
 
 template <template <typename _T> class Allocator, typename T>
@@ -235,7 +236,7 @@ public:
     template <typename... Args>
     static constexpr value_type* construct(value_type* ptr, Args&&... args)
     {
-        new (ptr) value_type(forward<Args>(args)...);
+        new (ptr) value_type(std::forward<Args>(args)...);
         return ptr;
     }
 
@@ -243,7 +244,7 @@ public:
     static constexpr value_type* allocate_and_construct(Args&&... args)
     {
         auto* ptr = allocate(1);
-        construct(ptr, forward<Args>(args)...);
+        construct(ptr, std::forward<Args>(args)...);
         return ptr;
     }
 

+ 0 - 16
include/types/cplusplus.hpp

@@ -121,22 +121,6 @@ public:
 } // namespace types::traits
 
 namespace types {
-template <typename T>
-constexpr typename traits::remove_reference<T>::type&& move(T&& val)
-{
-    return static_cast<typename traits::remove_reference<T>::type&&>(val);
-}
-template <typename T>
-constexpr T&& forward(typename traits::remove_reference<T>::type& val)
-{
-    return static_cast<T&&>(val);
-}
-template <typename T>
-constexpr T&& forward(typename traits::remove_reference<T>::type&& val)
-{
-    return static_cast<T&&>(val);
-}
-
 template <typename>
 struct template_true_type : public true_type {
 };

+ 5 - 5
include/types/function.hpp

@@ -20,14 +20,14 @@ namespace __inner {
 
     public:
         constexpr _function(FuncLike&& _func)
-            : func(types::forward<FuncLike>(_func))
+            : func(std::forward<FuncLike>(_func))
         {
         }
         constexpr ~_function() = default;
 
         constexpr Ret operator()(Args&&... args) const override
         {
-            return func(types::forward<Args>(args)...);
+            return func(std::forward<Args>(args)...);
         }
     };
 
@@ -51,14 +51,14 @@ public:
     constexpr function(FuncLike&& func)
     {
         static_assert(sizeof(FuncLike) <= sizeof(_data));
-        new (_f()) __inner::_function<FuncLike, Ret, Args...>(types::forward<FuncLike>(func));
+        new (_f()) __inner::_function<FuncLike, Ret, Args...>(std::forward<FuncLike>(func));
     }
 
     template <typename FuncPtr>
     constexpr function(FuncPtr* funcPtr)
     {
         new (_f()) __inner::_function<typename types::traits::decay<FuncPtr>::type, Ret, Args...>(
-            types::forward<typename types::traits::decay<FuncPtr>::type>(funcPtr));
+            std::forward<typename types::traits::decay<FuncPtr>::type>(funcPtr));
     }
 
     constexpr ~function()
@@ -68,7 +68,7 @@ public:
 
     constexpr Ret operator()(Args... args) const
     {
-        return (*_f())(types::forward<Args>(args)...);
+        return (*_f())(std::forward<Args>(args)...);
     }
 };
 

+ 4 - 3
include/types/hash_map.hpp

@@ -1,4 +1,5 @@
 #pragma once
+#include <utility>
 
 #include <stdint.h>
 #include <types/allocator.hpp>
@@ -201,7 +202,7 @@ public:
     }
 
     constexpr hash_map(hash_map&& v)
-        : buckets(move(v.buckets))
+        : buckets(std::move(v.buckets))
     {
     }
 
@@ -213,13 +214,13 @@ public:
     constexpr void emplace(pair_type&& p)
     {
         auto hash_value = _Hasher::hash(p.key, hash_length());
-        buckets.at(hash_value).push_back(move(p));
+        buckets.at(hash_value).push_back(std::move(p));
     }
 
     template <typename _key_type, typename _value_type>
     constexpr void emplace(_key_type&& key, _value_type&& value)
     {
-        emplace(make_pair(forward<_key_type>(key), forward<_value_type>(value)));
+        emplace(make_pair(std::forward<_key_type>(key), std::forward<_value_type>(value)));
     }
 
     constexpr void remove(const key_type& key)

+ 6 - 6
include/types/list.hpp

@@ -52,7 +52,7 @@ private:
         }
 
         explicit constexpr node(NodeValue&& v) noexcept
-            : value(move(v))
+            : value(std::move(v))
         {
         }
 
@@ -265,7 +265,7 @@ public:
     // insert the value v in front of the given iterator
     constexpr iterator_type insert(const iterator_type& iter, value_type&& v) noexcept
     {
-        node_type* new_node = allocator_traits<allocator_type>::allocate_and_construct(move(v));
+        node_type* new_node = allocator_traits<allocator_type>::allocate_and_construct(std::move(v));
         iterator_type ret(new_node);
         iter._node()->prev->connect(new_node);
         new_node->connect(static_cast<node_type*>(iter._node()));
@@ -281,13 +281,13 @@ public:
 
     constexpr void push_back(value_type&& v) noexcept
     {
-        insert(end(), move(v));
+        insert(end(), std::move(v));
     }
 
     template <typename... Args>
     constexpr iterator_type emplace_back(Args&&... args)
     {
-        return insert(end(), value_type(forward<Args>(args)...));
+        return insert(end(), value_type(std::forward<Args>(args)...));
     }
 
     constexpr void push_front(const value_type& v) noexcept
@@ -297,13 +297,13 @@ public:
 
     constexpr void push_front(value_type&& v) noexcept
     {
-        insert(begin(), move(v));
+        insert(begin(), std::move(v));
     }
 
     template <typename... Args>
     constexpr iterator_type emplace_front(Args&&... args)
     {
-        return insert(begin(), value_type(forward<Args>(args)...));
+        return insert(begin(), value_type(std::forward<Args>(args)...));
     }
 
     constexpr size_t size(void) const noexcept

+ 6 - 5
include/types/map.hpp

@@ -1,4 +1,5 @@
 #pragma once
+#include <utility>
 
 #include <types/allocator.hpp>
 #include <types/cplusplus.hpp>
@@ -29,7 +30,7 @@ public:
         pair_type v;
 
         constexpr node(pair_type&& pair)
-            : v(move(pair))
+            : v(std::move(pair))
         {
         }
         constexpr node(const pair_type& pair)
@@ -420,7 +421,7 @@ private:
     }
     static constexpr node* newnode(node* parent, pair_type&& val)
     {
-        auto* ptr = allocator_traits<allocator_type>::allocate_and_construct(move(val));
+        auto* ptr = allocator_traits<allocator_type>::allocate_and_construct(std::move(val));
         ptr->parent = parent;
         return ptr;
     }
@@ -639,7 +640,7 @@ public:
         while (likely(cur)) {
             if (val.key < cur->v.key) {
                 if (!cur->left) {
-                    node* nd = newnode(cur, move(val));
+                    node* nd = newnode(cur, std::move(val));
                     cur->left = nd;
                     this->balance(nd);
                     return iterator_type(nd);
@@ -648,7 +649,7 @@ public:
                 }
             } else {
                 if (!cur->right) {
-                    node* nd = newnode(cur, move(val));
+                    node* nd = newnode(cur, std::move(val));
                     cur->right = nd;
                     this->balance(nd);
                     return iterator_type(nd);
@@ -658,7 +659,7 @@ public:
             }
         }
 
-        root = newnode(nullptr, move(val));
+        root = newnode(nullptr, std::move(val));
         root->toblack();
         return iterator_type(root);
     }

+ 10 - 9
include/types/pair.hpp

@@ -1,4 +1,5 @@
 #pragma once
+#include <utility>
 
 #include <types/cplusplus.hpp>
 
@@ -19,8 +20,8 @@ struct pair {
 
     template <typename _key_type, typename _value_type>
     constexpr pair(_key_type&& _key, _value_type&& _value)
-        : key(forward<_key_type>(_key))
-        , value(forward<_value_type>(_value))
+        : key(std::forward<_key_type>(_key))
+        , value(std::forward<_value_type>(_value))
     {
     }
 
@@ -34,8 +35,8 @@ struct pair {
     }
     template <typename _key_type, typename _value_type>
     constexpr pair(pair<_key_type, _value_type>&& val)
-        : key(move(val.key))
-        , value(move(val.value))
+        : key(std::move(val.key))
+        , value(std::move(val.value))
     {
         static_assert(is_same<typename traits::decay<_key_type>::type, typename traits::decay<key_type>::type>::value);
         static_assert(is_same<typename traits::decay<_value_type>::type, typename traits::decay<value_type>::type>::value);
@@ -46,8 +47,8 @@ struct pair {
     {
     }
     constexpr pair(pair&& val)
-        : key(move(val.key))
-        , value(move(val.value))
+        : key(std::move(val.key))
+        , value(std::move(val.value))
     {
     }
     constexpr pair& operator=(const pair& val)
@@ -57,8 +58,8 @@ struct pair {
     }
     constexpr pair& operator=(pair&& val)
     {
-        key = move(val.key);
-        value = move(val.value);
+        key = std::move(val.key);
+        value = std::move(val.value);
     }
 
     constexpr bool key_eq(const pair& p)
@@ -86,7 +87,7 @@ template <typename T1, typename T2>
 constexpr pair<typename traits::decay<T1>::type, typename traits::decay<T2>::type>
 make_pair(T1&& t1, T2&& t2)
 {
-    return pair<typename traits::decay<T1>::type, typename traits::decay<T2>::type> { forward<T1>(t1), forward<T2>(t2) };
+    return pair<typename traits::decay<T1>::type, typename traits::decay<T2>::type> { std::forward<T1>(t1), std::forward<T2>(t2) };
 }
 
 } // namespace types

+ 3 - 2
include/types/vector.hpp

@@ -1,4 +1,5 @@
 #pragma once
+#include <utility>
 
 #include <assert.h>
 #include <types/allocator.hpp>
@@ -264,14 +265,14 @@ public:
     {
         if (m_size == m_capacity)
             resize(m_capacity * 2);
-        allocator_traits<allocator_type>::construct(m_arr + m_size, move(v));
+        allocator_traits<allocator_type>::construct(m_arr + m_size, std::move(v));
         ++m_size;
     }
 
     template <typename... Args>
     constexpr iterator_type emplace_back(Args&&... args)
     {
-        push_back(value_type(forward<Args>(args)...));
+        push_back(value_type(std::forward<Args>(args)...));
         return back();
     }
 

+ 7 - 7
src/kernel/process.cpp

@@ -90,11 +90,11 @@ void thread::free_kstack(uint32_t p)
 }
 
 process::process(process&& val)
-    : mms(types::move(val.mms))
-    , thds { types::move(val.thds), this }
+    : mms(std::move(val.mms))
+    , thds { std::move(val.thds), this }
     , attr { val.attr }
-    , files(types::move(val.files))
-    , pwd(types::move(val.pwd))
+    , files(std::move(val.files))
+    , pwd(std::move(val.pwd))
     , pid(val.pid)
     , ppid(val.ppid)
     , pgid(val.pgid)
@@ -129,8 +129,8 @@ process::process(pid_t _ppid,
     kernel::signal_list&& _sigs)
     : mms(*kernel_mms)
     , attr { .system = _system }
-    , pwd { types::move(path) }
-    , signals(types::move(_sigs))
+    , pwd { std::move(path) }
+    , signals(std::move(_sigs))
     , pid { process::alloc_pid() }
     , ppid { _ppid }
     , pgid { 0 }
@@ -257,7 +257,7 @@ void NORETURN _kernel_init(void)
     // eflags
     push_stack(esp, 0x200);
 
-    readythds->push(&proc->thds.Emplace(types::move(thd)));
+    readythds->push(&proc->thds.Emplace(std::move(thd)));
 
     // ------------------------------------------
 

+ 3 - 3
src/kernel/vfs.cpp

@@ -39,7 +39,7 @@ fs::vfs::dentry::dentry(dentry* _parent, inode* _ind, name_type&& _name)
     : parent(_parent)
     , ind(_ind)
     , flags { 0 }
-    , name(types::move(_name))
+    , name(std::move(_name))
 {
     if (!_ind || _ind->flags.in.directory) {
         children = types::pnew<allocator_type>(children);
@@ -52,7 +52,7 @@ fs::vfs::dentry::dentry(dentry&& val)
     , parent(val.parent)
     , ind(val.ind)
     , flags { val.flags }
-    , name(types::move(val.name))
+    , name(std::move(val.name))
 {
     if (children) {
         for (auto& item : *children)
@@ -77,7 +77,7 @@ fs::vfs::dentry* fs::vfs::dentry::append(inode* ind, const name_type& name, bool
 }
 fs::vfs::dentry* fs::vfs::dentry::append(inode* ind, name_type&& name, bool set_dirty)
 {
-    auto iter = children->emplace_back(this, ind, types::move(name));
+    auto iter = children->emplace_back(this, ind, std::move(name));
     idx_children->emplace(iter->name, &iter);
     if (set_dirty)
         this->flags.in.dirty = 1;