|
12 | 12 | namespace osrm::util |
13 | 13 | { |
14 | 14 |
|
15 | | -template <typename T> class PoolAllocator |
| 15 | +#if 1 |
| 16 | +template <typename T, size_t MinItemsInBlock = 1024> class PoolAllocator |
16 | 17 | { |
17 | 18 | public: |
18 | 19 | using value_type = T; |
19 | 20 |
|
| 21 | + PoolAllocator() noexcept = default; |
| 22 | + |
| 23 | + template <typename U> PoolAllocator(const PoolAllocator<U> &) noexcept {} |
| 24 | + |
| 25 | + template <typename U> struct rebind |
| 26 | + { |
| 27 | + using other = PoolAllocator<U, MinItemsInBlock>; |
| 28 | + }; |
| 29 | + |
20 | 30 | T *allocate(std::size_t n) |
21 | 31 | { |
22 | 32 | size_t free_list_index = get_next_power_of_two_exponent(n); |
@@ -56,28 +66,46 @@ template <typename T> class PoolAllocator |
56 | 66 | private: |
57 | 67 | size_t get_next_power_of_two_exponent(size_t n) const |
58 | 68 | { |
59 | | - return std::countr_zero(std::bit_ceil(n)); |
| 69 | + BOOST_ASSERT(n > 0); |
| 70 | + return (sizeof(size_t) * 8) - std::countl_zero(n - 1); |
60 | 71 | } |
61 | 72 |
|
62 | 73 | void allocate_block(size_t items_in_block) |
63 | 74 | { |
64 | | - size_t block_size = std::max(items_in_block, (size_t)256) * sizeof(T); |
65 | | - T *block = static_cast<T *>(std::malloc(block_size)); |
| 75 | + items_in_block = std::max(items_in_block, MinItemsInBlock); |
| 76 | + size_t block_size = items_in_block * sizeof(T); |
| 77 | + T *block = static_cast<T *>(std::aligned_alloc(alignof(T), block_size)); |
66 | 78 | if (!block) |
67 | 79 | { |
68 | 80 | throw std::bad_alloc(); |
69 | 81 | } |
| 82 | + total_allocated_ += block_size; |
70 | 83 | blocks_.push_back(block); |
71 | | - current_block_ = block; |
72 | 84 | current_block_ptr_ = block; |
73 | | - current_block_left_items_ = block_size / sizeof(T); |
| 85 | + current_block_left_items_ = items_in_block; |
74 | 86 | } |
75 | 87 |
|
76 | 88 | std::array<std::vector<T *>, 32> free_lists_; |
77 | 89 | std::vector<T *> blocks_; |
78 | | - T *current_block_ = nullptr; |
79 | 90 | T *current_block_ptr_ = nullptr; |
80 | 91 | size_t current_block_left_items_ = 0; |
| 92 | + |
| 93 | + size_t total_allocated_ = 0; |
81 | 94 | }; |
82 | 95 |
|
| 96 | +template <typename T, typename U> |
| 97 | +bool operator==(const PoolAllocator<T> &, const PoolAllocator<U> &) |
| 98 | +{ |
| 99 | + return true; |
| 100 | +} |
| 101 | + |
| 102 | +template <typename T, typename U> |
| 103 | +bool operator!=(const PoolAllocator<T> &, const PoolAllocator<U> &) |
| 104 | +{ |
| 105 | + return false; |
| 106 | +} |
| 107 | + |
| 108 | +#else |
| 109 | +template <typename T> using PoolAllocator = std::allocator<T>; |
| 110 | +#endif |
83 | 111 | } // namespace osrm::util |
0 commit comments