From ec1080b18add64c72dfc1fed1953f9e30876c26e Mon Sep 17 00:00:00 2001 From: asynts Date: Mon, 7 Sep 2020 11:53:54 +0200 Subject: [PATCH] Refactor: Replace usages of FixedArray with Vector. --- AK/Span.h | 9 +++++++++ Kernel/FileSystem/InodeMetadata.h | 14 +++++++------- Kernel/Interrupts/InterruptManagement.cpp | 1 + Kernel/Interrupts/InterruptManagement.h | 3 +-- Kernel/Process.cpp | 2 +- Kernel/Process.h | 5 ++--- Kernel/Syscalls/getuid.cpp | 6 +----- Kernel/VM/VMObject.cpp | 2 +- Kernel/VM/VMObject.h | 8 ++++---- 9 files changed, 27 insertions(+), 23 deletions(-) diff --git a/AK/Span.h b/AK/Span.h index 216ad889812..68321cede41 100644 --- a/AK/Span.h +++ b/AK/Span.h @@ -174,6 +174,15 @@ public: } } + bool contains_slow(const T& value) const + { + for (size_t i = 0; i < size(); ++i) { + if (at(i) == value) + return true; + } + return false; + } + ALWAYS_INLINE const T& at(size_t index) const { ASSERT(index < this->m_size); diff --git a/Kernel/FileSystem/InodeMetadata.h b/Kernel/FileSystem/InodeMetadata.h index 843a2b691f4..94840ca0895 100644 --- a/Kernel/FileSystem/InodeMetadata.h +++ b/Kernel/FileSystem/InodeMetadata.h @@ -26,7 +26,7 @@ #pragma once -#include +#include #include #include #include @@ -58,35 +58,35 @@ struct InodeMetadata { bool may_write(const Process&) const; bool may_execute(const Process&) const; - bool may_read(uid_t u, gid_t g, const FixedArray& eg) const + bool may_read(uid_t u, gid_t g, Span eg) const { if (u == 0) return true; if (uid == u) return mode & S_IRUSR; - if (gid == g || eg.contains(gid)) + if (gid == g || eg.contains_slow(gid)) return mode & S_IRGRP; return mode & S_IROTH; } - bool may_write(uid_t u, gid_t g, const FixedArray& eg) const + bool may_write(uid_t u, gid_t g, Span eg) const { if (u == 0) return true; if (uid == u) return mode & S_IWUSR; - if (gid == g || eg.contains(gid)) + if (gid == g || eg.contains_slow(gid)) return mode & S_IWGRP; return mode & S_IWOTH; } - bool may_execute(uid_t u, gid_t g, const FixedArray& eg) const + bool may_execute(uid_t u, gid_t g, Span eg) const { if (u == 0) return true; if (uid == u) return mode & S_IXUSR; - if (gid == g || eg.contains(gid)) + if (gid == g || eg.contains_slow(gid)) return mode & S_IXGRP; return mode & S_IXOTH; } diff --git a/Kernel/Interrupts/InterruptManagement.cpp b/Kernel/Interrupts/InterruptManagement.cpp index e92d78bc5e1..b59aecb23c5 100644 --- a/Kernel/Interrupts/InterruptManagement.cpp +++ b/Kernel/Interrupts/InterruptManagement.cpp @@ -137,6 +137,7 @@ PhysicalAddress InterruptManagement::search_for_madt() InterruptManagement::InterruptManagement() : m_madt(search_for_madt()) { + m_interrupt_controllers.resize(1); } void InterruptManagement::switch_to_pic_mode() diff --git a/Kernel/Interrupts/InterruptManagement.h b/Kernel/Interrupts/InterruptManagement.h index eb5edd2de6c..89f24780ec2 100644 --- a/Kernel/Interrupts/InterruptManagement.h +++ b/Kernel/Interrupts/InterruptManagement.h @@ -26,7 +26,6 @@ #pragma once -#include #include #include #include @@ -89,7 +88,7 @@ private: PhysicalAddress search_for_madt(); void locate_apic_data(); bool m_smp_enabled { false }; - FixedArray> m_interrupt_controllers { 1 }; + Vector> m_interrupt_controllers; Vector m_isa_interrupt_overrides; Vector m_pci_interrupt_overrides; PhysicalAddress m_madt; diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index f85a6fcb336..b925a3859f1 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -142,7 +142,7 @@ NonnullRefPtrVector Process::all_processes() bool Process::in_group(gid_t gid) const { - return m_gid == gid || m_extra_gids.contains(gid); + return m_gid == gid || m_extra_gids.contains_slow(gid); } Range Process::allocate_range(VirtualAddress vaddr, size_t size, size_t alignment) diff --git a/Kernel/Process.h b/Kernel/Process.h index 7d25648c3fd..ee745ab9124 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -27,7 +27,6 @@ #pragma once #include -#include #include #include #include @@ -162,7 +161,7 @@ public: bool is_session_leader() const { return m_sid.value() == m_pid.value(); } ProcessGroupID pgid() const { return m_pg ? m_pg->pgid() : 0; } bool is_group_leader() const { return pgid().value() == m_pid.value(); } - const FixedArray& extra_gids() const { return m_extra_gids; } + Span extra_gids() const { return m_extra_gids; } uid_t euid() const { return m_euid; } gid_t egid() const { return m_egid; } uid_t uid() const { return m_uid; } @@ -683,7 +682,7 @@ private: ProcessID m_ppid { 0 }; mode_t m_umask { 022 }; - FixedArray m_extra_gids; + Vector m_extra_gids; WeakPtr m_master_tls_region; size_t m_master_tls_size { 0 }; diff --git a/Kernel/Syscalls/getuid.cpp b/Kernel/Syscalls/getuid.cpp index d8bbda740d5..699ad9b6ca7 100644 --- a/Kernel/Syscalls/getuid.cpp +++ b/Kernel/Syscalls/getuid.cpp @@ -86,11 +86,7 @@ int Process::sys$getgroups(ssize_t count, Userspace user_gids) if (!validate_write_typed(user_gids, m_extra_gids.size())) return -EFAULT; - Vector gids; - for (auto gid : m_extra_gids) - gids.append(gid); - - copy_to_user(user_gids, gids.data(), sizeof(gid_t) * count); + copy_to_user(user_gids, m_extra_gids.data(), sizeof(gid_t) * count); return 0; } diff --git a/Kernel/VM/VMObject.cpp b/Kernel/VM/VMObject.cpp index 4b4c92c93ed..9c69b5a2704 100644 --- a/Kernel/VM/VMObject.cpp +++ b/Kernel/VM/VMObject.cpp @@ -38,8 +38,8 @@ VMObject::VMObject(const VMObject& other) } VMObject::VMObject(size_t size) - : m_physical_pages(ceil_div(size, PAGE_SIZE)) { + m_physical_pages.resize(ceil_div(size, PAGE_SIZE)); MM.register_vmobject(*this); } diff --git a/Kernel/VM/VMObject.h b/Kernel/VM/VMObject.h index e7043f03c14..11bb4dada58 100644 --- a/Kernel/VM/VMObject.h +++ b/Kernel/VM/VMObject.h @@ -26,11 +26,11 @@ #pragma once -#include #include #include #include #include +#include #include #include @@ -58,8 +58,8 @@ public: virtual bool is_contiguous() const { return false; } size_t page_count() const { return m_physical_pages.size(); } - const FixedArray>& physical_pages() const { return m_physical_pages; } - FixedArray>& physical_pages() { return m_physical_pages; } + const Vector>& physical_pages() const { return m_physical_pages; } + Vector>& physical_pages() { return m_physical_pages; } size_t size() const { return m_physical_pages.size() * PAGE_SIZE; } @@ -76,7 +76,7 @@ protected: template void for_each_region(Callback); - FixedArray> m_physical_pages; + Vector> m_physical_pages; Lock m_paging_lock { "VMObject" }; private: