56 lines
1.1 KiB
C++
56 lines
1.1 KiB
C++
#pragma once
|
|
#include "../types.h"
|
|
|
|
namespace Page {
|
|
/*! \brief Page Bits
|
|
* We use the least significant 12 Bits
|
|
*/
|
|
const uintptr_t BITS = 12;
|
|
|
|
/*! \brief Page size
|
|
* We go with 2^12 = 4K
|
|
*/
|
|
const uintptr_t SIZE = 1 << BITS;
|
|
|
|
/*! \brief Mask for Page
|
|
*/
|
|
const uintptr_t MASK = SIZE - 1;
|
|
|
|
/*! \brief Round to next (upper) page aligned address
|
|
* \param address memory address
|
|
* \return nearest ceiling page aligned address
|
|
*/
|
|
template <typename T>
|
|
T ceil(T address) {
|
|
return (address + MASK) & (~MASK);
|
|
}
|
|
|
|
/*! \brief Round to last (lower) page aligned address
|
|
* \param address memory address
|
|
* \return nearest floor page aligned address
|
|
*/
|
|
template <typename T>
|
|
T floor(T ptr) {
|
|
return ptr & (~MASK);
|
|
}
|
|
|
|
/*! \brief Get offset in page
|
|
* \param address memory address
|
|
* \return offset
|
|
*/
|
|
template <typename T>
|
|
T offset(T ptr) {
|
|
return ptr & MASK;
|
|
}
|
|
|
|
/*! \brief Check if address is page aligned
|
|
* \param address memory address
|
|
* \return true if page aligned address
|
|
*/
|
|
template <typename T>
|
|
bool aligned(T ptr) {
|
|
return offset(ptr) == 0;
|
|
}
|
|
|
|
} // namespace Page
|