Browse Source

change(kernel): remove types::pair

greatbridf 1 year ago
parent
commit
07638e3531

+ 0 - 1
CMakeLists.txt

@@ -80,7 +80,6 @@ set(KERNEL_MAIN_SOURCES src/fs/fat.cpp
                         include/types/elf.hpp
                         include/types/hash_map.hpp
                         include/types/map.hpp
-                        include/types/pair.hpp
                         include/types/types.h
                         include/types/size.h
                         include/types/status.h

+ 21 - 20
include/kernel/process.hpp

@@ -18,7 +18,6 @@
 #include <types/hash_map.hpp>
 #include <types/list.hpp>
 #include <types/map.hpp>
-#include <types/pair.hpp>
 #include <types/status.h>
 #include <types/string.hpp>
 #include <types/types.h>
@@ -214,15 +213,17 @@ public:
             if (!iter)
                 return -EBADF;
 
-            this->arr.insert(types::make_pair(new_fd, iter->value));
-            ++iter->value->ref;
+            auto [ _, iter_file ] = *iter;
+
+            this->arr.insert(std::make_pair(new_fd, iter_file));
+            ++iter_file->ref;
             return new_fd;
         }
 
         constexpr void dup_all(const filearr& orig)
         {
             for (auto [ fd, iter_file ] : orig.arr) {
-                this->arr.insert(types::make_pair(fd, iter_file));
+                this->arr.insert(std::make_pair(fd, iter_file));
                 ++iter_file->ref;
             }
         }
@@ -232,7 +233,7 @@ public:
             auto iter = arr.find(i);
             if (!iter)
                 return nullptr;
-            return &iter->value;
+            return &iter->second;
         }
 
         int pipe(int pipefd[2])
@@ -252,7 +253,7 @@ public:
                 },
             });
             int fd = _next_fd();
-            arr.insert(types::make_pair(fd, iter));
+            arr.insert(std::make_pair(fd, iter));
 
             // TODO: use copy_to_user()
             pipefd[0] = fd;
@@ -269,7 +270,7 @@ public:
                 },
             });
             fd = _next_fd();
-            arr.insert(types::make_pair(fd, iter));
+            arr.insert(std::make_pair(fd, iter));
 
             // TODO: use copy_to_user()
             pipefd[1] = fd;
@@ -306,7 +307,7 @@ public:
             });
 
             int fd = _next_fd();
-            arr.insert(types::make_pair(fd, iter));
+            arr.insert(std::make_pair(fd, iter));
             return fd;
         }
 
@@ -314,7 +315,7 @@ public:
         {
             auto iter = arr.find(fd);
             if (iter) {
-                _close(iter->value);
+                _close(iter->second);
                 arr.erase(iter);
             }
         }
@@ -400,7 +401,7 @@ public:
         process _proc(std::forward<Args>(args)...);
         auto pid = _proc.pid;
         auto ppid = _proc.ppid;
-        auto iter = m_procs.insert(types::make_pair(pid, std::move(_proc)));
+        auto iter = m_procs.insert(std::make_pair(pid, std::move(_proc)));
 
         auto children = m_child_idx.find(ppid);
         if (!children) {
@@ -408,7 +409,7 @@ public:
             children = m_child_idx.find(ppid);
         }
 
-        children->value.push_back(pid);
+        children->second.push_back(pid);
 
         return iter;
     }
@@ -418,9 +419,9 @@ public:
         auto iter = m_tty_idx.find(pid);
         _tty->set_pgrp(pid);
         if (iter) {
-            iter->value = _tty;
+            iter->second = _tty;
         } else {
-            m_tty_idx.insert(types::make_pair(pid, _tty));
+            m_tty_idx.insert(std::make_pair(pid, _tty));
         }
     }
 
@@ -429,7 +430,7 @@ public:
         auto iter = m_tty_idx.find(pid);
         if (!iter)
             return nullptr;
-        return iter->value;
+        return iter->second;
     }
 
     constexpr void remove(pid_t pid)
@@ -437,9 +438,9 @@ public:
         make_children_orphans(pid);
 
         auto proc_iter = m_procs.find(pid);
-        auto ppid = proc_iter->value.ppid;
+        auto ppid = proc_iter->second.ppid;
 
-        auto& parent_children = m_child_idx.find(ppid)->value;
+        auto& [ idx, parent_children ] = *m_child_idx.find(ppid);
 
         auto i = parent_children.find(pid);
         parent_children.erase(i);
