#ifndef LF_POOL_ALLOCATOR #define LF_POOL_ALLOCATOR #include #include #include namespace lfpAlloc { template class lfpAllocator { public: using value_type = T; using pointer = T*; using const_pointer = const T*; using reference = T&; using const_reference = T const&; template struct rebind { typedef lfpAllocator other; }; lfpAllocator() {} template lfpAllocator(lfpAllocator&&) noexcept {} template lfpAllocator(const lfpAllocator&) noexcept {} T* allocate(std::size_t count) { if (sizeof(T) * count <= alignof(std::max_align_t) * NumPools - sizeof(void*)) { return reinterpret_cast( dispatcher_.allocate(sizeof(T) * count)); } else { return new T[count]; } } void deallocate(T* p, std::size_t count) noexcept { if (sizeof(T) * count <= alignof(std::max_align_t) * NumPools - sizeof(void*)) { dispatcher_.deallocate(p, sizeof(T) * count); } else { delete[] p; } } // Should not be required, but allocator_traits is not complete in // gcc 4.9.1 template void destroy(U* p) { p->~U(); } template void construct(U* p, Args&&... args) { new (p) U(std::forward(args)...); } template friend bool operator==(const lfpAllocator&, const lfpAllocator&) noexcept; template friend class lfpAllocator; private: static PoolDispatcher dispatcher_; }; template PoolDispatcher lfpAllocator::dispatcher_; template inline bool operator==(const lfpAllocator&, const lfpAllocator&) noexcept { return N == M; } template inline bool operator!=(const lfpAllocator& left, const lfpAllocator& right) noexcept { return !(left == right); } } #endif