diff --git a/kernel/src/memory/UserVM.cpp b/kernel/src/memory/UserVM.cpp index 5fb02bfb..93dd04a0 100644 --- a/kernel/src/memory/UserVM.cpp +++ b/kernel/src/memory/UserVM.cpp @@ -97,7 +97,7 @@ Result UserVM::free_several_pages(u64 address, usize count) const u64 index = (address - VM_BASE) / ARCH_PAGE_SIZE; if ((index + count) > (MAX_VM_SIZE * 8)) return err(EINVAL); - if (!m_bitmap.match_region(index, count, true)) return err(EFAULT); + if (!TRY(m_bitmap.try_match_region(index, count, true))) return err(EFAULT); m_bitmap.clear_region(index, count, false); diff --git a/luna/include/luna/Bitmap.h b/luna/include/luna/Bitmap.h index 6c628927..e4990c04 100644 --- a/luna/include/luna/Bitmap.h +++ b/luna/include/luna/Bitmap.h @@ -50,6 +50,7 @@ class Bitmap Option find_and_toggle_region(bool value, usize count, usize begin = 0); bool match_region(usize start, usize bits, bool value); + Result try_match_region(usize start, usize bits, bool value); void clear(bool value); void clear_region(usize start, usize bits, bool value); @@ -60,6 +61,8 @@ class Bitmap return b ? 0xff : 0; } + bool match_region_impl(usize start, usize bits, bool value); + u8* m_location = nullptr; usize m_size_in_bytes = 0; }; diff --git a/luna/src/Bitmap.cpp b/luna/src/Bitmap.cpp index 989e3f26..2cdc8037 100644 --- a/luna/src/Bitmap.cpp +++ b/luna/src/Bitmap.cpp @@ -187,11 +187,8 @@ Option Bitmap::find_and_toggle_region(bool value, usize count, usize begi return index; } -bool Bitmap::match_region(usize start, usize bits, bool value) +bool Bitmap::match_region_impl(usize start, usize bits, bool value) { - expect(initialized(), "Bitmap was never initialized"); - expect((start + bits) <= size(), "Bitmap match out of range"); - if (!bits) return true; // Match individual bits while not on a byte boundary. @@ -223,3 +220,19 @@ bool Bitmap::match_region(usize start, usize bits, bool value) return true; } + +bool Bitmap::match_region(usize start, usize bits, bool value) +{ + expect(initialized(), "Bitmap was never initialized"); + expect((start + bits) <= size(), "Bitmap match out of range"); + + return match_region_impl(start, bits, value); +} + +Result Bitmap::try_match_region(usize start, usize bits, bool value) +{ + expect(initialized(), "Bitmap was never initialized"); + if ((start + bits) > size()) return err(EINVAL); + + return match_region_impl(start, bits, value); +}