瀏覽代碼

change(vfs): only allocate children lists for dirs

greatbridf 2 年之前
父節點
當前提交
5a58906104
共有 2 個文件被更改,包括 36 次插入13 次删除
  1. 6 2
      include/kernel/vfs.hpp
  2. 30 11
      src/kernel/vfs.cpp

+ 6 - 2
include/kernel/vfs.hpp

@@ -75,10 +75,12 @@ public:
     struct dentry {
     public:
         using name_type = types::string<>;
+        template <typename T>
+        using allocator_type = types::kernel_allocator<T>;
 
     private:
-        types::list<dentry> children;
-        types::hash_map<name_type, dentry*, types::string_hasher<const name_type&>> idx_children;
+        types::list<dentry, allocator_type>* children = nullptr;
+        types::hash_map<name_type, dentry*, types::string_hasher<const name_type&>, allocator_type>* idx_children = nullptr;
 
     public:
         dentry* parent;
@@ -101,6 +103,8 @@ public:
         dentry& operator=(const dentry& val) = delete;
         dentry& operator=(dentry&& val) = delete;
 
+        ~dentry();
+
         dentry* append(inode* ind, const name_type& name);
         dentry* append(inode* ind, name_type&& name);
 

+ 30 - 11
src/kernel/vfs.cpp

@@ -27,6 +27,10 @@ fs::vfs::dentry::dentry(dentry* _parent, inode* _ind, const name_type& _name)
     , flags { 0 }
     , name(_name)
 {
+    if (!_ind || _ind->flags.in.directory) {
+        children = types::pnew<allocator_type>(children);
+        idx_children = types::pnew<allocator_type>(idx_children);
+    }
 }
 fs::vfs::dentry::dentry(dentry* _parent, inode* _ind, name_type&& _name)
     : parent(_parent)
@@ -34,28 +38,40 @@ fs::vfs::dentry::dentry(dentry* _parent, inode* _ind, name_type&& _name)
     , flags { 0 }
     , name(types::move(_name))
 {
+    if (!_ind || _ind->flags.in.directory) {
+        children = types::pnew<allocator_type>(children);
+        idx_children = types::pnew<allocator_type>(idx_children);
+    }
 }
 fs::vfs::dentry::dentry(dentry&& val)
-    : children(types::move(val.children))
-    , idx_children(types::move(val.idx_children))
+    : children(val.children)
+    , idx_children(val.idx_children)
     , parent(val.parent)
     , ind(val.ind)
     , flags { val.flags }
     , name(types::move(val.name))
 {
-    for (auto& item : children)
+    for (auto& item : *children)
         item.parent = this;
+    memset(&val, 0x00, sizeof(dentry));
+}
+fs::vfs::dentry::~dentry()
+{
+    if (children) {
+        types::pdelete<allocator_type>(children);
+        types::pdelete<allocator_type>(idx_children);
+    }
 }
 fs::vfs::dentry* fs::vfs::dentry::append(inode* ind, const name_type& name)
 {
-    auto iter = children.emplace_back(this, ind, name);
-    idx_children.insert(iter->name, &iter);
+    auto iter = children->emplace_back(this, ind, name);
+    idx_children->insert(iter->name, &iter);
     return &iter;
 }
 fs::vfs::dentry* fs::vfs::dentry::append(inode* ind, name_type&& name)
 {
-    auto iter = children.emplace_back(this, ind, types::move(name));
-    idx_children.insert(iter->name, &iter);
+    auto iter = children->emplace_back(this, ind, types::move(name));
+    idx_children->insert(iter->name, &iter);
     return &iter;
 }
 fs::vfs::dentry* fs::vfs::dentry::find(const name_type& name)
@@ -63,7 +79,7 @@ fs::vfs::dentry* fs::vfs::dentry::find(const name_type& name)
     if (ind->flags.in.directory && !flags.in.present)
         ind->fs->load_dentry(this);
 
-    auto iter = idx_children.find(name);
+    auto iter = idx_children->find(name);
     if (!iter) {
         errno = ENOTFOUND;
         return nullptr;
@@ -74,15 +90,15 @@ fs::vfs::dentry* fs::vfs::dentry::find(const name_type& name)
 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)->value = val;
     return this;
 }
 void fs::vfs::dentry::invalidate(void)
 {
     // TODO: write back
     flags.in.dirty = 0;
-    children.clear();
-    idx_children.clear();
+    children->clear();
+    idx_children->clear();
     flags.in.present = 0;
 }
 fs::vfs::vfs(void)
@@ -133,6 +149,9 @@ int fs::vfs::mount(dentry* mnt, vfs* new_fs)
 
     auto* orig_ent = mnt->replace(new_ent);
     _mount_recover_list.insert(new_ent, orig_ent);
+
+    new_ent->ind->flags.in.mount_point = 1;
+
     return GB_OK;
 }
 size_t fs::vfs::inode_read(inode*, char*, size_t, size_t, size_t)