Bitmap: Add a 'find_and_toggle' method
All checks were successful
continuous-integration/drone/push Build is passing

Just like find(), but toggles the value when finding it.
Avoids doing this manually in MemoryManager and KernelVM.
This commit is contained in:
apio 2023-01-09 18:08:50 +01:00
parent 4287ec6cb0
commit c83f6c03b5
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 15 additions and 5 deletions

View File

@ -30,12 +30,11 @@ namespace KernelVM
{
auto kernelvm_bitmap = g_kernelvm_bitmap.lock();
const auto maybe_index = kernelvm_bitmap->find(false);
const auto maybe_index = kernelvm_bitmap->find_and_toggle(false);
if (!maybe_index.has_value()) return err(ENOMEM);
const usize index = maybe_index.value();
kernelvm_bitmap->set(index, true);
g_used_vm += ARCH_PAGE_SIZE;
return KERNEL_VM_RANGE_START + (index * ARCH_PAGE_SIZE);

View File

@ -146,13 +146,15 @@ namespace MemoryManager
{
auto frame_bitmap = g_frame_bitmap.lock();
const auto maybe_index = frame_bitmap->find(false, start_index);
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();
start_index = index + 1;
do_lock_frame(index, *frame_bitmap);
used_mem += ARCH_PAGE_SIZE;
free_mem -= ARCH_PAGE_SIZE;
return index * ARCH_PAGE_SIZE;
}

View File

@ -1,5 +1,5 @@
#pragma once
#include <luna/Option.h>
#include <luna/Result.h>
#include <luna/Types.h>
class Bitmap
@ -37,6 +37,8 @@ class Bitmap
Option<usize> find(bool value, usize begin = 0) const;
Option<usize> find_and_toggle(bool value, usize begin = 0);
void clear(bool value);
void clear_region(usize start, usize bits, bool value);

View File

@ -125,3 +125,10 @@ Option<usize> Bitmap::find(bool value, usize begin) const
return {};
}
Option<usize> Bitmap::find_and_toggle(bool value, usize begin)
{
usize index = TRY(find(value, begin));
set(index, !value);
return index;
}