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

218 lines
4.1 KiB
C
Raw Normal View History

2015-10-28 17:39:40 +03:00
/*
* MemPool.h
*
* Created on: 28 Oct 2015
* Author: hieu
*/
#ifndef MEMPOOL_H_
#define MEMPOOL_H_
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
class MemPool {
struct Page {
uint8_t *mem;
uint8_t *end;
size_t size;
Page(std::size_t size);
~Page();
};
public:
MemPool(std::size_t initSize = 10000);
~MemPool();
void *Allocate(std::size_t size) {
void *ret = current_;
current_ += size;
Page &page = m_pages[m_currPage];
if (current_ < page.end) {
// return what we got
} else {
ret = More(size);
}
return ret;
}
template<typename T>
2015-10-28 22:37:34 +03:00
T *Allocate() {
void *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) {
void *ret = Allocate(sizeof(T) * num);
return (T*) ret;
}
2015-10-28 17:39:40 +03:00
2015-10-28 22:37:34 +03:00
void Reset()
{
m_currPage = 0;
current_ = m_pages[0].mem;
}
2015-10-28 17:39:40 +03:00
private:
void *More(std::size_t size);
std::vector<Page> m_pages;
size_t m_currSize;
size_t m_currPage;
uint8_t *current_;
// no copying
MemPool(const MemPool &);
MemPool &operator=(const MemPool &);
};
2015-11-17 15:27:17 +03:00
////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
class MemPoolAllocator
{
public:
typedef T value_type;
typedef T* pointer;
2015-11-17 15:54:29 +03:00
typedef const T* const_pointer;
typedef T& reference;
2015-11-17 16:05:26 +03:00
typedef const T& const_reference;
2015-11-17 15:27:17 +03:00
typedef std::size_t size_type;
2015-11-17 16:05:26 +03:00
typedef std::ptrdiff_t difference_type;
template< class U >
struct rebind { typedef MemPoolAllocator<U> other; };
MemPoolAllocator() {}
MemPoolAllocator(const MemPoolAllocator &other) {}
template< class U >
MemPoolAllocator( const MemPoolAllocator<U>& other ) {}
2015-11-17 15:27:17 +03:00
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;
}
2015-11-27 22:42:27 +03:00
void construct( pointer p, const_reference val )
{
//std::cerr << "construct " << p << " " << n << std::endl;
2015-11-28 01:42:26 +03:00
new((void *)p) T(val);
2015-11-27 22:42:27 +03:00
}
void destroy( pointer p )
{
//std::cerr << "destroy " << p << " " << n << std::endl;
}
2015-11-17 15:40:33 +03:00
2015-11-17 15:27:17 +03:00
protected:
MemPool m_pool;
};
2015-11-23 16:43:56 +03:00
////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
class ObjectPoolContiguous {
2015-11-25 20:51:49 +03:00
friend std::ostream& operator<<(std::ostream &, const ObjectPoolContiguous &);
2015-11-23 16:43:56 +03:00
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-11-25 20:51:49 +03:00
template <typename T>
std::ostream& operator<<(std::ostream &out, const ObjectPoolContiguous<T> &obj)
{
for (size_t i = 0; i < obj.size(); ++i) {
T &ele = obj.get(i);
out << ele;
}
return out;
}
2015-10-28 17:39:40 +03:00
#endif /* MEMPOOL_H_ */