@@ -13,19 +13,18 @@ namespace osrm::util
13
13
{
14
14
15
15
#if 1
16
- template <typename T, size_t MinItemsInBlock = 1024 > class PoolAllocator
17
- {
18
- public:
19
- using value_type = T;
20
-
21
- PoolAllocator () noexcept = default ;
16
+ template <typename T, size_t MinItemsInBlock = 1024 >
17
+ class PoolAllocator ;
22
18
23
- template <typename U> PoolAllocator (const PoolAllocator<U> &) noexcept {}
24
-
25
- template <typename U> struct rebind
19
+ template <typename T, size_t MinItemsInBlock = 1024 >
20
+ class MemoryManager
21
+ {
22
+ public:
23
+ static MemoryManager &instance ()
26
24
{
27
- using other = PoolAllocator<U, MinItemsInBlock>;
28
- };
25
+ thread_local MemoryManager instance;
26
+ return instance;
27
+ }
29
28
30
29
T *allocate (std::size_t n)
31
30
{
@@ -55,52 +54,19 @@ template <typename T, size_t MinItemsInBlock = 1024> class PoolAllocator
55
54
free_lists_[free_list_index].push_back (p);
56
55
}
57
56
58
- ~PoolAllocator ()
57
+ ~MemoryManager ()
59
58
{
60
59
for (auto block : blocks_)
61
60
{
62
61
std::free (block);
63
62
}
64
63
}
65
64
66
- PoolAllocator (const PoolAllocator &) = delete ;
67
- PoolAllocator &operator =(const PoolAllocator &) = delete ;
68
-
69
- PoolAllocator (PoolAllocator &&other) noexcept
70
- : free_lists_(std::move(other.free_lists_)),
71
- blocks_ (std::move(other.blocks_)),
72
- current_block_ptr_(other.current_block_ptr_),
73
- current_block_left_items_(other.current_block_left_items_),
74
- total_allocated_(other.total_allocated_)
75
- {
76
- other.current_block_ptr_ = nullptr ;
77
- other.current_block_left_items_ = 0 ;
78
- other.total_allocated_ = 0 ;
79
- }
80
-
81
- PoolAllocator &operator =(PoolAllocator &&other) noexcept
82
- {
83
- if (this != &other)
84
- {
85
- for (auto block : blocks_)
86
- {
87
- std::free (block);
88
- }
89
-
90
- free_lists_ = std::move (other.free_lists_ );
91
- blocks_ = std::move (other.blocks_ );
92
- current_block_ptr_ = other.current_block_ptr_ ;
93
- current_block_left_items_ = other.current_block_left_items_ ;
94
- total_allocated_ = other.total_allocated_ ;
95
-
96
- other.current_block_ptr_ = nullptr ;
97
- other.current_block_left_items_ = 0 ;
98
- other.total_allocated_ = 0 ;
99
- }
100
- return *this ;
101
- }
102
-
103
65
private:
66
+ MemoryManager () = default ;
67
+ MemoryManager (const MemoryManager &) = delete ;
68
+ MemoryManager &operator =(const MemoryManager &) = delete ;
69
+
104
70
size_t get_next_power_of_two_exponent (size_t n) const
105
71
{
106
72
BOOST_ASSERT (n > 0 );
@@ -110,7 +76,7 @@ template <typename T, size_t MinItemsInBlock = 1024> class PoolAllocator
110
76
void allocate_block (size_t items_in_block)
111
77
{
112
78
items_in_block = std::max (items_in_block, MinItemsInBlock);
113
-
79
+
114
80
size_t block_size = items_in_block * sizeof (T);
115
81
T *block = static_cast <T *>(std::malloc (block_size));
116
82
if (!block)
@@ -131,6 +97,48 @@ template <typename T, size_t MinItemsInBlock = 1024> class PoolAllocator
131
97
size_t total_allocated_ = 0 ;
132
98
};
133
99
100
+ template <typename T, size_t MinItemsInBlock>
101
+ class PoolAllocator
102
+ {
103
+ public:
104
+ using value_type = T;
105
+
106
+ PoolAllocator () noexcept = default ;
107
+
108
+ template <typename U>
109
+ PoolAllocator (const PoolAllocator<U> &) noexcept {}
110
+
111
+ template <typename U>
112
+ struct rebind
113
+ {
114
+ using other = PoolAllocator<U, MinItemsInBlock>;
115
+ };
116
+
117
+ T *allocate (std::size_t n)
118
+ {
119
+ return MemoryManager<T, MinItemsInBlock>::instance ().allocate (n);
120
+ }
121
+
122
+ void deallocate (T *p, std::size_t n) noexcept
123
+ {
124
+ MemoryManager<T, MinItemsInBlock>::instance ().deallocate (p, n);
125
+ }
126
+
127
+ ~PoolAllocator () = default ;
128
+
129
+ PoolAllocator (const PoolAllocator &) = default ;
130
+ PoolAllocator &operator =(const PoolAllocator &) = default ;
131
+ PoolAllocator (PoolAllocator &&) noexcept = default ;
132
+ PoolAllocator &operator =(PoolAllocator &&) noexcept = default ;
133
+
134
+ private:
135
+ static size_t get_next_power_of_two_exponent (size_t n)
136
+ {
137
+ BOOST_ASSERT (n > 0 );
138
+ return (sizeof (size_t ) * 8 ) - std::countl_zero (n - 1 );
139
+ }
140
+ };
141
+
134
142
template <typename T, typename U>
135
143
bool operator ==(const PoolAllocator<T> &, const PoolAllocator<U> &)
136
144
{
0 commit comments