OpenDDS  Snapshot(2023/04/28-20:55)
PoolAllocator.h
Go to the documentation of this file.
1 #ifndef OPENDDS_DCPS_POOL_ALLOCATOR_H
2 #define OPENDDS_DCPS_POOL_ALLOCATOR_H
3 
4 #include <ace/config-macros.h>
5 #if defined OPENDDS_SAFETY_PROFILE && defined ACE_HAS_ALLOC_HOOKS
6 # define OPENDDS_POOL_ALLOCATOR 1
7 #else
8 # define OPENDDS_POOL_ALLOCATOR 0
9 #endif
10 
11 #if OPENDDS_POOL_ALLOCATOR
12 # include "dcps_export.h"
13 # include "SafetyProfilePool.h"
14 #endif
16 
17 #include <limits>
18 #include <string>
19 #include <map>
20 #include <list>
21 #include <vector>
22 #include <queue>
23 #include <set>
24 #ifdef ACE_HAS_CPP11
25 #include <unordered_map>
26 #endif
27 
29 
30 namespace OpenDDS {
31 namespace DCPS {
32 
33 #if OPENDDS_POOL_ALLOCATOR
34 
35 OpenDDS_Dcps_Export void* pool_alloc_memory(size_t size);
36 
37 OpenDDS_Dcps_Export void pool_free_memory(void* ptr);
38 
39 /// Adapt the MemoryPool for use with STL containers.
40 ///
41 /// See Definitions.h for macros that assist with instantiating STL containers
42 /// with switchable support for the allocator (when Safety Profile is enabled).
43 ///
44 /// This class template models the C++03 Allocator concept and is stateless.
45 template <typename T>
46 class OpenDDS_Dcps_Export PoolAllocator
47 {
48 public:
49  typedef T value_type;
50  typedef T* pointer;
51  typedef const T* const_pointer;
52  typedef T& reference;
53  typedef const T& const_reference;
54  typedef std::size_t size_type;
55  typedef std::ptrdiff_t difference_type;
56  template <typename U> struct rebind { typedef PoolAllocator<U> other; };
57 
58  PoolAllocator() {}
59 
60  template <typename U>
61  PoolAllocator(const PoolAllocator<U>&) {}
62 
63  static T* allocate(std::size_t n)
64  {
65  void* raw_mem = ACE_Allocator::instance()->malloc(n * sizeof(T));
66  if (!raw_mem) throw std::bad_alloc();
67  return static_cast<T*>(raw_mem);
68  }
69 
70  static void deallocate(T* ptr, std::size_t)
71  {
73  }
74 
75  static void construct(T* ptr, const T& value)
76  {
77  new (static_cast<void*>(ptr)) T(value);
78  }
79 
80 #ifdef ACE_HAS_CPP11
81  static void construct(T* ptr, T&& value)
82  {
83  new (static_cast<void*>(ptr)) T(std::move(value));
84  }
85 #endif
86 
87  static void destroy(T* ptr)
88  {
89  ptr->~T();
90  }
91 
92  static size_type max_size()
93  {
94  return std::numeric_limits<size_type>::max();
95  }
96 };
97 
98 template <typename T, typename U>
99 bool operator==(const PoolAllocator<T>&, const PoolAllocator<U>&)
100 {
101  return true;
102 }
103 
104 template <typename T, typename U>
105 bool operator!=(const PoolAllocator<T>&, const PoolAllocator<U>&)
106 {
107  return false;
108 }
109 
110 template <typename T>
111 struct add_const { typedef const T type; };
112 
113 // TODO(iguessthislldo): Rewrite with using in C++11
114 #define OPENDDS_ALLOCATOR(T) OpenDDS::DCPS::PoolAllocator<T >
115 typedef std::basic_string<char, std::char_traits<char>, OPENDDS_ALLOCATOR(char)> String;
116 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, OPENDDS_ALLOCATOR(wchar_t)>
117  WString;
118 #define OPENDDS_MAP(K, V) std::map<K, V, std::less<K >, \
119  OpenDDS::DCPS::PoolAllocator<std::pair<OpenDDS::DCPS::add_const<K >::type, V > > >
120 #define OPENDDS_MAP_CMP(K, V, C) std::map<K, V, C, \
121  OpenDDS::DCPS::PoolAllocator<std::pair<OpenDDS::DCPS::add_const<K >::type, V > > >
122 #define OPENDDS_MULTIMAP(K, T) std::multimap<K, T, std::less<K >, \
123  OpenDDS::DCPS::PoolAllocator<std::pair<OpenDDS::DCPS::add_const<K >::type, T > > >
124 #define OPENDDS_MULTIMAP_CMP(K, T, C) std::multimap<K, T, C, \
125  OpenDDS::DCPS::PoolAllocator<std::pair<OpenDDS::DCPS::add_const<K >::type, T > > >
126 #define OPENDDS_MAP_T(K, V) std::map<K, V, std::less<K >, \
127  OpenDDS::DCPS::PoolAllocator<std::pair<typename OpenDDS::DCPS::add_const<K >::type, V > > >
128 #define OPENDDS_MAP_CMP_T(K, V, C) std::map<K, V, C, \
129  OpenDDS::DCPS::PoolAllocator<std::pair<typename OpenDDS::DCPS::add_const<K >::type, V > > >
130 #define OPENDDS_MULTIMAP_T(K, T) std::multimap<K, T, std::less<K >, \
131  OpenDDS::DCPS::PoolAllocator<std::pair<typename OpenDDS::DCPS::add_const<K >::type, T > > >
132 #define OPENDDS_MULTIMAP_CMP_T(K, T, C) std::multimap<K, T, C, \
133  OpenDDS::DCPS::PoolAllocator<std::pair<typename OpenDDS::DCPS::add_const<K >::type, T > > >
134 #define OPENDDS_SET(K) std::set<K, std::less<K >, \
135  OpenDDS::DCPS::PoolAllocator<K > >
136 #define OPENDDS_SET_CMP(K, C) std::set<K, C, \
137  OpenDDS::DCPS::PoolAllocator<K > >
138 #define OPENDDS_MULTISET(K) std::multiset<K, std::less<K >, \
139  OpenDDS::DCPS::PoolAllocator<K > >
140 #define OPENDDS_MULTISET_CMP(K, C) std::multiset<K, C, \
141  OpenDDS::DCPS::PoolAllocator<K > >
142 #define OPENDDS_VECTOR(T) std::vector<T, \
143  OpenDDS::DCPS::PoolAllocator<T > >
144 #define OPENDDS_LIST(T) std::list<T, \
145  OpenDDS::DCPS::PoolAllocator<T > >
146 #define OPENDDS_DEQUE(T) std::deque<T, \
147  OpenDDS::DCPS::PoolAllocator<T > >
148 #define OPENDDS_QUEUE(T) std::queue<T, std::deque<T, \
149  OpenDDS::DCPS::PoolAllocator<T > > >
150 #ifdef ACE_HAS_CPP11
151 #define OPENDDS_UNORDERED_MAP(K, V) std::unordered_map<K, V, std::hash<K >, std::equal_to<K >, \
152  OpenDDS::DCPS::PoolAllocator<std::pair<OpenDDS::DCPS::add_const<K >::type, V > > >
153 #define OPENDDS_UNORDERED_MAP_CHASH(K, V, C) std::unordered_map<K, V, C, std::equal_to<K >, \
154  OpenDDS::DCPS::PoolAllocator<std::pair<OpenDDS::DCPS::add_const<K >::type, V > > >
155 #define OPENDDS_UNORDERED_MAP_T(K, V) std::unordered_map<K, V, std::hash<K >, std::equal_to<K >, \
156  OpenDDS::DCPS::PoolAllocator<std::pair<typename OpenDDS::DCPS::add_const<K >::type, V > > >
157 #define OPENDDS_UNORDERED_MAP_CHASH_T(K, V, C) std::unordered_map<K, V, C, std::equal_to<K >, \
158  OpenDDS::DCPS::PoolAllocator<std::pair<typename OpenDDS::DCPS::add_const<K >::type, V > > >
159 #endif
160 
161 #else // (!OPENDDS_POOL_ALLOCATOR)
162 #define OPENDDS_ALLOCATOR(T) std::allocator<T >
163 typedef std::string String;
164 typedef std::wstring WString;
165 #define OPENDDS_MAP(K, V) std::map<K, V >
166 #define OPENDDS_MAP_CMP(K, V, C) std::map<K, V, C >
167 #define OPENDDS_MULTIMAP(K, T) std::multimap<K, T >
168 #define OPENDDS_MULTIMAP_CMP(K, T, C) std::multimap<K, T, C >
169 #define OPENDDS_MAP_T OPENDDS_MAP
170 #define OPENDDS_MAP_CMP_T OPENDDS_MAP_CMP
171 #define OPENDDS_MULTIMAP_T OPENDDS_MULTIMAP
172 #define OPENDDS_MULTIMAP_CMP_T OPENDDS_MULTIMAP_CMP
173 #define OPENDDS_SET(K) std::set<K >
174 #define OPENDDS_SET_CMP(K, C) std::set<K, C >
175 #define OPENDDS_MULTISET(K) std::multiset<K >
176 #define OPENDDS_MULTISET_CMP(K, C) std::multiset<K, C >
177 #define OPENDDS_VECTOR(T) std::vector<T >
178 #define OPENDDS_LIST(T) std::list<T >
179 #define OPENDDS_DEQUE(T) std::deque<T >
180 #define OPENDDS_QUEUE(T) std::queue<T >
181 #ifdef ACE_HAS_CPP11
182 #define OPENDDS_UNORDERED_MAP(K, V) std::unordered_map<K, V >
183 #define OPENDDS_UNORDERED_MAP_CHASH(K, V, C) std::unordered_map<K, V, C >
184 #define OPENDDS_UNORDERED_MAP_T OPENDDS_UNORDERED_MAP
185 #define OPENDDS_UNORDERED_MAP_CHASH_T OPENDDS_UNORDERED_MAP_CHASH
186 #endif
187 
188 #endif // OPENDDS_POOL_ALLOCATOR
189 
190 #define OPENDDS_STRING OpenDDS::DCPS::String
191 #define OPENDDS_WSTRING OpenDDS::DCPS::WString
192 
193 } // namespace DCPS
194 } // namespace OpenDDS
195 
197 
198 #endif // OPENDDS_DCPS_POOL_ALLOCATOR_H
std::wstring WString
const LogLevel::Value value
Definition: debug.cpp:61
std::string String
#define OpenDDS_Dcps_Export
Definition: dcps_export.h:24
virtual void free(void *ptr)=0
T::rv_reference move(T &p)
Definition: unique_ptr.h:141
bool operator==(const DisjointSequence::OrderedRanges< T > &a, const DisjointSequence::OrderedRanges< T > &b)
#define OPENDDS_ALLOCATOR(T)
static ACE_Allocator * instance(void)
bool operator!=(const GUID_t &lhs, const GUID_t &rhs)
Definition: GuidUtils.h:125
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
virtual void * malloc(size_type nbytes)=0