7 #include "alignment.hpp" 10 namespace boost {
namespace memory {
12 static constexpr
bool is_power_of_2(std::size_t x) noexcept
14 while (((x & 1) == 0) && x > 1)
20 inline constexpr std::uint8_t* align_forward<1>(std::uint8_t* address) noexcept
25 template <std::
size_t alignment>
26 inline constexpr std::uint8_t* align_forward(std::uint8_t* address) noexcept
28 static_assert(is_power_of_2(alignment),
"alignment must be a power of 2");
30 auto address_ptr =
reinterpret_cast<std::uintptr_t
>(address);
31 return address_ptr % alignment == 0
33 :
reinterpret_cast<std::uint8_t*
>(address_ptr + (alignment - (address_ptr % alignment)));
37 inline constexpr std::uint8_t* align_backward<1>(std::uint8_t* address) noexcept
42 template <std::
size_t alignment>
43 inline constexpr std::uint8_t* align_backward(std::uint8_t* address) noexcept
45 static_assert(is_power_of_2(alignment),
"alignment must be a power of 2");
47 auto address_ptr =
reinterpret_cast<std::uintptr_t
>(address);
48 return address_ptr % alignment == 0
50 :
reinterpret_cast<std::uint8_t*
>(address_ptr - (address_ptr % alignment));
54 inline constexpr std::size_t align_size<1>(std::size_t size) noexcept
59 template <std::
size_t alignment>
60 inline constexpr std::size_t align_size(std::size_t size) noexcept
62 static_assert(is_power_of_2(alignment),
"alignment must be a power of 2");
64 return size % alignment == 0
66 : size + (alignment - (size % alignment));