diff --git a/kernel/src/arch/x86_64/MMU.cpp b/kernel/src/arch/x86_64/MMU.cpp index f6081f2a..f2bf8cf3 100644 --- a/kernel/src/arch/x86_64/MMU.cpp +++ b/kernel/src/arch/x86_64/MMU.cpp @@ -191,7 +191,7 @@ namespace MMU auto& l4 = l4_entry(virt); if (!l4.present) { - u64 addr = TRY(MemoryManager::alloc_frame()); + const u64 addr = TRY(MemoryManager::alloc_frame()); l4.present = true; l4.set_address(addr); memset(l3_table(virt), 0, ARCH_PAGE_SIZE); @@ -202,7 +202,7 @@ namespace MMU auto& l3 = l3_entry(virt); if (!l3.present) { - u64 addr = TRY(MemoryManager::alloc_frame()); + const u64 addr = TRY(MemoryManager::alloc_frame()); l3.present = true; l3.set_address(addr); memset(l2_table(virt), 0, ARCH_PAGE_SIZE); @@ -215,7 +215,7 @@ namespace MMU auto& l2 = l2_entry(virt); if (!l2.present) { - u64 addr = TRY(MemoryManager::alloc_frame()); + const u64 addr = TRY(MemoryManager::alloc_frame()); l2.present = true; l2.set_address(addr); memset(l1_table(virt), 0, ARCH_PAGE_SIZE); @@ -288,10 +288,10 @@ namespace MMU Result create_page_directory_for_userspace() { - u64 directory_virt = TRY(MemoryManager::alloc_for_kernel(1, MMU::ReadWrite | MMU::NoExecute)); - u64 directory_phys = MMU::get_physical(directory_virt).value(); + const u64 directory_virt = TRY(MemoryManager::alloc_for_kernel(1, MMU::ReadWrite | MMU::NoExecute)); + const u64 directory_phys = MMU::get_physical(directory_virt).value(); - PageDirectory* directory = (PageDirectory*)directory_virt; + PageDirectory* const directory = (PageDirectory*)directory_virt; memset(directory, 0, ARCH_PAGE_SIZE); PageTableEntry& recursive_entry = directory->entries[rindex]; recursive_entry.read_write = true; @@ -320,7 +320,7 @@ namespace MMU MemoryManager::free_frame((u64)directory); }); - PageDirectory* table = l4_table(); + PageDirectory* const table = l4_table(); // Let's iterate over every top-level entry, skipping the last two entries (recursive mapping and kernel pages) for (u64 i = 0; i < 510; i++) @@ -328,7 +328,7 @@ namespace MMU PageTableEntry& l4 = table->entries[i]; if (!l4.present) continue; - PageDirectory* pdp = raw_l3_table(i); + PageDirectory* const pdp = raw_l3_table(i); for (u64 j = 0; j < 512; j++) { @@ -340,7 +340,7 @@ namespace MMU TRY(MemoryManager::free_frame(l3.get_address())); } - PageDirectory* pd = raw_l2_table(i, j); + PageDirectory* const pd = raw_l2_table(i, j); for (u64 k = 0; k < 512; k++) { @@ -352,7 +352,7 @@ namespace MMU TRY(MemoryManager::free_frame(l2.get_address())); } - PageDirectory* pt = raw_l1_table(i, j, k); + PageDirectory* const pt = raw_l1_table(i, j, k); for (u64 l = 0; l < 512; l++) { diff --git a/kernel/src/memory/MemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp index 35fdcc4c..28e155bd 100644 --- a/kernel/src/memory/MemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -224,7 +224,7 @@ namespace MemoryManager while (pages_mapped < count) { - u64 frame = TRY(alloc_frame()); + const u64 frame = TRY(alloc_frame()); TRY(MMU::map(virt, frame, flags)); virt += ARCH_PAGE_SIZE; pages_mapped++; @@ -237,7 +237,7 @@ namespace MemoryManager Result alloc_for_kernel(usize count, int flags) { - u64 start = TRY(KernelVM::alloc_several_pages(count)); + const u64 start = TRY(KernelVM::alloc_several_pages(count)); usize pages_mapped = 0; auto guard = make_scope_guard([=, &pages_mapped] { @@ -249,7 +249,7 @@ namespace MemoryManager while (pages_mapped < count) { - u64 frame = TRY(alloc_frame()); + const u64 frame = TRY(alloc_frame()); TRY(MMU::map(virt, frame, flags)); virt += ARCH_PAGE_SIZE; pages_mapped++; @@ -262,7 +262,7 @@ namespace MemoryManager Result get_kernel_mapping_for_frames(u64 phys, usize count, int flags) { - u64 start = TRY(KernelVM::alloc_several_pages(count)); + const u64 start = TRY(KernelVM::alloc_several_pages(count)); usize pages_mapped = 0; @@ -292,7 +292,7 @@ namespace MemoryManager while (count--) { - u64 frame = TRY(MMU::unmap(virt)); + const u64 frame = TRY(MMU::unmap(virt)); TRY(free_frame(frame)); virt += ARCH_PAGE_SIZE; } diff --git a/kernel/src/memory/MemoryMap.cpp b/kernel/src/memory/MemoryMap.cpp index 3e67fd76..c32c7751 100644 --- a/kernel/src/memory/MemoryMap.cpp +++ b/kernel/src/memory/MemoryMap.cpp @@ -43,7 +43,7 @@ Option MemoryMapIterator::at(usize index) const Option MemoryMapIterator::next() { - auto entry = TRY(at(m_cur_ent++)); + const auto entry = TRY(at(m_cur_ent++)); #ifdef ARCH_X86_64 // Workaround for https://gitlab.com/qemu-project/qemu/-/commit/8504f129450b909c88e199ca44facd35d38ba4de diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index 851e7b47..f6730d2c 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -53,11 +53,11 @@ namespace Scheduler // If anything fails, make sure to clean up. auto guard = make_scope_guard([&] { delete thread; }); - u64 thread_stack_vm = TRY(MemoryManager::alloc_for_kernel(4, MMU::NoExecute | MMU::ReadWrite)); + const u64 thread_stack_vm = TRY(MemoryManager::alloc_for_kernel(4, MMU::NoExecute | MMU::ReadWrite)); guard.deactivate(); - Stack thread_stack { thread_stack_vm, ARCH_PAGE_SIZE * 4 }; + const Stack thread_stack { thread_stack_vm, ARCH_PAGE_SIZE * 4 }; thread->set_sp(thread_stack.top()); thread->stack = thread_stack; @@ -73,7 +73,7 @@ namespace Scheduler Result new_kernel_thread(u64 address) { - Thread* thread = TRY(new_thread()); + Thread* const thread = TRY(new_thread()); thread->init_regs_kernel(); thread->set_ip(address); @@ -82,7 +82,7 @@ namespace Scheduler Result new_kernel_thread(void (*func)(void)) { - Thread* thread = TRY(new_thread()); + Thread* const thread = TRY(new_thread()); thread->init_regs_kernel(); thread->set_ip((u64)func); @@ -91,7 +91,7 @@ namespace Scheduler Result new_kernel_thread(void (*func)(void*), void* arg) { - Thread* thread = TRY(new_thread()); + Thread* const thread = TRY(new_thread()); thread->init_regs_kernel(); thread->set_ip((u64)func); thread->set_arguments((u64)arg, 0, 0, 0); @@ -107,7 +107,7 @@ namespace Scheduler auto guard = make_scope_guard([&] { MemoryManager::unmap_owned(THREAD_STACK_BASE, 4); }); - u64 kernel_stack_base = TRY(MemoryManager::alloc_for_kernel(4, MMU::ReadWrite | MMU::NoExecute)); + const u64 kernel_stack_base = TRY(MemoryManager::alloc_for_kernel(4, MMU::ReadWrite | MMU::NoExecute)); guard.deactivate(); @@ -119,13 +119,13 @@ namespace Scheduler Result new_userspace_thread(const TarStream::Entry& entry, const TarStream& stream) { - Thread* thread = TRY(new_thread()); + Thread* const thread = TRY(new_thread()); thread->is_kernel = false; auto guard = make_scope_guard([&] { delete thread; }); - auto directory = TRY(MMU::create_page_directory_for_userspace()); + PageDirectory* const directory = TRY(MMU::create_page_directory_for_userspace()); auto directory_guard = make_scope_guard([&] { MMU::switch_page_directory(MMU::kernel_page_directory()); @@ -136,7 +136,7 @@ namespace Scheduler thread->init_regs_user(); - auto data = TRY(ELFLoader::load(entry, stream)); + const ELFData data = TRY(ELFLoader::load(entry, stream)); thread->set_ip(data.entry); diff --git a/kernel/src/thread/Thread.cpp b/kernel/src/thread/Thread.cpp index fea866e3..ae20d89e 100644 --- a/kernel/src/thread/Thread.cpp +++ b/kernel/src/thread/Thread.cpp @@ -13,7 +13,7 @@ void Thread::init() Result new_thread() { - Thread* thread = TRY(make()); + Thread* const thread = TRY(make()); thread->id = g_next_id++; diff --git a/kernel/src/video/TextConsole.cpp b/kernel/src/video/TextConsole.cpp index 86c7e4ec..09436fbf 100644 --- a/kernel/src/video/TextConsole.cpp +++ b/kernel/src/video/TextConsole.cpp @@ -125,7 +125,7 @@ namespace TextConsole { auto guard = make_scope_guard([] { utf8_decoder.reset(); }); - auto maybe_wchar = TRY(utf8_decoder.feed(c)); + const Option maybe_wchar = TRY(utf8_decoder.feed(c)); guard.deactivate(); @@ -194,7 +194,7 @@ namespace TextConsole { va_list ap; va_start(ap, format); - auto rc = TRY(cstyle_format( + const usize rc = TRY(cstyle_format( format, [](char c, void*) -> Result { return putchar(c); }, nullptr, ap)); va_end(ap); return rc; diff --git a/luna/include/luna/SharedPtr.h b/luna/include/luna/SharedPtr.h index 304b906e..3e1a9f2e 100644 --- a/luna/include/luna/SharedPtr.h +++ b/luna/include/luna/SharedPtr.h @@ -96,10 +96,10 @@ template Result> make_shared(Args... ar { using RefCount = __detail::RefCount; - RefCount* ref_count = TRY(make()); + RefCount* const ref_count = TRY(make()); auto guard = make_scope_guard([&] { delete ref_count; }); - T* ptr = TRY(make(args...)); + T* const ptr = TRY(make(args...)); guard.deactivate(); return SharedPtr { ptr, ref_count }; @@ -109,7 +109,7 @@ template Result> adopt_shared(T* ptr) { using RefCount = __detail::RefCount; - RefCount* ref_count = TRY(make()); + RefCount* const ref_count = TRY(make()); return SharedPtr { ptr, ref_count }; } @@ -126,9 +126,11 @@ template Result> adopt_shared_from_owned(OwnedPtr&& T* ptr = other.m_ptr; other.m_ptr = nullptr; + // FIXME: Should the pointee magically vanish on failure? Or go back into the OwnedPtr, even though it's been + // moved... auto guard = make_scope_guard([&] { delete ptr; }); - SharedPtr shared_ptr = TRY(adopt_shared(ptr)); + const SharedPtr shared_ptr = TRY(adopt_shared(ptr)); guard.deactivate(); diff --git a/luna/include/luna/Stack.h b/luna/include/luna/Stack.h index fbd3f64c..30d2ad79 100644 --- a/luna/include/luna/Stack.h +++ b/luna/include/luna/Stack.h @@ -6,14 +6,14 @@ struct Stack Stack() = default; Stack(u64 base, usize bytes); - u64 bottom() + u64 bottom() const { return m_base; } - u64 top(); + u64 top() const; - usize bytes() + usize bytes() const { return m_bytes; } diff --git a/luna/src/Bitmap.cpp b/luna/src/Bitmap.cpp index 9f4dae55..d5ba4aa8 100644 --- a/luna/src/Bitmap.cpp +++ b/luna/src/Bitmap.cpp @@ -91,7 +91,7 @@ Option Bitmap::find(bool value, usize begin) const { expect(initialized(), "Bitmap was never initialized"); - usize size = this->size(); + const usize size = this->size(); expect(begin < size, "Start index out of range"); @@ -104,7 +104,7 @@ Option Bitmap::find(bool value, usize begin) const if (begin == size) return {}; usize i = begin / 8; - u8 byte_that_does_not_contain_value = value_byte(!value); + const u8 byte_that_does_not_contain_value = value_byte(!value); while (i < m_size_in_bytes) { if (m_location[i] == byte_that_does_not_contain_value) @@ -128,7 +128,7 @@ Option Bitmap::find(bool value, usize begin) const Option Bitmap::find_and_toggle(bool value, usize begin) { - usize index = TRY(find(value, begin)); + const usize index = TRY(find(value, begin)); set(index, !value); return index; } diff --git a/luna/src/Stack.cpp b/luna/src/Stack.cpp index f323470b..a4fb02ef 100644 --- a/luna/src/Stack.cpp +++ b/luna/src/Stack.cpp @@ -4,7 +4,7 @@ Stack::Stack(u64 base, usize bytes) : m_base(base), m_bytes(bytes) { } -u64 Stack::top() +u64 Stack::top() const { return (m_base + m_bytes) - sizeof(void*); } diff --git a/luna/src/TarStream.cpp b/luna/src/TarStream.cpp index c4e4cced..fe36f423 100644 --- a/luna/src/TarStream.cpp +++ b/luna/src/TarStream.cpp @@ -99,7 +99,7 @@ usize TarStream::read_contents(const Entry& entry, void* buf, usize offset, usiz Result TarStream::read_contents_as_string(const Entry& entry, usize offset, usize max) const { - char* buf = TRY(make_array(max + 1)); + char* const buf = TRY(make_array(max + 1)); usize nread = read_contents(entry, buf, offset, max); diff --git a/luna/src/Units.cpp b/luna/src/Units.cpp index 1476e637..5ab2d572 100644 --- a/luna/src/Units.cpp +++ b/luna/src/Units.cpp @@ -20,7 +20,7 @@ usize to_dynamic_unit_cstr(usize value, char* buffer, usize max) Result to_dynamic_unit(usize value) { - char* buf = TRY(make_array(64)); + char* const buf = TRY(make_array(64)); to_dynamic_unit_cstr(value, buf, 64); diff --git a/luna/src/Utf8.cpp b/luna/src/Utf8.cpp index 5ebb880d..f065184a 100644 --- a/luna/src/Utf8.cpp +++ b/luna/src/Utf8.cpp @@ -32,7 +32,7 @@ static inline usize wide_char_length_as_utf8_unchecked(wchar_t c) static Result encode_wide_char_as_utf8(wchar_t c, char* result, usize& len) { - usize utf8_len = TRY(wide_char_length_as_utf8(c)); + const usize utf8_len = TRY(wide_char_length_as_utf8(c)); if (utf8_len > len) { return err(EILSEQ); } @@ -70,7 +70,7 @@ static Result encode_wide_char_as_utf8(wchar_t c, char* result, usize& len static Result encode_utf8_as_wide_char_impl(const char* beg, usize& len) { - usize utf8_len = TRY(utf8_char_length(*beg)); + const usize utf8_len = TRY(utf8_char_length(*beg)); if (utf8_len > len) return err(EILSEQ); // Unterminated sequence len = utf8_len; // Enough space for the sequence, let's return the resulting length @@ -115,7 +115,7 @@ static Result encode_utf8_as_wide_char_impl(const char* beg, usize& len static Result encode_utf8_as_wide_char(const char* beg, usize& len) { - wchar_t result = TRY(encode_utf8_as_wide_char_impl(beg, len)); + const wchar_t result = TRY(encode_utf8_as_wide_char_impl(beg, len)); // NOTE: We already know this is a valid code-point, since we constructed it ourselves and already checked the // range. if (len != wide_char_length_as_utf8_unchecked(result)) @@ -137,7 +137,7 @@ Result Utf8StringDecoder::code_points() const while ((usize)(it - m_str) < m_byte_length) { - usize utf8_len = TRY(utf8_char_length(*it)); + const usize utf8_len = TRY(utf8_char_length(*it)); if ((usize)(it - m_str) + utf8_len > m_byte_length) return err(EILSEQ); it += utf8_len; len++; @@ -234,7 +234,7 @@ Result> Utf8StateDecoder::feed(char c) if (m_state_index == m_state_len - 1) { usize len = m_state_len; - wchar_t wc = TRY(encode_utf8_as_wide_char(m_state, len)); + const wchar_t wc = TRY(encode_utf8_as_wide_char(m_state, len)); m_state_len = 0; return Option { wc }; }