From 10c567daee22bc23a407d47e9da1c5303c82f042 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Fri, 5 Apr 2019 09:05:35 +0200 Subject: [PATCH] base: add 'aligned' function to util/misc_math.h This function simplifies the sanity checking of values that are expected to be aligned, e.g., data offsets within packet streams. --- repos/base/include/util/misc_math.h | 6 +++++- repos/os/src/drivers/gpu/intel/ppgtt.h | 15 +++------------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/repos/base/include/util/misc_math.h b/repos/base/include/util/misc_math.h index 88090c759c..043bfbe8c4 100644 --- a/repos/base/include/util/misc_math.h +++ b/repos/base/include/util/misc_math.h @@ -26,7 +26,7 @@ namespace Genode { static constexpr T abs(T value) { return value >= 0 ? value : -value; } - /** + /* * Alignment to the power of two */ template @@ -41,6 +41,10 @@ namespace Genode { static constexpr T align_addr(T addr, int align) { return (addr + _align_offset((T)align)) & _align_mask((T)align); } + template + static constexpr bool aligned(T value, unsigned align_log2) { + return (_align_offset(align_log2) & value) == 0; } + /** * LOG2 diff --git a/repos/os/src/drivers/gpu/intel/ppgtt.h b/repos/os/src/drivers/gpu/intel/ppgtt.h index 54b6d1d750..ca1f1749d5 100644 --- a/repos/os/src/drivers/gpu/intel/ppgtt.h +++ b/repos/os/src/drivers/gpu/intel/ppgtt.h @@ -39,15 +39,6 @@ namespace Genode inline addr_t trunc(addr_t const addr, addr_t const alignm_log2) { return (addr >> alignm_log2) << alignm_log2; } - /** - * Return wether a pointer fullfills an alignment - * - * \param p pointer - * \param alignm_log2 log2 of the required alignment - */ - inline bool aligned(void * const p, addr_t const alignm_log2) { - return (addr_t)p == trunc((addr_t)p, alignm_log2); } - /** * Translation table allocator interface */ @@ -344,7 +335,7 @@ class Genode::Level_4_translation_table */ Level_4_translation_table(Scratch::Page *scratch) { - if (!aligned(this, ALIGNM_LOG2)) { throw Misaligned(); } + if (!aligned((addr_t)this, ALIGNM_LOG2)) { throw Misaligned(); } for (size_t i = 0; i < MAX_ENTRIES; i++) { _entries[i] = scratch->addr; @@ -592,7 +583,7 @@ class Genode::Page_directory Page_directory(Scratch::Page *scratch) { - if (!aligned(this, ALIGNM_LOG2)) { throw Misaligned(); } + if (!aligned((addr_t)this, ALIGNM_LOG2)) { throw Misaligned(); } for (size_t i = 0; i < MAX_ENTRIES; i++) { _entries[i] = scratch->addr; @@ -778,7 +769,7 @@ class Genode::Pml4_table Pml4_table(Scratch::Page *scratch) { - if (!aligned(this, ALIGNM_LOG2)) { throw Misaligned(); } + if (!aligned((addr_t)this, ALIGNM_LOG2)) { throw Misaligned(); } for (size_t i = 0; i < MAX_ENTRIES; i++) { _entries[i] = scratch->addr;