Skip to content

Commit e53df3b

Browse files
break
1 parent 1436e96 commit e53df3b

File tree

6 files changed

+58
-53
lines changed

6 files changed

+58
-53
lines changed

Diff for: include/customizer/cell_customizer.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ class CellCustomizer
116116
const std::vector<bool> &allowed_nodes,
117117
CellMetric &metric) const
118118
{
119-
// std::cerr << "Customizing cells\n";
120119
const auto number_of_nodes = graph.GetNumberOfNodes();
121120
HeapPtr heaps([number_of_nodes]{
122121
return Heap{number_of_nodes};

Diff for: include/util/pool_allocator.hpp

+37-42
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,40 @@
1414
namespace osrm::util
1515
{
1616

17-
class MemoryManager
17+
class MemoryPool
1818
{
1919
private:
20-
constexpr static size_t MIN_ITEMS_IN_BLOCK = 1024;
20+
constexpr static size_t MIN_CHUNK_SIZE_BYTES = 4096;
2121

2222
public:
23-
static std::shared_ptr<MemoryManager> instance()
23+
static std::shared_ptr<MemoryPool> instance()
2424
{
25-
static thread_local std::shared_ptr<MemoryManager> instance;
25+
static thread_local std::shared_ptr<MemoryPool> instance;
2626
if (!instance)
2727
{
28-
instance = std::shared_ptr<MemoryManager>(new MemoryManager());
28+
instance = std::shared_ptr<MemoryPool>(new MemoryPool());
2929
}
3030
return instance;
3131
}
3232

33-
// TODO: alignment!!!
3433
template <typename T>
35-
T *allocate(std::size_t n)
34+
T *allocate(std::size_t items_count)
3635
{
37-
size_t free_list_index = get_next_power_of_two_exponent(n * sizeof(T));
36+
size_t free_list_index = get_next_power_of_two_exponent(items_count * sizeof(T));
3837
auto &free_list = free_lists_[free_list_index];
39-
const auto items_in_block = 1u << free_list_index;
4038
if (free_list.empty())
4139
{
42-
// Check if there is space in current block
43-
if (current_block_left_items_ < items_in_block)
40+
const auto block_size_in_bytes = 1u << free_list_index;
41+
42+
// Check if there is space in current memory chunk
43+
if (current_chunk_left_bytes_ < block_size_in_bytes)
4444
{
45-
allocate_block<T>(items_in_block);
45+
allocate_block<T>(block_size_in_bytes);
4646
}
4747

48-
free_list.push_back(current_block_ptr_);
49-
current_block_left_items_ -= items_in_block;
50-
current_block_ptr_ += items_in_block * sizeof(T);
48+
free_list.push_back(current_chunk_ptr_);
49+
current_chunk_left_bytes_ -= block_size_in_bytes;
50+
current_chunk_ptr_ += items_count * sizeof(T);
5151
}
5252
auto ptr = static_cast<T*>(free_list.back());
5353
free_list.pop_back();
@@ -61,19 +61,19 @@ class MemoryManager
6161
free_lists_[free_list_index].push_back(p);
6262
}
6363

64-
~MemoryManager()
64+
~MemoryPool()
6565
{
66-
std::cerr << "~MemoryManager()" << std::endl;
67-
for (auto block : blocks_)
66+
for (auto chunk : chunks_)
6867
{
69-
std::free(block);
68+
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
69+
std::free(chunk);
7070
}
7171
}
7272

7373
private:
74-
MemoryManager() = default;
75-
MemoryManager(const MemoryManager &) = delete;
76-
MemoryManager &operator=(const MemoryManager &) = delete;
74+
MemoryPool() = default;
75+
MemoryPool(const MemoryPool &) = delete;
76+
MemoryPool &operator=(const MemoryPool &) = delete;
7777

7878
size_t get_next_power_of_two_exponent(size_t n) const
7979
{
@@ -82,26 +82,25 @@ class MemoryManager
8282
}
8383

8484
template <typename T>
85-
void allocate_block(size_t items_in_block)
85+
void allocate_block(size_t bytes)
8686
{
87-
items_in_block = std::max(items_in_block, MIN_ITEMS_IN_BLOCK);
88-
89-
size_t block_size = items_in_block * sizeof(T);
90-
void *block = std::malloc(block_size);
91-
if (!block)
87+
auto chunk_size = std::max(bytes, MIN_CHUNK_SIZE_BYTES);
88+
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
89+
void *chunk = std::malloc(chunk_size);
90+
if (!chunk)
9291
{
9392
throw std::bad_alloc();
9493
}
95-
total_allocated_ += block_size;
96-
blocks_.push_back(block);
97-
current_block_ptr_ = static_cast<uint8_t*>(block);
98-
current_block_left_items_ = items_in_block;
94+
total_allocated_ += chunk_size;
95+
chunks_.push_back(chunk);
96+
current_chunk_ptr_ = static_cast<uint8_t*>(chunk);
97+
current_chunk_left_bytes_ = chunk_size;
9998
}
10099

101100
std::array<std::vector<void *>, 32> free_lists_;
102-
std::vector<void *> blocks_;
103-
uint8_t *current_block_ptr_ = nullptr;
104-
size_t current_block_left_items_ = 0;
101+
std::vector<void *> chunks_;
102+
uint8_t *current_chunk_ptr_ = nullptr;
103+
size_t current_chunk_left_bytes_ = 0;
105104

106105
size_t total_allocated_ = 0;
107106
};
@@ -112,10 +111,10 @@ class PoolAllocator
112111
public:
113112
using value_type = T;
114113

115-
PoolAllocator() noexcept : pool(MemoryManager::instance()) {};
114+
PoolAllocator() noexcept : pool(MemoryPool::instance()) {};
116115

117116
template <typename U>
118-
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryManager::instance()) {}
117+
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryPool::instance()) {}
119118

120119
template <typename U>
121120
struct rebind
@@ -133,17 +132,13 @@ class PoolAllocator
133132
pool->deallocate<T>(p, n);
134133
}
135134

136-
~PoolAllocator() {
137-
std::cerr << "~PoolAllocator()" << std::endl;
138-
}
139-
140135
PoolAllocator(const PoolAllocator &) = default;
141136
PoolAllocator &operator=(const PoolAllocator &) = default;
142137
PoolAllocator(PoolAllocator &&) noexcept = default;
143138
PoolAllocator &operator=(PoolAllocator &&) noexcept = default;
144139

145140
private:
146-
std::shared_ptr<MemoryManager> pool;
141+
std::shared_ptr<MemoryPool> pool;
147142
};
148143
template <typename T, typename U>
149144
bool operator==(const PoolAllocator<T> &, const PoolAllocator<U> &)

