Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MemPoolAllocator.h « moses2 « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6cc699893c287be3071672b47bef072f16d909b8 (plain)
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
87
88
89
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:
};

}