From: Amelia Coutard Date: Sun, 19 Mar 2023 01:04:17 +0000 (+0100) Subject: Implemented memset and memcpy, to make some for loops clearer X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=461605cf0a6a1402a7fa5d4795305e4c9e55167a;p=voyage-au-centre-des-fichiers.git Implemented memset and memcpy, to make some for loops clearer --- diff --git a/kernel/src/elf64.cpp b/kernel/src/elf64.cpp index a093f02..422d0ba 100644 --- a/kernel/src/elf64.cpp +++ b/kernel/src/elf64.cpp @@ -39,9 +39,7 @@ os::process os::elf::load_elf(void* start, std::size_t length, const paging::PML }; // Copy kernel mappings to the new virtual address space. - for (size_t i = 0; i < 256; i++) { - result.PML4T->contents[256 + i] = original_PML4T.contents[256 + i]; - } + memcpy(result.PML4T->contents + 256, original_PML4T.contents + 256, 256 * sizeof(os::paging::PML4E)); for (std::size_t i = 0; i < elf_header.entry_count_program_header_table; i++) { const os::elf::program_header program_header = *(os::elf::program_header*)( @@ -62,9 +60,7 @@ os::process os::elf::load_elf(void* start, std::size_t length, const paging::PML for (std::size_t i = 0; i < nb_pages; i++) { std::byte* const page = os::paging::setup_page(*result.PML4T, program_header.p_vaddr + i * 0x1000, (program_header.flags & 2) >> 1, 1); - for (std::size_t j = 0; j < 0x1000 && j < program_header.p_filesz - i * 0x1000; j++) { - page[j] = ((std::byte*)start)[program_header.p_offset + j]; - } + memcpy(page, (std::byte*)start + program_header.p_offset, clamp(0ul, program_header.p_filesz - i * 0x1000, 0x1000ul)); } } diff --git a/kernel/src/lib/phys_ptr.hpp b/kernel/src/lib/phys_ptr.hpp index c736e5f..c42c47e 100644 --- a/kernel/src/lib/phys_ptr.hpp +++ b/kernel/src/lib/phys_ptr.hpp @@ -33,6 +33,12 @@ public: T* operator->() const { return get_virt_addr(); } + explicit operator T*() const { + return get_virt_addr(); + } + explicit operator void*() const { + return get_virt_addr(); + } constexpr phys_ptr& operator++() { return *this += 1; diff --git a/kernel/src/paging.cpp b/kernel/src/paging.cpp index 07d0aec..610a5e9 100644 --- a/kernel/src/paging.cpp +++ b/kernel/src/paging.cpp @@ -123,18 +123,14 @@ os::paging::page_allocator_t::block os::paging::page_allocator_t::allocate(std:: for (auto it = begin(); it != end(); it = it->next) { if (count == it->size) { erase(it); - for (std::size_t i = 0; i < count * 0x1000; i++) { - phys_ptr(it.get_phys_addr())[i] = 0; - } + memset((void*)it, 0, count * 0x1000); return { .ptr = phys_ptr(it.get_phys_addr()), .size = count, }; } else if (count < it->size) { erase(split_at_offset(it, count)); - for (std::size_t i = 0; i < count * 0x1000; i++) { - phys_ptr(it.get_phys_addr())[i] = 0; - } + memset((void*)it, 0, count * 0x1000); return { .ptr = phys_ptr(it.get_phys_addr()), .size = count, diff --git a/kernel/src/utils.hpp b/kernel/src/utils.hpp index df3f7db..c203c80 100644 --- a/kernel/src/utils.hpp +++ b/kernel/src/utils.hpp @@ -14,6 +14,7 @@ #pragma once #include +#include namespace os { @@ -26,5 +27,20 @@ std::uint8_t inb(std::uint16_t port); bool cpu_has_msr(); std::uint64_t get_msr(std::uint32_t msr); void set_msr(std::uint32_t msr, std::uint64_t v); +template T clamp(T min, T v, T max) { + return v < min ? min : max < v ? max : v; +} +inline void* memset(void* dest, int c, size_t n) { + while (n-- > 0) { + reinterpret_cast(dest)[n] = std::byte(c); + } + return dest; +} +inline void* memcpy(void* dest, const void* src, size_t n) { + while (n-- > 0) { + reinterpret_cast(dest)[n] = reinterpret_cast(src)[n]; + } + return dest; +} } // namespace os