14
14
namespace osrm ::util
15
15
{
16
16
17
- #if 1
18
-
19
- template <typename T, size_t MinItemsInBlock = 1024 >
17
+ template <typename T>
20
18
class PoolAllocator ;
21
19
22
- template <typename T, size_t MinItemsInBlock = 1024 >
20
+ template <typename T>
23
21
class MemoryManager
24
22
{
23
+ private:
24
+ constexpr static size_t MIN_ITEMS_IN_BLOCK = 1024 ;
25
25
public:
26
26
static std::shared_ptr<MemoryManager> instance ()
27
27
{
@@ -82,7 +82,7 @@ class MemoryManager
82
82
83
83
void allocate_block (size_t items_in_block)
84
84
{
85
- items_in_block = std::max (items_in_block, MinItemsInBlock );
85
+ items_in_block = std::max (items_in_block, MIN_ITEMS_IN_BLOCK );
86
86
87
87
size_t block_size = items_in_block * sizeof (T);
88
88
T *block = static_cast <T *>(std::malloc (block_size));
@@ -104,21 +104,21 @@ class MemoryManager
104
104
size_t total_allocated_ = 0 ;
105
105
};
106
106
107
- template <typename T, size_t MinItemsInBlock >
107
+ template <typename T>
108
108
class PoolAllocator
109
109
{
110
110
public:
111
111
using value_type = T;
112
112
113
- PoolAllocator () noexcept : pool(MemoryManager<T, MinItemsInBlock >::instance()) {};
113
+ PoolAllocator () noexcept : pool(MemoryManager<T>::instance()) {};
114
114
115
115
template <typename U>
116
- PoolAllocator (const PoolAllocator<U> &) noexcept : pool(MemoryManager<T, MinItemsInBlock >::instance()) {}
116
+ PoolAllocator (const PoolAllocator<U> &) noexcept : pool(MemoryManager<T>::instance()) {}
117
117
118
118
template <typename U>
119
119
struct rebind
120
120
{
121
- using other = PoolAllocator<U, MinItemsInBlock >;
121
+ using other = PoolAllocator<U>;
122
122
};
123
123
124
124
T *allocate (std::size_t n)
@@ -139,13 +139,7 @@ class PoolAllocator
139
139
PoolAllocator &operator =(PoolAllocator &&) noexcept = default ;
140
140
141
141
private:
142
- std::shared_ptr<MemoryManager<T, MinItemsInBlock>> pool;
143
-
144
- static size_t get_next_power_of_two_exponent (size_t n)
145
- {
146
- BOOST_ASSERT (n > 0 );
147
- return (sizeof (size_t ) * 8 ) - std::countl_zero (n - 1 );
148
- }
142
+ std::shared_ptr<MemoryManager<T>> pool;
149
143
};
150
144
151
145
template <typename T, typename U>
@@ -160,7 +154,4 @@ bool operator!=(const PoolAllocator<T> &, const PoolAllocator<U> &)
160
154
return false ;
161
155
}
162
156
163
- #else
164
- template <typename T> using PoolAllocator = std::allocator<T>;
165
- #endif
166
157
} // namespace osrm::util
0 commit comments