reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
StlPoolAllocator.hxx
Go to the documentation of this file.
1#ifndef StlPoolAllocator_Include_Guard
2#define StlPoolAllocator_Include_Guard
3
4#include <limits>
5#include <memory>
6#include <stddef.h>
7
8// .bwc. gcc 4.2 and above support stateful allocators. I don't know about other
9// compilers; if you do, add them here please.
10#if ( (__GNUC__ == 4) && (__GNUC_MINOR__ >= 2) ) || ( __GNUC__ > 4 )
11#define RESIP_HAS_STATEFUL_ALLOCATOR_SUPPORT
12#endif
13
14#ifdef max // Max is defined under WIN32 and conflicts with std::numeric_limits<size_type>::max use below
15#undef max
16#endif
17
18namespace resip
19{
27template<typename T, typename P>
29{
30 public:
31 typedef T value_type;
33 typedef const value_type* const_pointer;
36 typedef size_t size_type;
37 typedef ptrdiff_t difference_type;
38
39#ifdef RESIP_HAS_STATEFUL_ALLOCATOR_SUPPORT
40 explicit StlPoolAllocator(P* pool=0) :
41 mPool(pool){}
43 mPool(other.mPool){}
44 StlPoolAllocator& operator=(const StlPoolAllocator& rhs)
45 {
46 if (&rhs != this)
47 {
48 mPool=rhs.mPool;
49 }
50 return *this;
51 }
52#else
53 // Disable pool allocation if stateful allocators are not supported by the
54 // STL
55 explicit StlPoolAllocator(P* pool=0) :
56 mPool(0){}
58 mPool(0){}
60 {
61 return *this;
62 }
63#endif
64
65
66 template<typename U>
69
71
72
73 template<typename U>
74 struct rebind
75 {
77 };
78
80 {
81 return &ref;
82 }
83
85 {
86 return &ref;
87 }
88
89 pointer allocate(size_type n, const void* hint=0)
90 {
91 return (pointer)(allocate_raw(n*sizeof(T)));
92 }
93
95 {
96 deallocate_raw((void*)ptr);
97 }
98
99 inline void* allocate_raw(size_type bytes)
100 {
101 if(mPool)
102 {
103 return mPool->allocate(bytes);
104 }
105 return ::operator new(bytes);
106 }
107
108 void deallocate_raw(void* ptr)
109 {
110 if(mPool)
111 {
112 mPool->deallocate(ptr);
113 return;
114 }
115 ::operator delete(ptr);
116 }
117
119 {
120 if(mPool)
121 {
122 return mPool->max_size();
123 }
124 return std::numeric_limits<size_type>::max()/sizeof(T);
125 }
126
128 {
129 if(mPool)
130 {
131 return mPool->max_size();
132 }
133 return std::numeric_limits<size_type>::max()/_sz;
134 }
135
137 {
138 new (p) T(orig);
139 }
140
141 void destroy(pointer ptr)
142 {
143 ptr->~T();
144 }
145
146 bool operator==(const StlPoolAllocator& rhs) const
147 {
148 return mPool == rhs.mPool;
149 }
150
151 bool operator!=(const StlPoolAllocator& rhs) const
152 {
153 return mPool != rhs.mPool;
154 }
155
157};
158
159}
160
161#endif
162
163
164/* ====================================================================
165 * The Vovida Software License, Version 1.0
166 *
167 * Redistribution and use in source and binary forms, with or without
168 * modification, are permitted provided that the following conditions
169 * are met:
170 *
171 * 1. Redistributions of source code must retain the above copyright
172 * notice, this list of conditions and the following disclaimer.
173 *
174 * 2. Redistributions in binary form must reproduce the above copyright
175 * notice, this list of conditions and the following disclaimer in
176 * the documentation and/or other materials provided with the
177 * distribution.
178 *
179 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
180 * and "Vovida Open Communication Application Library (VOCAL)" must
181 * not be used to endorse or promote products derived from this
182 * software without prior written permission. For written
183 * permission, please contact vocal@vovida.org.
184 *
185 * 4. Products derived from this software may not be called "VOCAL", nor
186 * may "VOCAL" appear in their name, without prior written
187 * permission of Vovida Networks, Inc.
188 *
189 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
190 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
191 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
192 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
193 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
194 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
195 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
196 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
197 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
198 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
200 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
201 * DAMAGE.
202 *
203 * ====================================================================
204 *
205 * This software consists of voluntary contributions made by Vovida
206 * Networks, Inc. and many individuals on behalf of Vovida Networks,
207 * Inc. For more information on Vovida Networks, Inc., please see
208 * <http://www.vovida.org/>.
209 *
210 */
A dirt-simple lightweight stl pool allocator meant for use in short-lifetime objects.
bool operator!=(const StlPoolAllocator &rhs) const
void * allocate_raw(size_type bytes)
void deallocate(pointer ptr, size_type n)
void construct(pointer p, const_reference orig)
pointer allocate(size_type n, const void *hint=0)
const value_type & const_reference
const_pointer address(const_reference ref) const
const value_type * const_pointer
StlPoolAllocator(const StlPoolAllocator< U, P > &other)
pointer address(reference ref) const
size_type max_size(size_type _sz) const
StlPoolAllocator & operator=(const StlPoolAllocator &rhs)
StlPoolAllocator(const StlPoolAllocator &other)
bool operator==(const StlPoolAllocator &rhs) const
dcm! – add UnusedChecking(_enum) below;
StlPoolAllocator< U, P > other