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 namespace OpenDDS {
00018 namespace DCPS {
00019 
00020 OpenDDS_Dcps_Export void* pool_alloc_memory(size_t size);
00021 
00022 OpenDDS_Dcps_Export void pool_free_memory(void* ptr);
00023 
00024 /// Adapt the MemoryPool for use with STL containers.
00025 ///
00026 /// See Definitions.h for macros that assist with instantiating STL containers
00027 /// with switchable support for the allocator (when Safety Profile is enabled).
00028 ///
00029 /// This class template models the C++03 Allocator concept and is stateless.
00030 template <typename T>
00031 class OpenDDS_Dcps_Export PoolAllocator
00032 {
00033 public:
00034   typedef T value_type;
00035   typedef T* pointer;
00036   typedef const T* const_pointer;
00037   typedef T& reference;
00038   typedef const T& const_reference;
00039   typedef std::size_t size_type;
00040   typedef std::ptrdiff_t difference_type;
00041   template <typename U> struct rebind { typedef PoolAllocator<U> other; };
00042 
00043   PoolAllocator() {}
00044 
00045   template <typename U>
00046   PoolAllocator(const PoolAllocator<U>&) {}
00047 
00048   static T* allocate(std::size_t n)
00049   {
00050     void* raw_mem = ACE_Allocator::instance()->malloc(n * sizeof(T));
00051     if (!raw_mem) throw std::bad_alloc();
00052     return static_cast<T*>(raw_mem);
00053   }
00054 
00055   static void deallocate(T* ptr, std::size_t)
00056   {
00057     ACE_Allocator::instance()->free(ptr);
00058   }
00059 
00060   static void construct(T* ptr, const T& value)
00061   {
00062     new (static_cast<void*>(ptr)) T(value);
00063   }
00064 
00065   static void destroy(T* ptr)
00066   {
00067     ptr->~T();
00068   }
00069 
00070   static size_type max_size()
00071   {
00072     return std::numeric_limits<size_type>::max();
00073   }
00074 };
00075 
00076 template <typename T, typename U>
00077 bool operator==(const PoolAllocator<T>&, const PoolAllocator<U>&)
00078 {
00079   return true;
00080 }
00081 
00082 template <typename T, typename U>
00083 bool operator!=(const PoolAllocator<T>&, const PoolAllocator<U>&)
00084 {
00085   return false;
00086 }
00087 
00088 }}
00089 
00090 #define OPENDDS_STRING std::basic_string<char, std::char_traits<char>, \
00091           OpenDDS::DCPS::PoolAllocator<char> >
00092 #define OPENDDS_MAP(K, V) std::map<K, V, std::less<K >, \
00093           OpenDDS::DCPS::PoolAllocator<std::pair<const K, V > > >
00094 #define OPENDDS_MAP_CMP(K, V, C) std::map<K, V, C, \
00095           OpenDDS::DCPS::PoolAllocator<std::pair<const K, V > > >
00096 #define OPENDDS_MULTIMAP(K, T) std::multimap<K, T, std::less<K >, \
00097           OpenDDS::DCPS::PoolAllocator<std::pair<const K, T > > >
00098 #define OPENDDS_MULTIMAP_CMP(K, T, C) std::multimap<K, T, C, \
00099           OpenDDS::DCPS::PoolAllocator<std::pair<const K, T > > >
00100 #define OPENDDS_SET(K) std::set<K, std::less<K >, \
00101           OpenDDS::DCPS::PoolAllocator<K > >
00102 #define OPENDDS_SET_CMP(K, C) std::set<K, C, \
00103           OpenDDS::DCPS::PoolAllocator<K > >
00104 #define OPENDDS_MULTISET_CMP(K, C) std::multiset<K, C, \
00105           OpenDDS::DCPS::PoolAllocator<K > >
00106 #define OPENDDS_VECTOR(T) std::vector<T, \
00107           OpenDDS::DCPS::PoolAllocator<T > >
00108 #define OPENDDS_LIST(T) std::list<T, \
00109           OpenDDS::DCPS::PoolAllocator<T > >
00110 #define OPENDDS_QUEUE(T) std::queue<T, std::deque<T, \
00111           OpenDDS::DCPS::PoolAllocator<T > > >
00112 
00113 #else
00114 #define OPENDDS_STRING std::string
00115 #define OPENDDS_MAP(K, V) std::map<K, V >
00116 #define OPENDDS_MAP_CMP(K, V, C) std::map<K, V, C >
00117 #define OPENDDS_MULTIMAP(K, T) std::multimap<K, T >
00118 #define OPENDDS_MULTIMAP_CMP(K, T, C) std::multimap<K, T, C >
00119 #define OPENDDS_SET(K) std::set<K >
00120 #define OPENDDS_SET_CMP(K, C) std::set<K, C >
00121 #define OPENDDS_MULTISET_CMP(K, C) std::multiset<K, C >
00122 #define OPENDDS_VECTOR(T) std::vector<T >
00123 #define OPENDDS_LIST(T) std::list<T >
00124 #define OPENDDS_QUEUE(T) std::queue<T >
00125 
00126 #endif // OPENDDS_SAFETY_PROFILE && ACE_HAS_ALLOC_HOOKS
00127 
00128 
00129 #endif // OPENDDS_DCPS_POOL_ALLOCATOR_H

Generated on Fri Feb 12 20:05:24 2016 for OpenDDS by  doxygen 1.4.7