38 #ifndef NEKTAR_LIB_UTILITIES_NEK_MEMORY_MANAGER_H
39 #define NEKTAR_LIB_UTILITIES_NEK_MEMORY_MANAGER_H
45 #include <boost/mpl/contains.hpp>
46 #include <boost/mpl/list_c.hpp>
47 #include <boost/shared_ptr.hpp>
48 #include <boost/utility/enable_if.hpp>
49 #include <boost/mpl/integral_c.hpp>
50 #include <boost/bind.hpp>
51 #include <boost/type_traits.hpp>
53 #include <boost/preprocessor/repetition/enum_params.hpp>
54 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
55 #include <boost/preprocessor/repetition/enum_trailing.hpp>
56 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
57 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
58 #include <boost/preprocessor/control/if.hpp>
59 #include <boost/preprocessor/facilities/empty.hpp>
60 #include <boost/preprocessor/comparison/equal.hpp>
61 #include <boost/preprocessor/punctuation/comma.hpp>
62 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
73 #ifndef NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS
74 #define NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS 15
75 #endif //NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS
101 template<
typename DataType>
105 template<
typename ObjectType,
typename CustomDeallocator>
149 #ifdef NEKTAR_MEMORY_POOL_ENABLED
152 #else //NEKTAR_MEMORY_POOL_ENABLED
154 #endif //NEKTAR_MEMORY_POOL_ENABLED
159 #ifdef NEKTAR_MEMORY_POOL_ENABLED
175 new (result) DataType();
187 #define ALLOCATE_METHOD_GENERATOR(z, i, methodName) \
188 template<BOOST_PP_ENUM_PARAMS(i, typename Arg)> \
189 static DataType* methodName(BOOST_PP_ENUM_BINARY_PARAMS(i, const Arg, & arg)) \
191 DataType* result = static_cast<DataType*>(GetMemoryPool().Allocate(sizeof(DataType))); \
197 new (result) DataType(BOOST_PP_ENUM_PARAMS(i, arg)); \
201 GetMemoryPool().Deallocate(result, sizeof(DataType)); \
208 #else //NEKTAR_MEMORY_POOL_ENABLED
217 return new DataType();
220 #define ALLOCATE_METHOD_GENERATOR(z, i, methodName) \
221 template<BOOST_PP_ENUM_PARAMS(i, typename Arg)> \
222 static DataType* methodName(BOOST_PP_ENUM_BINARY_PARAMS(i, const Arg, & arg)) \
224 return new DataType(BOOST_PP_ENUM_PARAMS(i, arg)); \
226 #endif //NEKTAR_MEMORY_POOL_ENABLED
264 #define ALLOCATE_SHARED_PTR_METHOD_GENERATOR(z, i, methodName) \
265 template<BOOST_PP_ENUM_PARAMS(i, typename Arg)> \
266 static boost::shared_ptr<DataType> methodName(BOOST_PP_ENUM_BINARY_PARAMS(i, const Arg, & arg)) \
268 return AllocateSharedPtrD(DefaultCustomDeallocator(), BOOST_PP_ENUM_PARAMS(i, arg)); \
271 #define ALLOCATE_SHARED_PTR_METHOD_WITH_DEALLOCATOR_GENERATOR(z, i, methodName) \
272 template<typename DeallocatorType BOOST_PP_ENUM_TRAILING_PARAMS(i, typename Arg)> \
273 static boost::shared_ptr<DataType> methodName(const DeallocatorType& d BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(i, Arg, & arg)) \
275 DataType* data = Allocate(BOOST_PP_ENUM_PARAMS(i, arg)); \
276 return boost::shared_ptr<DataType>(data, DeallocateSharedPtr<DataType, DeallocatorType>(d)); \
292 #ifdef NEKTAR_MEMORY_POOL_ENABLED
294 #else //NEKTAR_MEMORY_POOL_ENABLED
295 return static_cast<DataType*
>(::operator
new(NumberOfElements *
sizeof(DataType)));
296 #endif //NEKTAR_MEMORY_POOL_ENABLED
308 #ifdef NEKTAR_MEMORY_POOL_ENABLED
310 #else //NEKTAR_MEMORY_POOL_ENABLED
311 ::operator
delete(array);
312 #endif //NEKTAR_MEMORY_POOL_ENABLED
335 pointer
address(reference r)
const {
return &r; }
336 const_pointer
address(const_reference r)
const {
return &r; }
338 pointer
allocate(size_type n, std::allocator<void>::const_pointer hint = 0)
350 new(p) DataType(val);
360 return std::numeric_limits<size_type>::max()/
sizeof(DataType);
377 template<
typename DataType>
383 template<
typename DataType>
386 return !(lhs == rhs);
391 #endif //NEKTAR_LIB_UTILITIES_NEK_MEMORY_MANAGER_H
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
#define ALLOCATE_SHARED_PTR_METHOD_GENERATOR(z, i, methodName)
Generator for allocating shared pointers to objects with constructors having a variable number of par...
void Deallocate(void *p, size_t bytes)
Deallocate memory claimed by an earlier call to allocate.
bool operator==(const Array< OneD, NekDouble > &lhs, const Array< OneD, NekDouble > &rhs)
DeallocateSharedPtr(const CustomDeallocator &d)
pointer address(reference r) const
const DataType * const_pointer
#define ALLOCATE_METHOD_GENERATOR(z, i, methodName)
void * Allocate(size_t bytes)
Allocate a block of memory of size ByteSize.
void deallocate(pointer p, size_type n)
ptrdiff_t difference_type
static void RawDeallocate(DataType *array, unsigned int NumberOfElements)
Deallocates memory allocated from RawAllocate.
static DataType * Allocate()
Allocates a single object from the memory pool.
bool operator!=(const Array< OneD, T1 > &lhs, const Array< OneD, T2 > &rhs)
pointer allocate(size_type n, std::allocator< void >::const_pointer hint=0)
void construct(pointer p, const_reference val)
#define NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS
MemPool & GetMemoryPool()
const DataType & const_reference
MemoryManager(const MemoryManager< T > &rhs)
static void Deallocate(DataType *&data)
Deallocate a pointer allocated by MemoryManager::Allocate.
CustomDeallocator m_dealloc
BOOST_PP_REPEAT_FROM_TO(1, NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS, ALLOCATE_METHOD_GENERATOR, Allocate)
static DataType * RawAllocate(unsigned int NumberOfElements)
Allocates a chunk of raw, uninitialized memory, capable of holding NumberOfElements objects...
#define ALLOCATE_SHARED_PTR_METHOD_WITH_DEALLOCATOR_GENERATOR(z, i, methodName)
void operator()(ObjectType *&m) const
const_pointer address(const_reference r) const