alignment.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. public:
  43. pointer address(reference r) {
  44. return std::addressof(r);
  45. }
  46. const_pointer address(const_reference r) const {
  47. return std::addressof(r);
  48. }
  49. pointer allocate(size_type n) {
  50. return static_cast<pointer>(::operator new (n, std::align_val_t{Align}));
  51. }
  52. void deallocate(pointer p, size_type) {
  53. ::operator delete (p, std::align_val_t{Align});
  54. }
  55. void construct(pointer p, const value_type& wert) {
  56. new (p) value_type(wert);
  57. }
  58. void destroy(pointer p) {
  59. p->~value_type();
  60. }
  61. size_type max_size() const noexcept {
  62. return size_type(-1) / sizeof(value_type);
  63. }
  64. template <typename T2>
  65. struct rebind {
  66. typedef AlignmentAllocator<T2, Align> other;
  67. };
  68. bool operator!=(const AlignmentAllocator<T, Align>& other) const {
  69. return !(*this == other);
  70. }
  71. // Returns true if and only if storage allocated from *this
  72. // can be deallocated from other, and vice versa.
  73. // Always returns true for stateless allocators.
  74. bool operator==(const AlignmentAllocator<T, Align>& other) const {
  75. return true;
  76. }
  77. };
  78. } // namespace Common