This commit is contained in:
Siarhei Fedartsou
2024-07-12 20:13:40 +02:00
parent 81d128b100
commit fdd1ca05df
5 changed files with 92 additions and 63 deletions
+37 -46
View File
@@ -6,21 +6,31 @@
#include <boost/assert.hpp>
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <mutex>
#include <new>
#include <vector>
#include <memory>
namespace osrm::util
{
inline size_t align_up(size_t n, size_t alignment)
{
return (n + alignment - 1) & ~(alignment - 1);
}
inline size_t get_next_power_of_two_exponent(size_t n)
{
BOOST_ASSERT(n > 0);
return (sizeof(size_t) * 8) - std::countl_zero(n - 1);
}
class MemoryPool
{
private:
private:
constexpr static size_t MIN_CHUNK_SIZE_BYTES = 4096;
public:
public:
static std::shared_ptr<MemoryPool> instance()
{
static thread_local std::shared_ptr<MemoryPool> instance;
@@ -31,10 +41,10 @@ public:
return instance;
}
template <typename T>
T *allocate(std::size_t items_count)
template <typename T> T *allocate(std::size_t items_count)
{
static_assert(alignof(T) <= alignof(std::max_align_t), "Type is over-aligned for this allocator.");
static_assert(alignof(T) <= alignof(std::max_align_t),
"Type is over-aligned for this allocator.");
size_t free_list_index = get_next_power_of_two_exponent(items_count * sizeof(T));
auto &free_list = free_lists_[free_list_index];
@@ -42,7 +52,7 @@ public:
{
size_t block_size_in_bytes = 1u << free_list_index;
block_size_in_bytes = align_up(block_size_in_bytes, alignof(std::max_align_t));
// Check if there is space in current memory chunk
// check if there is space in current memory chunk
if (current_chunk_left_bytes_ < block_size_in_bytes)
{
allocate_chunk(block_size_in_bytes);
@@ -52,13 +62,12 @@ public:
current_chunk_left_bytes_ -= block_size_in_bytes;
current_chunk_ptr_ += block_size_in_bytes;
}
auto ptr = static_cast<T*>(free_list.back());
auto ptr = static_cast<T *>(free_list.back());
free_list.pop_back();
return ptr;
}
template <typename T>
void deallocate(T *p, std::size_t n) noexcept
template <typename T> void deallocate(T *p, std::size_t n) noexcept
{
size_t free_list_index = get_next_power_of_two_exponent(n * sizeof(T));
free_lists_[free_list_index].push_back(p);
@@ -73,27 +82,11 @@ public:
}
}
private:
private:
MemoryPool() = default;
MemoryPool(const MemoryPool &) = delete;
MemoryPool &operator=(const MemoryPool &) = delete;
inline size_t get_next_power_of_two_exponent(size_t n) const
{
BOOST_ASSERT(n > 0);
return (sizeof(size_t) * 8) - std::countl_zero(n - 1);
}
inline size_t align_up(size_t n, size_t alignment)
{
return (n + alignment - 1) & ~(alignment - 1);
}
inline void* align_pointer(void* ptr, size_t alignment)
{
return reinterpret_cast<void*>(align_up(reinterpret_cast<uintptr_t>(ptr), alignment));
}
void allocate_chunk(size_t bytes)
{
auto chunk_size = std::max(bytes, MIN_CHUNK_SIZE_BYTES);
@@ -104,49 +97,47 @@ private:
throw std::bad_alloc();
}
chunks_.push_back(chunk);
current_chunk_ptr_ = static_cast<uint8_t*>(chunk);
current_chunk_ptr_ = static_cast<uint8_t *>(chunk);
current_chunk_left_bytes_ = chunk_size;
}
std::array<std::vector<void *>, 32> free_lists_;
// we have 64 free lists, one for each possible power of two
std::array<std::vector<void *>, sizeof(std::size_t) * 8> free_lists_;
// list of allocated memory chunks, we don't free them until the pool is destroyed
std::vector<void *> chunks_;
uint8_t *current_chunk_ptr_ = nullptr;
size_t current_chunk_left_bytes_ = 0;
};
template <typename T>
class PoolAllocator
template <typename T> class PoolAllocator
{
public:
public:
using value_type = T;
PoolAllocator() noexcept : pool(MemoryPool::instance()) {};
PoolAllocator() noexcept : pool(MemoryPool::instance()){};
template <typename U>
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryPool::instance()) {}
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryPool::instance())
{
}
template <typename U>
struct rebind
template <typename U> struct rebind
{
using other = PoolAllocator<U>;
};
T *allocate(std::size_t n)
{
return pool->allocate<T>(n);
}
T *allocate(std::size_t n) { return pool->allocate<T>(n); }
void deallocate(T *p, std::size_t n) noexcept
{
pool->deallocate<T>(p, n);
}
void deallocate(T *p, std::size_t n) noexcept { pool->deallocate<T>(p, n); }
PoolAllocator(const PoolAllocator &) = default;
PoolAllocator &operator=(const PoolAllocator &) = default;
PoolAllocator(PoolAllocator &&) noexcept = default;
PoolAllocator &operator=(PoolAllocator &&) noexcept = default;
private:
private:
std::shared_ptr<MemoryPool> pool;
};
template <typename T, typename U>
+2 -4
View File
@@ -148,8 +148,6 @@ class QueryHeap
}
};
using HeapContainer = boost::heap::d_ary_heap<HeapData,
boost::heap::arity<4>,
boost::heap::mutable_<true>,
@@ -169,8 +167,8 @@ class QueryHeap
Data data;
};
QueryHeap(const QueryHeap& other) = delete;
QueryHeap(QueryHeap&& other) = delete;
QueryHeap(const QueryHeap &other) = delete;
QueryHeap(QueryHeap &&other) = delete;
template <typename... StorageArgs> explicit QueryHeap(StorageArgs... args) : node_index(args...)
{