alignment.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // This file is under the public domain.
  2. #pragma once
  3. #include <cstddef>
  4. #include <memory>
  5. #include <type_traits>
  6. namespace Common {
  7. template <typename T>
  8. constexpr T AlignUp(T value, std::size_t size) {
  9. static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
  10. return static_cast<T>(value + (size - value % size) % size);
  11. }
  12. template <typename T>
  13. constexpr T AlignDown(T value, std::size_t size) {
  14. static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
  15. return static_cast<T>(value - value % size);
  16. }
  17. template <typename T>
  18. constexpr T AlignBits(T value, std::size_t align) {
  19. static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
  20. return static_cast<T>((value + ((1ULL << align) - 1)) >> align << align);
  21. }
  22. template <typename T>
  23. constexpr bool Is4KBAligned(T value) {
  24. static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
  25. return (value & 0xFFF) == 0;
  26. }
  27. template <typename T>
  28. constexpr bool IsWordAligned(T value) {
  29. static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
  30. return (value & 0b11) == 0;
  31. }
  32. template <typename T>
  33. constexpr bool IsAligned(T value, std::size_t alignment) {
  34. using U = typename std::make_unsigned<T>::type;
  35. const U mask = static_cast<U>(alignment - 1);
  36. return (value & mask) == 0;
  37. }
  38. template <typename T, std::size_t Align = 16>
  39. class AlignmentAllocator {
  40. public:
  41. using value_type = T;
  42. using size_type = std::size_t;
  43. using difference_type = std::ptrdiff_t;
  44. using pointer = T*;
  45. using const_pointer = const T*;
  46. using reference = T&;
  47. using const_reference = const T&;
  48. using propagate_on_container_copy_assignment = std::true_type;
  49. using propagate_on_container_move_assignment = std::true_type;
  50. using propagate_on_container_swap = std::true_type;
  51. using is_always_equal = std::true_type;
  52. public:
  53. constexpr AlignmentAllocator() noexcept = default;
  54. template <typename T2>
  55. constexpr AlignmentAllocator(const AlignmentAllocator<T2, Align>&) noexcept {}
  56. pointer address(reference r) noexcept {
  57. return std::addressof(r);
  58. }
  59. const_pointer address(const_reference r) const noexcept {
  60. return std::addressof(r);
  61. }
  62. pointer allocate(size_type n) {
  63. return static_cast<pointer>(::operator new (n, std::align_val_t{Align}));
  64. }
  65. void deallocate(pointer p, size_type) {
  66. ::operator delete (p, std::align_val_t{Align});
  67. }
  68. void construct(pointer p, const value_type& wert) {
  69. new (p) value_type(wert);
  70. }
  71. void destroy(pointer p) {
  72. p->~value_type();
  73. }
  74. size_type max_size() const noexcept {
  75. return size_type(-1) / sizeof(value_type);
  76. }
  77. template <typename T2>
  78. struct rebind {
  79. using other = AlignmentAllocator<T2, Align>;
  80. };
  81. bool operator!=(const AlignmentAllocator<T, Align>& other) const noexcept {
  82. return !(*this == other);
  83. }
  84. // Returns true if and only if storage allocated from *this
  85. // can be deallocated from other, and vice versa.
  86. // Always returns true for stateless allocators.
  87. bool operator==(const AlignmentAllocator<T, Align>& other) const noexcept {
  88. return true;
  89. }
  90. };
  91. } // namespace Common