Make alignment a template parameter to help the compiler optimize
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2022-12-06 19:27:58 +01:00
parent d48eb85d1d
commit 39b310b6b9
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 26 additions and 21 deletions

View File

@ -86,7 +86,7 @@ static usize get_fair_offset_to_split_at(HeapBlock* block, usize min)
available -= (available / available -= (available /
2); // reserve half of the rest for the new block, while still leaving another half for the old one. 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; return available + block->req_size;
} }
@ -165,7 +165,7 @@ Result<void*> kmalloc(usize size)
{ {
if (!size) return (void*)BLOCK_MAGIC; if (!size) return (void*)BLOCK_MAGIC;
size = align_up(size, 16UL); size = align_up<16>(size);
if (!heap.first().has_value()) if (!heap.first().has_value())
{ {
@ -291,7 +291,7 @@ Result<void*> krealloc(void* ptr, usize size)
return err(EFAULT); return err(EFAULT);
} }
size = align_up(size, 16UL); size = align_up<16>(size);
if (is_block_free(block)) if (is_block_free(block))
{ {

View File

@ -23,7 +23,7 @@ static u64 start_index = 0;
static Bitmap g_frame_bitmap; 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<ARCH_PAGE_SIZE>(address), "Address is not page-aligned")
static usize get_physical_address_space_size() static usize get_physical_address_space_size()
{ {
@ -215,8 +215,8 @@ namespace MemoryManager
Result<void> remap_unaligned(u64 address, usize count, int flags) Result<void> remap_unaligned(u64 address, usize count, int flags)
{ {
if (!is_aligned(address, ARCH_PAGE_SIZE)) count++; if (!is_aligned<ARCH_PAGE_SIZE>(address)) count++;
address = align_down(address, ARCH_PAGE_SIZE); address = align_down<ARCH_PAGE_SIZE>(address);
while (count--) while (count--)
{ {

View File

@ -1,32 +1,37 @@
#pragma once #pragma once
template <typename T> constexpr T is_aligned(T value, T alignment) // Must ALWAYS be called with a power of two as alignment.
template <usize alignment, typename T> constexpr T is_aligned(T value)
{ {
static_assert((alignment & (alignment - 1)) == 0);
return (value % alignment == 0); return (value % alignment == 0);
} }
static_assert(is_aligned(1024, 512)); static_assert(is_aligned<512>(1024));
static_assert(!is_aligned(235, 32)); static_assert(!is_aligned<32>(235));
static_assert(is_aligned(40960, 4096)); static_assert(is_aligned<4096>(40960));
template <typename T> constexpr T align_down(T value, T alignment) // Must ALWAYS be called with a power of two as alignment.
template <usize alignment, typename T> 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<512>(598) == 512);
static_assert(align_down(194, 64) == 192); static_assert(align_down<64>(194) == 192);
static_assert(align_down(62, 31) == 62); static_assert(align_down<32>(64) == 64);
template <typename T> constexpr T align_up(T value, T alignment) // Must ALWAYS be called with a power of two as alignment.
template <usize alignment, typename T> constexpr T align_up(T value)
{ {
if (is_aligned(value, alignment)) return value; if (is_aligned<alignment>(value)) return value;
return align_down(value, alignment) + alignment; return align_down<alignment>(value) + alignment;
} }
static_assert(align_up(598, 512) == 1024); static_assert(align_up<512>(598) == 1024);
static_assert(align_up(194, 64) == 256); static_assert(align_up<64>(194) == 256);
static_assert(align_up(62, 31) == 62); static_assert(align_up<32>(64) == 64);
template <typename T> constexpr T get_blocks_from_size(T value, T block_size) template <typename T> constexpr T get_blocks_from_size(T value, T block_size)
{ {