diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-08-16 06:32:48 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-16 21:16:14 +0200 |
commit | e9feced041e1855327a01c2905a7353031ceaa91 (patch) | |
tree | a9c07f54db4c730da5aafa8b953042157782c94b | |
parent | c1ffc1713455f8e340bccaa89713a2c14cf6ce95 (diff) | |
download | serenity-e9feced041e1855327a01c2905a7353031ceaa91.zip |
Kernel: Add a Process::FileDescriptions::get_if_valid(index) API
Note that this is not the same as ::at(index), which has a different
precondition ("allocated" vs "valid").
-rw-r--r-- | Kernel/Process.cpp | 23 | ||||
-rw-r--r-- | Kernel/Process.h | 3 |
2 files changed, 26 insertions, 0 deletions
diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index a3ad914a69..f197cb4f92 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -428,6 +428,29 @@ RefPtr<Process> Process::from_pid(ProcessID pid) }); } +const Process::FileDescriptionAndFlags* Process::FileDescriptions::get_if_valid(size_t i) const +{ + ScopedSpinLock lock(m_fds_lock); + if (m_fds_metadatas.size() <= i) + return nullptr; + + if (auto& metadata = m_fds_metadatas[i]; metadata.is_valid()) + return &metadata; + + return nullptr; +} +Process::FileDescriptionAndFlags* Process::FileDescriptions::get_if_valid(size_t i) +{ + ScopedSpinLock lock(m_fds_lock); + if (m_fds_metadatas.size() <= i) + return nullptr; + + if (auto& metadata = m_fds_metadatas[i]; metadata.is_valid()) + return &metadata; + + return nullptr; +} + const Process::FileDescriptionAndFlags& Process::FileDescriptions::at(size_t i) const { ScopedSpinLock lock(m_fds_lock); diff --git a/Kernel/Process.h b/Kernel/Process.h index a5d1bc9c1c..6c0d57b58c 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -643,6 +643,9 @@ public: const FileDescriptionAndFlags& at(size_t i) const; FileDescriptionAndFlags& at(size_t i); + FileDescriptionAndFlags const* get_if_valid(size_t i) const; + FileDescriptionAndFlags* get_if_valid(size_t i); + void enumerate(Function<void(const FileDescriptionAndFlags&)>) const; void change_each(Function<void(FileDescriptionAndFlags&)>); |