Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Public Member Functions | Static Public Member Functions | List of all members
Nektar::MemoryManager< DataType > Class Template Reference

General purpose memory allocation routines with the ability to allocate from thread specific memory pools. More...

#include <NekMemoryManager.hpp>

Classes

class  DeallocateSharedPtr
class  DefaultCustomDeallocator
struct  rebind

Public Member Functions

 BOOST_PP_REPEAT_FROM_TO (1, NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS, ALLOCATE_METHOD_GENERATOR, Allocate)
 BOOST_PP_REPEAT_FROM_TO (0, NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS, ALLOCATE_SHARED_PTR_METHOD_WITH_DEALLOCATOR_GENERATOR, AllocateSharedPtrD)
 BOOST_PP_REPEAT_FROM_TO (1, NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS, ALLOCATE_SHARED_PTR_METHOD_GENERATOR, AllocateSharedPtr)

Static Public Member Functions

static void Deallocate (DataType *&data)
 Deallocate a pointer allocated by MemoryManager::Allocate.
static DataType * Allocate ()
 Allocates a single object from the memory pool.
static boost::shared_ptr
< DataType > 
AllocateSharedPtr ()
 Allocate a shared pointer from the memory pool.
static DataType * RawAllocate (unsigned int NumberOfElements)
 Allocates a chunk of raw, uninitialized memory, capable of holding NumberOfElements objects.
static void RawDeallocate (DataType *array, unsigned int NumberOfElements)
 Deallocates memory allocated from RawAllocate.

Allocator Interface

The allocator interface allows a MemoryManager object to be used in any object that allows an allocator parameter, such as STL containers. //////////////////////////////////////////////////////////////

typedef DataType value_type
typedef size_t size_type
typedef ptrdiff_t difference_type
typedef DataType * pointer
typedef const DataType * const_pointer
typedef DataType & reference
typedef const DataType & const_reference
 MemoryManager ()
template<typename T >
 MemoryManager (const MemoryManager< T > &rhs)
 ~MemoryManager ()
pointer address (reference r) const
const_pointer address (const_reference r) const
pointer allocate (size_type n, std::allocator< void >::const_pointer hint=0)
void deallocate (pointer p, size_type n)
void construct (pointer p, const_reference val)
void destroy (pointer p)
size_type max_size ()

Detailed Description

template<typename DataType>
class Nektar::MemoryManager< DataType >

General purpose memory allocation routines with the ability to allocate from thread specific memory pools.

If compiled with NEKTAR_MEMORY_POOL_ENABLED, the MemoryManager allocates from thread specific memory pools for small objects. Large objects are managed with the system supplied new/delete. These memory pools provide faster allocation and deallocation of small objects (particularly useful for shared pointers which allocate many 4 byte objects).

Warning
All memory allocated from the memory manager must be returned to the memory manager. Calling delete on memory allocated from the manager will likely cause undefined behavior. A particularly subtle violation of this rule occurs when giving memory allocated from the manager to a shared pointer.
boost::shared_ptr<Obj> f(MemoryManager<Obj>::Allocate());
Shared pointers call delete when they go out of scope, so this line of code will cause problems. Instead, you should call the AllocateSharedPtr method:
boost::shared_ptr<Obj> f = MemoryManager<Obj>::AllocateSharedPtr();

Definition at line 102 of file NekMemoryManager.hpp.

Member Typedef Documentation

template<typename DataType>
typedef const DataType* Nektar::MemoryManager< DataType >::const_pointer

Definition at line 326 of file NekMemoryManager.hpp.

template<typename DataType>
typedef const DataType& Nektar::MemoryManager< DataType >::const_reference

Definition at line 328 of file NekMemoryManager.hpp.

template<typename DataType>
typedef ptrdiff_t Nektar::MemoryManager< DataType >::difference_type

Definition at line 324 of file NekMemoryManager.hpp.

template<typename DataType>
typedef DataType* Nektar::MemoryManager< DataType >::pointer

