From e3ea39f182a5c2a07c516e2cf97943046ea8b89a Mon Sep 17 00:00:00 2001 From: Richard Roberts Date: Thu, 15 Aug 2013 17:21:14 +0000 Subject: [PATCH] Made allocator switchable at compile time in Fast* --- gtsam/base/FastDefaultAllocator.h | 64 +++++++++++++++++++++++++++++++ gtsam/base/FastList.h | 9 +++-- gtsam/base/FastMap.h | 8 ++-- gtsam/base/FastSet.h | 9 +++-- gtsam/base/FastVector.h | 7 ++-- 5 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 gtsam/base/FastDefaultAllocator.h diff --git a/gtsam/base/FastDefaultAllocator.h b/gtsam/base/FastDefaultAllocator.h new file mode 100644 index 000000000..4a9d9d8f2 --- /dev/null +++ b/gtsam/base/FastDefaultAllocator.h @@ -0,0 +1,64 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file FastDefaultAllocator.h + * @brief An easy way to control which allocator is used for Fast* collections + * @author Richard Roberts + * @date Aug 15, 2013 + */ + +#pragma once + +#if !defined GTSAM_ALLOCATOR_BOOSTPOOL && !defined GTSAM_ALLOCATOR_TBB && !defined GTSAM_ALLOCATOR_STL +// Use TBB allocator by default +# define GTSAM_ALLOCATOR_TBB +#endif + +#if defined GTSAM_ALLOCATOR_BOOSTPOOL +# include +#elif defined GTSAM_ALLOCATOR_TBB +# include +# undef min +# undef max +# undef ERROR +#elif defined GTSAM_ALLOCATOR_STL +# include +#endif + +namespace gtsam +{ + + namespace internal + { + template + struct FastDefaultAllocator + { +#if defined GTSAM_ALLOCATOR_BOOSTPOOL + typedef boost::fast_pool_allocator type; + static const bool isBoost = true; + static const bool isTBB = false; + static const bool isSTL = false; +#elif defined GTSAM_ALLOCATOR_TBB + typedef tbb::tbb_allocator type; + static const bool isBoost = false; + static const bool isTBB = true; + static const bool isSTL = false; +#elif defined GTSAM_ALLOCATOR_STL + typedef std::allocator type; + static const bool isBoost = false; + static const bool isTBB = false; + static const bool isSTL = true; +#endif + }; + } + +} \ No newline at end of file diff --git a/gtsam/base/FastList.h b/gtsam/base/FastList.h index a03121024..589d0a689 100644 --- a/gtsam/base/FastList.h +++ b/gtsam/base/FastList.h @@ -18,8 +18,9 @@ #pragma once +#include #include -#include +#include #include #include @@ -34,11 +35,11 @@ namespace gtsam { * @addtogroup base */ template -class FastList: public std::list > { +class FastList: public std::list::type> { public: - typedef std::list > Base; + typedef std::list::type> Base; /** Default constructor */ FastList() {} @@ -54,7 +55,7 @@ public: FastList(const Base& x) : Base(x) {} /** Copy constructor from a standard STL container */ - FastList(const std::list& x) { + FastList(const std::list& x, typename boost::disable_if_c::isSTL>::type* = 0) { // This if statement works around a bug in boost pool allocator and/or // STL vector where if the size is zero, the pool allocator will allocate // huge amounts of memory. diff --git a/gtsam/base/FastMap.h b/gtsam/base/FastMap.h index 5015a77d8..2ef201b9d 100644 --- a/gtsam/base/FastMap.h +++ b/gtsam/base/FastMap.h @@ -18,8 +18,8 @@ #pragma once +#include #include -#include #include #include @@ -34,11 +34,13 @@ namespace gtsam { * @addtogroup base */ template -class FastMap : public std::map, boost::fast_pool_allocator > > { +class FastMap : public std::map, + typename internal::FastDefaultAllocator >::type> { public: - typedef std::map, boost::fast_pool_allocator > > Base; + typedef std::map, + typename internal::FastDefaultAllocator >::type > Base; /** Default constructor */ FastMap() {} diff --git a/gtsam/base/FastSet.h b/gtsam/base/FastSet.h index 4c8227c22..25f012201 100644 --- a/gtsam/base/FastSet.h +++ b/gtsam/base/FastSet.h @@ -18,11 +18,12 @@ #pragma once +#include + #include #include #include #include -#include #include #include #include @@ -45,11 +46,11 @@ struct FastSetTestableHelper; * @addtogroup base */ template -class FastSet: public std::set, boost::fast_pool_allocator > { +class FastSet: public std::set, typename internal::FastDefaultAllocator::type> { public: - typedef std::set, boost::fast_pool_allocator > Base; + typedef std::set, typename internal::FastDefaultAllocator::type> Base; /** Default constructor */ FastSet() { @@ -78,7 +79,7 @@ public: } /** Copy constructor from a standard STL container */ - FastSet(const std::set& x) { + FastSet(const std::set& x, typename boost::disable_if_c::isSTL>::type* = 0) { // This if statement works around a bug in boost pool allocator and/or // STL vector where if the size is zero, the pool allocator will allocate // huge amounts of memory. diff --git a/gtsam/base/FastVector.h b/gtsam/base/FastVector.h index f725cacc0..309f4d2f0 100644 --- a/gtsam/base/FastVector.h +++ b/gtsam/base/FastVector.h @@ -19,7 +19,6 @@ #pragma once #include -#include #include #include @@ -36,11 +35,11 @@ namespace gtsam { * @addtogroup base */ template -class FastVector: public std::vector > { +class FastVector: public std::vector::type> { public: - typedef std::vector > Base; + typedef std::vector::type> Base; /** Default constructor */ FastVector() {} @@ -80,7 +79,7 @@ public: FastVector(const Base& x) : Base(x) {} /** Copy constructor from a standard STL container */ - FastVector(const std::vector& x) { + FastVector(const std::vector& x, typename boost::disable_if_c::isSTL>::type* = 0) { // This if statement works around a bug in boost pool allocator and/or // STL vector where if the size is zero, the pool allocator will allocate // huge amounts of memory.