/moses/src/CompactPT/MmapAllocator.h

https://bitbucket.org/xwd/moses-csp · C Header · 204 lines · 154 code · 30 blank · 20 comment · 21 complexity · 7af8a60d10cf3f5c41f03e41d7ad07c0 MD5 · raw file

  1. // $Id$
  2. // vim:tabstop=2
  3. /***********************************************************************
  4. Moses - factored phrase-based language decoder
  5. Copyright (C) 2006 University of Edinburgh
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ***********************************************************************/
  18. #ifndef moses_MmapAllocator_h
  19. #define moses_MmapAllocator_h
  20. #include <limits>
  21. #include <iostream>
  22. #include <sys/mman.h>
  23. #include <cstdio>
  24. #include <unistd.h>
  25. namespace Moses
  26. {
  27. template <class T>
  28. class MmapAllocator
  29. {
  30. protected:
  31. std::FILE* m_file_ptr;
  32. size_t m_file_desc;
  33. size_t m_page_size;
  34. size_t m_map_size;
  35. char* m_data_ptr;
  36. size_t m_data_offset;
  37. bool m_fixed;
  38. size_t* m_count;
  39. public:
  40. typedef T value_type;
  41. typedef T* pointer;
  42. typedef const T* const_pointer;
  43. typedef T& reference;
  44. typedef const T& const_reference;
  45. typedef std::size_t size_type;
  46. typedef std::ptrdiff_t difference_type;
  47. MmapAllocator() throw()
  48. : m_file_ptr(std::tmpfile()), m_file_desc(fileno(m_file_ptr)),
  49. m_page_size(sysconf(_SC_PAGE_SIZE)), m_map_size(0), m_data_ptr(0),
  50. m_data_offset(0), m_fixed(false), m_count(new size_t(0))
  51. { }
  52. MmapAllocator(std::FILE* f_ptr) throw()
  53. : m_file_ptr(f_ptr), m_file_desc(fileno(m_file_ptr)),
  54. m_page_size(sysconf(_SC_PAGE_SIZE)), m_map_size(0), m_data_ptr(0),
  55. m_data_offset(0), m_fixed(false), m_count(new size_t(0))
  56. { }
  57. MmapAllocator(std::FILE* f_ptr, size_t data_offset = 0) throw()
  58. : m_file_ptr(f_ptr), m_file_desc(fileno(m_file_ptr)),
  59. m_page_size(sysconf(_SC_PAGE_SIZE)), m_map_size(0), m_data_ptr(0),
  60. m_data_offset(data_offset), m_fixed(true), m_count(new size_t(0))
  61. { }
  62. MmapAllocator(std::string fileName) throw()
  63. : m_file_ptr(std::fopen(fileName.c_str(), "wb+")), m_file_desc(fileno(m_file_ptr)),
  64. m_page_size(sysconf(_SC_PAGE_SIZE)), m_map_size(0), m_data_ptr(0),
  65. m_data_offset(0), m_fixed(false), m_count(new size_t(0))
  66. { }
  67. MmapAllocator(const MmapAllocator& c) throw()
  68. : m_file_ptr(c.m_file_ptr), m_file_desc(c.m_file_desc),
  69. m_page_size(c.m_page_size), m_map_size(c.m_map_size),
  70. m_data_ptr(c.m_data_ptr), m_data_offset(c.m_data_offset),
  71. m_fixed(c.m_fixed), m_count(c.m_count)
  72. {
  73. (*m_count)++;
  74. }
  75. ~MmapAllocator() throw()
  76. {
  77. if(m_data_ptr && *m_count == 0)
  78. {
  79. munmap(m_data_ptr, m_map_size);
  80. if(!m_fixed && std::ftell(m_file_ptr) != -1)
  81. std::fclose(m_file_ptr);
  82. }
  83. (*m_count)--;
  84. }
  85. template <class U>
  86. struct rebind {
  87. typedef MmapAllocator<U> other;
  88. };
  89. pointer address (reference value) const
  90. {
  91. return &value;
  92. }
  93. const_pointer address (const_reference value) const
  94. {
  95. return &value;
  96. }
  97. size_type max_size () const throw()
  98. {
  99. return std::numeric_limits<size_t>::max() / sizeof(value_type);
  100. }
  101. pointer allocate (size_type num, const void* = 0)
  102. {
  103. m_map_size = num * sizeof(T);
  104. if(!m_fixed)
  105. {
  106. size_t read = 0;
  107. read += ftruncate(m_file_desc, m_map_size);
  108. m_data_ptr = (char*)mmap(0, m_map_size, PROT_READ|PROT_WRITE, MAP_SHARED,
  109. m_file_desc, 0);
  110. if(m_data_ptr == MAP_FAILED)
  111. std::cerr << "Error: mmapping" << std::endl;
  112. return (pointer)m_data_ptr;
  113. }
  114. else
  115. {
  116. size_t map_offset = (m_data_offset / m_page_size) * m_page_size;
  117. size_t relative_offset = m_data_offset - map_offset;
  118. size_t map_size = m_map_size + relative_offset;
  119. m_data_ptr = (char*)mmap(0, map_size, PROT_READ, MAP_SHARED,
  120. m_file_desc, map_offset);
  121. return (pointer)(m_data_ptr + relative_offset);
  122. }
  123. }
  124. void deallocate (pointer p, size_type num)
  125. {
  126. if(!m_fixed) {
  127. munmap(p, num * sizeof(T));
  128. }
  129. else {
  130. size_t map_offset = (m_data_offset / m_page_size) * m_page_size;
  131. size_t relative_offset = m_data_offset - map_offset;
  132. munmap((pointer)((char*)p - relative_offset), num * sizeof(T));
  133. }
  134. }
  135. void construct (pointer p, const T& value)
  136. {
  137. if(!m_fixed)
  138. new(p) value_type(value);
  139. }
  140. void destroy (pointer p)
  141. {
  142. if(!m_fixed)
  143. p->~T();
  144. }
  145. template <class T1, class T2>
  146. friend bool operator== (const MmapAllocator<T1>&, const MmapAllocator<T2>&) throw();
  147. template <class T1, class T2>
  148. friend bool operator!= (const MmapAllocator<T1>&, const MmapAllocator<T2>&) throw();
  149. };
  150. template <class T1, class T2>
  151. bool operator== (const MmapAllocator<T1>& a1,
  152. const MmapAllocator<T2>& a2) throw()
  153. {
  154. bool equal = true;
  155. equal &= a1.m_file_ptr == a2.m_file_ptr;
  156. equal &= a1.m_file_desc == a2.m_file_desc;
  157. equal &= a1.m_page_size == a2.m_page_size;
  158. equal &= a1.m_map_size == a2.m_map_size;
  159. equal &= a1.m_data_ptr == a2.m_data_ptr;
  160. equal &= a1.m_data_offset == a2.m_data_offset;
  161. equal &= a1.m_fixed == a2.m_fixed;
  162. return equal;
  163. }
  164. template <class T1, class T2>
  165. bool operator!=(const MmapAllocator<T1>& a1,
  166. const MmapAllocator<T2>& a2) throw()
  167. {
  168. return !(a1 == a2);
  169. }
  170. }
  171. #endif