Diff for: include/util/query_heap.hpp

-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ template <typename NodeID, typename Key> class UnorderedMapStorage
3636
public:
3737
explicit UnorderedMapStorage(std::size_t) { nodes.rehash(1000); }
3838

39-
~UnorderedMapStorage() {
40-
std::cerr << "~UnorderedMapStorage()" << std::endl;
41-
}
42-
4339
Key &operator[](const NodeID node) { return nodes[node]; }
4440

4541
Key peek_index(const NodeID node) const

Diff for: src/engine/search_engine_data.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,10 @@ void SearchEngineData<CH>::InitializeOrClearManyToManyThreadLocalStorage(unsigne
118118
using MLD = routing_algorithms::mld::Algorithm;
119119
thread_local SearchEngineData<MLD>::SearchEngineHeapPtr SearchEngineData<MLD>::forward_heap_1;
120120
thread_local SearchEngineData<MLD>::SearchEngineHeapPtr SearchEngineData<MLD>::reverse_heap_1;
121-
<<<<<<< HEAD
122-
thread_local SearchEngineData<MLD>::MapMatchingHeapPtr SearchEngineData<MLD>::map_matching_forward_heap_1;
123-
thread_local SearchEngineData<MLD>::MapMatchingHeapPtr SearchEngineData<MLD>::map_matching_reverse_heap_1;
124-
=======
125121
thread_local SearchEngineData<MLD>::MapMatchingHeapPtr
126122
SearchEngineData<MLD>::map_matching_forward_heap_1;
127123
thread_local SearchEngineData<MLD>::MapMatchingHeapPtr
128124
SearchEngineData<MLD>::map_matching_reverse_heap_1;
129-
>>>>>>> master
130125
thread_local SearchEngineData<MLD>::ManyToManyHeapPtr SearchEngineData<MLD>::many_to_many_heap;
131126

132127
void SearchEngineData<MLD>::InitializeOrClearMapMatchingThreadLocalStorage(

Diff for: test/data/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ benchmark: data $(DATA_NAME).requests
6565
checksum:
6666
$(MD5SUM) $(DATA_NAME).osm.pbf $(DATA_NAME).poly > data.md5sum
6767

68-
.PHONY: clean checksum benchmark data
68+
.PHONY: clean checksum benchmark data

Diff for: unit_tests/util/pool_allocator.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,24 @@ BOOST_AUTO_TEST_CASE(unordered_map)
8585
BOOST_CHECK_EQUAL(map[2], 43);
8686
}
8787

88+
BOOST_AUTO_TEST_CASE(alignment)
89+
{
90+
PoolAllocator<char> pool_char;
91+
PoolAllocator<double> pool_double;
92+
93+
auto ptr_char = pool_char.allocate(1);
94+
auto ptr_double = pool_double.allocate(1);
95+
96+
std::cerr << "ptr_char: " << reinterpret_cast<uintptr_t>(ptr_char) << std::endl;
97+
std::cerr << "ptr_double: " << reinterpret_cast<uintptr_t>(ptr_double) << std::endl;
98+
99+
BOOST_CHECK_NE(ptr_double, nullptr);
100+
BOOST_CHECK_EQUAL(reinterpret_cast<uintptr_t>(ptr_double) % alignof(double), 0);
101+
BOOST_CHECK_NE(ptr_char, nullptr);
102+
BOOST_CHECK_EQUAL(reinterpret_cast<uintptr_t>(ptr_char) % alignof(char), 0);
103+
104+
pool_double.deallocate(ptr_double, 1);
105+
pool_char.deallocate(ptr_char, 1);
106+
}
107+
88108
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)