Skip to content

Commit cc46303

Browse files
committed
std::allocator compatibility fix
1 parent ff895ce commit cc46303

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

‎include/shared_allocator/cached_allocator.hpp

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,11 @@ namespace salloc {
5555
{
5656
protected:
5757

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;
6260

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
6563

6664
public:
6765

@@ -82,7 +80,7 @@ namespace salloc {
8280
template <class U>
8381
struct rebind
8482
{
85-
typedef cached_allocator<U> other;
83+
typedef cached_allocator<U, typename TAlloc::template rebind<U>::other> other;
8684
};
8785

8886
/** \brief Returns an actual adress of value.
@@ -112,38 +110,44 @@ namespace salloc {
112110
113111
Does nothing. */
114112
template <class U>
115-
cached_allocator(const cached_allocator<U>&) throw()
113+
cached_allocator(const U&) throw()
116114
{
117115
}
118116

119117
/** \brief Move constructor.
120118
121119
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))
123121
{
124122
}
125123

126124
/** \brief Empty assignment operator.
127125
128126
Does nothing and returns reference to this allocator. */
129127
template <class U>
130-
cached_allocator<T>& operator=(const cached_allocator<U>&)
128+
ThisType& operator=(const U&)
131129
{
132130
return *this;
133131
}
134132

135133
~cached_allocator()
136134
{
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();
141145
}
142146

143147
/** \brief Swaps two allocators with their cache.
144148
145149
\param _anotherAlloc Reference to another allocator. */
146-
void swap(cached_allocator<T>& _anotherAlloc)
150+
void swap(ThisType& _anotherAlloc)
147151
{
148152
m_memoryCache.swap(_anotherAlloc.m_memoryCache);
149153
}
@@ -187,9 +191,9 @@ namespace salloc {
187191
/** \brief Truly deallocates memory.
188192
189193
\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)
191195
{
192-
m_allocator.deallocate(_memory);
196+
m_allocator.deallocate(_memory, 0);
193197
}
194198

195199
/** \brief Allocate elements.
@@ -211,7 +215,7 @@ namespace salloc {
211215
212216
\param _number Required number of elements.
213217
\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)
215219
{
216220
if (!m_memoryCache.empty())
217221
{
@@ -226,9 +230,9 @@ namespace salloc {
226230
/** \brief Construct new object on preallocated memory using default constructor.
227231
228232
\param _singleObject Pointer to preallocated memory. */
229-
inline void construct(T* _singleObject) const
233+
inline void construct(T* _singleObject)
230234
{
231-
shared_construct(_singleObject);
235+
m_allocator.construct(_singleObject);
232236
}
233237

234238
/** \brief Construct new object on preallocated memory using copy-constructor.
@@ -239,9 +243,9 @@ namespace salloc {
239243
\note Declared as template function to make it possible to use this allocator with
240244
types without public copy-constructor. */
241245
template <class U>
242-
inline void construct(U* _singleObject, const U& _value) const
246+
inline void construct(U* _singleObject, const U& _value)
243247
{
244-
shared_construct(_singleObject, _value);
248+
m_allocator.construct(_singleObject, _value);
245249
}
246250

247251
/** \brief Construct new object on preallocated memory using move-constructor.
@@ -252,9 +256,9 @@ namespace salloc {
252256
\note Declared as template function to make it possible to use this allocator with
253257
types without public move-constructor. */
254258
template <class U>
255-
inline void construct(U* _singleObject, U&& _value) const
259+
inline void construct(U* _singleObject, U&& _value)
256260
{
257-
shared_construct(_singleObject, _value);
261+
m_allocator.construct(_singleObject, _value);
258262
}
259263

260264
/** \brief Construct new object on preallocated memory using arguments list.
@@ -265,7 +269,7 @@ namespace salloc {
265269
\note Declared as template function to make it possible to use this allocator with
266270
types without specific constructor with arguments. */
267271
template <class U, class... TArgs>
268-
inline void construct(U* _singleObject, TArgs&&... _constructorArguments) const
272+
inline void construct(U* _singleObject, TArgs&&... _constructorArguments)
269273
{
270274
::new (static_cast<void*>(_singleObject)) U(::std::forward<TArgs>(_constructorArguments)...);
271275
}
@@ -279,9 +283,9 @@ namespace salloc {
279283
\note Declared as template function to make it possible to use this allocator with
280284
types without public destructor. */
281285
template <class U>
282-
inline void destroy(U* _singleObject) const
286+
inline void destroy(U* _singleObject)
283287
{
284-
_singleObject->~U();
288+
m_allocator.destroy(_singleObject);
285289
}
286290

287291
/** \brief Estimate maximum array size. */
@@ -290,21 +294,44 @@ namespace salloc {
290294
return (size_t)(-1) / sizeof(T);
291295
}
292296

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+
293308
}; // END class single_cached_allocator<T>.
294309

295310
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
296311

297312
// Using single_cached_allocator for itself is restricted.
298313
template <class T, class U>
299-
class cached_allocator<T, cached_allocator<U> >;
314+
class cached_allocator<T, ::salloc::cached_allocator<U> >;
300315

301316
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
302317

303318
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> >;
305320

306321
} // END namespace salloc.
307322

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+
308335
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
309336
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
310337

0 commit comments

Comments
 (0)