@@ -452,13 +453,13 @@ public:
         auto iter = m_procs.find(pid);
         // TODO: change this
         assert(!!iter);
-        return &iter->value;
+        return &iter->second;
     }
 
     constexpr bool has_child(pid_t pid)
     {
         auto children = m_child_idx.find(pid);
-        return children && !children->value.empty();
+        return children && !children->second.empty();
     }
 
     constexpr void make_children_orphans(pid_t pid)
@@ -469,8 +470,8 @@ public:
         if (!children || !init_children)
             return;
 
-        for (auto item : children->value) {
-            init_children->value.push_back(item);
+        for (auto item : children->second) {
+            init_children->second.push_back(item);
             find(item)->ppid = 1;
         }
 

+ 8 - 9
include/types/hash_map.hpp

@@ -7,7 +7,6 @@
 #include <types/allocator.hpp>
 #include <types/cplusplus.hpp>
 #include <types/list.hpp>
-#include <types/pair.hpp>
 #include <types/string.hpp>
 #include <types/types.h>
 #include <types/vector.hpp>
@@ -100,7 +99,7 @@ public:
 
     using key_type = std::add_const_t<Key>;
     using value_type = Value;
-    using pair_type = pair<key_type, value_type>;
+    using pair_type = std::pair<key_type, value_type>;
     using size_type = size_t;
     using difference_type = ssize_t;
     using iterator_type = iterator<pair_type*>;
@@ -209,16 +208,16 @@ public:
         buckets.clear();
     }
 
-    constexpr void emplace(pair_type&& p)
+    constexpr void emplace(pair_type p)
     {
-        auto hash_value = hasher_type::hash(p.key, hash_length());
+        auto hash_value = hasher_type::hash(p.first, hash_length());
         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(std::forward<_key_type>(key), std::forward<_value_type>(value)));
+        emplace(std::make_pair(std::forward<_key_type>(key), std::forward<_value_type>(value)));
     }
 
     constexpr void remove(const key_type& key)
