short_alloc.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #ifndef SHORT_ALLOC_H
  2. #define SHORT_ALLOC_H
  3. // The MIT License (MIT)
  4. //
  5. // Copyright (c) 2015 Howard Hinnant
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. #include <cstddef>
  25. #include <cassert>
  26. template <std::size_t N, std::size_t alignment = alignof(std::max_align_t)>
  27. class arena
  28. {
  29. alignas(alignment) char buf_[N];
  30. char* ptr_;
  31. public:
  32. ~arena() {ptr_ = nullptr;}
  33. arena() noexcept : ptr_(buf_) {}
  34. arena(const arena&) = delete;
  35. arena& operator=(const arena&) = delete;
  36. template <std::size_t ReqAlign> char* allocate(std::size_t n);
  37. void deallocate(char* p, std::size_t n) noexcept;
  38. static constexpr std::size_t size() noexcept {return N;}
  39. std::size_t used() const noexcept {return static_cast<std::size_t>(ptr_ - buf_);}
  40. void reset() noexcept {ptr_ = buf_;}
  41. private:
  42. static
  43. std::size_t
  44. align_up(std::size_t n) noexcept
  45. {return (n + (alignment-1)) & ~(alignment-1);}
  46. bool
  47. pointer_in_buffer(char* p) noexcept
  48. {return buf_ <= p && p <= buf_ + N;}
  49. };
  50. template <std::size_t N, std::size_t alignment>
  51. template <std::size_t ReqAlign>
  52. char*
  53. arena<N, alignment>::allocate(std::size_t n)
  54. {
  55. static_assert(ReqAlign <= alignment, "alignment is too small for this arena");
  56. assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
  57. auto const aligned_n = align_up(n);
  58. if (static_cast<decltype(aligned_n)>(buf_ + N - ptr_) >= aligned_n)
  59. {
  60. char* r = ptr_;
  61. ptr_ += aligned_n;
  62. return r;
  63. }
  64. static_assert(alignment <= alignof(std::max_align_t), "you've chosen an "
  65. "alignment that is larger than alignof(std::max_align_t), and "
  66. "cannot be guaranteed by normal operator new");
  67. return static_cast<char*>(::operator new(n));
  68. }
  69. template <std::size_t N, std::size_t alignment>
  70. void
  71. arena<N, alignment>::deallocate(char* p, std::size_t n) noexcept
  72. {
  73. assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
  74. if (pointer_in_buffer(p))
  75. {
  76. n = align_up(n);
  77. if (p + n == ptr_) {
  78. ptr_ = p;
  79. }
  80. }
  81. else {
  82. ::operator delete(p);
  83. }
  84. }
  85. template <class T, std::size_t N, std::size_t Align = alignof(std::max_align_t)>
  86. class short_alloc
  87. {
  88. public:
  89. using value_type = T;
  90. static auto constexpr alignment = Align;
  91. static auto constexpr size = N;
  92. using arena_type = arena<size, alignment>;
  93. private:
  94. arena_type& a_;
  95. public:
  96. short_alloc(const short_alloc&) = default;
  97. short_alloc& operator=(const short_alloc&) = delete;
  98. explicit short_alloc(arena_type& a) noexcept : a_(a)
  99. {
  100. static_assert(size % alignment == 0,
  101. "size N needs to be a multiple of alignment Align");
  102. }
  103. template <class U>
  104. explicit short_alloc(const short_alloc<U, N, alignment>& a) noexcept
  105. : a_(a.a_) {}
  106. template <class _Up> struct rebind {using other = short_alloc<_Up, N, alignment>;};
  107. T* allocate(std::size_t n)
  108. {
  109. return reinterpret_cast<T*>(a_.template allocate<alignof(T)>(n*sizeof(T)));
  110. }
  111. void deallocate(T* p, std::size_t n) noexcept
  112. {
  113. a_.deallocate(reinterpret_cast<char*>(p), n*sizeof(T));
  114. }
  115. template <class T1, std::size_t N1, std::size_t A1,
  116. class U, std::size_t M, std::size_t A2>
  117. friend
  118. bool
  119. operator==(const short_alloc<T1, N1, A1>& x, const short_alloc<U, M, A2>& y) noexcept;
  120. template <class U, std::size_t M, std::size_t A> friend class short_alloc;
  121. };
  122. template <class T, std::size_t N, std::size_t A1, class U, std::size_t M, std::size_t A2>
  123. inline
  124. bool
  125. operator==(const short_alloc<T, N, A1>& x, const short_alloc<U, M, A2>& y) noexcept
  126. {
  127. return N == M && A1 == A2 && &x.a_ == &y.a_;
  128. }
  129. template <class T, std::size_t N, std::size_t A1, class U, std::size_t M, std::size_t A2>
  130. inline
  131. bool
  132. operator!=(const short_alloc<T, N, A1>& x, const short_alloc<U, M, A2>& y) noexcept
  133. {
  134. return !(x == y);
  135. }
  136. #endif // SHORT_ALLOC_HPP