Browse Source

change(kernel): replace types::list with std::list

greatbridf 1 year ago
parent
commit
719a421750

+ 3 - 2
include/kernel/event/evtqueue.hpp

@@ -1,7 +1,8 @@
 #pragma once
 
+#include <list>
+
 #include <types/cplusplus.hpp>
-#include <types/list.hpp>
 #include <types/lock.hpp>
 
 namespace kernel {
@@ -15,7 +16,7 @@ struct thread;
 
 class cond_var : public types::non_copyable {
 private:
-    using list_type = types::list<tasks::thread*>;
+    using list_type = std::list<tasks::thread*>;
 
     types::mutex m_mtx;
     list_type m_subscribers;

+ 10 - 9
include/kernel/mm.hpp

@@ -1,5 +1,6 @@
 #pragma once
 
+#include <list>
 #include <bit>
 #include <cstddef>
 #include <utility>
@@ -9,7 +10,6 @@
 #include <stdint.h>
 #include <types/allocator.hpp>
 #include <types/cplusplus.hpp>
-#include <types/list.hpp>
 #include <types/size.h>
 #include <types/status.h>
 #include <types/types.h>
@@ -185,9 +185,10 @@ public:
 
 class mm_list {
 public:
-    using list_type = ::types::list<mm, types::kernel_ident_allocator>;
-    using iterator_type = list_type::iterator_type;
-    using const_iterator_type = list_type::const_iterator_type;
+    using list_type = std::list<mm,
+        types::allocator_adapter<mm, types::kernel_ident_allocator>>;
+    using iterator_type = list_type::iterator;
+    using const_iterator_type = list_type::const_iterator;
 
 private:
     list_type m_areas;
@@ -243,7 +244,7 @@ public:
         return m_areas.cend();
     }
 
-    constexpr iterator_type addarea(void* start, bool w, bool system)
+    constexpr mm& addarea(void* start, bool w, bool system)
     {
         return m_areas.emplace_back(mm {
             .start = start,
@@ -275,16 +276,16 @@ public:
 
     constexpr int mirror_area(mm& src)
     {
-        auto area = this->addarea(
+        auto& area = this->addarea(
             src.start, src.attr.in.write, src.attr.in.system);
 
         if (src.mapped_file) {
-            area->mapped_file = src.mapped_file;
-            area->file_offset = src.file_offset;
+            area.mapped_file = src.mapped_file;
+            area.file_offset = src.file_offset;
         }
 
         for (auto& pg : *src.pgs) {
-            if (area->append_page(pg,
+            if (area.append_page(pg,
                     PAGE_COW | (pg.attr & PAGE_MMAP),
                     src.attr.in.system)
                 != GB_OK) {

+ 19 - 29
include/kernel/process.hpp

@@ -1,6 +1,7 @@
 #pragma once
 
 #include <map>
+#include <list>
 #include <set>
 #include <tuple>
 #include <utility>
@@ -91,8 +92,8 @@ public:
 
 class filearr {
 public:
-    using container_type = types::list<fs::file>;
-    using array_type = std::map<int, container_type::iterator_type>;
+    using container_type = std::list<fs::file>;
+    using array_type = std::map<int, container_type::iterator>;
 
 private:
     inline static container_type* files;
@@ -106,7 +107,7 @@ public:
 
 private:
     // iter should not be nullptr
-    constexpr void _close(container_type::iterator_type iter)
+    constexpr void _close(container_type::iterator iter)
     {
         if (iter->ref == 1) {
             if (iter->type == fs::file::types::pipe) {
@@ -188,7 +189,7 @@ public:
         // TODO: set read/write flags
         auto* pipe = new fs::pipe;
 
-        auto iter = files->emplace_back(fs::file {
+        auto iter = files->emplace(files->cend(), fs::file {
             fs::file::types::pipe,
             { .pp = pipe },
             nullptr,
@@ -202,14 +203,13 @@ public:
 
         bool inserted = false;
         int fd = _next_fd();
-        std::tie(std::ignore, inserted) =
-            arr.insert(std::make_pair(fd, iter));
+        std::tie(std::ignore, inserted) = arr.emplace(fd, iter);
         assert(inserted);
 
         // TODO: use copy_to_user()
         pipefd[0] = fd;
 
-        iter = files->emplace_back(fs::file {
+        iter = files->emplace(files->cend(), fs::file {
             fs::file::types::pipe,
             { .pp = pipe },
             nullptr,
@@ -246,7 +246,7 @@ public:
             return -1;
         }
 
-        auto iter = files->emplace_back(fs::file {
+        auto iter = files->emplace(files->cend(), fs::file {
             fs::file::types::ind,
             { .ind = dentry->ind },
             dentry->parent,
@@ -298,7 +298,7 @@ public:
     mutable kernel::mm_list mms;
     std::set<kernel::tasks::thread> thds;
     kernel::cond_var cv_wait;
-    types::list<wait_obj> waitlist;
+    std::list<wait_obj> waitlist;
     process_attr attr;
     filearr files;
     types::string<> pwd;
@@ -424,13 +424,11 @@ public:
     void kill(pid_t pid, int exit_code);
 };
 
+// TODO: lock and unlock
 class readyqueue final {
 public:
     using thread = kernel::tasks::thread;
-
-    using list_type = types::list<thread*>;
-    using iterator_type = list_type::iterator_type;
-    using const_iterator_type = list_type::const_iterator_type;
+    using list_type = std::list<thread*>;
 
 private:
     list_type m_thds;
@@ -447,18 +445,16 @@ public:
     constexpr explicit readyqueue(void) = default;
 
     constexpr void push(thread* thd)
-    {
-        m_thds.push_back(thd);
-    }
+    { m_thds.push_back(thd); }
 
     constexpr thread* pop(void)
     {
-        auto iter = m_thds.begin();
-        while (!((*iter)->attr.ready))
-            iter = m_thds.erase(iter);
-        auto* ptr = *iter;
-        m_thds.erase(iter);
-        return ptr;
+        m_thds.remove_if([](thread* item) {
+            return !item->attr.ready;
+        });
+        auto* retval = m_thds.front();
+        m_thds.pop_front();
+        return retval;
     }
 
     constexpr thread* query(void)
@@ -469,13 +465,7 @@ public:
     }
 
     constexpr void remove_all(thread* thd)
-    {
-        auto iter = m_thds.find(thd);
-        while (iter != m_thds.end()) {
-            m_thds.erase(iter);
-            iter = m_thds.find(thd);
-        }
-    }
+    { m_thds.remove(thd); }
 };
 
 void NORETURN init_scheduler(void);

+ 3 - 2
include/kernel/signal.hpp

@@ -1,8 +1,9 @@
 #pragma once
 
+#include <list>
+
 #include <stdint.h>
 #include <types/cplusplus.hpp>
-#include <types/list.hpp>
 
 namespace kernel {
 
@@ -15,7 +16,7 @@ constexpr sig_t SIGPIPE = 1 << 3;
 
 class signal_list {
 public:
-    using list_type = types::list<sig_t>;
+    using list_type = std::list<sig_t>;
 
 private:
     list_type m_list;

+ 2 - 2
include/kernel/vfs.hpp

@@ -1,6 +1,7 @@
 #pragma once
 
 #include <map>
+#include <list>
 #include <functional>
 
 #include <assert.h>
@@ -10,7 +11,6 @@
 #include <types/buffer.hpp>
 #include <types/cplusplus.hpp>
 #include <types/hash_map.hpp>
-#include <types/list.hpp>
 #include <types/lock.hpp>
 #include <types/types.h>
 #include <types/vector.hpp>
@@ -108,7 +108,7 @@ public:
         using allocator_type = types::kernel_allocator<T>;
 
     private:
-        types::list<dentry, allocator_type>* children = nullptr;
+        std::list<dentry, types::allocator_adapter<dentry, allocator_type>>* children = nullptr;
         types::hash_map<name_type, dentry*, types::linux_hasher, allocator_type>* idx_children = nullptr;
 
     public:

+ 21 - 0
include/types/allocator.hpp

@@ -294,4 +294,25 @@ public:
         __allocator::m_palloc->free(ptr);
     }
 };
+
+template <typename T, template <typename> typename Allocator>
+struct allocator_adapter {
+    using value_type = typename Allocator<T>::value_type;
+    using propagate_on_container_move_assignment = std::true_type;
+
+    constexpr allocator_adapter() = default;
+
+    template <template <typename> typename UAlloc, typename U>
+    constexpr allocator_adapter(const allocator_adapter<U, UAlloc>&)
+        noexcept {}
+    
+    constexpr T* allocate(std::size_t n)
+    { return types::allocator_traits<Allocator<T>>::allocate(n); }
+    constexpr void deallocate(T* ptr, std::size_t)
+    { return types::allocator_traits<Allocator<T>>::deallocate(ptr); }
+
+    template <typename U>
+    struct rebind { using other = allocator_adapter<U, Allocator>; };
+};
+
 } // namespace types

+ 3 - 2
include/types/hash_map.hpp

@@ -1,4 +1,5 @@
 #pragma once
+#include <list>
 #include <bit>
 #include <utility>
 #include <type_traits>
@@ -6,7 +7,6 @@
 #include <stdint.h>
 #include <types/allocator.hpp>
 #include <types/cplusplus.hpp>
-#include <types/list.hpp>
 #include <types/string.hpp>
 #include <types/types.h>
 #include <types/vector.hpp>
@@ -105,7 +105,8 @@ public:
     using iterator_type = iterator<pair_type*>;
     using const_iterator_type = iterator<const pair_type*>;
 
-    using bucket_type = list<pair_type, Allocator>;
+    using bucket_type = std::list<pair_type,
+        types::allocator_adapter<pair_type, Allocator>>;
     using bucket_array_type = vector<bucket_type, Allocator>;
 
     using hasher_type = Hasher<Key>;

+ 0 - 362
include/types/list.hpp

@@ -1,362 +0,0 @@
-#pragma once
-#include <utility>
-#include <type_traits>
-
-#include <types/allocator.hpp>
-#include <types/types.h>
-
-namespace types {
-
-template <typename T, template <typename _value_type> class Allocator = kernel_allocator>
-class list {
-private:
-    class node_base;
-    template <typename NodeValue>
-    class node;
-
-public:
-    template <typename Pointer>
-    class iterator;
-
-    using value_type = T;
-    using pointer_type = value_type*;
-    using reference_type = value_type&;
-    using iterator_type = iterator<value_type*>;
-    using const_iterator_type = iterator<const value_type*>;
-    using size_type = size_t;
-    using difference_type = ssize_t;
-    using node_base_type = node_base;
-    using node_type = node<value_type>;
-    using allocator_type = Allocator<node_type>;
-    using sentry_node_type = node<size_t>;
-    using sentry_allocator_type = Allocator<sentry_node_type>;
-
-private:
-    class node_base {
-    public:
-        node_type* prev = 0;
-        node_type* next = 0;
-
-        constexpr void connect(node_type* _next) noexcept
-        {
-            this->next = _next;
-            _next->prev = static_cast<node_type*>(this);
-        }
-    };
-
-    template <typename NodeValue>
-    class node : public node_base {
-    public:
-        explicit constexpr node(const NodeValue& v) noexcept
-            : value(v)
-        {
-        }
-
-        explicit constexpr node(NodeValue&& v) noexcept
-            : value(std::move(v))
-        {
-        }
-
-        NodeValue value;
-    };
-
-public:
-    template <typename Pointer>
-    class iterator {
-    public:
-        using Value = std::remove_pointer_t<Pointer>;
-        using Reference = std::add_lvalue_reference_t<Value>;
-
-        friend class list;
-
-    public:
-        constexpr iterator(const iterator& iter) noexcept
-            : n(iter.n)
-        {
-        }
-
-        constexpr iterator(iterator&& iter) noexcept
-            : n(iter.n)
-        {
-            iter.n = nullptr;
-        }
-
-        constexpr iterator& operator=(const iterator& iter)
-        {
-            n = iter.n;
-            return *this;
-        }
-
-        explicit constexpr iterator(node_type* _n) noexcept
-            : n(_n)
-        {
-        }
-
-        constexpr bool operator==(const iterator& iter) const noexcept
-        {
-            return this->_node() == iter._node();
-        }
-
-        constexpr bool operator!=(const iterator& iter) const noexcept
-        {
-            return !(*this == iter);
-        }
-
-        constexpr iterator& operator++(void) noexcept
-        {
-            n = n->next;
-            return *this;
-        }
-
-        constexpr iterator operator++(int) noexcept
-        {
-            iterator iter(*this);
-            n = n->next;
-            return iter;
-        }
-
-        constexpr iterator& operator--(void) noexcept
-        {
-            n = n->prev;
-            return *this;
-        }
-
-        constexpr iterator operator--(int) noexcept
-        {
-            iterator iter(*this);
-            n = n->prev;
-            return iter;
-        }
-
-        constexpr Reference operator*(void) const noexcept
-        {
-            return n->value;
-        }
-
-        constexpr Pointer operator&(void) const noexcept
-        {
-            return &n->value;
-        }
-
-        constexpr Pointer operator->(void) const noexcept
-        {
-            return &n->value;
-        }
-
-    protected:
-        constexpr node_base_type* _node(void) const noexcept
-        {
-            return n;
-        }
-
-    protected:
-        node_type* n;
-    };
-
-private:
-    node_base_type* head;
-    node_base_type* tail;
-
-    constexpr const size_t& _size(void) const noexcept
-    {
-        return (static_cast<sentry_node_type*>(head))->value;
-    }
-
-    constexpr size_t& _size(void) noexcept
-    {
-        return (static_cast<sentry_node_type*>(head))->value;
-    }
-
-    constexpr void destroy(void)
-    {
-        if (!head || !tail)
-            return;
-        clear();
-        allocator_traits<sentry_allocator_type>::deconstruct_and_deallocate(static_cast<sentry_node_type*>(head));
-        allocator_traits<sentry_allocator_type>::deconstruct_and_deallocate(static_cast<sentry_node_type*>(tail));
-    }
-
-public:
-    constexpr list() noexcept
-        // size is stored in the 'head' node
-        : head(allocator_traits<sentry_allocator_type>::allocate_and_construct(0))
-        , tail(allocator_traits<sentry_allocator_type>::allocate_and_construct(0))
-    {
-        head->connect(static_cast<node_type*>(tail));
-        tail->connect(static_cast<node_type*>(head));
-    }
-
-    constexpr list(const list& v)
-        : list()
-    {
-        for (const auto& item : v)
-            push_back(item);
-    }
-
-    constexpr list(list&& v)
-        : head(v.head)
-        , tail(v.tail)
-    {
-        v.head = nullptr;
-        v.tail = nullptr;
-    }
-
-    constexpr list& operator=(const list& v)
-    {
-        clear();
-        for (const auto& item : v)
-            push_back(item);
-        return *this;
-    }
-
-    constexpr list& operator=(list&& v)
-    {
-        destroy();
-
-        head = v.head;
-        tail = v.tail;
-        v.head = nullptr;
-        v.tail = nullptr;
-
-        return *this;
-    }
-
-    constexpr ~list() noexcept
-    {
-        destroy();
-    }
-
-    constexpr iterator_type find(const value_type& v) noexcept
-    {
-        for (iterator_type iter = begin(); iter != end(); ++iter)
-            if (*iter == v)
-                return iter;
-        return end();
-    }
-
-    // erase the node which iter points to
-    constexpr iterator_type erase(const iterator_type& iter) noexcept
-    {
-        node_base_type* current_node = iter._node();
-        iterator_type ret(current_node->next);
-        current_node->prev->connect(current_node->next);
-        allocator_traits<allocator_type>::deconstruct_and_deallocate(static_cast<node_type*>(current_node));
-        --_size();
-        return ret;
-    }
-
-    constexpr void clear(void)
-    {
-        for (auto iter = begin(); iter != end();)
-            iter = erase(iter);
-    }
-
-    // insert the value v in front of the given iterator
-    constexpr iterator_type insert(const iterator_type& iter, const value_type& v) noexcept
-    {
-        node_type* new_node = allocator_traits<allocator_type>::allocate_and_construct(v);
-        iterator_type ret(new_node);
-        iter._node()->prev->connect(new_node);
-        new_node->connect(static_cast<node_type*>(iter._node()));
-
-        ++_size();
-        return ret;
-    }
-
-    // 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(std::move(v));
-        iterator_type ret(new_node);
-        iter._node()->prev->connect(new_node);
-        new_node->connect(static_cast<node_type*>(iter._node()));
-
-        ++_size();
-        return ret;
-    }
-
-    constexpr void push_back(const value_type& v) noexcept
-    {
-        insert(end(), v);
-    }
-
-    constexpr void push_back(value_type&& v) noexcept
-    {
-        insert(end(), std::move(v));
-    }
-
-    template <typename... Args>
-    constexpr iterator_type emplace_back(Args&&... args)
-    {
-        return insert(end(), value_type(std::forward<Args>(args)...));
-    }
-
-    constexpr void push_front(const value_type& v) noexcept
-    {
-        insert(begin(), v);
-    }
-
-    constexpr void push_front(value_type&& v) noexcept
-    {
-        insert(begin(), std::move(v));
-    }
-
-    template <typename... Args>
-    constexpr iterator_type emplace_front(Args&&... args)
-    {
-        return insert(begin(), value_type(std::forward<Args>(args)...));
-    }
-
-    constexpr size_t size(void) const noexcept
-    {
-        return _size();
-    }
-
-    constexpr iterator_type begin() noexcept
-    {
-        if (head)
-            return iterator_type(head->next);
-        return end();
-    }
-
-    constexpr iterator_type end() noexcept
-    {
-        return iterator_type(static_cast<node_type*>(tail));
-    }
-
-    constexpr const_iterator_type begin() const noexcept
-    {
-        if (head)
-            return const_iterator_type(head->next);
-        return end();
-    }
-
-    constexpr const_iterator_type end() const noexcept
-    {
-        return const_iterator_type(static_cast<node_type*>(tail));
-    }
-
-    constexpr const_iterator_type cbegin() const noexcept
-    {
-        return begin();
-    }
-
-    constexpr const_iterator_type cend() const noexcept
-    {
-        return end();
-    }
-
-    constexpr bool empty(void) const noexcept
-    {
-        return size() == 0;
-    }
-
-    // TODO
-    // iterator_type r_start() noexcept;
-    // iterator_type r_end() noexcept;
-
-    // iterator_type cr_start() noexcept;
-    // iterator_type cr_end() noexcept;
-};
-
-} // namespace types

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

@@ -8,16 +8,15 @@
 #include <stdio.h>
 #include <types/allocator.hpp>
 #include <types/cplusplus.hpp>
-#include <types/list.hpp>
 #include <types/lock.hpp>
 
-static ::types::list<::input_event>* _input_event_queue;
+static std::list<::input_event>* _input_event_queue;
 
 namespace event {
-::types::list<::input_event>& input_event_queue(void)
+std::list<::input_event>& input_event_queue(void)
 {
     if (!_input_event_queue) {
-        _input_event_queue = new types::list<input_event>;
+        _input_event_queue = new std::list<input_event>;
     }
     return *_input_event_queue;
 }

+ 7 - 7
src/kernel/mem.cpp

@@ -315,12 +315,12 @@ int mmap(
         return GB_FAILED;
     }
 
-    auto mm = mms.addarea(hint, write, priv);
-    mm->mapped_file = file;
-    mm->file_offset = offset;
+    auto& mm = mms.addarea(hint, write, priv);
+    mm.mapped_file = file;
+    mm.file_offset = offset;
 
     for (size_t i = 0; i < n_pgs; ++i)
-        mm->append_page(empty_page, PAGE_MMAP | PAGE_COW, priv);
+        mm.append_page(empty_page, PAGE_MMAP | PAGE_COW, priv);
 
     return GB_OK;
 }
@@ -332,7 +332,7 @@ void init_mem(void)
 
     // TODO: replace early kernel pd
     kernel_mms = types::pnew<types::kernel_ident_allocator>(kernel_mms, EARLY_KERNEL_PD_PAGE);
-    auto heap_mm = kernel_mms->addarea(KERNEL_HEAP_START, true, true);
+    auto& heap_mm = kernel_mms->addarea(KERNEL_HEAP_START, true, true);
 
     // create empty_page struct
     empty_page.attr = 0;
@@ -341,8 +341,8 @@ void init_mem(void)
     empty_page.pg_pteidx = 0x00002000;
 
     // 0xd0000000 to 0xd4000000 or 3.5GiB, size 64MiB
-    while (heap_mm->pgs->size() < 64 * 1024 * 1024 / PAGE_SIZE)
-        heap_mm->append_page(empty_page, PAGE_COW, true);
+    while (heap_mm.pgs->size() < 64 * 1024 * 1024 / PAGE_SIZE)
+        heap_mm.append_page(empty_page, PAGE_COW, true);
 
     types::__allocator::init_kernel_heap(KERNEL_HEAP_START,
         vptrdiff(KERNEL_HEAP_LIMIT, KERNEL_HEAP_START));

+ 0 - 1
src/kernel/process.cpp

@@ -19,7 +19,6 @@
 #include <types/cplusplus.hpp>
 #include <types/elf.hpp>
 #include <types/hash_map.hpp>
-#include <types/list.hpp>
 #include <types/lock.hpp>
 #include <types/size.h>
 #include <types/status.h>

+ 7 - 8
src/kernel/vfs.cpp

@@ -11,7 +11,6 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <types/allocator.hpp>
-#include <types/list.hpp>
 #include <types/status.h>
 #include <types/string.hpp>
 #include <types/vector.hpp>
@@ -51,19 +50,19 @@ fs::vfs::dentry::dentry(dentry* _parent, inode* _ind, name_type&& _name)
 
 fs::vfs::dentry* fs::vfs::dentry::append(inode* ind, const name_type& name, bool set_dirty)
 {
-    auto iter = children->emplace_back(this, ind, name);
-    idx_children->emplace(iter->name, &iter);
+    auto& ent = children->emplace_back(this, ind, name);
+    idx_children->emplace(ent.name, &ent);
     if (set_dirty)
         this->flags.in.dirty = 1;
-    return &iter;
+    return &ent;
 }
 fs::vfs::dentry* fs::vfs::dentry::append(inode* ind, name_type&& name, bool set_dirty)
 {
-    auto iter = children->emplace_back(this, ind, std::move(name));
-    idx_children->emplace(iter->name, &iter);
+    auto& ent = children->emplace_back(this, ind, std::move(name));
+    idx_children->emplace(ent.name, &ent);
     if (set_dirty)
         this->flags.in.dirty = 1;
-    return &iter;
+    return &ent;
 }
 fs::vfs::dentry* fs::vfs::dentry::find(const name_type& name)
 {
@@ -528,7 +527,7 @@ int fs::vfs_stat(fs::vfs::dentry* ent, stat* stat)
     return ent->ind->fs->inode_stat(ent, stat);
 }
 
-static types::list<fs::vfs*>* fs_es;
+static std::list<fs::vfs*>* fs_es;
 
 void fs::register_special_block(
     uint16_t major,