19 #ifndef PSTORE_SUPPORT_ALIGNED_HPP 20 #define PSTORE_SUPPORT_ALIGNED_HPP 23 #include <type_traits> 30 template <typename Ty, typename = typename std::enable_if<std::is_unsigned<Ty>::value>>
33 return n > 0U && !(n & (n - 1U));
40 template <
typename IntType,
typename AlignType>
41 constexpr IntType
aligned (IntType
const v, AlignType
const align) noexcept {
42 static_assert (std::is_unsigned<IntType>::value,
"aligned() IntType must be unsigned");
43 static_assert (std::is_unsigned<AlignType>::value,
"aligned() AlignType must be unsigned");
46 return (v + align - 1U) &
static_cast<IntType
> (~(align - 1U));
52 template <
typename AlignType,
typename IntType,
53 typename = std::enable_if<std::is_integral<IntType>::value>>
54 constexpr IntType
aligned (IntType
const v) noexcept {
55 return aligned (v,
alignof (AlignType));
58 template <
typename Po
inteeType>
59 constexpr PointeeType
const *
aligned (
void const *
const p) noexcept {
60 return reinterpret_cast<PointeeType
const *
> (
61 aligned (reinterpret_cast<std::uintptr_t> (p),
alignof (PointeeType)));
63 template <
typename Po
inteeType>
64 constexpr PointeeType *
aligned (
void *
const p) noexcept {
65 return reinterpret_cast<PointeeType *
> (
66 aligned (reinterpret_cast<std::uintptr_t> (p),
alignof (PointeeType)));
69 template <
typename DestPo
inteeType,
typename SrcPo
inteeType = DestPo
inteeType>
70 constexpr DestPointeeType
const * aligned_ptr (SrcPointeeType
const *
const p) noexcept {
71 return aligned<DestPointeeType> (
reinterpret_cast<void const *
> (p));
73 template <
typename DestPo
inteeType,
typename SrcPo
inteeType = DestPo
inteeType>
74 constexpr DestPointeeType * aligned_ptr (SrcPointeeType *
const p) noexcept {
75 return aligned<DestPointeeType> (
reinterpret_cast<void *
> (p));
85 template <
typename Ty>
88 return (align == 0U) ? 0U : ((v + align - 1U) & ~(align - 1U)) - v;
97 template <
typename Ty>
104 #endif // PSTORE_SUPPORT_ALIGNED_HPP constexpr Ty calc_alignment(Ty const v, std::size_t const align) noexcept
Calculate the value that must be added to p v in order that it has the alignment given by align...
Definition: aligned.hpp:86
Definition: nonpod2.cpp:40
An implementation of the standard assert() macro with the exception that it will, on failure...
constexpr IntType aligned(IntType const v, AlignType const align) noexcept
Definition: aligned.hpp:41
constexpr bool is_power_of_two(Ty const n) noexcept
Definition: aligned.hpp:31