@@ -55,13 +55,11 @@ namespace salloc {
55
55
{
56
56
protected:
57
57
58
- template <class U >
59
- using TSameAlloc = typename TAlloc::template rebind<U>::other;
60
-
61
- typedef ::std::vector<T*, TSameAlloc<T*> > MemoryCache;
58
+ typedef cached_allocator<T, TAlloc> ThisType;
59
+ typedef ::std::vector<T*, typename TAlloc::template rebind<T*>::other> MemoryCache;
62
60
63
- MemoryCache m_memoryCache; // /< Memory cache of objects
64
- const TAlloc m_allocator; // /< Allocator to allocate new instances of T
61
+ MemoryCache m_memoryCache; // /< Memory cache of objects
62
+ TAlloc m_allocator; // /< Allocator to allocate new instances of T
65
63
66
64
public:
67
65
@@ -82,7 +80,7 @@ namespace salloc {
82
80
template <class U >
83
81
struct rebind
84
82
{
85
- typedef cached_allocator<U> other;
83
+ typedef cached_allocator<U, typename TAlloc:: template rebind<U>::other > other;
86
84
};
87
85
88
86
/* * \brief Returns an actual adress of value.
@@ -112,38 +110,44 @@ namespace salloc {
112
110
113
111
Does nothing. */
114
112
template <class U >
115
- cached_allocator (const cached_allocator<U> &) throw ()
113
+ cached_allocator (const U &) throw ()
116
114
{
117
115
}
118
116
119
117
/* * \brief Move constructor.
120
118
121
119
Moves reserved memory. */
122
- cached_allocator (cached_allocator<T> && _rvalueAlloc) : m_memoryCache(::std::move(_rvalueAlloc.m_memoryCache))
120
+ cached_allocator (ThisType && _rvalueAlloc) : m_memoryCache(::std::move(_rvalueAlloc.m_memoryCache))
123
121
{
124
122
}
125
123
126
124
/* * \brief Empty assignment operator.
127
125
128
126
Does nothing and returns reference to this allocator. */
129
127
template <class U >
130
- cached_allocator<T> & operator =(const cached_allocator<U> &)
128
+ ThisType & operator =(const U &)
131
129
{
132
130
return *this ;
133
131
}
134
132
135
133
~cached_allocator ()
136
134
{
137
- for (auto pMem : m_memoryCache)
138
- {
139
- m_allocator.deallocate (pMem);
140
- }
135
+ private_clear ();
136
+ }
137
+
138
+ /* * \brief Clear memory cache.
139
+
140
+ Also deallocates all cached memory. */
141
+ void clear ()
142
+ {
143
+ private_clear ();
144
+ m_memoryCache.clear ();
141
145
}
142
146
143
147
/* * \brief Swaps two allocators with their cache.
144
148
145
149
\param _anotherAlloc Reference to another allocator. */
146
- void swap (cached_allocator<T> & _anotherAlloc)
150
+ void swap (ThisType & _anotherAlloc)
147
151
{
148
152
m_memoryCache.swap (_anotherAlloc.m_memoryCache );
149
153
}
@@ -187,9 +191,9 @@ namespace salloc {
187
191
/* * \brief Truly deallocates memory.
188
192
189
193
\param _memory Pointer to allocated memory. */
190
- inline void deallocate_force (pointer _memory, size_type = 0 ) const
194
+ inline void deallocate_force (pointer _memory, size_type = 0 )
191
195
{
192
- m_allocator.deallocate (_memory);
196
+ m_allocator.deallocate (_memory, 0 );
193
197
}
194
198
195
199
/* * \brief Allocate elements.
@@ -211,7 +215,7 @@ namespace salloc {
211
215
212
216
\param _number Required number of elements.
213
217
\param _currentMemory Pointer to memory allocated earlier (it contains size which will be used as a hint). */
214
- pointer allocate (size_type _number, void * _currentMemory) const
218
+ pointer allocate (size_type _number, void * _currentMemory)
215
219
{
216
220
if (!m_memoryCache.empty ())
217
221
{
@@ -226,9 +230,9 @@ namespace salloc {
226
230
/* * \brief Construct new object on preallocated memory using default constructor.
227
231
228
232
\param _singleObject Pointer to preallocated memory. */
229
- inline void construct (T* _singleObject) const
233
+ inline void construct (T* _singleObject)
230
234
{
231
- shared_construct (_singleObject);
235
+ m_allocator. construct (_singleObject);
232
236
}
233
237
234
238
/* * \brief Construct new object on preallocated memory using copy-constructor.
@@ -239,9 +243,9 @@ namespace salloc {
239
243
\note Declared as template function to make it possible to use this allocator with
240
244
types without public copy-constructor. */
241
245
template <class U >
242
- inline void construct (U* _singleObject, const U& _value) const
246
+ inline void construct (U* _singleObject, const U& _value)
243
247
{
244
- shared_construct (_singleObject, _value);
248
+ m_allocator. construct (_singleObject, _value);
245
249
}
246
250
247
251
/* * \brief Construct new object on preallocated memory using move-constructor.
@@ -252,9 +256,9 @@ namespace salloc {
252
256
\note Declared as template function to make it possible to use this allocator with
253
257
types without public move-constructor. */
254
258
template <class U >
255
- inline void construct (U* _singleObject, U&& _value) const
259
+ inline void construct (U* _singleObject, U&& _value)
256
260
{
257
- shared_construct (_singleObject, _value);
261
+ m_allocator. construct (_singleObject, _value);
258
262
}
259
263
260
264
/* * \brief Construct new object on preallocated memory using arguments list.
@@ -265,7 +269,7 @@ namespace salloc {
265
269
\note Declared as template function to make it possible to use this allocator with
266
270
types without specific constructor with arguments. */
267
271
template <class U , class ... TArgs>
268
- inline void construct (U* _singleObject, TArgs&&... _constructorArguments) const
272
+ inline void construct (U* _singleObject, TArgs&&... _constructorArguments)
269
273
{
270
274
::new (static_cast <void *>(_singleObject)) U (::std::forward<TArgs>(_constructorArguments)...);
271
275
}
@@ -279,9 +283,9 @@ namespace salloc {
279
283
\note Declared as template function to make it possible to use this allocator with
280
284
types without public destructor. */
281
285
template <class U >
282
- inline void destroy (U* _singleObject) const
286
+ inline void destroy (U* _singleObject)
283
287
{
284
- _singleObject-> ~U ( );
288
+ m_allocator. destroy (_singleObject );
285
289
}
286
290
287
291
/* * \brief Estimate maximum array size. */
@@ -290,21 +294,44 @@ namespace salloc {
290
294
return (size_t )(-1 ) / sizeof (T);
291
295
}
292
296
297
+ protected:
298
+
299
+ /* * \brief Deallocates all memory stored in memory cache. */
300
+ inline void private_clear ()
301
+ {
302
+ for (auto pMem : m_memoryCache)
303
+ {
304
+ m_allocator.deallocate (pMem, 0 );
305
+ }
306
+ }
307
+
293
308
}; // END class single_cached_allocator<T>.
294
309
295
310
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
296
311
297
312
// Using single_cached_allocator for itself is restricted.
298
313
template <class T , class U >
299
- class cached_allocator <T, cached_allocator<U> >;
314
+ class cached_allocator <T, ::salloc:: cached_allocator<U> >;
300
315
301
316
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
302
317
303
318
template <class T >
304
- using local_cached_allocator = cached_allocator<T, std::allocator<T> >;
319
+ using local_cached_allocator = ::salloc:: cached_allocator<T, :: std::allocator<T> >;
305
320
306
321
} // END namespace salloc.
307
322
323
+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
324
+
325
+ namespace std {
326
+
327
+ template <class T , class TAlloc >
328
+ inline void swap (::salloc::cached_allocator<T, TAlloc>& _left, ::salloc::cached_allocator<T, TAlloc>& _right)
329
+ {
330
+ _left.swap (_right);
331
+ }
332
+
333
+ } // END namespace std.
334
+
308
335
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
309
336
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
310
337
0 commit comments