Files
externals/linux/include/ActivMiddleware/ActivBase/ThreadSpecificPoolAllocators.h
T
2023-12-15 13:15:08 -05:00

371 lines
8.6 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/ThreadSpecificPoolAllocators.h
*
* @brief Thread-specific boost (fast_)pool_allocator like classes.
*/
#if (!defined ACTIV_BASE_THREAD_SPECIFIC_POOL_ALLOCATORS_H)
#define ACTIV_BASE_THREAD_SPECIFIC_POOL_ALLOCATORS_H
#include "ActivMiddleware/ActivBase/ThreadSpecificData.h"
#include "ActivMiddleware/Misc/Singleton.h"
#include ACTIV_BOOST_INCLUDE(pool/poolfwd.hpp)
#include ACTIV_BOOST_INCLUDE(pool/pool.hpp)
//! Activ namespace.
namespace Activ
{
/**
* @brief Boost::pool_allocator like std allocator, except the pool is thread local rather than process wide.
*
* See http://boost.org/doc/libs/1_35_0/libs/pool/doc/interfaces/pool_alloc.html for more information.
*
* Memory must be allocated and freed from the same thread!
*
* This class is suited to any container type.
*/
template <typename T, typename UserAllocator = Boost::default_user_allocator_new_delete, unsigned NextSize = 32>
class ThreadSpecificPoolAllocator
{
public:
typedef T value_type;
typedef UserAllocator user_allocator;
static const unsigned next_size = NextSize;
typedef value_type * pointer;
typedef const value_type * const_pointer;
typedef value_type & reference;
typedef const value_type & const_reference;
typedef typename Boost::pool<UserAllocator>::size_type size_type;
typedef typename Boost::pool<UserAllocator>::difference_type difference_type;
template <typename U>
struct rebind
{
typedef ThreadSpecificPoolAllocator<U, UserAllocator, NextSize> other;
};
public:
ThreadSpecificPoolAllocator()
{
}
// default copy constructor
// default assignment operator
// not explicit, mimicking std::allocator [20.4.1]
template <typename U>
ThreadSpecificPoolAllocator(const ThreadSpecificPoolAllocator<U, UserAllocator, NextSize> &)
{
}
static pointer address(reference r)
{
return &r;
}
static const_pointer address(const_reference s)
{
return &s;
}
static size_type max_size()
{
return Boost::integer_traits<size_type>::const_max;
}
static void construct(const pointer ptr, const value_type &t)
{
new (ptr) T(t);
}
static void destroy(const pointer ptr)
{
ptr->~T();
(void) ptr; // avoid unused variable warning
}
bool operator==(const ThreadSpecificPoolAllocator &) const
{
return true;
}
bool operator!=(const ThreadSpecificPoolAllocator &) const
{
return false;
}
static pointer allocate(const size_type n)
{
Pool *pPool = ThreadSingleton::GetInstance().m_threadInfo.Get();
if (ACTIV_UNLIKELY(0 == pPool))
pPool = CreatePool();
const pointer ret = static_cast<pointer>(pPool->ordered_malloc(n));
if (ACTIV_UNLIKELY(0 == ret))
throw std::bad_alloc();
return ret;
}
static pointer allocate(const size_type n, const void * const)
{
return allocate(n);
}
static void deallocate(const pointer ptr, const size_type n)
{
Pool *pPool = ThreadSingleton::GetInstance().m_threadInfo.Get();
// If there's no pool for this thread, then the memory can't have been allocated by this thread, either.
if (ACTIV_UNLIKELY(0 == pPool))
{
assert(false);
return;
}
pPool->free(ptr, n);
}
/**
* @brief ACTIV interface addition: release memory.
*
* Frees every memory block that doesn't have any allocated chunks.
*
* @return true if at least one memory block was freed.
*/
static bool ReleaseMemory()
{
Pool *pPool = ThreadSingleton::GetInstance().m_threadInfo.Get();
if (ACTIV_UNLIKELY(0 == pPool))
return false;
return pPool->release_memory();
}
private:
typedef Boost::pool<UserAllocator> Pool;
typedef Activ::ThreadSpecificData<Pool, DeleteObject> ThreadInfo;
class ThreadSingleton : public Singleton<ThreadSingleton>
{
public:
ThreadInfo m_threadInfo;
static ACTIV_NEVER_INLINE ThreadSingleton &GetInstance()
{
static ThreadSingleton threadSingleton;
return threadSingleton;
}
private:
friend class Singleton<ThreadSingleton>;
};
static ACTIV_NEVER_INLINE Pool *CreatePool()
{
Pool * const pPool = new Pool(sizeof(T), NextSize);
ThreadSingleton::GetInstance().m_threadInfo.Set(pPool);
return pPool;
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Boost::fast_pool_allocator like std allocator, except pool is thread local rather than process wide.
*
* See http://boost.org/doc/libs/1_35_0/libs/pool/doc/interfaces/pool_alloc.html for more information.
*
* Memory must be allocated and freed from the same thread! There is no global "return allocated memory to pool"
* function for fast allocators, so ensure all allocated memory is deallocated before the allocator thread exits.
*
* This class is better suited to containers where single nodes are allocated (e.g. set, list, etc.) rather than
* contiguous blocks of nodes (e.g. vector, possibly hash_map).
*/
template <typename T, typename UserAllocator = Boost::default_user_allocator_new_delete, unsigned NextSize = 32>
class ThreadSpecificFastPoolAllocator
{
public:
typedef T value_type;
typedef UserAllocator user_allocator;
static const unsigned next_size = NextSize;
typedef value_type * pointer;
typedef const value_type * const_pointer;
typedef value_type & reference;
typedef const value_type & const_reference;
typedef typename Boost::pool<UserAllocator>::size_type size_type;
typedef typename Boost::pool<UserAllocator>::difference_type difference_type;
template <typename U>
struct rebind
{
typedef ThreadSpecificFastPoolAllocator<U, UserAllocator, NextSize> other;
};
public:
ThreadSpecificFastPoolAllocator()
{
}
// default copy constructor
// default assignment operator
// not explicit, mimicking std::allocator [20.4.1]
template <typename U>
ThreadSpecificFastPoolAllocator(const ThreadSpecificFastPoolAllocator<U, UserAllocator, NextSize> &)
{
}
static pointer address(reference r)
{
return &r;
}
static const_pointer address(const_reference s)
{
return &s;
}
static size_type max_size()
{
return Boost::integer_traits<size_type>::const_max;
}
static void construct(const pointer ptr, const value_type &t)
{
new (ptr) T(t);
}
static void destroy(const pointer ptr)
{
ptr->~T();
(void) ptr; // avoid unused variable warning
}
bool operator==(const ThreadSpecificFastPoolAllocator &) const
{
return true;
}
bool operator!=(const ThreadSpecificFastPoolAllocator &) const
{
return false;
}
static pointer allocate(const size_type n)
{
Pool *pPool = ThreadSingleton::GetInstance().m_threadInfo.Get();
if (ACTIV_UNLIKELY(0 == pPool))
pPool = CreatePool();
const pointer ret = (1 == n) ?
static_cast<pointer>(pPool->malloc()) :
static_cast<pointer>(pPool->ordered_malloc(n));
if (ACTIV_UNLIKELY(0 == ret))
throw std::bad_alloc();
return ret;
}
static pointer allocate(const size_type n, const void * const)
{
return allocate(n);
}
static pointer allocate()
{
Pool *pPool = ThreadSingleton::GetInstance().m_threadInfo.Get();
if (ACTIV_UNLIKELY(0 == pPool))
pPool = CreatePool();
const pointer ret = static_cast<pointer>(pPool->malloc());
if (ACTIV_UNLIKELY(0 == ret))
throw std::bad_alloc();
return ret;
}
static void deallocate(const pointer ptr, const size_type n)
{
Pool *pPool = ThreadSingleton::GetInstance().m_threadInfo.Get();
// If there's no pool for this thread, then the memory can't have been allocated by this thread, either.
if (ACTIV_UNLIKELY(0 == pPool))
{
assert(false);
return;
}
if (1 == n)
pPool->free(ptr);
else
pPool->free(ptr, n);
}
static void deallocate(const pointer ptr)
{
Pool *pPool = ThreadSingleton::GetInstance().m_threadInfo.Get();
// If there's no pool for this thread, then the memory can't have been allocated by this thread, either.
if (ACTIV_UNLIKELY(0 == pPool))
{
assert(false);
return;
}
return pPool->deallocate(ptr);
}
private:
typedef Boost::pool<UserAllocator> Pool;
typedef Activ::ThreadSpecificData<Pool, DeleteObject> ThreadInfo;
class ThreadSingleton : public Singleton<ThreadSingleton>
{
public:
ThreadInfo m_threadInfo;
static ACTIV_NEVER_INLINE ThreadSingleton &GetInstance()
{
static ThreadSingleton threadSingleton;
return threadSingleton;
}
private:
friend class Singleton<ThreadSingleton>;
};
static ACTIV_NEVER_INLINE Pool *CreatePool()
{
Pool * const pPool = new Pool(sizeof(T), NextSize);
ThreadSingleton::GetInstance().m_threadInfo.Set(pPool);
return pPool;
}
};
} // namespace Activ
#endif // (!defined ACTIV_BASE_THREAD_SPECIFIC_POOL_ALLOCATORS_H)