| 59 | } |
| 60 | |
| 61 | auto FileDescriptorTable::Alloc(vfs::File* file) -> Expected<int> { |
| 62 | if (file == nullptr) { |
| 63 | return std::unexpected(Error(ErrorCode::kInvalidArgument)); |
| 64 | } |
| 65 | |
| 66 | LockGuard guard(lock_); |
| 67 | |
| 68 | // 从 3 开始查找(0/1/2 预留给标准流) |
| 69 | for (int fd = 3; fd < kMaxFd; ++fd) { |
| 70 | if (table_[fd] == nullptr) { |
| 71 | table_[fd] = file; |
| 72 | ++open_count_; |
| 73 | return fd; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return std::unexpected(Error(ErrorCode::kFsFdTableFull)); |
| 78 | } |
| 79 | |
| 80 | auto FileDescriptorTable::Get(int fd) -> vfs::File* { |
| 81 | if (fd < 0 || fd >= kMaxFd) { |