From 944d32de36d1116a3cdebd1fe9e68ca234ad918c Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 21 Jan 2023 23:16:50 +0100 Subject: [PATCH] Bitmap: Introduce new malloc-aware initialization functions Lets us call resize(new_size) instead of initialize(realloc(location, new_size), new_size) --- kernel/src/memory/UserVM.cpp | 13 +++++-------- luna/include/luna/Bitmap.h | 6 ++++++ luna/src/Bitmap.cpp | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/kernel/src/memory/UserVM.cpp b/kernel/src/memory/UserVM.cpp index c6f39f55..5fb02bfb 100644 --- a/kernel/src/memory/UserVM.cpp +++ b/kernel/src/memory/UserVM.cpp @@ -33,18 +33,15 @@ Result UserVM::try_expand(usize size) { if (m_bitmap.size_in_bytes() == MAX_VM_SIZE) { return false; } - usize new_size = m_bitmap.size_in_bytes() + size; + const usize old_size = m_bitmap.size_in_bytes(); + usize new_size = old_size + size; if (new_size > MAX_VM_SIZE) new_size = MAX_VM_SIZE; - usize old_size = m_bitmap.size_in_bytes(); - - void* const base = TRY(krealloc(m_bitmap.location(), new_size)); - - m_bitmap.initialize(base, new_size); + m_bitmap.resize(new_size); m_bitmap.clear_region(old_size * 8, (new_size - old_size) * 8, false); - kdbgln("user vm expanded to base=%p, size=%zu", base, new_size); + kdbgln("user vm expanded to base=%p, size=%zu", m_bitmap.location(), new_size); return true; } @@ -110,5 +107,5 @@ Result UserVM::free_several_pages(u64 address, usize count) UserVM::~UserVM() { kdbgln("user vm destroyed: base=%p, size=%zu", m_bitmap.location(), m_bitmap.size_in_bytes()); - kfree(m_bitmap.location()); + m_bitmap.deallocate(); } diff --git a/luna/include/luna/Bitmap.h b/luna/include/luna/Bitmap.h index 1e35d655..6c628927 100644 --- a/luna/include/luna/Bitmap.h +++ b/luna/include/luna/Bitmap.h @@ -8,9 +8,15 @@ class Bitmap Bitmap(); Bitmap(void* location, usize size_in_bytes); + // Naive initialization functions. void initialize(void* location, usize size_in_bytes); void* move(void* new_location, usize new_location_size_in_bytes); + // Dynamic memory initialization functions. + Result allocate(usize size_in_bytes); + Result resize(usize new_size_in_bytes); + Result deallocate(); + void set(usize index, bool value); bool get(usize index) const; diff --git a/luna/src/Bitmap.cpp b/luna/src/Bitmap.cpp index 5326c891..989e3f26 100644 --- a/luna/src/Bitmap.cpp +++ b/luna/src/Bitmap.cpp @@ -1,6 +1,7 @@ #include #include #include +#include Bitmap::Bitmap() { @@ -16,6 +17,24 @@ void Bitmap::initialize(void* location, usize size_in_bytes) m_size_in_bytes = size_in_bytes; } +Result Bitmap::allocate(usize size_in_bytes) +{ + initialize(TRY(malloc_impl(size_in_bytes)), size_in_bytes); + return {}; +} + +Result Bitmap::resize(usize new_size_in_bytes) +{ + m_location = (u8*)TRY(realloc_impl(m_location, new_size_in_bytes)); + m_size_in_bytes = new_size_in_bytes; + return (void*)m_location; +} + +Result Bitmap::deallocate() +{ + return free_impl(m_location); +} + void* Bitmap::move(void* new_location, usize new_location_size_in_bytes) { expect(initialized(), "Bitmap was never initialized");