From 0e607c2fefa776256444a591b94182b01aa27df7 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 21 Jan 2023 23:04:21 +0100 Subject: [PATCH] Bitmap: Declare more variables as const I miss Rust's immutability-by-default... --- luna/src/Bitmap.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/luna/src/Bitmap.cpp b/luna/src/Bitmap.cpp index ad400024..5326c891 100644 --- a/luna/src/Bitmap.cpp +++ b/luna/src/Bitmap.cpp @@ -24,7 +24,7 @@ void* Bitmap::move(void* new_location, usize new_location_size_in_bytes) else 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_size_in_bytes = new_location_size_in_bytes; @@ -36,8 +36,8 @@ void Bitmap::set(usize index, bool value) { expect(initialized(), "Bitmap was never initialized"); expect(index < size(), "Bitmap access out of range"); - u64 byte_index = index / 8; - u8 bit_mask = (u8)(0b10000000 >> (index % 8)); + const u64 byte_index = index / 8; + const u8 bit_mask = (u8)(0b10000000 >> (index % 8)); m_location[byte_index] &= (u8)(~bit_mask); if (value) { m_location[byte_index] |= bit_mask; } } @@ -46,8 +46,8 @@ bool Bitmap::get(usize index) const { expect(initialized(), "Bitmap was never initialized"); expect(index < size(), "Bitmap access out of range"); - usize byte_index = index / 8; - u8 bit_mask = (u8)(0b10000000 >> (index % 8)); + const usize byte_index = index / 8; + const u8 bit_mask = (u8)(0b10000000 >> (index % 8)); return (m_location[byte_index] & bit_mask) > 0; } @@ -73,7 +73,7 @@ void Bitmap::clear_region(usize start, usize bits, bool value) } // Clear out the rest in bytes. - usize bytes = bits / 8; + const usize bytes = bits / 8; memset(&m_location[start / 8], value_byte(value), bytes); start += bytes * 8; @@ -163,7 +163,7 @@ Option Bitmap::find_region(bool value, usize count, usize begin) const Option 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); return index; } @@ -184,7 +184,7 @@ bool Bitmap::match_region(usize start, usize bits, bool value) } // Match the rest in bytes. - usize bytes = bits / 8; + const usize bytes = bits / 8; const u8 byte_that_contains_only_value = value_byte(value); for (usize i = start; i < start + bytes; i += 8)