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

MemPool.h « moses2 « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5820ce2be0954cf97265f10c15e670661283ece6 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * MemPool.h
 *
 *  Created on: 28 Oct 2015
 *      Author: hieu
 */

#pragma once

#include <algorithm>
#include <iostream>
#include <vector>
#include <stdint.h>
#include <stdlib.h>
#include <limits>
#include <iostream>

namespace Moses2
{

class MemPool
{
  struct Page
  {
    uint8_t *mem;
    uint8_t *end;
    size_t size;

    Page()
    {
    }
    Page(std::size_t size);
    ~Page();
  };

public:
  MemPool(std::size_t initSize = 10000);

  ~MemPool();

  uint8_t *Allocate(std::size_t size)
  {
    size = (size + 3) & 0xfffffffc;

    uint8_t *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>
  T *Allocate()
  {
    uint8_t *ret = Allocate(sizeof(T));
    return (T*) ret;
  }

  template<typename T>
  T *Allocate(size_t num)
  {
    uint8_t *ret = Allocate(sizeof(T) * num);
    return (T*) ret;
  }

  // re-use pool
  void Reset();

private:
  uint8_t *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 &);
};

////////////////////////////////////////////////////////////////////////////////////////////////
template<typename T>
class ObjectPoolContiguous
{

public:
  ObjectPoolContiguous(std::size_t initSize = 100000) :
      m_size(0), m_actualSize(initSize)
  {
    m_vec = (T*) malloc(sizeof(T) * initSize);
  }

  ~ObjectPoolContiguous()
  {
    free(m_vec);
  }

  void Add(T &obj)
  {
    if (m_size >= m_actualSize) {
      //std::cerr << std::endl << "MORE " << m_size << std::endl;
      m_actualSize *= 2;
      m_vec = (T*) realloc(m_vec, sizeof(T) * m_actualSize);

    }
    m_vec[m_size] = obj;
    ++m_size;
  }

  bool IsEmpty() const
  {
    return m_size == 0;
  }

  void Reset()
  {
    m_size = 0;
  }

  // vector op
  size_t GetSize() const
  {
    return m_size;
  }

  const T& operator[](size_t ind) const
  {
    return m_vec[ind];
  }

  // stack op
  const T &Get() const
  {
    return m_vec[m_size - 1];
  }

  void Pop()
  {
    --m_size;
  }

  T *GetData()
  {
    return m_vec;
  }

  template<typename ORDERER>
  void Sort(const ORDERER &orderer)
  {
    std::sort(m_vec, m_vec + m_size, orderer);
  }

private:
  T *m_vec;
  size_t m_size, m_actualSize;

  // no copying
  ObjectPoolContiguous(const ObjectPoolContiguous &);
  ObjectPoolContiguous &operator=(const ObjectPoolContiguous &);
};

//////////////////////////////////////////////////////////////////////////////////////////
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:
};

}