PoolAllocator.h

Go to the documentation of this file.
00001 #ifndef OPENDDS_DCPS_POOL_ALLOCATOR_H
00002 #define OPENDDS_DCPS_POOL_ALLOCATOR_H
00003 
00004 #include "ace/config-macros.h"
00005 #include <limits>
00006 #include <string>
00007 #include <map>
00008 #include <list>
00009 #include <vector>
00010 #include <queue>
00011 #include <set>
00012 
00013 #if defined OPENDDS_SAFETY_PROFILE && defined ACE_HAS_ALLOC_HOOKS
00014 #include "dcps_export.h"
00015 #include "SafetyProfilePool.h"
00016 
00017 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00018 
00019 namespace OpenDDS {
00020 namespace DCPS {
00021 
00022 OpenDDS_Dcps_Export void* pool_alloc_memory(size_t size);
00023 
00024 OpenDDS_Dcps_Export void pool_free_memory(void* ptr);
00025 
00026 /// Adapt the MemoryPool for use with STL containers.
00027 ///
00028 /// See Definitions.h for macros that assist with instantiating STL containers
00029 /// with switchable support for the allocator (when Safety Profile is enabled).
00030 ///
00031 /// This class template models the C++03 Allocator concept and is stateless.
00032 template <typename T>
00033 class OpenDDS_Dcps_Export PoolAllocator
00034 {
00035 public:
00036   typedef T value_type;
00037   typedef T* pointer;
00038   typedef const T* const_pointer;
00039   typedef T& reference;
00040   typedef const T& const_reference;
00041   typedef std::size_t size_type;
00042   typedef std::ptrdiff_t difference_type;
00043   template <typename U> struct rebind { typedef PoolAllocator<U> other; };
00044 
00045   PoolAllocator() {}
00046 
00047   template <typename U>
00048   PoolAllocator(const PoolAllocator<U>&) {}
00049 
00050   static T* allocate(std::size_t n)
00051   {
00052     void* raw_mem = ACE_Allocator::instance()->malloc(n * sizeof(T));
00053     if (!raw_mem) throw std::bad_alloc();
00054     return static_cast<T*>(raw_mem);
00055   }
00056 
00057   static void deallocate(T* ptr, std::size_t)
00058   {
00059     ACE_Allocator::instance()->free(ptr);
00060   }
00061 
00062   static void construct(T* ptr, const T& value)
00063   {
00064     new (static_cast<void*>(ptr)) T(value);
00065   }
00066 
00067 #ifdef ACE_HAS_CPP11
00068   static void construct(T* ptr, T&& value)
00069   {
00070     new (static_cast<void*>(ptr)) T(std::move(value));
00071   }
00072 #endif
00073 
00074   static void destroy(T* ptr)
00075   {
00076     ptr->~T();
00077   }
00078 
00079   static size_type max_size()
00080   {
00081     return std::numeric_limits<size_type>::max();
00082   }
00083 };
00084 
00085 template <typename T, typename U>
00086 bool operator==(const PoolAllocator<T>&, const PoolAllocator<U>&)
00087 {
00088   return true;
00089 }
00090 
00091 template <typename T, typename U>
00092 bool operator!=(const PoolAllocator<T>&, const PoolAllocator<U>&)
00093 {
00094   return false;
00095 }
00096 
00097 template <typename T>
00098 struct add_const { typedef const T type; };
00099 
00100 }}
00101 
00102 OPENDDS_END_VERSIONED_NAMESPACE_DECL
00103 
00104 #define OPENDDS_STRING std::basic_string<char, std::char_traits<char>, \
00105           OpenDDS::DCPS::PoolAllocator<char> >
00106 #define OPENDDS_MAP(K, V) std::map<K, V, std::less<K >, \
00107           OpenDDS::DCPS::PoolAllocator<std::pair<OpenDDS::DCPS::add_const<K >::type, V > > >
00108 #define OPENDDS_MAP_CMP(K, V, C) std::map<K, V, C, \
00109           OpenDDS::DCPS::PoolAllocator<std::pair<OpenDDS::DCPS::add_const<K >::type, V > > >
00110 #define OPENDDS_MULTIMAP(K, T) std::multimap<K, T, std::less<K >, \
00111           OpenDDS::DCPS::PoolAllocator<std::pair<OpenDDS::DCPS::add_const<K >::type, T > > >
00112 #define OPENDDS_MULTIMAP_CMP(K, T, C) std::multimap<K, T, C, \
00113           OpenDDS::DCPS::PoolAllocator<std::pair<OpenDDS::DCPS::add_const<K >::type, T > > >
00114 #define OPENDDS_MAP_T(K, V) std::map<K, V, std::less<K >, \
00115           OpenDDS::DCPS::PoolAllocator<std::pair<typename OpenDDS::DCPS::add_const<K >::type, V > > >
00116 #define OPENDDS_MAP_CMP_T(K, V, C) std::map<K, V, C, \
00117           OpenDDS::DCPS::PoolAllocator<std::pair<typename OpenDDS::DCPS::add_const<K >::type, V > > >
00118 #define OPENDDS_MULTIMAP_T(K, T) std::multimap<K, T, std::less<K >, \
00119           OpenDDS::DCPS::PoolAllocator<std::pair<typename OpenDDS::DCPS::add_const<K >::type, T > > >
00120 #define OPENDDS_MULTIMAP_CMP_T(K, T, C) std::multimap<K, T, C, \
00121           OpenDDS::DCPS::PoolAllocator<std::pair<typename OpenDDS::DCPS::add_const<K >::type, T > > >
00122 #define OPENDDS_SET(K) std::set<K, std::less<K >, \
00123           OpenDDS::DCPS::PoolAllocator<K > >
00124 #define OPENDDS_SET_CMP(K, C) std::set<K, C, \
00125           OpenDDS::DCPS::PoolAllocator<K > >
00126 #define OPENDDS_MULTISET_CMP(K, C) std::multiset<K, C, \
00127           OpenDDS::DCPS::PoolAllocator<K > >
00128 #define OPENDDS_VECTOR(T) std::vector<T, \
00129           OpenDDS::DCPS::PoolAllocator<T > >
00130 #define OPENDDS_LIST(T) std::list<T, \
00131           OpenDDS::DCPS::PoolAllocator<T > >
00132 #define OPENDDS_DEQUE(T) std::deque<T, \
00133           OpenDDS::DCPS::PoolAllocator<T > >
00134 #define OPENDDS_QUEUE(T) std::queue<T, std::deque<T, \
00135           OpenDDS::DCPS::PoolAllocator<T > > >
00136 
00137 #else
00138 #define OPENDDS_STRING std::string
00139 #define OPENDDS_MAP(K, V) std::map<K, V >
00140 #define OPENDDS_MAP_CMP(K, V, C) std::map<K, V, C >
00141 #define OPENDDS_MULTIMAP(K, T) std::multimap<K, T >
00142 #define OPENDDS_MULTIMAP_CMP(K, T, C) std::multimap<K, T, C >
00143 #define OPENDDS_MAP_T OPENDDS_MAP
00144 #define OPENDDS_MAP_CMP_T OPENDDS_MAP_CMP
00145 #define OPENDDS_MULTIMAP_T OPENDDS_MULTIMAP
00146 #define OPENDDS_MULTIMAP_CMP_T OPENDDS_MULTIMAP_CMP
00147 #define OPENDDS_SET(K) std::set<K >
00148 #define OPENDDS_SET_CMP(K, C) std::set<K, C >
00149 #define OPENDDS_MULTISET_CMP(K, C) std::multiset<K, C >
00150 #define OPENDDS_VECTOR(T) std::vector<T >
00151 #define OPENDDS_LIST(T) std::list<T >
00152 #define OPENDDS_DEQUE(T) std::deque<T >
00153 #define OPENDDS_QUEUE(T) std::queue<T >
00154 
00155 #endif // OPENDDS_SAFETY_PROFILE && ACE_HAS_ALLOC_HOOKS
00156 
00157 
00158 #endif // OPENDDS_DCPS_POOL_ALLOCATOR_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1