Skip to content

Commit

Permalink
break
Browse files Browse the repository at this point in the history
  • Loading branch information
SiarheiFedartsou committed Jul 12, 2024
1 parent 1436e96 commit e53df3b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 53 deletions.
1 change: 0 additions & 1 deletion include/customizer/cell_customizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class CellCustomizer
const std::vector<bool> &allowed_nodes,
CellMetric &metric) const
{
// std::cerr << "Customizing cells\n";
const auto number_of_nodes = graph.GetNumberOfNodes();
HeapPtr heaps([number_of_nodes]{
return Heap{number_of_nodes};
Expand Down
79 changes: 37 additions & 42 deletions include/util/pool_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,40 @@
namespace osrm::util
{

class MemoryManager
class MemoryPool
{
private:
constexpr static size_t MIN_ITEMS_IN_BLOCK = 1024;
constexpr static size_t MIN_CHUNK_SIZE_BYTES = 4096;

public:
static std::shared_ptr<MemoryManager> instance()
static std::shared_ptr<MemoryPool> instance()
{
static thread_local std::shared_ptr<MemoryManager> instance;
static thread_local std::shared_ptr<MemoryPool> instance;
if (!instance)
{
instance = std::shared_ptr<MemoryManager>(new MemoryManager());
instance = std::shared_ptr<MemoryPool>(new MemoryPool());
}
return instance;
}

// TODO: alignment!!!
template <typename T>
T *allocate(std::size_t n)
T *allocate(std::size_t items_count)
{
size_t free_list_index = get_next_power_of_two_exponent(n * sizeof(T));
size_t free_list_index = get_next_power_of_two_exponent(items_count * sizeof(T));
auto &free_list = free_lists_[free_list_index];
const auto items_in_block = 1u << free_list_index;
if (free_list.empty())
{
// Check if there is space in current block
if (current_block_left_items_ < items_in_block)
const auto block_size_in_bytes = 1u << free_list_index;

// Check if there is space in current memory chunk
if (current_chunk_left_bytes_ < block_size_in_bytes)
{
allocate_block<T>(items_in_block);
allocate_block<T>(block_size_in_bytes);
}

free_list.push_back(current_block_ptr_);
current_block_left_items_ -= items_in_block;
current_block_ptr_ += items_in_block * sizeof(T);
free_list.push_back(current_chunk_ptr_);
current_chunk_left_bytes_ -= block_size_in_bytes;
current_chunk_ptr_ += items_count * sizeof(T);
}
auto ptr = static_cast<T*>(free_list.back());
free_list.pop_back();
Expand All @@ -61,19 +61,19 @@ class MemoryManager
free_lists_[free_list_index].push_back(p);
}

~MemoryManager()
~MemoryPool()
{
std::cerr << "~MemoryManager()" << std::endl;
for (auto block : blocks_)
for (auto chunk : chunks_)
{
std::free(block);
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
std::free(chunk);
}
}

private:
MemoryManager() = default;
MemoryManager(const MemoryManager &) = delete;
MemoryManager &operator=(const MemoryManager &) = delete;
MemoryPool() = default;
MemoryPool(const MemoryPool &) = delete;
MemoryPool &operator=(const MemoryPool &) = delete;

size_t get_next_power_of_two_exponent(size_t n) const
{
Expand All @@ -82,26 +82,25 @@ class MemoryManager
}

template <typename T>
void allocate_block(size_t items_in_block)
void allocate_block(size_t bytes)
{
items_in_block = std::max(items_in_block, MIN_ITEMS_IN_BLOCK);

size_t block_size = items_in_block * sizeof(T);
void *block = std::malloc(block_size);
if (!block)
auto chunk_size = std::max(bytes, MIN_CHUNK_SIZE_BYTES);
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
void *chunk = std::malloc(chunk_size);
if (!chunk)
{
throw std::bad_alloc();
}
total_allocated_ += block_size;
blocks_.push_back(block);
current_block_ptr_ = static_cast<uint8_t*>(block);
current_block_left_items_ = items_in_block;
total_allocated_ += chunk_size;
chunks_.push_back(chunk);
current_chunk_ptr_ = static_cast<uint8_t*>(chunk);
current_chunk_left_bytes_ = chunk_size;
}

std::array<std::vector<void *>, 32> free_lists_;
std::vector<void *> blocks_;
uint8_t *current_block_ptr_ = nullptr;
size_t current_block_left_items_ = 0;
std::vector<void *> chunks_;
uint8_t *current_chunk_ptr_ = nullptr;
size_t current_chunk_left_bytes_ = 0;

size_t total_allocated_ = 0;
};
Expand All @@ -112,10 +111,10 @@ class PoolAllocator
public:
using value_type = T;

PoolAllocator() noexcept : pool(MemoryManager::instance()) {};
PoolAllocator() noexcept : pool(MemoryPool::instance()) {};

template <typename U>
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryManager::instance()) {}
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryPool::instance()) {}

template <typename U>
struct rebind
Expand All @@ -133,17 +132,13 @@ class PoolAllocator
pool->deallocate<T>(p, n);
}

~PoolAllocator() {
std::cerr << "~PoolAllocator()" << std::endl;
}

PoolAllocator(const PoolAllocator &) = default;
PoolAllocator &operator=(const PoolAllocator &) = default;
PoolAllocator(PoolAllocator &&) noexcept = default;
PoolAllocator &operator=(PoolAllocator &&) noexcept = default;

private:
std::shared_ptr<MemoryManager> pool;
std::shared_ptr<MemoryPool> pool;
};
template <typename T, typename U>
bool operator==(const PoolAllocator<T> &, const PoolAllocator<U> &)
Expand Down
4 changes: 0 additions & 4 deletions include/util/query_heap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ template <typename NodeID, typename Key> class UnorderedMapStorage
public:
explicit UnorderedMapStorage(std::size_t) { nodes.rehash(1000); }