@@ -226,7 +225,7 @@ public:
         auto hash_value = hasher_type::hash(key, hash_length());
         auto& bucket = buckets.at(hash_value);
         for (auto iter = bucket.begin(); iter != bucket.end(); ++iter) {
-            if (iter->key == key) {
+            if (iter->first == key) {
                 bucket.erase(iter);
                 return;
             }
@@ -235,12 +234,12 @@ public:
 
     constexpr void remove(iterator_type iter)
     {
-        remove(iter->key);
+        remove(iter->first);
         iter.p = nullptr;
     }
     constexpr void remove(const_iterator_type iter)
     {
-        remove(iter->key);
+        remove(iter->first);
         iter.p = nullptr;
     }
 
@@ -249,7 +248,7 @@ public:
         auto hash_value = hasher_type::hash(key, hash_length());
         auto& bucket = buckets.at(hash_value);
         for (auto& item : bucket) {
-            if (key == item.key)
+            if (key == item.first)
                 return iterator_type(&item);
         }
         return iterator_type(nullptr);

+ 6 - 9
include/types/map.hpp

@@ -3,7 +3,6 @@
 #include <type_traits>
 
 #include <types/allocator.hpp>
-#include <types/pair.hpp>
 #include <types/types.h>
 
 namespace types {
@@ -13,7 +12,7 @@ class map {
 public:
     using key_type = std::add_const_t<Key>;
     using value_type = Value;
-    using pair_type = pair<key_type, value_type>;
+    using pair_type = std::pair<key_type, value_type>;
 
     struct node {
         node* parent = nullptr;
@@ -517,13 +516,11 @@ private:
 
     constexpr node* _find(const key_type& key) const
     {
-        node* cur = root;
-
-        for (; cur;) {
-            if (cur->v.key == key)
+        for (node* cur = root; cur;) {
+            if (cur->v.first == key)
                 return cur;
 
-            if (key < cur->v.key)
+            if (key < cur->v.first)
                 cur = cur->left;
             else
                 cur = cur->right;
@@ -633,12 +630,12 @@ public:
         return const_iterator_type(_find(key));
     }
 
-    constexpr iterator_type insert(pair_type&& val)
+    constexpr iterator_type insert(pair_type val)
     {
         node* cur = root;
 
         while (likely(cur)) {
-            if (val.key < cur->v.key) {
+            if (val.first < cur->v.first) {
                 if (!cur->left) {
                     node* nd = newnode(cur, std::move(val));
                     cur->left = nd;

+ 0 - 92
include/types/pair.hpp

@@ -1,92 +0,0 @@
-#pragma once
-#include <utility>
-#include <type_traits>
-
-namespace types {
-
-template <typename Key, typename Value>
-struct pair {
-    using key_type = Key;
-    using value_type = Value;
-
-    key_type key;
-    value_type value;
-
-    constexpr pair(void) = delete;
-    constexpr ~pair()
-    {
-    }
-
-    template <typename _key_type, typename _value_type>
-    constexpr pair(_key_type&& _key, _value_type&& _value)
-        : key(std::forward<_key_type>(_key))
-        , value(std::forward<_value_type>(_value))
-    {
-    }
-
-    template <typename _key_type, typename _value_type>
-    constexpr pair(const pair<_key_type, _value_type>& val)
-        : key(val.key)
-        , value(val.value)
-    {
-        static_assert(std::is_same_v<std::decay_t<_key_type>, std::decay_t<key_type>>);
-        static_assert(std::is_same_v<std::decay_t<_value_type>, std::decay_t<value_type>>);
-    }
-    template <typename _key_type, typename _value_type>
-    constexpr pair(pair<_key_type, _value_type>&& val)
-        : key(std::move(val.key))
-        , value(std::move(val.value))
-    {
-        static_assert(std::is_same_v<std::decay_t<_key_type>, std::decay_t<key_type>>);
-        static_assert(std::is_same_v<std::decay_t<_value_type>, std::decay_t<value_type>>);
-    }
-    constexpr pair(const pair& val)
-        : key(val.key)
-        , value(val.value)
-    {
-    }
-    constexpr pair(pair&& val)
-        : key(std::move(val.key))
-        , value(std::move(val.value))
-    {
-    }
-    constexpr pair& operator=(const pair& val)
-    {
-        key = val.key;
-        value = val.vaule;
-    }
-    constexpr pair& operator=(pair&& val)
-    {
-        key = std::move(val.key);
-        value = std::move(val.value);
-    }
-
-    constexpr bool key_eq(const pair& p)
-    {
-        return key == p.key;
-    }
-
-    constexpr bool value_eq(const pair& p)
-    {
-        return value == p.value;
-    }
-
-    constexpr bool operator==(const pair& p)
-    {
-        return key_eq(p) && value_eq(p);
-    }
-
-    constexpr bool operator!=(const pair& p)
-    {
-        return !this->operator==(p);
-    }
-};
-
-template <typename T1, typename T2>
-constexpr pair<std::decay_t<T1>, std::decay_t<T2>>
-make_pair(T1&& t1, T2&& t2)
-{
-    return pair<std::decay_t<T1>, std::decay_t<T2>> { std::forward<T1>(t1), std::forward<T2>(t2) };
-}
-
-} // namespace types

+ 2 - 2
pretty-print.py

@@ -157,8 +157,8 @@ class mapIteratorPrinter:
         if self.val['p'] == 0:
             return
         
-        yield '[key]', self.val['p']['v']['key']
-        yield '[value]', self.val['p']['v']['value']
+        yield '[first]', self.val['p']['v']['first']
+        yield '[second]', self.val['p']['v']['second']
 
 class vectorIteratorPrinter:
     def __init__(self, val):

+ 4 - 3
src/fs/fat.cpp

@@ -41,8 +41,9 @@ char* fat32::read_cluster(cluster_t no)
 {
     auto iter = buf.find(no);
     if (iter) {
-        ++iter->value.ref;
-        return iter->value.data;
+        auto [ idx, buf ] = *iter;
+        ++buf.ref;
+        return buf.data;
     }
     auto* data = new char[sectors_per_cluster * SECTOR_SIZE];
     _raw_read_cluster(data, no);
@@ -59,7 +60,7 @@ void fat32::release_cluster(cluster_t no)
 {
     auto iter = buf.find(no);
     if (iter)
-        --iter->value.ref;
+        --iter->second.ref;
 }
 
 int fat32::inode_readdir(fs::inode* dir, size_t offset, fs::vfs::filldir_func filldir)

+ 4 - 3
src/kernel/mem.cpp

@@ -386,8 +386,9 @@ void* kernel::pmap(page_t pg)
 
     auto iter = __physmapper::mapped.find(pg);
     if (iter) {
-        ++iter->value.ref;
-        return iter->value.ptr;
+        auto [ idx, area ] = *iter;
+        ++area.ref;
+        return area.ptr;
     }
 
     for (int i = 2; i < 0x400; ++i) {
@@ -416,7 +417,7 @@ void kernel::pfree(page_t pg)
     auto iter = __physmapper::mapped.find(pg);
     if (!iter)
         return;
-    auto& [ ref, ptr ] = iter->value;
+    auto& [ ref, ptr ] = iter->second;
 
     if (ref > 1) {
         --ref;

+ 2 - 2
src/kernel/process.cpp

@@ -237,7 +237,7 @@ void kernel_threadd_main(void)
 void NORETURN _kernel_init(void)
 {
     // pid 2 is kernel thread daemon
-    auto* proc = &procs->emplace(1)->value;
+    auto* proc = &procs->emplace(1)->second;
 
     // create thread
     thread thd(proc, true);
@@ -344,7 +344,7 @@ void NORETURN init_scheduler(void)
     process::filearr::init_global_file_container();
 
     // init process has no parent
-    auto* init = &procs->emplace(0)->value;
+    auto* init = &procs->emplace(0)->second;
     init->files.open("/dev/console", O_RDONLY);
     init->files.open("/dev/console", O_WRONLY);
     init->files.open("/dev/console", O_WRONLY);

+ 1 - 1
src/kernel/syscall.cpp

@@ -25,7 +25,7 @@ syscall_handler syscall_handlers[SYSCALL_HANDLERS_SIZE];
 extern "C" void _syscall_stub_fork_return(void);
 int _syscall_fork(interrupt_stack* data)
 {
-    auto* newproc = &procs->emplace(*current_process)->value;
+    auto* newproc = &procs->emplace(*current_process)->second;
     auto* newthd = &newproc->thds.Emplace(*current_thread, newproc);
     readythds->push(newthd);
 

+ 8 - 8
src/kernel/vfs.cpp

@@ -1,4 +1,5 @@
 #include <bit>
+#include <utility>
 
 #include <assert.h>
 #include <kernel/errno.h>
@@ -11,7 +12,6 @@
 #include <types/allocator.hpp>
 #include <types/list.hpp>
 #include <types/map.hpp>
-#include <types/pair.hpp>
 #include <types/status.h>
 #include <types/string.hpp>
 #include <types/vector.hpp>
@@ -99,12 +99,12 @@ fs::vfs::dentry* fs::vfs::dentry::find(const name_type& name)
         return nullptr;
     }
 
-    return iter->value;
+    return iter->second;
 }
 fs::vfs::dentry* fs::vfs::dentry::replace(dentry* val)
 {
     // TODO: prevent the dirent to be swapped out of memory
-    parent->idx_children->find(this->name)->value = val;
+    parent->idx_children->find(this->name)->second = val;
     return this;
 }
 void fs::vfs::dentry::invalidate(void)
@@ -121,15 +121,15 @@ fs::vfs::vfs(void)
 }
 fs::inode* fs::vfs::cache_inode(inode_flags flags, uint32_t perm, size_t size, ino_t ino)
 {
-    auto iter = _inodes.insert(types::make_pair(ino, inode { flags, perm, ino, this, size }));
-    return &iter->value;
+    auto iter = _inodes.insert(std::make_pair(ino, inode { flags, perm, ino, this, size }));
+    return &iter->second;
 }
 fs::inode* fs::vfs::get_inode(ino_t ino)
 {
     auto iter = _inodes.find(ino);
     // TODO: load inode from disk if not found
     if (iter)
-        return &iter->value;
+        return &iter->second;
     else
         return nullptr;
 }
@@ -255,12 +255,12 @@ private:
     }
     inline void* _getdata(fs::ino_t ino) const
     {
-        return inode_data.find(ino)->value;
+        return inode_data.find(ino)->second;
     }
     inline fs::ino_t _savedata(void* data)
     {
         fs::ino_t ino = _assign_ino();
-        inode_data.insert(types::make_pair(ino, data));
+        inode_data.insert(std::make_pair(ino, data));
         return ino;
     }
     inline fs::ino_t _savedata(ptr_t data)