Definition at line 325 of file NekMemoryManager.hpp.

template<typename DataType>
typedef DataType& Nektar::MemoryManager< DataType >::reference

Definition at line 327 of file NekMemoryManager.hpp.

template<typename DataType>
typedef size_t Nektar::MemoryManager< DataType >::size_type

Definition at line 323 of file NekMemoryManager.hpp.

template<typename DataType>
typedef DataType Nektar::MemoryManager< DataType >::value_type

Definition at line 322 of file NekMemoryManager.hpp.

Constructor & Destructor Documentation

template<typename DataType>
Nektar::MemoryManager< DataType >::MemoryManager ( )
inline

Definition at line 330 of file NekMemoryManager.hpp.

{}
template<typename DataType>
template<typename T >
Nektar::MemoryManager< DataType >::MemoryManager ( const MemoryManager< T > &  rhs)
inline

Definition at line 332 of file NekMemoryManager.hpp.

{}
template<typename DataType>
Nektar::MemoryManager< DataType >::~MemoryManager ( )
inline

Definition at line 333 of file NekMemoryManager.hpp.

{}

Member Function Documentation

template<typename DataType>
pointer Nektar::MemoryManager< DataType >::address ( reference  r) const
inline

Definition at line 335 of file NekMemoryManager.hpp.

{ return &r; }
template<typename DataType>
const_pointer Nektar::MemoryManager< DataType >::address ( const_reference  r) const
inline

Definition at line 336 of file NekMemoryManager.hpp.

{ return &r; }
template<typename DataType>
static DataType* Nektar::MemoryManager< DataType >::Allocate ( )
inlinestatic

Allocates a single object from the memory pool.

Exceptions
unknownAny exception thrown by DataType's default constructor will propogate through this method.

The allocated object must be returned to the memory pool via Deallocate.

Definition at line 215 of file NekMemoryManager.hpp.

{
return new DataType();
}
template<typename DataType>
pointer Nektar::MemoryManager< DataType >::allocate ( size_type  n,
std::allocator< void >::const_pointer  hint = 0 
)
inline

Definition at line 338 of file NekMemoryManager.hpp.

References Nektar::MemoryManager< DataType >::RawAllocate().

{
return RawAllocate(n);
}
template<typename DataType>
static boost::shared_ptr<DataType> Nektar::MemoryManager< DataType >::AllocateSharedPtr ( )
inlinestatic

Allocate a shared pointer from the memory pool.

The shared pointer does not need to be returned to the memory pool. When the reference count to this object reaches 0, the shared pointer will automatically return the memory.

Definition at line 235 of file NekMemoryManager.hpp.

Referenced by Diffusion::Diffusion(), main(), and SetFields().

{
return AllocateSharedPtrD(DefaultCustomDeallocator());
}
template<typename DataType>
Nektar::MemoryManager< DataType >::BOOST_PP_REPEAT_FROM_TO ( ,
NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS  ,
ALLOCATE_METHOD_GENERATOR  ,
Allocate   
)
template<typename DataType>
Nektar::MemoryManager< DataType >::BOOST_PP_REPEAT_FROM_TO ( ,
NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS  ,
ALLOCATE_SHARED_PTR_METHOD_WITH_DEALLOCATOR_GENERATOR  ,
AllocateSharedPtrD   
)
template<typename DataType>
Nektar::MemoryManager< DataType >::BOOST_PP_REPEAT_FROM_TO ( ,
NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS  ,
ALLOCATE_SHARED_PTR_METHOD_GENERATOR  ,
AllocateSharedPtr   
)
template<typename DataType>
void Nektar::MemoryManager< DataType >::construct ( pointer  p,
const_reference  val 
)
inline

Definition at line 348 of file NekMemoryManager.hpp.

{
new(p) DataType(val);
}
template<typename DataType>
static void Nektar::MemoryManager< DataType >::Deallocate ( DataType *&  data)
inlinestatic