~UnorderedMapStorage() {
std::cerr << "~UnorderedMapStorage()" << std::endl;
}

Key &operator[](const NodeID node) { return nodes[node]; }

Key peek_index(const NodeID node) const
Expand Down
5 changes: 0 additions & 5 deletions src/engine/search_engine_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,10 @@ void SearchEngineData<CH>::InitializeOrClearManyToManyThreadLocalStorage(unsigne
using MLD = routing_algorithms::mld::Algorithm;
thread_local SearchEngineData<MLD>::SearchEngineHeapPtr SearchEngineData<MLD>::forward_heap_1;
thread_local SearchEngineData<MLD>::SearchEngineHeapPtr SearchEngineData<MLD>::reverse_heap_1;
<<<<<<< HEAD
thread_local SearchEngineData<MLD>::MapMatchingHeapPtr SearchEngineData<MLD>::map_matching_forward_heap_1;
thread_local SearchEngineData<MLD>::MapMatchingHeapPtr SearchEngineData<MLD>::map_matching_reverse_heap_1;
=======
thread_local SearchEngineData<MLD>::MapMatchingHeapPtr
SearchEngineData<MLD>::map_matching_forward_heap_1;
thread_local SearchEngineData<MLD>::MapMatchingHeapPtr
SearchEngineData<MLD>::map_matching_reverse_heap_1;
>>>>>>> master
thread_local SearchEngineData<MLD>::ManyToManyHeapPtr SearchEngineData<MLD>::many_to_many_heap;

void SearchEngineData<MLD>::InitializeOrClearMapMatchingThreadLocalStorage(
Expand Down
2 changes: 1 addition & 1 deletion test/data/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ benchmark: data $(DATA_NAME).requests
checksum:
$(MD5SUM) $(DATA_NAME).osm.pbf $(DATA_NAME).poly > data.md5sum

.PHONY: clean checksum benchmark data
.PHONY: clean checksum benchmark data
20 changes: 20 additions & 0 deletions unit_tests/util/pool_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,24 @@ BOOST_AUTO_TEST_CASE(unordered_map)
BOOST_CHECK_EQUAL(map[2], 43);
}

BOOST_AUTO_TEST_CASE(alignment)
{
PoolAllocator<char> pool_char;
PoolAllocator<double> pool_double;

auto ptr_char = pool_char.allocate(1);
auto ptr_double = pool_double.allocate(1);

std::cerr << "ptr_char: " << reinterpret_cast<uintptr_t>(ptr_char) << std::endl;
std::cerr << "ptr_double: " << reinterpret_cast<uintptr_t>(ptr_double) << std::endl;

BOOST_CHECK_NE(ptr_double, nullptr);
BOOST_CHECK_EQUAL(reinterpret_cast<uintptr_t>(ptr_double) % alignof(double), 0);
BOOST_CHECK_NE(ptr_char, nullptr);
BOOST_CHECK_EQUAL(reinterpret_cast<uintptr_t>(ptr_char) % alignof(char), 0);

pool_double.deallocate(ptr_double, 1);
pool_char.deallocate(ptr_char, 1);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit e53df3b

Please sign in to comment.