#include #include #include #include #include #include #include namespace { constinit auto static global_open_file_table = std::optional{}; } // namespace namespace kernel::filesystem { auto open_file_table::init() -> void { if (global_open_file_table) { kapi::system::panic("[FILESYSTEM] Open file table has already been initialized."); } global_open_file_table.emplace(open_file_table{}); } auto open_file_table::get() -> open_file_table & { if (!global_open_file_table) { kapi::system::panic("[FILESYSTEM] Open file table has not been initialized."); } return *global_open_file_table; } auto open_file_table::add_file(kstd::shared_ptr const & file_descriptor) -> int { if (!file_descriptor) { return -1; } auto it = std::ranges::find_if(m_open_files, [](auto const & open_file) { return open_file == nullptr; }); if (it != m_open_files.end()) { *it = file_descriptor; return static_cast(it - m_open_files.begin()); } m_open_files.push_back(file_descriptor); return static_cast(m_open_files.size() - 1); } auto open_file_table::get_file(int fd) const -> kstd::shared_ptr { if (fd < 0) { return nullptr; } auto const index = static_cast(fd); if (index >= m_open_files.size()) { return nullptr; } return m_open_files.at(index); } auto open_file_table::remove_file(int fd) -> int { if (fd < 0) { return -1; } auto const index = static_cast(fd); if (index >= m_open_files.size()) { return -1; } m_open_files.at(index) = nullptr; return 0; } } // namespace kernel::filesystem namespace kernel::tests::filesystem::open_file_table { auto deinit() -> void { global_open_file_table.reset(); } } // namespace kernel::tests::filesystem::open_file_table