/* Copyright (C) 2014 InfiniDB, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /****************************************************************************************** * $Id$ * ******************************************************************************************/ /* Makes PoolAllocator STL-compliant */ #include #include #include "poolallocator.h" #undef min #undef max #ifndef STLPOOLALLOCATOR_H_ #define STLPOOLALLOCATOR_H_ namespace utils { /* If using the pool allocator with a boost smart ptr, use an instance of this as the deleter. */ struct BoostPoolDeallocator { inline void operator()(void *ptr) { }; }; /* This is an STL-compliant wrapper for PoolAllocator + an optimization for containers * that aren't entirely node based (ex: vectors and hash tables) */ template class STLPoolAllocator { public: typedef size_t size_type; typedef ptrdiff_t difference_type; typedef T *pointer; typedef const T *const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; template struct rebind { typedef STLPoolAllocator other; }; STLPoolAllocator() throw(); STLPoolAllocator(const STLPoolAllocator &) throw(); STLPoolAllocator(uint32_t capacity) throw(); template STLPoolAllocator(const STLPoolAllocator &) throw(); ~STLPoolAllocator(); STLPoolAllocator& operator=(const STLPoolAllocator &); void usePoolAllocator(boost::shared_ptr b); boost::shared_ptr getPoolAllocator(); pointer allocate(size_type, const void *hint = 0); void deallocate(pointer p, size_type n); size_type max_size() const throw(); inline uint64_t getMemUsage() const { return pa->getMemUsage(); } void construct(pointer p, const T& val); void destroy(pointer p); static const uint32_t DEFAULT_SIZE = 4096*sizeof(T); boost::shared_ptr pa; }; template STLPoolAllocator::STLPoolAllocator() throw() { pa.reset(new PoolAllocator(DEFAULT_SIZE)); } template STLPoolAllocator::STLPoolAllocator(const STLPoolAllocator &s) throw() { pa = s.pa; } template STLPoolAllocator::STLPoolAllocator(uint32_t capacity) throw() { pa.reset(new PoolAllocator(capacity)); } template template STLPoolAllocator::STLPoolAllocator(const STLPoolAllocator &s) throw() { pa = s.pa; } template STLPoolAllocator::~STLPoolAllocator() { } template void STLPoolAllocator::usePoolAllocator(boost::shared_ptr p) { pa = p; } template boost::shared_ptr STLPoolAllocator::getPoolAllocator() { return pa; } template typename STLPoolAllocator::pointer STLPoolAllocator::allocate(typename STLPoolAllocator::size_type s, typename std::allocator::const_pointer hint) { return (pointer) pa->allocate(s*sizeof(T)); } template void STLPoolAllocator::deallocate(typename STLPoolAllocator::pointer p, typename STLPoolAllocator::size_type n) { pa->deallocate((void *) p); } template typename STLPoolAllocator::size_type STLPoolAllocator::max_size() const throw() { return std::numeric_limits::max()/sizeof(T); } template void STLPoolAllocator::construct(typename STLPoolAllocator::pointer p, const T& val) { new((void *)p) T(val); } template void STLPoolAllocator::destroy(typename STLPoolAllocator::pointer p) { p->T::~T(); } template STLPoolAllocator& STLPoolAllocator::operator=(const STLPoolAllocator &c) { pa = c.pa; return *this; } template bool operator==(const STLPoolAllocator &, const STLPoolAllocator &) { return true; } template bool operator!=(const STLPoolAllocator &, const STLPoolAllocator &) { return false; } } #endif