pool.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_POOL_H
  23. #define MEMORY_POOL_H
  24. #include <climits>
  25. #include <cstddef>
  26. template <typename T, size_t BlockSize = 4096>
  27. class MemoryPool
  28. {
  29. public:
  30. /* Member types */
  31. typedef T value_type;
  32. typedef T* pointer;
  33. typedef T& reference;
  34. typedef const T* const_pointer;
  35. typedef const T& const_reference;
  36. typedef size_t size_type;
  37. typedef ptrdiff_t difference_type;
  38. typedef std::false_type propagate_on_container_copy_assignment;
  39. typedef std::true_type propagate_on_container_move_assignment;
  40. typedef std::true_type propagate_on_container_swap;
  41. template <typename U> struct rebind {
  42. typedef MemoryPool<U> other;
  43. };
  44. /* Member functions */
  45. MemoryPool() noexcept;
  46. MemoryPool(const MemoryPool& memoryPool) noexcept;
  47. MemoryPool(MemoryPool&& memoryPool) noexcept;
  48. template <class U> MemoryPool(const MemoryPool<U>& memoryPool) noexcept;
  49. ~MemoryPool() noexcept;
  50. MemoryPool& operator=(const MemoryPool& memoryPool) = delete;
  51. MemoryPool& operator=(MemoryPool&& memoryPool) noexcept;
  52. pointer address(reference x) const noexcept;
  53. const_pointer address(const_reference x) const noexcept;
  54. // Can only allocate one object at a time. n and hint are ignored
  55. pointer allocate(size_type n = 1, const_pointer hint = 0);
  56. void deallocate(pointer p, size_type n = 1);
  57. size_type max_size() const noexcept;
  58. template <class U, class... Args> void construct(U* p, Args&&... args);
  59. template <class U> void destroy(U* p);
  60. template <class... Args> pointer newElement(Args&&... args);
  61. void deleteElement(pointer p);
  62. private:
  63. union Slot_ {
  64. value_type element;
  65. Slot_* next;
  66. };
  67. typedef char* data_pointer_;
  68. typedef Slot_ slot_type_;
  69. typedef Slot_* slot_pointer_;
  70. slot_pointer_ currentBlock_;
  71. slot_pointer_ currentSlot_;
  72. slot_pointer_ lastSlot_;
  73. slot_pointer_ freeSlots_;
  74. size_type padPointer(data_pointer_ p, size_type align) const noexcept;
  75. void allocateBlock();
  76. static_assert(BlockSize >= 2 * sizeof(slot_type_), "BlockSize too small.");
  77. };
  78. #include "pool.inl"
  79. #endif // MEMORY_POOL_H