Commit 0dcd57d7 authored by David Moxey's avatar David Moxey

Refactor deallocation structures to avoid use of functional

parent 0c6c96e4
......@@ -39,7 +39,6 @@
#include <type_traits>
#include <memory>
#include <functional>
#include <LibUtilities/BasicConst/NektarUnivTypeDefs.hpp>
#include <LibUtilities/Memory/NekMemoryManager.hpp>
......@@ -94,9 +93,9 @@ namespace Nektar
/// \param itemsToCreate The size of data.
static void Initialize(ObjectType* data, unsigned int itemsToCreate)
{
DoInitialization(data, itemsToCreate,
std::bind(&ArrayInitializationPolicy<ObjectType>::DefaultConstructionWithPlacementNew,
std::placeholders::_1));
DoInitialization(
data, itemsToCreate,
[](ObjectType *element) { new (element) ObjectType; });
}
/// \brief Initalize each element in the array with ObjectType's copy constructor.
......@@ -105,19 +104,21 @@ namespace Nektar
/// \param initValue The inital value each element in data will have.
static void Initialize(ObjectType* data, unsigned int itemsToCreate, const ObjectType& initValue)
{
DoInitialization(data, itemsToCreate,
std::bind(&ArrayInitializationPolicy<ObjectType>::CopyConstructionWithPlacementNew, std::placeholders::_1, std::ref(initValue)));
DoInitialization(
data, itemsToCreate,
[&initValue](ObjectType *element) { new (element) ObjectType(initValue); });
}
static void Initialize(ObjectType* data, unsigned int itemsToCreate, const ObjectType* initValue)
{
DoInitialization(data, itemsToCreate,
std::bind(&ArrayInitializationPolicy<ObjectType>::CopyConstructionFromArray, std::placeholders::_1, std::ref(initValue)));
DoInitialization(
data, itemsToCreate,
[&initValue](ObjectType *element) { new (element) ObjectType(*initValue); initValue++; });
}
private:
template<typename CreateType>
static void DoInitialization(ObjectType* data, unsigned int itemsToCreate, CreateType& f)
static void DoInitialization(ObjectType* data, unsigned int itemsToCreate, const CreateType &f)
{
unsigned int nextObjectToCreate = 0;
try
......@@ -139,22 +140,6 @@ namespace Nektar
throw;
}
}
static void DefaultConstructionWithPlacementNew(ObjectType* element)
{
new (element) ObjectType;
}
static void CopyConstructionWithPlacementNew(ObjectType* element, const ObjectType& initValue)
{
new (element) ObjectType(initValue);
}
static void CopyConstructionFromArray(ObjectType* element, const ObjectType*& rhs)
{
new (element) ObjectType(*rhs);
rhs += 1;
}
};
......@@ -186,13 +171,6 @@ namespace Nektar
}
};
template<typename DataType>
void DeleteStorage(DataType* data, unsigned int num)
{
ArrayDestructionPolicy<DataType>::Destroy(data, num);
MemoryManager<DataType>::RawDeallocate(data, num);
}
template<typename Dim, typename DataType, typename ExtentListType>
std::shared_ptr<boost::multi_array_ref<DataType, Dim::Value> >
CreateStorage(const ExtentListType& extent)
......@@ -202,8 +180,11 @@ namespace Nektar
std::multiplies<unsigned int>());
DataType* storage = MemoryManager<DataType>::RawAllocate(size);
return MemoryManager<ArrayType>::AllocateSharedPtrD(
std::bind(DeleteStorage<DataType>, storage, size),
storage, extent);
[storage, size](DataType *ptr) {
ArrayDestructionPolicy<DataType>::Destroy(storage, size);
MemoryManager<DataType>::RawDeallocate(storage, size);
},
storage, extent);
}
template<typename DataType>
......
......@@ -40,7 +40,6 @@
#include <memory>
#include <type_traits>
#include <functional>
#include <LibUtilities/Memory/ThreadSpecificPool.hpp>
#include <LibUtilities/BasicUtils/ErrorUtil.hpp>
......@@ -82,34 +81,6 @@ namespace Nektar
template<typename DataType>
class MemoryManager
{
template<typename ObjectType, typename CustomDeallocator>
class DeallocateSharedPtr
{
public:
explicit DeallocateSharedPtr(const CustomDeallocator& d) :
m_dealloc(d)
{
}
void operator()(ObjectType*& m)
{
m_dealloc();
MemoryManager<ObjectType>::Deallocate(m);
}
private:
CustomDeallocator m_dealloc;
};
class DefaultCustomDeallocator
{
public:
void operator()() const
{
}
};
public:
/// @brief Deallocate a pointer allocated by
/// MemoryManager::Allocate.
......@@ -188,7 +159,7 @@ public:
template<typename... Args>
static std::shared_ptr<DataType> AllocateSharedPtr(const Args &...args)
{
return AllocateSharedPtrD(DefaultCustomDeallocator(), args...);
return AllocateSharedPtrD([](DataType *ptr){}, args...);
}
template<typename DeallocatorType, typename... Args>
......@@ -197,7 +168,10 @@ public:
{
DataType* data = Allocate(args...);
return std::shared_ptr<DataType>(
data, DeallocateSharedPtr<DataType, DeallocatorType>(d));
data, [&d](DataType *ptr){
d(ptr);
MemoryManager<DataType>::Deallocate(ptr);
});
}
/// \brief Allocates a chunk of raw, uninitialized memory, capable of
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment