|
@@ -29,19 +29,20 @@ namespace anki {
|
|
|
/// This is a template that accepts memory pools with a specific interface
|
|
/// This is a template that accepts memory pools with a specific interface
|
|
|
///
|
|
///
|
|
|
/// @tparam T The type
|
|
/// @tparam T The type
|
|
|
-/// @tparam deallocationFlag If true then the allocator will try to deallocate
|
|
|
|
|
-/// the memory. It is extremely important to
|
|
|
|
|
-/// understand when it should be true. See notes
|
|
|
|
|
|
|
+/// @tparam checkFree If false then the allocator will throw an exception
|
|
|
|
|
+/// if the free() method of the memory pool returns false.
|
|
|
|
|
+/// It is extremely important to understand when it should be
|
|
|
|
|
+/// true. See the notes.
|
|
|
///
|
|
///
|
|
|
-/// @note The deallocationFlag can brake the allocator when used with stack
|
|
|
|
|
-/// pools and the deallocations are not in the correct order.
|
|
|
|
|
|
|
+/// @note The checkFree can brake the allocator when used with stack pools
|
|
|
|
|
+/// and the deallocations are not in the correct order.
|
|
|
///
|
|
///
|
|
|
/// @note Don't ever EVER remove the double copy constructor and the double
|
|
/// @note Don't ever EVER remove the double copy constructor and the double
|
|
|
/// operator=. The compiler will create defaults
|
|
/// operator=. The compiler will create defaults
|
|
|
-template<typename T, typename TPool, Bool deallocationFlag = false>
|
|
|
|
|
|
|
+template<typename T, typename TPool, Bool checkFree = false>
|
|
|
class GenericPoolAllocator
|
|
class GenericPoolAllocator
|
|
|
{
|
|
{
|
|
|
- template<typename Y, typename TPool_, Bool deallocationFlag_>
|
|
|
|
|
|
|
+ template<typename Y, typename TPool_, Bool checkFree_>
|
|
|
friend class GenericPoolAllocator;
|
|
friend class GenericPoolAllocator;
|
|
|
|
|
|
|
|
public:
|
|
public:
|
|
@@ -63,7 +64,7 @@ public:
|
|
|
template<typename Y>
|
|
template<typename Y>
|
|
|
struct rebind
|
|
struct rebind
|
|
|
{
|
|
{
|
|
|
- typedef GenericPoolAllocator<Y, TPool, deallocationFlag> other;
|
|
|
|
|
|
|
+ typedef GenericPoolAllocator<Y, TPool, checkFree> other;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
/// Default constructor
|
|
/// Default constructor
|
|
@@ -79,7 +80,7 @@ public:
|
|
|
/// Copy constructor
|
|
/// Copy constructor
|
|
|
template<typename Y>
|
|
template<typename Y>
|
|
|
GenericPoolAllocator(const GenericPoolAllocator<
|
|
GenericPoolAllocator(const GenericPoolAllocator<
|
|
|
- Y, TPool, deallocationFlag>& b) noexcept
|
|
|
|
|
|
|
+ Y, TPool, checkFree>& b) noexcept
|
|
|
{
|
|
{
|
|
|
*this = b;
|
|
*this = b;
|
|
|
}
|
|
}
|
|
@@ -103,7 +104,7 @@ public:
|
|
|
/// Copy
|
|
/// Copy
|
|
|
template<typename U>
|
|
template<typename U>
|
|
|
GenericPoolAllocator& operator=(const GenericPoolAllocator<
|
|
GenericPoolAllocator& operator=(const GenericPoolAllocator<
|
|
|
- U, TPool, deallocationFlag>& b)
|
|
|
|
|
|
|
+ U, TPool, checkFree>& b)
|
|
|
{
|
|
{
|
|
|
m_pool = b.m_pool;
|
|
m_pool = b.m_pool;
|
|
|
return *this;
|
|
return *this;
|
|
@@ -150,18 +151,14 @@ public:
|
|
|
/// Deallocate memory
|
|
/// Deallocate memory
|
|
|
void deallocate(void* p, size_type n)
|
|
void deallocate(void* p, size_type n)
|
|
|
{
|
|
{
|
|
|
- (void)p;
|
|
|
|
|
(void)n;
|
|
(void)n;
|
|
|
|
|
|
|
|
- if(deallocationFlag)
|
|
|
|
|
- {
|
|
|
|
|
- Bool ok = m_pool.free(p);
|
|
|
|
|
|
|
+ Bool ok = m_pool.free(p);
|
|
|
|
|
|
|
|
- if(!ok)
|
|
|
|
|
- {
|
|
|
|
|
- throw ANKI_EXCEPTION("Freeing wrong pointer. "
|
|
|
|
|
- "Pool's free returned false");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if(checkFree && !ok)
|
|
|
|
|
+ {
|
|
|
|
|
+ throw ANKI_EXCEPTION("Freeing wrong pointer. "
|
|
|
|
|
+ "Pool's free returned false");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -275,38 +272,38 @@ private:
|
|
|
/// @{
|
|
/// @{
|
|
|
|
|
|
|
|
/// Another allocator of the same type can deallocate from this one
|
|
/// Another allocator of the same type can deallocate from this one
|
|
|
-template<typename T1, typename T2, typename TPool, Bool deallocationFlag>
|
|
|
|
|
|
|
+template<typename T1, typename T2, typename TPool, Bool checkFree>
|
|
|
inline bool operator==(
|
|
inline bool operator==(
|
|
|
- const GenericPoolAllocator<T1, TPool, deallocationFlag>&,
|
|
|
|
|
- const GenericPoolAllocator<T2, TPool, deallocationFlag>&)
|
|
|
|
|
|
|
+ const GenericPoolAllocator<T1, TPool, checkFree>&,
|
|
|
|
|
+ const GenericPoolAllocator<T2, TPool, checkFree>&)
|
|
|
{
|
|
{
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Another allocator of the another type cannot deallocate from this one
|
|
/// Another allocator of the another type cannot deallocate from this one
|
|
|
template<typename T1, typename AnotherAllocator, typename TPool,
|
|
template<typename T1, typename AnotherAllocator, typename TPool,
|
|
|
- Bool deallocationFlag>
|
|
|
|
|
|
|
+ Bool checkFree>
|
|
|
inline bool operator==(
|
|
inline bool operator==(
|
|
|
- const GenericPoolAllocator<T1, TPool, deallocationFlag>&,
|
|
|
|
|
|
|
+ const GenericPoolAllocator<T1, TPool, checkFree>&,
|
|
|
const AnotherAllocator&)
|
|
const AnotherAllocator&)
|
|
|
{
|
|
{
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Another allocator of the same type can deallocate from this one
|
|
/// Another allocator of the same type can deallocate from this one
|
|
|
-template<typename T1, typename T2, typename TPool, Bool deallocationFlag>
|
|
|
|
|
|
|
+template<typename T1, typename T2, typename TPool, Bool checkFree>
|
|
|
inline bool operator!=(
|
|
inline bool operator!=(
|
|
|
- const GenericPoolAllocator<T1, TPool, deallocationFlag>&,
|
|
|
|
|
- const GenericPoolAllocator<T2, TPool, deallocationFlag>&)
|
|
|
|
|
|
|
+ const GenericPoolAllocator<T1, TPool, checkFree>&,
|
|
|
|
|
+ const GenericPoolAllocator<T2, TPool, checkFree>&)
|
|
|
{
|
|
{
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Another allocator of the another type cannot deallocate from this one
|
|
/// Another allocator of the another type cannot deallocate from this one
|
|
|
template<typename T1, typename AnotherAllocator, typename TPool,
|
|
template<typename T1, typename AnotherAllocator, typename TPool,
|
|
|
- Bool deallocationFlag>
|
|
|
|
|
|
|
+ Bool checkFree>
|
|
|
inline bool operator!=(
|
|
inline bool operator!=(
|
|
|
- const GenericPoolAllocator<T1, TPool, deallocationFlag>&,
|
|
|
|
|
|
|
+ const GenericPoolAllocator<T1, TPool, checkFree>&,
|
|
|
const AnotherAllocator&)
|
|
const AnotherAllocator&)
|
|
|
{
|
|
{
|
|
|
return true;
|
|
return true;
|
|
@@ -321,14 +318,14 @@ using HeapAllocator =
|
|
|
GenericPoolAllocator<T, HeapMemoryPool, true>;
|
|
GenericPoolAllocator<T, HeapMemoryPool, true>;
|
|
|
|
|
|
|
|
/// Allocator that uses a StackMemoryPool
|
|
/// Allocator that uses a StackMemoryPool
|
|
|
-template<typename T, Bool deallocationFlag = false>
|
|
|
|
|
|
|
+template<typename T, Bool checkFree = false>
|
|
|
using StackAllocator =
|
|
using StackAllocator =
|
|
|
- GenericPoolAllocator<T, StackMemoryPool, deallocationFlag>;
|
|
|
|
|
|
|
+ GenericPoolAllocator<T, StackMemoryPool, checkFree>;
|
|
|
|
|
|
|
|
/// Allocator that uses a ChainMemoryPool
|
|
/// Allocator that uses a ChainMemoryPool
|
|
|
-template<typename T, Bool deallocationFlag = true>
|
|
|
|
|
|
|
+template<typename T, Bool checkFree = true>
|
|
|
using ChainAllocator =
|
|
using ChainAllocator =
|
|
|
- GenericPoolAllocator<T, ChainMemoryPool, deallocationFlag>;
|
|
|
|
|
|
|
+ GenericPoolAllocator<T, ChainMemoryPool, checkFree>;
|
|
|
|
|
|
|
|
/// @}
|
|
/// @}
|
|
|
|
|
|