File size: 1,798 Bytes
fd49381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#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;
  }

  MemPoolAllocator<T>& operator=(const MemPoolAllocator<T>& allocator) {
    return *this;
  }

  MemPool &m_pool;
protected:
};

}