Bitmap: Introduce new malloc-aware initialization functions
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Lets us call resize(new_size) instead of initialize(realloc(location, new_size), new_size)
This commit is contained in:
parent
0e607c2fef
commit
944d32de36
@ -33,18 +33,15 @@ Result<bool> 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<void> 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();
|
||||
}
|
||||
|
@ -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<void> allocate(usize size_in_bytes);
|
||||
Result<void*> resize(usize new_size_in_bytes);
|
||||
Result<void> deallocate();
|
||||
|
||||
void set(usize index, bool value);
|
||||
bool get(usize index) const;
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include <luna/Bitmap.h>
|
||||
#include <luna/CString.h>
|
||||
#include <luna/Check.h>
|
||||
#include <luna/Heap.h>
|
||||
|
||||
Bitmap::Bitmap()
|
||||
{
|
||||
@ -16,6 +17,24 @@ void Bitmap::initialize(void* location, usize size_in_bytes)
|
||||
m_size_in_bytes = size_in_bytes;
|
||||
}
|
||||
|
||||
Result<void> Bitmap::allocate(usize size_in_bytes)
|
||||
{
|
||||
initialize(TRY(malloc_impl(size_in_bytes)), size_in_bytes);
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void*> 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<void> 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");
|
||||
|
Loading…
Reference in New Issue
Block a user