diff options
author | vrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb> | 2015-10-02 13:13:38 +0000 |
---|---|---|
committer | vrouvrea <vrouvrea@636b058d-ea47-450e-bf9e-a15bfbe3eedb> | 2015-10-02 13:13:38 +0000 |
commit | c14909eae41883308428095758360de3a7202a0d (patch) | |
tree | 6c3c40dbe6c006ec65a54a6373b5e6129012a4fb /src/common/include/gudhi/Simple_object_pool.h | |
parent | 91b4846b860988f94346cdf1302c1cfd965c3d27 (diff) |
Backmerge of trunk
git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/alphashapes@820 636b058d-ea47-450e-bf9e-a15bfbe3eedb
Former-commit-id: 812e0b84d187dd5e30a6a18c12612ebc9bf9206a
Diffstat (limited to 'src/common/include/gudhi/Simple_object_pool.h')
-rw-r--r-- | src/common/include/gudhi/Simple_object_pool.h | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/common/include/gudhi/Simple_object_pool.h b/src/common/include/gudhi/Simple_object_pool.h new file mode 100644 index 00000000..fffcb2ef --- /dev/null +++ b/src/common/include/gudhi/Simple_object_pool.h @@ -0,0 +1,64 @@ +/* This file is part of the Gudhi Library. The Gudhi library + * (Geometric Understanding in Higher Dimensions) is a generic C++ + * library for computational topology. + * + * Author(s): Marc Glisse + * + * Copyright (C) 2015 INRIA Saclay - Ile de France + * + * 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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, see <http://www.gnu.org/licenses/>. + */ + +#include <boost/pool/pool.hpp> +#include <utility> + +namespace Gudhi { + /** \private + * This is a simpler version of boost::object_pool, that requires + * that users explicitly destroy all objects. This lets the + * performance scale much better, see + * https://svn.boost.org/trac/boost/ticket/3789 . + */ +template <class T> +class Simple_object_pool : protected boost::pool<boost::default_user_allocator_malloc_free> +{ + protected: + typedef boost::pool<boost::default_user_allocator_malloc_free> Base; + typedef T* pointer; + Base& base(){return *this;} + Base const& base()const{return *this;} + public: + typedef T element_type; + typedef boost::default_user_allocator_malloc_free user_allocator; + typedef typename Base::size_type size_type; + typedef typename Base::difference_type difference_type; + template<class...U> + Simple_object_pool(U&&...u) : Base(sizeof(T), std::forward<U>(u)...) {} + template<class...U> + pointer construct(U&&...u){ + void* p=base().malloc BOOST_PREVENT_MACRO_SUBSTITUTION(); + assert(p); + try { new(p) T(std::forward<U>(u)...); } + catch(...) { + base().free BOOST_PREVENT_MACRO_SUBSTITUTION(p); + throw; + } + return static_cast<pointer>(p); + } + void destroy(pointer p){ + p->~T(); + base().free BOOST_PREVENT_MACRO_SUBSTITUTION(p); + } +}; +} |