Deallocate a pointer allocated by MemoryManager::Allocate.

Note
Results are undefined if called with a pointer to something that was not allocated with the memory manager.

Use this method to deallocate a pointer you have allocated from the MemoryManager using the Allocate method.

Example:

CustObj* c = MemoryManager::Allocate<CustObj>();

Definition at line 147 of file NekMemoryManager.hpp.

References Nektar::MemPool::Deallocate(), and Nektar::GetMemoryPool().

Referenced by Nektar::MemoryManager< DataType >::DeallocateSharedPtr< ObjectType, CustomDeallocator >::operator()().

{
#ifdef NEKTAR_MEMORY_POOL_ENABLED
data->~DataType();
GetMemoryPool().Deallocate(data, sizeof(DataType));
#else //NEKTAR_MEMORY_POOL_ENABLED
delete data;
#endif //NEKTAR_MEMORY_POOL_ENABLED
data = NULL;
}
template<typename DataType>
void Nektar::MemoryManager< DataType >::deallocate ( pointer  p,
size_type  n 
)
inline

Definition at line 343 of file NekMemoryManager.hpp.

References Nektar::MemoryManager< DataType >::RawDeallocate().

{
return RawDeallocate(p, n);
}
template<typename DataType>
void Nektar::MemoryManager< DataType >::destroy ( pointer  p)
inline

Definition at line 353 of file NekMemoryManager.hpp.

{
p->~DataType();
}
template<typename DataType>
size_type Nektar::MemoryManager< DataType >::max_size ( )
inline

Definition at line 358 of file NekMemoryManager.hpp.

{
return std::numeric_limits<size_type>::max()/sizeof(DataType);
}
template<typename DataType>
static DataType* Nektar::MemoryManager< DataType >::RawAllocate ( unsigned int  NumberOfElements)
inlinestatic

Allocates a chunk of raw, uninitialized memory, capable of holding NumberOfElements objects.

Parameters
NumberOfElementsThe number of elements the array should be capable of holding.

This method is not meant to be called by client code. Use Array instead. Any memory allocated from this method must be returned to the memory pool via RawDeallocate. Failure to do so will result in memory leaks and undefined behavior.

Definition at line 290 of file NekMemoryManager.hpp.

References Nektar::MemPool::Allocate(), and Nektar::GetMemoryPool().

Referenced by Nektar::MemoryManager< DataType >::allocate(), and Nektar::CreateStorage().

{
#ifdef NEKTAR_MEMORY_POOL_ENABLED
return static_cast<DataType*>(GetMemoryPool().Allocate(sizeof(DataType)*NumberOfElements));
#else //NEKTAR_MEMORY_POOL_ENABLED
return static_cast<DataType*>(::operator new(NumberOfElements * sizeof(DataType)));
#endif //NEKTAR_MEMORY_POOL_ENABLED
}
template<typename DataType>
static void Nektar::MemoryManager< DataType >::RawDeallocate ( DataType *  array,
unsigned int  NumberOfElements 
)
inlinestatic

Deallocates memory allocated from RawAllocate.

Parameters
arrayA pointer to the memory returned from RawAllocate.
NumberOfElementsThe number of object held in the array.

This method is not meant to be called by client code. Use Array instead. Only memory allocated via RawAllocate should be returned to the pool here.

Definition at line 306 of file NekMemoryManager.hpp.

References Nektar::MemPool::Deallocate(), and Nektar::GetMemoryPool().

Referenced by Nektar::MemoryManager< DataType >::deallocate(), and Nektar::DeleteStorage().

{
#ifdef NEKTAR_MEMORY_POOL_ENABLED
GetMemoryPool().Deallocate(array, sizeof(DataType)*NumberOfElements);
#else //NEKTAR_MEMORY_POOL_ENABLED
::operator delete(array);
#endif //NEKTAR_MEMORY_POOL_ENABLED
}