Compare commits

...

2 Commits

Author SHA1 Message Date
944d32de36
Bitmap: Introduce new malloc-aware initialization functions
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)
2023-01-21 23:16:50 +01:00
0e607c2fef
Bitmap: Declare more variables as const
I miss Rust's immutability-by-default...
2023-01-21 23:04:21 +01:00
3 changed files with 38 additions and 16 deletions

View File

@ -33,18 +33,15 @@ Result<bool> UserVM::try_expand(usize size)
{ {
if (m_bitmap.size_in_bytes() == MAX_VM_SIZE) { return false; } 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; if (new_size > MAX_VM_SIZE) new_size = MAX_VM_SIZE;
usize old_size = m_bitmap.size_in_bytes(); m_bitmap.resize(new_size);
void* const base = TRY(krealloc(m_bitmap.location(), new_size));
m_bitmap.initialize(base, new_size);
m_bitmap.clear_region(old_size * 8, (new_size - old_size) * 8, false); 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; return true;
} }
@ -110,5 +107,5 @@ Result<void> UserVM::free_several_pages(u64 address, usize count)
UserVM::~UserVM() UserVM::~UserVM()
{ {
kdbgln("user vm destroyed: base=%p, size=%zu", m_bitmap.location(), m_bitmap.size_in_bytes()); kdbgln("user vm destroyed: base=%p, size=%zu", m_bitmap.location(), m_bitmap.size_in_bytes());
kfree(m_bitmap.location()); m_bitmap.deallocate();
} }

View File

@ -8,9 +8,15 @@ class Bitmap
Bitmap(); Bitmap();
Bitmap(void* location, usize size_in_bytes); Bitmap(void* location, usize size_in_bytes);
// Naive initialization functions.
void initialize(void* location, usize size_in_bytes); void initialize(void* location, usize size_in_bytes);
void* move(void* new_location, usize new_location_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); void set(usize index, bool value);
bool get(usize index) const; bool get(usize index) const;

View File

@ -1,6 +1,7 @@
#include <luna/Bitmap.h> #include <luna/Bitmap.h>
#include <luna/CString.h> #include <luna/CString.h>
#include <luna/Check.h> #include <luna/Check.h>
#include <luna/Heap.h>
Bitmap::Bitmap() Bitmap::Bitmap()
{ {
@ -16,6 +17,24 @@ void Bitmap::initialize(void* location, usize size_in_bytes)
m_size_in_bytes = 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) void* Bitmap::move(void* new_location, usize new_location_size_in_bytes)
{ {
expect(initialized(), "Bitmap was never initialized"); expect(initialized(), "Bitmap was never initialized");
@ -24,7 +43,7 @@ void* Bitmap::move(void* new_location, usize new_location_size_in_bytes)
else else
memcpy(new_location, m_location, new_location_size_in_bytes); memcpy(new_location, m_location, new_location_size_in_bytes);
void* old_location = (void*)m_location; void* const old_location = (void*)m_location;
m_location = (u8*)new_location; m_location = (u8*)new_location;
m_size_in_bytes = new_location_size_in_bytes; m_size_in_bytes = new_location_size_in_bytes;
@ -36,8 +55,8 @@ void Bitmap::set(usize index, bool value)
{ {
expect(initialized(), "Bitmap was never initialized"); expect(initialized(), "Bitmap was never initialized");
expect(index < size(), "Bitmap access out of range"); expect(index < size(), "Bitmap access out of range");
u64 byte_index = index / 8; const u64 byte_index = index / 8;
u8 bit_mask = (u8)(0b10000000 >> (index % 8)); const u8 bit_mask = (u8)(0b10000000 >> (index % 8));
m_location[byte_index] &= (u8)(~bit_mask); m_location[byte_index] &= (u8)(~bit_mask);
if (value) { m_location[byte_index] |= bit_mask; } if (value) { m_location[byte_index] |= bit_mask; }
} }
@ -46,8 +65,8 @@ bool Bitmap::get(usize index) const
{ {
expect(initialized(), "Bitmap was never initialized"); expect(initialized(), "Bitmap was never initialized");
expect(index < size(), "Bitmap access out of range"); expect(index < size(), "Bitmap access out of range");
usize byte_index = index / 8; const usize byte_index = index / 8;
u8 bit_mask = (u8)(0b10000000 >> (index % 8)); const u8 bit_mask = (u8)(0b10000000 >> (index % 8));
return (m_location[byte_index] & bit_mask) > 0; return (m_location[byte_index] & bit_mask) > 0;
} }
@ -73,7 +92,7 @@ void Bitmap::clear_region(usize start, usize bits, bool value)
} }
// Clear out the rest in bytes. // Clear out the rest in bytes.
usize bytes = bits / 8; const usize bytes = bits / 8;
memset(&m_location[start / 8], value_byte(value), bytes); memset(&m_location[start / 8], value_byte(value), bytes);
start += bytes * 8; start += bytes * 8;
@ -163,7 +182,7 @@ Option<usize> Bitmap::find_region(bool value, usize count, usize begin) const
Option<usize> Bitmap::find_and_toggle_region(bool value, usize count, usize begin) Option<usize> Bitmap::find_and_toggle_region(bool value, usize count, usize begin)
{ {
usize index = TRY(find_region(value, count, begin)); const usize index = TRY(find_region(value, count, begin));
clear_region(index, count, !value); clear_region(index, count, !value);
return index; return index;
} }
@ -184,7 +203,7 @@ bool Bitmap::match_region(usize start, usize bits, bool value)
} }
// Match the rest in bytes. // Match the rest in bytes.
usize bytes = bits / 8; const usize bytes = bits / 8;
const u8 byte_that_contains_only_value = value_byte(value); const u8 byte_that_contains_only_value = value_byte(value);
for (usize i = start; i < start + bytes; i += 8) for (usize i = start; i < start + bytes; i += 8)