osrm-backend/include/util/pool_allocator.hpp

159 lines
4.4 KiB
C++
Raw Permalink Normal View History

2024-07-09 15:28:22 -04:00
#pragma once
#include <algorithm>
#include <array>
#include <bit>
#include <boost/assert.hpp>
2024-07-12 13:57:23 -04:00
#include <cstddef>
2024-07-09 15:28:22 -04:00
#include <cstdlib>
2024-07-12 14:13:40 -04:00
#include <memory>
2024-07-11 12:05:16 -04:00
#include <mutex>
2024-07-09 15:28:22 -04:00
#include <new>
#include <vector>
namespace osrm::util
{
2024-07-12 14:13:40 -04:00
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);
}
2024-07-12 13:57:23 -04:00
class MemoryPool
2024-07-10 16:01:25 -04:00
{
2024-07-12 14:13:40 -04:00
private:
2024-07-12 13:57:23 -04:00
constexpr static size_t MIN_CHUNK_SIZE_BYTES = 4096;
2024-07-11 17:21:48 -04:00
2024-07-12 14:13:40 -04:00
public:
2024-07-12 13:57:23 -04:00
static std::shared_ptr<MemoryPool> instance()
2024-07-09 16:27:21 -04:00
{
2024-07-12 13:57:23 -04:00
static thread_local std::shared_ptr<MemoryPool> instance;
2024-07-11 14:42:47 -04:00
if (!instance)
{
2024-07-12 13:57:23 -04:00
instance = std::shared_ptr<MemoryPool>(new MemoryPool());
2024-07-11 14:42:47 -04:00
}
2024-07-10 16:01:25 -04:00
return instance;
}
2024-07-09 16:27:21 -04:00
2024-07-12 14:13:40 -04:00
template <typename T> T *allocate(std::size_t items_count)
2024-07-09 15:28:22 -04:00
{
2024-07-12 14:13:40 -04:00
static_assert(alignof(T) <= alignof(std::max_align_t),
"Type is over-aligned for this allocator.");
2024-07-12 13:57:23 -04:00
size_t free_list_index = get_next_power_of_two_exponent(items_count * sizeof(T));
2024-07-09 15:28:22 -04:00
auto &free_list = free_lists_[free_list_index];
if (free_list.empty())
{
2024-07-12 13:57:23 -04:00
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));
2024-07-12 14:13:40 -04:00
// check if there is space in current memory chunk
2024-07-12 13:57:23 -04:00
if (current_chunk_left_bytes_ < block_size_in_bytes)
2024-07-09 15:28:22 -04:00
{
2024-07-12 13:57:23 -04:00
allocate_chunk(block_size_in_bytes);
2024-07-09 15:28:22 -04:00
}
2024-07-12 13:57:23 -04:00
free_list.push_back(current_chunk_ptr_);
current_chunk_left_bytes_ -= block_size_in_bytes;
current_chunk_ptr_ += block_size_in_bytes;
2024-07-09 15:28:22 -04:00
}
2024-07-12 16:10:05 -04:00
auto ptr = reinterpret_cast<T *>(free_list.back());
2024-07-09 15:28:22 -04:00
free_list.pop_back();
return ptr;
}
2024-07-12 14:13:40 -04:00
template <typename T> void deallocate(T *p, std::size_t n) noexcept
2024-07-09 15:28:22 -04:00
{
2024-07-11 17:21:48 -04:00
size_t free_list_index = get_next_power_of_two_exponent(n * sizeof(T));
2024-07-12 16:10:05 -04:00
// NOLINTNEXTLINE(bugprone-multi-level-implicit-pointer-conversion)
free_lists_[free_list_index].push_back(reinterpret_cast<void *>(p));
2024-07-09 15:28:22 -04:00
}
2024-07-12 13:57:23 -04:00
~MemoryPool()
2024-07-09 15:28:22 -04:00
{
2024-07-12 13:57:23 -04:00
for (auto chunk : chunks_)
2024-07-11 14:42:47 -04:00
{
2024-07-12 13:57:23 -04:00
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
std::free(chunk);
2024-07-11 14:42:47 -04:00
}
2024-07-09 15:28:22 -04:00
}
2024-07-11 17:21:48 -04:00
2024-07-12 14:13:40 -04:00
private:
2024-07-12 13:57:23 -04:00
MemoryPool() = default;
MemoryPool(const MemoryPool &) = delete;
MemoryPool &operator=(const MemoryPool &) = delete;
2024-07-10 16:01:25 -04:00
2024-07-12 13:57:23 -04:00
void allocate_chunk(size_t bytes)
{
auto chunk_size = std::max(bytes, MIN_CHUNK_SIZE_BYTES);
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
void *chunk = std::malloc(chunk_size);
if (!chunk)
2024-07-09 15:28:22 -04:00
{
throw std::bad_alloc();
}
2024-07-12 13:57:23 -04:00
chunks_.push_back(chunk);
2024-07-12 14:13:40 -04:00
current_chunk_ptr_ = static_cast<uint8_t *>(chunk);
2024-07-12 13:57:23 -04:00
current_chunk_left_bytes_ = chunk_size;
2024-07-09 15:28:22 -04:00
}
2024-07-12 14:13:40 -04:00
// 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
2024-07-12 13:57:23 -04:00
std::vector<void *> chunks_;
2024-07-12 14:13:40 -04:00
2024-07-12 13:57:23 -04:00
uint8_t *current_chunk_ptr_ = nullptr;
size_t current_chunk_left_bytes_ = 0;
2024-07-09 15:28:22 -04:00
};
2024-07-12 14:13:40 -04:00
template <typename T> class PoolAllocator
2024-07-10 16:01:25 -04:00
{
2024-07-12 14:13:40 -04:00
public:
2024-07-10 16:01:25 -04:00
using value_type = T;
2024-07-12 14:13:40 -04:00
PoolAllocator() noexcept : pool(MemoryPool::instance()){};
2024-07-10 16:01:25 -04:00
template <typename U>
2024-07-12 14:13:40 -04:00
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryPool::instance())
{
}
2024-07-10 16:01:25 -04:00
2024-07-12 14:13:40 -04:00
template <typename U> struct rebind
2024-07-10 16:01:25 -04:00
{
2024-07-11 16:32:41 -04:00
using other = PoolAllocator<U>;
2024-07-10 16:01:25 -04:00
};
2024-07-12 14:13:40 -04:00
T *allocate(std::size_t n) { return pool->allocate<T>(n); }
2024-07-10 16:01:25 -04:00
2024-07-12 14:13:40 -04:00
void deallocate(T *p, std::size_t n) noexcept { pool->deallocate<T>(p, n); }
2024-07-10 16:01:25 -04:00
PoolAllocator(const PoolAllocator &) = default;
PoolAllocator &operator=(const PoolAllocator &) = default;
PoolAllocator(PoolAllocator &&) noexcept = default;
PoolAllocator &operator=(PoolAllocator &&) noexcept = default;
2024-07-12 14:13:40 -04:00
private:
2024-07-12 16:10:05 -04:00
// using shared_ptr guarantees that memory pool won't be destroyed before all allocators using
// it (important if there are static instances of PoolAllocator)
2024-07-12 13:57:23 -04:00
std::shared_ptr<MemoryPool> pool;
2024-07-10 16:01:25 -04:00
};
2024-07-09 16:27:21 -04:00
template <typename T, typename U>
bool operator==(const PoolAllocator<T> &, const PoolAllocator<U> &)
{
return true;
}
template <typename T, typename U>
bool operator!=(const PoolAllocator<T> &, const PoolAllocator<U> &)
{
return false;
}
2024-07-09 15:28:22 -04:00
} // namespace osrm::util