]> git.ameliathe1st.gay Git - voyage-au-centre-des-fichiers.git/commitdiff
Implemented memset and memcpy, to make some for loops clearer
authorAmelia Coutard <eliottulio.coutard@gmail.com>
Sun, 19 Mar 2023 01:04:17 +0000 (02:04 +0100)
committerAmelia Coutard <eliottulio.coutard@gmail.com>
Sun, 19 Mar 2023 01:04:17 +0000 (02:04 +0100)
kernel/src/elf64.cpp
kernel/src/lib/phys_ptr.hpp
kernel/src/paging.cpp
kernel/src/utils.hpp

index a093f02feacae176cd9506923d3971d2f711e66c..422d0ba4b9a94b94625996338b46cda795f9001c 100644 (file)
@@ -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));
                }
        }
 
index c736e5f70d668d16f10f77f7c0e4f7c271734fa3..c42c47ed251b0696087ee9fd0246adc1086bc1bb 100644 (file)
@@ -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<T>& operator++() {
                return *this += 1;
index 07d0aec64486972d094ffd3b67ac6240073dd906..610a5e9ac190051e9b9da721f98f0c27a5ce00a8 100644 (file)
@@ -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<char>(it.get_phys_addr())[i] = 0;
-                       }
+                       memset((void*)it, 0, count * 0x1000);
                        return {
                                .ptr = phys_ptr<paging::page>(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<char>(it.get_phys_addr())[i] = 0;
-                       }
+                       memset((void*)it, 0, count * 0x1000);
                        return {
                                .ptr = phys_ptr<paging::page>(it.get_phys_addr()),
                                .size = count,
index df3f7db209ab18d0fa79a2df6302b13d51ad2a78..c203c8089dea46cb92260928d379bcc9811b13f3 100644 (file)
@@ -14,6 +14,7 @@
 #pragma once
 
 #include <cstdint>
+#include <cstddef>
 
 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 <typename T> 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<std::byte*>(dest)[n] = std::byte(c);
+       }
+       return dest;
+}
+inline void* memcpy(void* dest, const void* src, size_t n) {
+       while (n-- > 0) {
+               reinterpret_cast<std::byte*>(dest)[n] = reinterpret_cast<const std::byte*>(src)[n];
+       }
+       return dest;
+}
 
 } // namespace os