alignment.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, std::size_t Align = 16>
  33. class AlignmentAllocator {
  34. public:
  35. using value_type = T;
  36. using size_type = std::size_t;
  37. using difference_type = std::ptrdiff_t;
  38. using pointer = T*;
  39. using const_pointer = const T*;
  40. using reference = T&;
  41. using const_reference = const T&;
  42. using propagate_on_container_copy_assignment = std::true_type;
  43. using propagate_on_container_move_assignment = std::true_type;
  44. using propagate_on_container_swap = std::true_type;
  45. using is_always_equal = std::true_type;
  46. public:
  47. constexpr AlignmentAllocator() noexcept = default;
  48. template <typename T2>
  49. constexpr AlignmentAllocator(const AlignmentAllocator<T2, Align>&) noexcept {}
  50. pointer address(reference r) noexcept {
  51. return std::addressof(r);
  52. }
  53. const_pointer address(const_reference r) const noexcept {
  54. return std::addressof(r);
  55. }
  56. pointer allocate(size_type n) {
  57. return static_cast<pointer>(::operator new (n, std::align_val_t{Align}));
  58. }
  59. void deallocate(pointer p, size_type) {
  60. ::operator delete (p, std::align_val_t{Align});
  61. }
  62. void construct(pointer p, const value_type& wert) {
  63. new (p) value_type(wert);
  64. }
  65. void destroy(pointer p) {
  66. p->~value_type();
  67. }
  68. size_type max_size() const noexcept {
  69. return size_type(-1) / sizeof(value_type);
  70. }
  71. template <typename T2>
  72. struct rebind {
  73. using other = AlignmentAllocator<T2, Align>;
  74. };
  75. bool operator!=(const AlignmentAllocator<T, Align>& other) const noexcept {
  76. return !(*this == other);
  77. }
  78. // Returns true if and only if storage allocated from *this
  79. // can be deallocated from other, and vice versa.
  80. // Always returns true for stateless allocators.
  81. bool operator==(const AlignmentAllocator<T, Align>& other) const noexcept {
  82. return true;
  83. }
  84. };
  85. } // namespace Common