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)
{
auto maybe_argv = split_command_into_argv(command);
if (maybe_argv.has_error())
Vector<char*> argv;
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");
return 1;
}
auto argv = maybe_argv.release_value();
if (argv[0] == NULL) return 0;
sh_execvp(argv.data());
perror(argv[0]);

View File

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

View File

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

View File

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

View File

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

View File

@ -24,22 +24,22 @@ Result<u64> sys_open(Registers*, SyscallArgs args)
// Caller did not pass either O_RDONLY, O_WRONLY or O_RDWR
if ((flags & O_RDWR) == 0) { return err(EINVAL); }
auto maybe_inode = VFS::resolve_path(path.chars());
if (maybe_inode.has_error())
int 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->chmod(mode);
}
else
return maybe_inode.release_error();
return err(error);
}
else if (flags & O_EXCL)
return err(EEXIST);
else
{
inode = maybe_inode.release_value();
if ((flags & O_RDONLY) && (inode->mode() & S_IRUSR) == 0) return err(EACCES);
if ((flags & O_WRONLY) && (inode->mode() & S_IWUSR) == 0) return err(EACCES);
}