Coverage Report

Created: 2024-07-23 06:39

/src/resiprocate/rutil/StlPoolAllocator.hxx
Line
Count
Source
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
18
namespace resip
19
{
20
/**
21
   A dirt-simple lightweight stl pool allocator meant for use in short-lifetime 
22
   objects. This will pool-allocate at most S bytes, after which no further pool
23
   allocation will be performed, and fallback to the system new/delete will be 
24
   used (deallocating a pool allocated object will _not_ free up room in the 
25
   pool).
26
*/
27
template<typename T, typename P>
28
class StlPoolAllocator
29
{
30
   public:
31
      typedef T value_type;
32
      typedef value_type* pointer;
33
      typedef const value_type* const_pointer;
34
      typedef value_type& reference;
35
      typedef const value_type& const_reference;
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
418k
         mPool(pool){}
resip::StlPoolAllocator<resip::Parameter*, resip::PoolBase>::StlPoolAllocator(resip::PoolBase*)
Line
Count
Source
41
376k
         mPool(pool){}
resip::StlPoolAllocator<resip::ParserContainerBase::HeaderKit, resip::PoolBase>::StlPoolAllocator(resip::PoolBase*)
Line
Count
Source
41
219
         mPool(pool){}
resip::StlPoolAllocator<resip::HeaderFieldValue, resip::PoolBase>::StlPoolAllocator(resip::PoolBase*)
Line
Count
Source
41
21.9k
         mPool(pool){}
resip::StlPoolAllocator<resip::HeaderFieldValueList*, resip::PoolBase>::StlPoolAllocator(resip::PoolBase*)
Line
Count
Source
41
9.98k
         mPool(pool){}
resip::StlPoolAllocator<std::__1::pair<resip::Data, resip::HeaderFieldValueList*>, resip::PoolBase>::StlPoolAllocator(resip::PoolBase*)
Line
Count
Source
41
9.98k
         mPool(pool){}
42
      StlPoolAllocator(const StlPoolAllocator& other) :
43
418k
         mPool(other.mPool){}
resip::StlPoolAllocator<resip::Parameter*, resip::PoolBase>::StlPoolAllocator(resip::StlPoolAllocator<resip::Parameter*, resip::PoolBase> const&)
Line
Count
Source
43
376k
         mPool(other.mPool){}
resip::StlPoolAllocator<resip::ParserContainerBase::HeaderKit, resip::PoolBase>::StlPoolAllocator(resip::StlPoolAllocator<resip::ParserContainerBase::HeaderKit, resip::PoolBase> const&)
Line
Count
Source
43
219
         mPool(other.mPool){}
resip::StlPoolAllocator<resip::HeaderFieldValue, resip::PoolBase>::StlPoolAllocator(resip::StlPoolAllocator<resip::HeaderFieldValue, resip::PoolBase> const&)
Line
Count
Source
43
21.9k
         mPool(other.mPool){}
resip::StlPoolAllocator<resip::HeaderFieldValueList*, resip::PoolBase>::StlPoolAllocator(resip::StlPoolAllocator<resip::HeaderFieldValueList*, resip::PoolBase> const&)
Line
Count
Source
43
9.98k
         mPool(other.mPool){}
resip::StlPoolAllocator<std::__1::__list_node<std::__1::pair<resip::Data, resip::HeaderFieldValueList*>, void*>, resip::PoolBase>::StlPoolAllocator(resip::StlPoolAllocator<std::__1::__list_node<std::__1::pair<resip::Data, resip::HeaderFieldValueList*>, void*>, resip::PoolBase> const&)
Line
Count
Source
43
9.98k
         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){}
57
      StlPoolAllocator(const StlPoolAllocator& other) :
58
         mPool(0){}
59
      StlPoolAllocator& operator=(const StlPoolAllocator& rhs)
60
      {
61
         return *this;
62
      }
63
#endif
64
65
66
      template<typename U>
67
      StlPoolAllocator(const StlPoolAllocator<U,P>& other) :
68
9.98k
         mPool(other.mPool){}
69
70
847k
      ~StlPoolAllocator(){}
resip::StlPoolAllocator<resip::Parameter*, resip::PoolBase>::~StlPoolAllocator()
Line
Count
Source
70
753k
      ~StlPoolAllocator(){}
resip::StlPoolAllocator<resip::ParserContainerBase::HeaderKit, resip::PoolBase>::~StlPoolAllocator()
Line
Count
Source
70
438
      ~StlPoolAllocator(){}
resip::StlPoolAllocator<std::__1::__list_node<std::__1::pair<resip::Data, resip::HeaderFieldValueList*>, void*>, resip::PoolBase>::~StlPoolAllocator()
Line
Count
Source
70
19.9k
      ~StlPoolAllocator(){}
resip::StlPoolAllocator<resip::HeaderFieldValue, resip::PoolBase>::~StlPoolAllocator()
Line
Count
Source
70
43.8k
      ~StlPoolAllocator(){}
resip::StlPoolAllocator<resip::HeaderFieldValueList*, resip::PoolBase>::~StlPoolAllocator()
Line
Count
Source
70
19.9k
      ~StlPoolAllocator(){}
resip::StlPoolAllocator<std::__1::pair<resip::Data, resip::HeaderFieldValueList*>, resip::PoolBase>::~StlPoolAllocator()
Line
Count
Source
70
9.98k
      ~StlPoolAllocator(){}
71
72
73
      template<typename U>
74
      struct rebind
75
      {
76
         typedef StlPoolAllocator<U, P> other;
77
      };
78
79
      pointer address(reference ref) const
80
      {
81
         return &ref;
82
      }
83
84
      const_pointer address(const_reference ref) const
85
      {
86
         return &ref;
87
      }
88
89
      pointer allocate(size_type n, const void* hint=0)
90
77.8k
      {
91
77.8k
         return (pointer)(allocate_raw(n*sizeof(T)));
92
77.8k
      }
resip::StlPoolAllocator<resip::HeaderFieldValue, resip::PoolBase>::allocate(unsigned long, void const*)
Line
Count
Source
90
14.7k
      {
91
14.7k
         return (pointer)(allocate_raw(n*sizeof(T)));
92
14.7k
      }
resip::StlPoolAllocator<resip::ParserContainerBase::HeaderKit, resip::PoolBase>::allocate(unsigned long, void const*)
Line
Count
Source
90
219
      {
91
219
         return (pointer)(allocate_raw(n*sizeof(T)));
92
219
      }
resip::StlPoolAllocator<resip::Parameter*, resip::PoolBase>::allocate(unsigned long, void const*)
Line
Count
Source
90
42.3k
      {
91
42.3k
         return (pointer)(allocate_raw(n*sizeof(T)));
92
42.3k
      }
resip::StlPoolAllocator<resip::HeaderFieldValueList*, resip::PoolBase>::allocate(unsigned long, void const*)
Line
Count
Source
90
10.0k
      {
91
10.0k
         return (pointer)(allocate_raw(n*sizeof(T)));
92
10.0k
      }
resip::StlPoolAllocator<std::__1::__list_node<std::__1::pair<resip::Data, resip::HeaderFieldValueList*>, void*>, resip::PoolBase>::allocate(unsigned long, void const*)
Line
Count
Source
90
10.5k
      {
91
10.5k
         return (pointer)(allocate_raw(n*sizeof(T)));
92
10.5k
      }
93
94
      void deallocate(pointer ptr, size_type n)
95
77.8k
      {
96
77.8k
         deallocate_raw((void*)ptr);
97
77.8k
      }
resip::StlPoolAllocator<resip::HeaderFieldValue, resip::PoolBase>::deallocate(resip::HeaderFieldValue*, unsigned long)
Line
Count
Source
95
14.7k
      {
96
14.7k
         deallocate_raw((void*)ptr);
97
14.7k
      }
resip::StlPoolAllocator<resip::ParserContainerBase::HeaderKit, resip::PoolBase>::deallocate(resip::ParserContainerBase::HeaderKit*, unsigned long)
Line
Count
Source
95
219
      {
96
219
         deallocate_raw((void*)ptr);
97
219
      }
resip::StlPoolAllocator<resip::Parameter*, resip::PoolBase>::deallocate(resip::Parameter**, unsigned long)
Line
Count
Source
95
42.3k
      {
96
42.3k
         deallocate_raw((void*)ptr);
97
42.3k
      }
resip::StlPoolAllocator<std::__1::__list_node<std::__1::pair<resip::Data, resip::HeaderFieldValueList*>, void*>, resip::PoolBase>::deallocate(std::__1::__list_node<std::__1::pair<resip::Data, resip::HeaderFieldValueList*>, void*>*, unsigned long)
Line
Count
Source
95
10.5k
      {
96
10.5k
         deallocate_raw((void*)ptr);
97
10.5k
      }
resip::StlPoolAllocator<resip::HeaderFieldValueList*, resip::PoolBase>::deallocate(resip::HeaderFieldValueList**, unsigned long)
Line
Count
Source
95
10.0k
      {
96
10.0k
         deallocate_raw((void*)ptr);
97
10.0k
      }
98
99
      inline void* allocate_raw(size_type bytes)
100
77.8k
      {
101
77.8k
         if(mPool)
102
35.9k
         {
103
35.9k
            return mPool->allocate(bytes);
104
35.9k
         }
105
41.9k
         return ::operator new(bytes);
106
77.8k
      }
resip::StlPoolAllocator<resip::HeaderFieldValue, resip::PoolBase>::allocate_raw(unsigned long)
Line
Count
Source
100
14.7k
      {
101
14.7k
         if(mPool)
102
14.7k
         {
103
14.7k
            return mPool->allocate(bytes);
104
14.7k
         }
105
0
         return ::operator new(bytes);
106
14.7k
      }
resip::StlPoolAllocator<resip::ParserContainerBase::HeaderKit, resip::PoolBase>::allocate_raw(unsigned long)
Line
Count
Source
100
219
      {
101
219
         if(mPool)
102
219
         {
103
219
            return mPool->allocate(bytes);
104
219
         }
105
0
         return ::operator new(bytes);
106
219
      }
resip::StlPoolAllocator<resip::Parameter*, resip::PoolBase>::allocate_raw(unsigned long)
Line
Count
Source
100
42.3k
      {
101
42.3k
         if(mPool)
102
408
         {
103
408
            return mPool->allocate(bytes);
104
408
         }
105
41.9k
         return ::operator new(bytes);
106
42.3k
      }
resip::StlPoolAllocator<resip::HeaderFieldValueList*, resip::PoolBase>::allocate_raw(unsigned long)
Line
Count
Source
100
10.0k
      {
101
10.0k
         if(mPool)
102
10.0k
         {
103
10.0k
            return mPool->allocate(bytes);
104
10.0k
         }
105
0
         return ::operator new(bytes);
106
10.0k
      }
resip::StlPoolAllocator<std::__1::__list_node<std::__1::pair<resip::Data, resip::HeaderFieldValueList*>, void*>, resip::PoolBase>::allocate_raw(unsigned long)
Line
Count
Source
100
10.5k
      {
101
10.5k
         if(mPool)
102
10.5k
         {
103
10.5k
            return mPool->allocate(bytes);
104
10.5k
         }
105
0
         return ::operator new(bytes);
106
10.5k
      }
107
108
      void deallocate_raw(void* ptr)
109
77.8k
      {
110
77.8k
         if(mPool)
111
35.9k
         {
112
35.9k
            mPool->deallocate(ptr);
113
35.9k
            return;
114
35.9k
         }
115
41.9k
         ::operator delete(ptr);
116
41.9k
      }
resip::StlPoolAllocator<resip::HeaderFieldValue, resip::PoolBase>::deallocate_raw(void*)
Line
Count
Source
109
14.7k
      {
110
14.7k
         if(mPool)
111
14.7k
         {
112
14.7k
            mPool->deallocate(ptr);
113
14.7k
            return;
114
14.7k
         }
115
0
         ::operator delete(ptr);
116
0
      }
resip::StlPoolAllocator<resip::ParserContainerBase::HeaderKit, resip::PoolBase>::deallocate_raw(void*)
Line
Count
Source
109
219
      {
110
219
         if(mPool)
111
219
         {
112
219
            mPool->deallocate(ptr);
113
219
            return;
114
219
         }
115
0
         ::operator delete(ptr);
116
0
      }
resip::StlPoolAllocator<resip::Parameter*, resip::PoolBase>::deallocate_raw(void*)
Line
Count
Source
109
42.3k
      {
110
42.3k
         if(mPool)
111
408
         {
112
408
            mPool->deallocate(ptr);
113
408
            return;
114
408
         }
115
41.9k
         ::operator delete(ptr);
116
41.9k
      }
resip::StlPoolAllocator<std::__1::__list_node<std::__1::pair<resip::Data, resip::HeaderFieldValueList*>, void*>, resip::PoolBase>::deallocate_raw(void*)
Line
Count
Source
109
10.5k
      {
110
10.5k
         if(mPool)
111
10.5k
         {
112
10.5k
            mPool->deallocate(ptr);
113
10.5k
            return;
114
10.5k
         }
115
0
         ::operator delete(ptr);
116
0
      }
resip::StlPoolAllocator<resip::HeaderFieldValueList*, resip::PoolBase>::deallocate_raw(void*)
Line
Count
Source
109
10.0k
      {
110
10.0k
         if(mPool)
111
10.0k
         {
112
10.0k
            mPool->deallocate(ptr);
113
10.0k
            return;
114
10.0k
         }
115
0
         ::operator delete(ptr);
116
0
      }
117
118
      size_type max_size() const
119
67.3k
      {
120
67.3k
         if(mPool)
121
25.3k
         {
122
25.3k
            return mPool->max_size();
123
25.3k
         }
124
41.9k
         return std::numeric_limits<size_type>::max()/sizeof(T);
125
67.3k
      }
resip::StlPoolAllocator<resip::HeaderFieldValue, resip::PoolBase>::max_size() const
Line
Count
Source
119
14.7k
      {
120
14.7k
         if(mPool)
121
14.7k
         {
122
14.7k
            return mPool->max_size();
123
14.7k
         }
124
0
         return std::numeric_limits<size_type>::max()/sizeof(T);
125
14.7k
      }
resip::StlPoolAllocator<resip::ParserContainerBase::HeaderKit, resip::PoolBase>::max_size() const
Line
Count
Source
119
219
      {
120
219
         if(mPool)
121
219
         {
122
219
            return mPool->max_size();
123
219
         }
124
0
         return std::numeric_limits<size_type>::max()/sizeof(T);
125
219
      }
resip::StlPoolAllocator<resip::Parameter*, resip::PoolBase>::max_size() const
Line
Count
Source
119
42.3k
      {
120
42.3k
         if(mPool)
121
408
         {
122
408
            return mPool->max_size();
123
408
         }
124
41.9k
         return std::numeric_limits<size_type>::max()/sizeof(T);
125
42.3k
      }
resip::StlPoolAllocator<resip::HeaderFieldValueList*, resip::PoolBase>::max_size() const
Line
Count
Source
119
10.0k
      {
120
10.0k
         if(mPool)
121
10.0k
         {
122
10.0k
            return mPool->max_size();
123
10.0k
         }
124
0
         return std::numeric_limits<size_type>::max()/sizeof(T);
125
10.0k
      }
126
127
      size_type max_size(size_type _sz) const
128
      {
129
         if(mPool)
130
         {
131
            return mPool->max_size();
132
         }
133
         return std::numeric_limits<size_type>::max()/_sz;
134
      }
135
136
      void construct(pointer p, const_reference orig)
137
54.6M
      {
138
54.6M
         new (p) T(orig);
139
54.6M
      }
resip::StlPoolAllocator<resip::HeaderFieldValue, resip::PoolBase>::construct(resip::HeaderFieldValue*, resip::HeaderFieldValue const&)
Line
Count
Source
137
2.61M
      {
138
2.61M
         new (p) T(orig);
139
2.61M
      }
resip::StlPoolAllocator<resip::ParserContainerBase::HeaderKit, resip::PoolBase>::construct(resip::ParserContainerBase::HeaderKit*, resip::ParserContainerBase::HeaderKit const&)
Line
Count
Source
137
219
      {
138
219
         new (p) T(orig);
139
219
      }
resip::StlPoolAllocator<resip::Parameter*, resip::PoolBase>::construct(resip::Parameter**, resip::Parameter* const&)
Line
Count
Source
137
52.0M
      {
138
52.0M
         new (p) T(orig);
139
52.0M
      }
resip::StlPoolAllocator<resip::HeaderFieldValueList*, resip::PoolBase>::construct(resip::HeaderFieldValueList**, resip::HeaderFieldValueList* const&)
Line
Count
Source
137
11.6k
      {
138
11.6k
         new (p) T(orig);
139
11.6k
      }
140
141
      void destroy(pointer ptr)
142
54.6M
      {
143
54.6M
         ptr->~T();
144
54.6M
      }
resip::StlPoolAllocator<resip::HeaderFieldValue, resip::PoolBase>::destroy(resip::HeaderFieldValue*)
Line
Count
Source
142
2.61M
      {
143
2.61M
         ptr->~T();
144
2.61M
      }
resip::StlPoolAllocator<resip::ParserContainerBase::HeaderKit, resip::PoolBase>::destroy(resip::ParserContainerBase::HeaderKit*)
Line
Count
Source
142
219
      {
143
219
         ptr->~T();
144
219
      }
resip::StlPoolAllocator<resip::Parameter*, resip::PoolBase>::destroy(resip::Parameter**)
Line
Count
Source
142
52.0M
      {
143
52.0M
         ptr->~T();
144
52.0M
      }
resip::StlPoolAllocator<resip::HeaderFieldValueList*, resip::PoolBase>::destroy(resip::HeaderFieldValueList**)
Line
Count
Source
142
11.6k
      {
143
11.6k
         ptr->~T();
144
11.6k
      }
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
156
      P* mPool;
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
 */