From 39b310b6b9ce114984c04677e5eaced75e96bf44 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 6 Dec 2022 19:27:58 +0100 Subject: [PATCH] Make alignment a template parameter to help the compiler optimize --- kernel/src/memory/Heap.cpp | 6 ++--- kernel/src/memory/MemoryManager.cpp | 6 ++--- luna/include/luna/Alignment.h | 35 ++++++++++++++++------------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/kernel/src/memory/Heap.cpp b/kernel/src/memory/Heap.cpp index 703b7922..ad16eebe 100644 --- a/kernel/src/memory/Heap.cpp +++ b/kernel/src/memory/Heap.cpp @@ -86,7 +86,7 @@ static usize get_fair_offset_to_split_at(HeapBlock* block, usize min) available -= (available / 2); // reserve half of the rest for the new block, while still leaving another half for the old one. - available = align_down(available, 16UL); // Everything has to be aligned on a 16-byte boundary + available = align_down<16>(available); // Everything has to be aligned on a 16-byte boundary return available + block->req_size; } @@ -165,7 +165,7 @@ Result kmalloc(usize size) { if (!size) return (void*)BLOCK_MAGIC; - size = align_up(size, 16UL); + size = align_up<16>(size); if (!heap.first().has_value()) { @@ -291,7 +291,7 @@ Result krealloc(void* ptr, usize size) return err(EFAULT); } - size = align_up(size, 16UL); + size = align_up<16>(size); if (is_block_free(block)) { diff --git a/kernel/src/memory/MemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp index d220710d..acaaf1b1 100644 --- a/kernel/src/memory/MemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -23,7 +23,7 @@ static u64 start_index = 0; static Bitmap g_frame_bitmap; -#define CHECK_PAGE_ALIGNED(address) expect(is_aligned(address, ARCH_PAGE_SIZE), "Address is not page-aligned") +#define CHECK_PAGE_ALIGNED(address) expect(is_aligned(address), "Address is not page-aligned") static usize get_physical_address_space_size() { @@ -215,8 +215,8 @@ namespace MemoryManager Result remap_unaligned(u64 address, usize count, int flags) { - if (!is_aligned(address, ARCH_PAGE_SIZE)) count++; - address = align_down(address, ARCH_PAGE_SIZE); + if (!is_aligned(address)) count++; + address = align_down(address); while (count--) { diff --git a/luna/include/luna/Alignment.h b/luna/include/luna/Alignment.h index 0721aae5..439bf2a1 100644 --- a/luna/include/luna/Alignment.h +++ b/luna/include/luna/Alignment.h @@ -1,32 +1,37 @@ #pragma once -template constexpr T is_aligned(T value, T alignment) +// Must ALWAYS be called with a power of two as alignment. +template constexpr T is_aligned(T value) { + static_assert((alignment & (alignment - 1)) == 0); return (value % alignment == 0); } -static_assert(is_aligned(1024, 512)); -static_assert(!is_aligned(235, 32)); -static_assert(is_aligned(40960, 4096)); +static_assert(is_aligned<512>(1024)); +static_assert(!is_aligned<32>(235)); +static_assert(is_aligned<4096>(40960)); -template constexpr T align_down(T value, T alignment) +// Must ALWAYS be called with a power of two as alignment. +template constexpr T align_down(T value) { - return value - (value % alignment); + static_assert((alignment & (alignment - 1)) == 0); + return value - value % alignment; } -static_assert(align_down(598, 512) == 512); -static_assert(align_down(194, 64) == 192); -static_assert(align_down(62, 31) == 62); +static_assert(align_down<512>(598) == 512); +static_assert(align_down<64>(194) == 192); +static_assert(align_down<32>(64) == 64); -template constexpr T align_up(T value, T alignment) +// Must ALWAYS be called with a power of two as alignment. +template constexpr T align_up(T value) { - if (is_aligned(value, alignment)) return value; - return align_down(value, alignment) + alignment; + if (is_aligned(value)) return value; + return align_down(value) + alignment; } -static_assert(align_up(598, 512) == 1024); -static_assert(align_up(194, 64) == 256); -static_assert(align_up(62, 31) == 62); +static_assert(align_up<512>(598) == 1024); +static_assert(align_up<64>(194) == 256); +static_assert(align_up<32>(64) == 64); template constexpr T get_blocks_from_size(T value, T block_size) {