#pragma once #include #include #include #include #include #include #include #include #include namespace osrm::util { #if 1 template class PoolAllocator; template class MemoryManager { public: static MemoryManager &instance() { thread_local MemoryManager instance; return instance; } T *allocate(std::size_t n) { std::lock_guard lock(mutex_); size_t free_list_index = get_next_power_of_two_exponent(n); 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) { allocate_block(items_in_block); } free_list.push_back(current_block_ptr_); current_block_left_items_ -= items_in_block; current_block_ptr_ += items_in_block; } auto ptr = free_list.back(); free_list.pop_back(); return ptr; } void deallocate(T *p, std::size_t n) noexcept { std::lock_guard lock(mutex_); size_t free_list_index = get_next_power_of_two_exponent(n); free_lists_[free_list_index].push_back(p); } ~MemoryManager() { // std::lock_guard lock(mutex_); // for (auto block : blocks_) // { // std::free(block); // } } private: std::mutex mutex_; MemoryManager() = default; MemoryManager(const MemoryManager &) = delete; MemoryManager &operator=(const MemoryManager &) = delete; 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); } void allocate_block(size_t items_in_block) { items_in_block = std::max(items_in_block, MinItemsInBlock); size_t block_size = items_in_block * sizeof(T); T *block = static_cast(std::malloc(block_size)); if (!block) { throw std::bad_alloc(); } total_allocated_ += block_size; blocks_.push_back(block); current_block_ptr_ = block; current_block_left_items_ = items_in_block; } std::array, 32> free_lists_; std::vector blocks_; T *current_block_ptr_ = nullptr; size_t current_block_left_items_ = 0; size_t total_allocated_ = 0; }; template class PoolAllocator { public: using value_type = T; PoolAllocator() noexcept = default; template PoolAllocator(const PoolAllocator &) noexcept {} template struct rebind { using other = PoolAllocator; }; T *allocate(std::size_t n) { return MemoryManager::instance().allocate(n); } void deallocate(T *p, std::size_t n) noexcept { MemoryManager::instance().deallocate(p, n); } ~PoolAllocator() = default; PoolAllocator(const PoolAllocator &) = default; PoolAllocator &operator=(const PoolAllocator &) = default; PoolAllocator(PoolAllocator &&) noexcept = default; PoolAllocator &operator=(PoolAllocator &&) noexcept = default; private: static 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); } }; template bool operator==(const PoolAllocator &, const PoolAllocator &) { return true; } template bool operator!=(const PoolAllocator &, const PoolAllocator &) { return false; } #else template using PoolAllocator = std::allocator; #endif } // namespace osrm::util