Separate MemPoolAllocator from MemPool

This commit is contained in:
Hieu Hoang 2016-10-26 16:50:20 +01:00
parent 80ae436db1
commit 7f84462785
11 changed files with 99 additions and 83 deletions

View File

@ -11,6 +11,7 @@
#include "HypothesisColl.h"
#include "ManagerBase.h"
#include "System.h"
#include "MemPoolAllocator.h"
using namespace std;

View File

@ -7,7 +7,7 @@
#pragma once
#include <boost/unordered_set.hpp>
#include "HypothesisBase.h"
#include "MemPool.h"
#include "MemPoolAllocator.h"
#include "Recycler.h"
#include "Array.h"
#include "legacy/Util2.h"

View File

@ -5,7 +5,7 @@
#include "System.h"
#include "Phrase.h"
#include "TranslationTask.h"
#include "MemPool.h"
#include "MemPoolAllocator.h"
#include "server/Server.h"
#include "legacy/InputFileStream.h"
#include "legacy/Parameter.h"

View File

@ -169,86 +169,7 @@ private:
};
//////////////////////////////////////////////////////////////////////////////////////////
template<typename T>
class MemPoolAllocator
{
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template<class U>
struct rebind
{
typedef MemPoolAllocator<U> other;
};
MemPoolAllocator(Moses2::MemPool &pool) :
m_pool(pool)
{
}
MemPoolAllocator(const MemPoolAllocator &other) :
m_pool(other.m_pool)
{
}
template<class U>
MemPoolAllocator(const MemPoolAllocator<U>& other) :
m_pool(other.m_pool)
{
}
size_type max_size() const
{
return std::numeric_limits<size_type>::max();
}
void deallocate(pointer p, size_type n)
{
//std::cerr << "deallocate " << p << " " << n << std::endl;
}
pointer allocate(size_type n, std::allocator<void>::const_pointer hint = 0)
{
//std::cerr << "allocate " << n << " " << hint << std::endl;
pointer ret = m_pool.Allocate<T>(n);
return ret;
}
void construct(pointer p, const_reference val)
{
//std::cerr << "construct " << p << " " << n << std::endl;
new ((void *) p) T(val);
}
void destroy(pointer p)
{
//std::cerr << "destroy " << p << " " << n << std::endl;
}
// return address of values
pointer address (reference value) const {
return &value;
}
const_pointer address (const_reference value) const {
return &value;
}
bool operator==(const MemPoolAllocator<T> &allocator) const {
return true;
}
bool operator!=(const MemPoolAllocator<T> &allocator) const {
return false;
}
MemPool &m_pool;
protected:
};
}

View File

@ -0,0 +1,90 @@
#pragma once
#include "MemPool.h"
namespace Moses2
{
template<typename T>
class MemPoolAllocator
{
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template<class U>
struct rebind
{
typedef MemPoolAllocator<U> other;
};
MemPoolAllocator(Moses2::MemPool &pool) :
m_pool(pool)
{
}
MemPoolAllocator(const MemPoolAllocator &other) :
m_pool(other.m_pool)
{
}
template<class U>
MemPoolAllocator(const MemPoolAllocator<U>& other) :
m_pool(other.m_pool)
{
}
size_type max_size() const
{
return std::numeric_limits<size_type>::max();
}
void deallocate(pointer p, size_type n)
{
//std::cerr << "deallocate " << p << " " << n << std::endl;
}
pointer allocate(size_type n, std::allocator<void>::const_pointer hint = 0)
{
//std::cerr << "allocate " << n << " " << hint << std::endl;
pointer ret = m_pool.Allocate<T>(n);
return ret;
}
void construct(pointer p, const_reference val)
{
//std::cerr << "construct " << p << " " << n << std::endl;
new ((void *) p) T(val);
}
void destroy(pointer p)
{
//std::cerr << "destroy " << p << " " << n << std::endl;
}
// return address of values
pointer address (reference value) const {
return &value;
}
const_pointer address (const_reference value) const {
return &value;
}
bool operator==(const MemPoolAllocator<T> &allocator) const {
return true;
}
bool operator!=(const MemPoolAllocator<T> &allocator) const {
return false;
}
MemPool &m_pool;
protected:
};
}

View File

@ -14,6 +14,7 @@
#include "../Hypothesis.h"
#include "../../TypeDef.h"
#include "../../Vector.h"
#include "../../MemPoolAllocator.h"
#include "Stack.h"
namespace Moses2

View File

@ -11,6 +11,7 @@
#include "Misc.h"
#include "Stack.h"
#include "../../legacy/Range.h"
#include "../../MemPoolAllocator.h"
namespace Moses2
{

View File

@ -12,6 +12,7 @@
#include "../../TypeDef.h"
#include "../../Vector.h"
#include "../../MemPool.h"
#include "../../MemPoolAllocator.h"
#include "../../Recycler.h"
#include "../../HypothesisColl.h"
#include "../../legacy/Util2.h"

View File

@ -9,7 +9,7 @@
#include "TargetPhrases.h"
#include "ActiveChart.h"
#include "../TranslationModel/PhraseTable.h"
#include "../MemPool.h"
#include "../MemPoolAllocator.h"
using namespace std;

View File

@ -11,6 +11,7 @@
#include <list>
#include <boost/unordered_map.hpp>
#include "../InputPathBase.h"
#include "../MemPoolAllocator.h"
#include "TargetPhrases.h"
#include "ActiveChart.h"
#include "Word.h"

View File

@ -7,7 +7,7 @@
#pragma once
#include <cassert>
#include "MemPool.h"
#include "MemPoolAllocator.h"
namespace Moses2
{