mirror of
https://github.com/genodelabs/genode.git
synced 2024-12-21 22:47:50 +00:00
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.
This commit is contained in:
parent
6ea1179145
commit
10c567daee
@ -26,7 +26,7 @@ namespace Genode {
|
||||
static constexpr T abs(T value) { return value >= 0 ? value : -value; }
|
||||
|
||||
|
||||
/**
|
||||
/*
|
||||
* Alignment to the power of two
|
||||
*/
|
||||
template <typename T>
|
||||
@ -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 <typename T>
|
||||
static constexpr bool aligned(T value, unsigned align_log2) {
|
||||
return (_align_offset(align_log2) & value) == 0; }
|
||||
|
||||
|
||||
/**
|
||||
* LOG2
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user