Files

272 lines
9.2 KiB
C++

/**
* @file include/ActivMiddleware/Misc/PoolAllocatorHelper.h
*
* @brief Wrapper classes for Boost's (fast_)pool_allocator to add C++11 support.
*/
#if (!defined ACTIV_MISC_POOL_ALLOCATOR_HELPER_H)
#define ACTIV_MISC_POOL_ALLOCATOR_HELPER_H
#include "ActivMiddleware/ActivMiddleware.h"
#include ACTIV_BOOST_INCLUDE(config.hpp)
#if ((!defined ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES) && (!defined ACTIV_BOOST_NO_CXX11_TEMPLATE_ALIASES) && \
(!defined ACTIV_BOOST_NO_TEMPLATE_TEMPLATES))
#include ACTIV_BOOST_INCLUDE(pool/pool_alloc.hpp)
#include <type_traits>
#if (defined ACTIV_PLATFORM_POSIX)
#include <sys/mman.h>
#endif
//! Activ namespace.
namespace Activ
{
/**
* @brief Boost's pool_allocator now properly supports C++11, so this typedef only exists for backwards compatibility.
*/
template <
typename T,
typename UserAllocator = Boost::default_user_allocator_new_delete,
typename Mutex = Boost::details::pool::default_mutex,
unsigned NextSize = 32,
unsigned MaxSize = 0>
using Cpp11PoolAllocator = Boost::pool_allocator<T, UserAllocator, Mutex, NextSize, MaxSize>;
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Boost's fast_pool_allocator now properly supports C++11, so this typedef only exists for backwards compatibility.
*/
template <
typename T,
typename UserAllocator = Boost::default_user_allocator_new_delete,
typename Mutex = Boost::details::pool::default_mutex,
unsigned NextSize = 32,
unsigned MaxSize = 0>
using Cpp11FastPoolAllocator = Boost::fast_pool_allocator<T, UserAllocator, Mutex, NextSize, MaxSize>;
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
namespace Detail
{
/**
* @brief Pool allocator that allocates a given memory block size, rather than a given number of objects.
*
* Do not use this templatized version directly, use the PoolBlockAllocator and FastPoolBlockAllocator typedefs below.
*
* Ideally this would derive from BaseAllocatorT, however the sizeof(T) is sometimes not known at class instaciation because the
* type we are trying to allocate is rebound to some other type (e.g. a tree node) that has only been forward declared.
* Most of the allocator functions are static, and sizeof(T) must be known by the time any of the functions are called,
* so we work out the base type in each function call using BaseAllocator<>().
*/
template <
template<typename, typename, typename, unsigned, unsigned> class BaseAllocatorT,
typename T,
unsigned BlockSize,
typename UserAllocator,
typename Mutex>
class PoolBlockAllocatorT
{
public:
using value_type = T;
ACTIV_BOOST_STATIC_CONSTEXPR unsigned block_size = BlockSize;
using user_allocator = UserAllocator;
using mutex = Mutex;
using pointer = value_type *;
using const_pointer = const value_type *;
using reference = value_type &;
using const_reference = const value_type &;
using size_type = typename Boost::pool<UserAllocator>::size_type;
using difference_type = typename Boost::pool<UserAllocator>::difference_type;
private:
// PooledObjectCount and BaseAllocator are type-erased so that the exact types are not calculated until they are used.
// Otherwise we may get an error because sizeof(T) may not be known when the class is instanciated.
/**
* @brief Meta-function to calculate the number of objects that can fit in a memory block.
* Note that Boost::pool reserves 2 * size_t of the buffer for it's own housekeeping.
*/
template <typename T2 = T>
using PooledObjectCount = std::integral_constant<unsigned, (BlockSize - (2 * sizeof(size_t))) / sizeof(T2)>;
//! The underlying pool allocator that we will use.
template <typename T2 = T>
using BaseAllocator = BaseAllocatorT<T2, UserAllocator, Mutex, PooledObjectCount<T2>::value, PooledObjectCount<T2>::value>;
public:
template <typename U>
struct rebind
{
using other = PoolBlockAllocatorT<BaseAllocatorT, U, BlockSize, UserAllocator, Mutex>;
};
PoolBlockAllocatorT()
{
static_assert(PooledObjectCount<>::value != 0, "BlockSize size is too small to hold a T");
// Call the base constructor via a temporary to ensure the underlying singleton pool is created.
// Must use C++11 squigly-bracket "uniform initialization" or the compiler thinks we are declaring a function.
BaseAllocator<>{};
}
template <typename U>
PoolBlockAllocatorT(const PoolBlockAllocatorT<BaseAllocatorT, U, BlockSize, UserAllocator, Mutex> &rhs)
{
static_assert(PooledObjectCount<>::value != 0, "BlockSize size is too small to hold a T");
// Call the base constructor to ensure the underlying singleton pool is created.
// Must use C++11 squigly-bracket "uniform initialization" or the compiler thinks we are declaring a function.
BaseAllocator<>{};
}
static pointer address(reference r)
{
return BaseAllocator<>::address(r);
}
static const_pointer address(const_reference r)
{
return BaseAllocator<>::address(r);
}
static size_type max_size()
{
return BaseAllocator<>::max_size();
}
template <typename U, typename... Args>
void construct(U * ptr, Args&&... args)
{
new (ptr) U(std::forward<Args>(args)...);
}
void destroy(const pointer ptr)
{
ptr->~T();
}
bool operator==(const PoolBlockAllocatorT &) const
{
return true;
}
bool operator!=(const PoolBlockAllocatorT &) const
{
return false;
}
static pointer allocate(const size_type n)
{
return BaseAllocator<>::allocate(n);
}
static pointer allocate(const size_type n, const void * const pLocalityHint)
{
return BaseAllocator<>::allocate(n, pLocalityHint);
}
static pointer allocate()
{
return BaseAllocator<>::allocate();
}
static void deallocate(const pointer ptr, const size_type n)
{
return BaseAllocator<>::deallocate(ptr, n);
}
};
} // namespace Detail
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Pool allocator that allocates a given memory block size, rather than a given number of objects.
*/
template <
typename T,
unsigned BlockSize,
typename UserAllocator = Boost::default_user_allocator_new_delete,
typename Mutex = Boost::details::pool::default_mutex>
using PoolBlockAllocator = Detail::PoolBlockAllocatorT<Boost::pool_allocator, T, BlockSize, UserAllocator, Mutex>;
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Fast pool allocator that allocates a given memory block size, rather than a given number of objects.
*/
template <
typename T,
unsigned BlockSize,
typename UserAllocator = Boost::default_user_allocator_new_delete,
typename Mutex = Boost::details::pool::default_mutex>
using FastPoolBlockAllocator = Detail::PoolBlockAllocatorT<Boost::fast_pool_allocator, T, BlockSize, UserAllocator, Mutex>;
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
#if (defined ACTIV_PLATFORM_POSIX)
/**
* @brief User allocator for Boost:pool that uses huge pages
*/
struct HugePageMalloc
{
using size_type = size_t; //!< An unsigned integral type that can represent the size of the largest object to be allocated.
using difference_type = ptrdiff_t; //!< A signed integral type that can represent the difference of any two pointers.
using HugePageSize = std::integral_constant<size_t, (2 * 1024 * 1024)>;
static char * malloc ACTIV_BOOST_PREVENT_MACRO_SUBSTITUTION(const size_type nBytes)
{
// Round the whole buffer space up to 2MB to use huge pages where possible.
const size_type mallocSize = AlignValueToPowerOfTwoByteBoundary<size_type, HugePageSize::value>(nBytes);
void *pMemory = ::mmap(ACTIV_NULLPTR, mallocSize, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_HUGETLB, -1, 0);
if (MAP_FAILED == pMemory)
{
// Try again without the huge page flag.
pMemory = ::mmap(ACTIV_NULLPTR, mallocSize, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (ACTIV_UNLIKELY(MAP_FAILED == pMemory))
pMemory = nullptr;
}
return static_cast<char *>(pMemory);
}
static void free ACTIV_BOOST_PREVENT_MACRO_SUBSTITUTION(char * const pBlock)
{
// HACK: we don't actually know the correct size to unmap (might be a multiple of the page size).
// However, the static_pool is never freed so this function never actually gets called.
::munmap(static_cast<void *>(pBlock), HugePageSize::value);
}
};
#elif (defined ACTIV_PLATFORM_WINDOWS)
// TODO. Just use the default pool allocator in Windows for now.
struct HugePageMalloc : public Boost::default_user_allocator_new_delete
{
using HugePageSize = std::integral_constant<size_t, (2 * 1024 * 1024)>;
};
#else
#error need to implement for this platform.
#endif
} // namespace Activ
#endif /* #if ((!defined ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES) && (!defined ACTIV_BOOST_NO_CXX11_TEMPLATE_ALIASES) && \
(!defined ACTIV_BOOST_NO_TEMPLATE_TEMPLATES)) */
#endif // (!defined ACTIV_MISC_POOL_ALLOCATOR_HELPER_H)