SimpleKernel 1.17.0
Loading...
Searching...
No Matches
file_descriptor.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <array>
8#include <cstddef>
9#include <cstdint>
10
11#include "expected.hpp"
12#include "spinlock.hpp"
13#include "vfs.hpp"
14
15namespace filesystem {
16
24 public:
26 static constexpr int kMaxFd = 64;
27
29 static constexpr int kStdinFd = 0;
30 static constexpr int kStdoutFd = 1;
31 static constexpr int kStderrFd = 2;
32
42
49 [[nodiscard]] auto Alloc(vfs::File* file) -> Expected<int>;
50
57 [[nodiscard]] auto Get(int fd) -> vfs::File*;
58
65 [[nodiscard]] auto Free(int fd) -> Expected<void>;
73 [[nodiscard]] auto Dup(int old_fd, int new_fd = -1) -> Expected<int>;
79 [[nodiscard]] auto CloseAll() -> Expected<void>;
80
88 [[nodiscard]] auto SetupStandardFiles(vfs::File* stdin_file,
89 vfs::File* stdout_file,
90 vfs::File* stderr_file)
96 [[nodiscard]] auto GetOpenCount() const -> int;
97
98 private:
99 std::array<vfs::File*, kMaxFd> table_;
101 SpinLock lock_{"fd_table"};
102};
103
104} // namespace filesystem
自旋锁
Definition spinlock.hpp:27
进程级文件描述符表
auto SetupStandardFiles(vfs::File *stdin_file, vfs::File *stdout_file, vfs::File *stderr_file) -> Expected< void >
设置标准文件描述符
auto CloseAll() -> Expected< void >
关闭所有文件描述符
auto GetOpenCount() const -> int
获取已打开文件描述符数量
auto Alloc(vfs::File *file) -> Expected< int >
分配一个最小可用 fd 并关联 File
auto Get(int fd) -> vfs::File *
获取 fd 对应的 File 对象
auto operator=(const FileDescriptorTable &) -> FileDescriptorTable &=delete
auto Dup(int old_fd, int new_fd=-1) -> Expected< int >
复制文件描述符(用于 dup/dup2)
auto Free(int fd) -> Expected< void >
释放 fd
static constexpr int kStdinFd
标准文件描述符
FileDescriptorTable(const FileDescriptorTable &)=delete
static constexpr int kMaxFd
最大文件描述符数
std::array< vfs::File *, kMaxFd > table_
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
File — 打开的文件实例(每次 open 产生一个)
Definition vfs.hpp:65