#ifndef LF_POOL_DISPATCHER #define LF_POOL_DISPATCHER #include #include #include #include #ifndef LFP_ALLOCATIONS_PER_CHUNK #define LFP_ALLOCATIONS_PER_CHUNK 64 * 100 #endif namespace lfpAlloc { namespace detail { template struct Pools : Pools {}; template struct Pools<0, Size...> { using type = std::tuple...>; }; } template class PoolDispatcher { public: void* allocate(std::size_t size) { return dispatchAllocate<0>(size); } void deallocate(void* p, std::size_t size) noexcept { dispatchDeallocate<0>(p, size); } private: thread_local static typename detail::Pools::type pools_; static_assert(NumPools > 0, "Invalid number of pools"); template typename std::enable_if < Index::type dispatchAllocate(std::size_t const& requestSize) { if (requestSize <= std::get(pools_).CellSize) { return std::get(pools_).allocate(); } else { return dispatchAllocate(requestSize); } } template typename std::enable_if::type dispatchAllocate(std::size_t const&) { assert(false && "Invalid allocation size."); return nullptr; } template typename std::enable_if < Index::type dispatchDeallocate(void* p, std::size_t const& requestSize) noexcept { if (requestSize <= std::get(pools_).CellSize) { std::get(pools_).deallocate(p); } else { dispatchDeallocate(p, requestSize); } } template typename std::enable_if::type dispatchDeallocate(void*, std::size_t const&) noexcept { assert(false && "Invalid deallocation size."); } }; template thread_local typename detail::Pools::type PoolDispatcher::pools_; } #endif