kernel: Use try_set_value(_or_error) in various places

This commit is contained in:
apio 2023-04-07 10:55:04 +02:00
parent baa2296aed
commit 1f0e185904
Signed by: apio
GPG Key ID: B8A7D06E42258954
6 changed files with 22 additions and 31 deletions

View File

@ -93,16 +93,14 @@ int main()
if (child == 0) if (child == 0)
{ {
auto maybe_argv = split_command_into_argv(command); Vector<char*> argv;
if (maybe_argv.has_error()) bool ok = split_command_into_argv(command).try_set_value_or_error(argv, errno);
if (!ok)
{ {
errno = maybe_argv.error();
perror("failed to parse command"); perror("failed to parse command");
return 1; return 1;
} }
auto argv = maybe_argv.release_value();
if (argv[0] == NULL) return 0; if (argv[0] == NULL) return 0;
sh_execvp(argv.data()); sh_execvp(argv.data());
perror(argv[0]); perror(argv[0]);

View File

@ -30,10 +30,9 @@ namespace KernelVM
{ {
auto kernelvm_bitmap = g_kernelvm_bitmap.lock(); auto kernelvm_bitmap = g_kernelvm_bitmap.lock();
const auto maybe_index = kernelvm_bitmap->find_and_toggle(false); usize index;
if (!maybe_index.has_value()) return err(ENOMEM); bool ok = kernelvm_bitmap->find_and_toggle(false).try_set_value(index);
if (!ok) return err(ENOMEM);
const usize index = maybe_index.value();
g_used_vm += ARCH_PAGE_SIZE; g_used_vm += ARCH_PAGE_SIZE;

View File

@ -139,10 +139,9 @@ namespace MemoryManager
{ {
auto frame_bitmap = g_frame_bitmap.lock(); auto frame_bitmap = g_frame_bitmap.lock();
const auto maybe_index = frame_bitmap->find_and_toggle(false, start_index); usize index;
if (!maybe_index.has_value()) return err(ENOMEM); bool ok = frame_bitmap->find_and_toggle(false, start_index).try_set_value(index);
if (!ok) return err(ENOMEM);
const usize index = maybe_index.value();
start_index = index + 1; start_index = index + 1;

View File

@ -65,15 +65,13 @@ Result<bool> UserVM::try_expand(usize size)
Result<u64> UserVM::alloc_one_page() Result<u64> UserVM::alloc_one_page()
{ {
u64 index; u64 index;
const auto maybe_index = m_bitmap.find_and_toggle(false); bool ok = m_bitmap.find_and_toggle(false).try_set_value(index);
if (!maybe_index.has_value()) if (!ok)
{ {
bool success = TRY(try_expand()); bool success = TRY(try_expand());
if (!success) return err(ENOMEM); if (!success) return err(ENOMEM);
index = TRY(Result<u64>::from_option(m_bitmap.find_and_toggle(false), ENOMEM)); index = TRY(Result<u64>::from_option(m_bitmap.find_and_toggle(false), ENOMEM));
} }
else
index = maybe_index.value();
return VM_BASE + index * ARCH_PAGE_SIZE; return VM_BASE + index * ARCH_PAGE_SIZE;
} }
@ -81,15 +79,13 @@ Result<u64> UserVM::alloc_one_page()
Result<u64> UserVM::alloc_several_pages(usize count) Result<u64> UserVM::alloc_several_pages(usize count)
{ {
u64 index; u64 index;
const auto maybe_index = m_bitmap.find_and_toggle_region(false, count); bool ok = m_bitmap.find_and_toggle_region(false, count).try_set_value(index);
if (!maybe_index.has_value()) if (!ok)
{ {
bool success = TRY(try_expand((count / 8) + INITIAL_VM_SIZE)); bool success = TRY(try_expand((count / 8) + INITIAL_VM_SIZE));
if (!success) return err(ENOMEM); if (!success) return err(ENOMEM);
index = TRY(Result<u64>::from_option(m_bitmap.find_and_toggle_region(false, count), ENOMEM)); index = TRY(Result<u64>::from_option(m_bitmap.find_and_toggle_region(false, count), ENOMEM));
} }
else
index = maybe_index.value();
return VM_BASE + index * ARCH_PAGE_SIZE; return VM_BASE + index * ARCH_PAGE_SIZE;
} }

View File

@ -19,13 +19,12 @@ Result<u64> sys_getdents(Registers*, SyscallArgs args)
usize nwrite = 0; usize nwrite = 0;
while (nwrite < count) while (nwrite < count)
{ {
auto maybe_entry = descriptor.inode->get(descriptor.offset); VFS::DirectoryEntry entry;
if (!maybe_entry.has_value()) break; bool ok = descriptor.inode->get(descriptor.offset).try_set_value(entry);
if (!ok) break;
descriptor.offset++; descriptor.offset++;
auto entry = maybe_entry.release_value();
luna_dirent kent; luna_dirent kent;
kent.inode = entry.inode->inode_number(); kent.inode = entry.inode->inode_number();
strlcpy(kent.name, entry.name.chars(), entry.name.length() + 1); strlcpy(kent.name, entry.name.chars(), entry.name.length() + 1);

View File

@ -24,27 +24,27 @@ Result<u64> sys_open(Registers*, SyscallArgs args)
// Caller did not pass either O_RDONLY, O_WRONLY or O_RDWR // Caller did not pass either O_RDONLY, O_WRONLY or O_RDWR
if ((flags & O_RDWR) == 0) { return err(EINVAL); } if ((flags & O_RDWR) == 0) { return err(EINVAL); }
auto maybe_inode = VFS::resolve_path(path.chars()); int error;
if (maybe_inode.has_error()) bool ok = VFS::resolve_path(path.chars()).try_set_value_or_error(inode, error);
if (!ok)
{ {
if (maybe_inode.error() == ENOENT && (flags & O_CREAT)) if (error == ENOENT && (flags & O_CREAT))
{ {
inode = TRY(VFS::create_file(path.chars())); inode = TRY(VFS::create_file(path.chars()));
inode->chmod(mode); inode->chmod(mode);
} }
else else
return maybe_inode.release_error(); return err(error);
} }
else if (flags & O_EXCL) else if (flags & O_EXCL)
return err(EEXIST); return err(EEXIST);
else else
{ {
inode = maybe_inode.release_value();
if ((flags & O_RDONLY) && (inode->mode() & S_IRUSR) == 0) return err(EACCES); if ((flags & O_RDONLY) && (inode->mode() & S_IRUSR) == 0) return err(EACCES);
if ((flags & O_WRONLY) && (inode->mode() & S_IWUSR) == 0) return err(EACCES); if ((flags & O_WRONLY) && (inode->mode() & S_IWUSR) == 0) return err(EACCES);
} }
if(inode->type() != VFS::InodeType::Directory && (flags & O_DIRECTORY)) return err(ENOTDIR); if (inode->type() != VFS::InodeType::Directory && (flags & O_DIRECTORY)) return err(ENOTDIR);
if ((flags & O_WRONLY) && (flags & O_TRUNC)) inode->truncate(0); if ((flags & O_WRONLY) && (flags & O_TRUNC)) inode->truncate(0);