pool.inl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*-
  2. * Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #ifndef MEMORY_BLOCK_TCC
  23. #define MEMORY_BLOCK_TCC
  24. template <typename T, size_t BlockSize>
  25. inline typename MemoryPool<T, BlockSize>::size_type
  26. MemoryPool<T, BlockSize>::padPointer(data_pointer_ p, size_type align)
  27. const noexcept
  28. {
  29. uintptr_t result = reinterpret_cast<uintptr_t>(p);
  30. return ((align - result) % align);
  31. }
  32. template <typename T, size_t BlockSize>
  33. MemoryPool<T, BlockSize>::MemoryPool()
  34. noexcept
  35. {
  36. currentBlock_ = nullptr;
  37. currentSlot_ = nullptr;
  38. lastSlot_ = nullptr;
  39. freeSlots_ = nullptr;
  40. }
  41. template <typename T, size_t BlockSize>
  42. MemoryPool<T, BlockSize>::MemoryPool(const MemoryPool& memoryPool)
  43. noexcept :
  44. MemoryPool()
  45. {}
  46. template <typename T, size_t BlockSize>
  47. MemoryPool<T, BlockSize>::MemoryPool(MemoryPool&& memoryPool)
  48. noexcept
  49. {
  50. currentBlock_ = memoryPool.currentBlock_;
  51. memoryPool.currentBlock_ = nullptr;
  52. currentSlot_ = memoryPool.currentSlot_;
  53. lastSlot_ = memoryPool.lastSlot_;
  54. freeSlots_ = memoryPool.freeSlots;
  55. }
  56. template <typename T, size_t BlockSize>
  57. template<class U>
  58. MemoryPool<T, BlockSize>::MemoryPool(const MemoryPool<U>& memoryPool)
  59. noexcept :
  60. MemoryPool()
  61. {}
  62. template <typename T, size_t BlockSize>
  63. MemoryPool<T, BlockSize>&
  64. MemoryPool<T, BlockSize>::operator=(MemoryPool&& memoryPool)
  65. noexcept
  66. {
  67. if (this != &memoryPool)
  68. {
  69. std::swap(currentBlock_, memoryPool.currentBlock_);
  70. currentSlot_ = memoryPool.currentSlot_;
  71. lastSlot_ = memoryPool.lastSlot_;
  72. freeSlots_ = memoryPool.freeSlots;
  73. }
  74. return *this;
  75. }
  76. template <typename T, size_t BlockSize>
  77. MemoryPool<T, BlockSize>::~MemoryPool()
  78. noexcept
  79. {
  80. slot_pointer_ curr = currentBlock_;
  81. while (curr != nullptr) {
  82. slot_pointer_ prev = curr->next;
  83. operator delete(reinterpret_cast<void*>(curr));
  84. curr = prev;
  85. }
  86. }
  87. template <typename T, size_t BlockSize>
  88. inline typename MemoryPool<T, BlockSize>::pointer
  89. MemoryPool<T, BlockSize>::address(reference x)
  90. const noexcept
  91. {
  92. return &x;
  93. }
  94. template <typename T, size_t BlockSize>
  95. inline typename MemoryPool<T, BlockSize>::const_pointer
  96. MemoryPool<T, BlockSize>::address(const_reference x)
  97. const noexcept
  98. {
  99. return &x;
  100. }
  101. template <typename T, size_t BlockSize>
  102. void
  103. MemoryPool<T, BlockSize>::allocateBlock()
  104. {
  105. // Allocate space for the new block and store a pointer to the previous one
  106. data_pointer_ newBlock = reinterpret_cast<data_pointer_>
  107. (operator new(BlockSize));
  108. reinterpret_cast<slot_pointer_>(newBlock)->next = currentBlock_;
  109. currentBlock_ = reinterpret_cast<slot_pointer_>(newBlock);
  110. // Pad block body to staisfy the alignment requirements for elements
  111. data_pointer_ body = newBlock + sizeof(slot_pointer_);
  112. size_type bodyPadding = padPointer(body, alignof(slot_type_));
  113. currentSlot_ = reinterpret_cast<slot_pointer_>(body + bodyPadding);
  114. lastSlot_ = reinterpret_cast<slot_pointer_>
  115. (newBlock + BlockSize - sizeof(slot_type_) + 1);
  116. }
  117. template <typename T, size_t BlockSize>
  118. inline typename MemoryPool<T, BlockSize>::pointer
  119. MemoryPool<T, BlockSize>::allocate(size_type /*n*/, const_pointer /*hint*/)
  120. {
  121. if (freeSlots_ != nullptr) {
  122. pointer result = reinterpret_cast<pointer>(freeSlots_);
  123. freeSlots_ = freeSlots_->next;
  124. return result;
  125. }
  126. else {
  127. if (currentSlot_ >= lastSlot_)
  128. allocateBlock();
  129. return reinterpret_cast<pointer>(currentSlot_++);
  130. }
  131. }
  132. template <typename T, size_t BlockSize>
  133. inline void
  134. MemoryPool<T, BlockSize>::deallocate(pointer p, size_type /*n*/)
  135. {
  136. if (p != nullptr) {
  137. reinterpret_cast<slot_pointer_>(p)->next = freeSlots_;
  138. freeSlots_ = reinterpret_cast<slot_pointer_>(p);
  139. }
  140. }
  141. template <typename T, size_t BlockSize>
  142. inline typename MemoryPool<T, BlockSize>::size_type
  143. MemoryPool<T, BlockSize>::max_size()
  144. const noexcept
  145. {
  146. size_type maxBlocks = -1 / BlockSize;
  147. return (BlockSize - sizeof(data_pointer_)) / sizeof(slot_type_) * maxBlocks;
  148. }
  149. template <typename T, size_t BlockSize>
  150. template <class U, class... Args>
  151. inline void
  152. MemoryPool<T, BlockSize>::construct(U* p, Args&&... args)
  153. {
  154. new (p) U (std::forward<Args>(args)...);
  155. }
  156. template <typename T, size_t BlockSize>
  157. template <class U>
  158. inline void
  159. MemoryPool<T, BlockSize>::destroy(U* p)
  160. {
  161. p->~U();
  162. }
  163. template <typename T, size_t BlockSize>
  164. template <class... Args>
  165. inline typename MemoryPool<T, BlockSize>::pointer
  166. MemoryPool<T, BlockSize>::newElement(Args&&... args)
  167. {
  168. pointer result = allocate();
  169. construct<value_type>(result, std::forward<Args>(args)...);
  170. return result;
  171. }
  172. template <typename T, size_t BlockSize>
  173. inline void
  174. MemoryPool<T, BlockSize>::deleteElement(pointer p)
  175. {
  176. if (p != nullptr) {
  177. p->~value_type();
  178. deallocate(p);
  179. }
  180. }
  181. #endif // MEMORY_BLOCK_TCC