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

MmapAllocator.h « CompactPT « TranslationModel « moses2 « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e40d8d410fb5f0af2769680c259bcddb47369e8 (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
// $Id$
// vim:tabstop=2
/***********************************************************************
 Moses - factored phrase-based language decoder
 Copyright (C) 2006 University of Edinburgh

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ***********************************************************************/

#ifndef moses_MmapAllocator_h
#define moses_MmapAllocator_h

#include <limits>
#include <iostream>
#include <cstdio>
#include <unistd.h>

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <io.h>
#else
#include <sys/mman.h>
#endif

#include "util/mmap.hh"

namespace Moses2
{
template<class T>
class MmapAllocator
{
protected:
  std::FILE* m_file_ptr;
  size_t m_file_desc;

  size_t m_page_size;
  size_t m_map_size;

  char* m_data_ptr;
  size_t m_data_offset;
  bool m_fixed;
  size_t* m_count;

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;

  MmapAllocator() throw () :
      m_file_ptr(std::tmpfile()), m_file_desc(fileno(m_file_ptr)), m_page_size(
          util::SizePage()), m_map_size(0), m_data_ptr(0), m_data_offset(0), m_fixed(
          false), m_count(new size_t(0))
  {
  }

  MmapAllocator(std::FILE* f_ptr) throw () :
      m_file_ptr(f_ptr), m_file_desc(fileno(m_file_ptr)), m_page_size(
          util::SizePage()), m_map_size(0), m_data_ptr(0), m_data_offset(0), m_fixed(
          false), m_count(new size_t(0))
  {
  }

  MmapAllocator(std::FILE* f_ptr, size_t data_offset) throw () :
      m_file_ptr(f_ptr), m_file_desc(fileno(m_file_ptr)), m_page_size(
          util::SizePage()), m_map_size(0), m_data_ptr(0), m_data_offset(
          data_offset), m_fixed(true), m_count(new size_t(0))
  {
  }

  MmapAllocator(std::string fileName) throw () :
      m_file_ptr(std::fopen(fileName.c_str(), "wb+")), m_file_desc(
          fileno(m_file_ptr)), m_page_size(util::SizePage()), m_map_size(0), m_data_ptr(
          0), m_data_offset(0), m_fixed(false), m_count(new size_t(0))
  {
  }

  MmapAllocator(const MmapAllocator& c) throw () :
      m_file_ptr(c.m_file_ptr), m_file_desc(c.m_file_desc), m_page_size(
          c.m_page_size), m_map_size(c.m_map_size), m_data_ptr(c.m_data_ptr), m_data_offset(
          c.m_data_offset), m_fixed(c.m_fixed), m_count(c.m_count)
  {
    (*m_count)++;
  }

  ~MmapAllocator() throw ()
  {
    if (m_data_ptr && *m_count == 0) {
      util::UnmapOrThrow(m_data_ptr, m_map_size);
      if (!m_fixed && std::ftell(m_file_ptr) != -1) std::fclose(m_file_ptr);
    }
    (*m_count)--;
  }

  template<class U>
  struct rebind
  {
    typedef MmapAllocator<U> other;
  };

  pointer address(reference value) const
  {
    return &value;
  }

  const_pointer address(const_reference value) const
  {
    return &value;
  }

  size_type max_size() const throw ()
  {
    return std::numeric_limits<size_t>::max() / sizeof(value_type);
  }

  pointer allocate(size_type num, const void* = 0)
  {
    m_map_size = num * sizeof(T);

#if defined(_WIN32) || defined(_WIN64)
    // On Windows, MAP_SHARED is not defined and MapOrThrow ignores the flags.
    const int map_shared = 0;
#else
    const int map_shared = MAP_SHARED;
#endif
    if (!m_fixed) {
      size_t read = 0;
      read += ftruncate(m_file_desc, m_map_size);
      m_data_ptr = (char *) util::MapOrThrow(m_map_size, true, map_shared,
          false, m_file_desc, 0);
      return (pointer) m_data_ptr;
    }
    else {
      const size_t map_offset = (m_data_offset / m_page_size) * m_page_size;
      const size_t relative_offset = m_data_offset - map_offset;
      const size_t adjusted_map_size = m_map_size + relative_offset;

      m_data_ptr = (char *) util::MapOrThrow(adjusted_map_size, false,
          map_shared, false, m_file_desc, map_offset);

      return (pointer) (m_data_ptr + relative_offset);
    }
  }

  void deallocate(pointer p, size_type num)
  {
    if (!m_fixed) {
      util::UnmapOrThrow(p, num * sizeof(T));
    }
    else {
      const size_t map_offset = (m_data_offset / m_page_size) * m_page_size;
      const size_t relative_offset = m_data_offset - map_offset;
      const size_t adjusted_map_size = m_map_size + relative_offset;

      util::UnmapOrThrow((pointer) ((char*) p - relative_offset),
          adjusted_map_size);
    }
  }

  void construct(pointer p, const T& value)
  {
    if (!m_fixed) new (p) value_type(value);
  }
  void destroy(pointer p)
  {
    if (!m_fixed) p->~T();
  }

  template<class T1, class T2>
  friend bool operator==(const MmapAllocator<T1>&,
      const MmapAllocator<T2>&) throw ();

  template<class T1, class T2>
  friend bool operator!=(const MmapAllocator<T1>&,
      const MmapAllocator<T2>&) throw ();
};

template<class T1, class T2>
bool operator==(const MmapAllocator<T1>& a1,
    const MmapAllocator<T2>& a2) throw ()
{
  bool equal = true;
  equal &= a1.m_file_ptr == a2.m_file_ptr;
  equal &= a1.m_file_desc == a2.m_file_desc;
  equal &= a1.m_page_size == a2.m_page_size;
  equal &= a1.m_map_size == a2.m_map_size;
  equal &= a1.m_data_ptr == a2.m_data_ptr;
  equal &= a1.m_data_offset == a2.m_data_offset;
  equal &= a1.m_fixed == a2.m_fixed;
  return equal;
}

template<class T1, class T2>
bool operator!=(const MmapAllocator<T1>& a1,
    const MmapAllocator<T2>& a2) throw ()
{
  return !(a1 == a2);
}

}

#endif