mosesdecoder/contrib/other-builds/moses2/MemPool.h

215 lines
3.9 KiB
C
Raw Normal View History

2015-10-28 17:39:40 +03:00
/*
* MemPool.h
*
* Created on: 28 Oct 2015
* Author: hieu
*/
2015-12-26 03:59:43 +03:00
#pragma once
2015-10-28 17:39:40 +03:00
2015-11-26 01:51:17 +03:00
#include <algorithm>
2015-11-25 20:51:49 +03:00
#include <iostream>
2015-10-28 17:39:40 +03:00
#include <vector>
#include <stdint.h>
2015-11-23 17:02:03 +03:00
#include <stdlib.h>
2015-11-17 15:54:29 +03:00
#include <limits>
2015-11-24 03:08:27 +03:00
#include <iostream>
2015-10-28 17:39:40 +03:00
2015-12-10 23:49:30 +03:00
namespace Moses2
{
2015-10-28 17:39:40 +03:00
class MemPool {
struct Page {
uint8_t *mem;
uint8_t *end;
size_t size;
2015-12-04 22:25:57 +03:00
Page() {}
2015-10-28 17:39:40 +03:00
Page(std::size_t size);
~Page();
};
public:
MemPool(std::size_t initSize = 10000);
~MemPool();
2015-12-24 23:11:28 +03:00
uint8_t *Allocate(std::size_t size) {
uint8_t *ret = current_;
2015-10-28 17:39:40 +03:00
current_ += size;
2015-12-04 23:27:56 +03:00
Page &page = *m_pages[m_currPage];
2015-12-24 23:11:28 +03:00
if (current_ <= page.end) {
2015-10-28 17:39:40 +03:00
// return what we got
} else {
ret = More(size);
}
return ret;
}
template<typename T>
2015-10-28 22:37:34 +03:00
T *Allocate() {
2015-12-24 23:11:28 +03:00
uint8_t *ret = Allocate(sizeof(T));
2015-10-28 17:39:40 +03:00
return (T*) ret;
}
2015-10-28 22:37:34 +03:00
template<typename T>
T *Allocate(size_t num) {
2015-12-24 23:11:28 +03:00
uint8_t *ret = Allocate(sizeof(T) * num);
2015-10-28 22:37:34 +03:00
return (T*) ret;
}
2015-10-28 17:39:40 +03:00
2015-12-04 22:16:59 +03:00
void Reset();
2015-10-28 17:39:40 +03:00
private:
2015-12-24 23:11:28 +03:00
uint8_t *More(std::size_t size);
2015-10-28 17:39:40 +03:00
2015-12-04 23:27:56 +03:00
std::vector<Page*> m_pages;
2015-10-28 17:39:40 +03:00
size_t m_currSize;
size_t m_currPage;
uint8_t *current_;
// no copying
MemPool(const MemPool &);
MemPool &operator=(const MemPool &);
};
2015-11-23 16:43:56 +03:00
////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
class ObjectPoolContiguous {
public:
2015-11-23 18:50:20 +03:00
ObjectPoolContiguous(std::size_t initSize = 100000)
:m_size(0)
2015-11-23 19:07:27 +03:00
,m_actualSize(initSize)
2015-11-23 17:24:54 +03:00
{
2015-11-23 19:07:27 +03:00
m_vec = (T*) malloc(sizeof(T) * initSize);
2015-11-23 16:43:56 +03:00
}
~ObjectPoolContiguous()
{
2015-11-23 19:07:27 +03:00
free (m_vec);
2015-11-23 16:43:56 +03:00
}
2015-11-26 22:08:34 +03:00
void Add(T &obj) {
2015-11-23 19:07:27 +03:00
if (m_size >= m_actualSize) {
2015-11-24 03:08:27 +03:00
//std::cerr << std::endl << "MORE " << m_size << std::endl;
m_actualSize *= 2;
m_vec = (T*) realloc(m_vec, sizeof(T) * m_actualSize);
2015-11-23 18:50:20 +03:00
}
m_vec[m_size] = obj;
++m_size;
2015-11-23 16:43:56 +03:00
}
2015-11-26 22:08:34 +03:00
bool IsEmpty() const
{ return m_size == 0; }
void Reset()
2015-11-23 16:43:56 +03:00
{
2015-11-23 18:50:20 +03:00
m_size = 0;
2015-11-23 16:43:56 +03:00
}
// vector op
2015-11-26 22:08:34 +03:00
size_t GetSize() const
2015-11-23 18:50:20 +03:00
{ return m_size; }
2015-11-23 16:43:56 +03:00
2015-11-26 22:08:34 +03:00
const T& operator[](size_t ind) const {
2015-11-23 17:24:54 +03:00
return m_vec[ind];
2015-11-23 16:43:56 +03:00
}
// stack op
2015-11-26 22:08:34 +03:00
const T &Get() const {
return m_vec[m_size - 1];
}
2015-11-26 22:08:34 +03:00
void Pop()
{
--m_size;
}
2015-11-25 20:35:22 +03:00
T *GetData()
{
return m_vec;
}
2015-11-26 01:51:17 +03:00
template<typename ORDERER>
void Sort(const ORDERER &orderer)
{
std::sort(m_vec, m_vec + m_size, orderer);
}
2015-11-23 16:43:56 +03:00
private:
2015-11-23 19:07:27 +03:00
T *m_vec;
size_t m_size, m_actualSize;
2015-11-23 17:02:03 +03:00
2015-11-23 16:43:56 +03:00
// no copying
ObjectPoolContiguous(const ObjectPoolContiguous &);
ObjectPoolContiguous &operator=(const ObjectPoolContiguous &);
};
2015-12-26 03:59:43 +03:00
//////////////////////////////////////////////////////////////////////////////////////////
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;
}
MemPool &m_pool;
protected:
};
2015-12-10 23:49:30 +03:00
}