memory_segment_mapped.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <util/memory_segment_mapped.h>
  15. #include <boost/scoped_ptr.hpp>
  16. #include <boost/interprocess/exceptions.hpp>
  17. #include <boost/interprocess/managed_mapped_file.hpp>
  18. #include <boost/interprocess/offset_ptr.hpp>
  19. #include <boost/interprocess/mapped_region.hpp>
  20. #include <cassert>
  21. #include <string>
  22. #include <new>
  23. using boost::interprocess::managed_mapped_file;
  24. using boost::interprocess::open_or_create;
  25. using boost::interprocess::open_only;
  26. using boost::interprocess::open_read_only;
  27. using boost::interprocess::offset_ptr;
  28. namespace isc {
  29. namespace util {
  30. struct MemorySegmentMapped::Impl {
  31. Impl(const std::string& filename, size_t initial_size) :
  32. read_only_(false), filename_(filename),
  33. base_sgmt_(new managed_mapped_file(open_or_create, filename.c_str(),
  34. initial_size))
  35. {}
  36. Impl(const std::string& filename, bool read_only) :
  37. read_only_(read_only), filename_(filename),
  38. base_sgmt_(read_only ?
  39. new managed_mapped_file(open_read_only, filename.c_str()) :
  40. new managed_mapped_file(open_only, filename.c_str()))
  41. {}
  42. // Internal helper to grow the underlying mapped segment.
  43. void growSegment() {
  44. // We first need to unmap it before calling grow().
  45. const size_t prev_size = base_sgmt_->get_size();
  46. base_sgmt_->flush();
  47. base_sgmt_.reset();
  48. const size_t new_size = prev_size * 2;
  49. assert(new_size != 0); // assume grow fails before size overflow
  50. if (!managed_mapped_file::grow(filename_.c_str(),
  51. new_size - prev_size))
  52. {
  53. throw std::bad_alloc();
  54. }
  55. try {
  56. // Remap the grown file; this should succeed, but it's not 100%
  57. // guaranteed. If it fails we treat it as if we fail to create
  58. // the new segment.
  59. base_sgmt_.reset(new managed_mapped_file(open_only,
  60. filename_.c_str()));
  61. } catch (const boost::interprocess::interprocess_exception& ex) {
  62. throw std::bad_alloc();
  63. }
  64. }
  65. // remember if the segment is opened read-only or not
  66. const bool read_only_;
  67. // mapped file; remember it in case we need to grow it.
  68. const std::string filename_;
  69. // actual Boost implementation of mapped segment.
  70. boost::scoped_ptr<managed_mapped_file> base_sgmt_;
  71. };
  72. MemorySegmentMapped::MemorySegmentMapped(const std::string& filename) :
  73. impl_(0)
  74. {
  75. try {
  76. impl_ = new Impl(filename, true);
  77. } catch (const boost::interprocess::interprocess_exception& ex) {
  78. isc_throw(MemorySegmentOpenError,
  79. "failed to open mapped memory segment for " << filename
  80. << ": " << ex.what());
  81. }
  82. }
  83. MemorySegmentMapped::MemorySegmentMapped(const std::string& filename,
  84. bool create, size_t initial_size) :
  85. impl_(0)
  86. {
  87. try {
  88. if (create) {
  89. impl_ = new Impl(filename, initial_size);
  90. } else {
  91. impl_ = new Impl(filename, false);
  92. }
  93. } catch (const boost::interprocess::interprocess_exception& ex) {
  94. isc_throw(MemorySegmentOpenError,
  95. "failed to open mapped memory segment for " << filename
  96. << ": " << ex.what());
  97. }
  98. }
  99. MemorySegmentMapped::~MemorySegmentMapped() {
  100. if (impl_->base_sgmt_ && !impl_->read_only_) {
  101. impl_->base_sgmt_->flush(); // note: this is exception free
  102. }
  103. delete impl_;
  104. }
  105. void*
  106. MemorySegmentMapped::allocate(size_t size) {
  107. if (impl_->read_only_) {
  108. isc_throw(InvalidOperation, "allocate attempt on read-only segment");
  109. }
  110. // We explicitly check the free memory size; it appears
  111. // managed_mapped_file::allocate() could incorrectly return a seemingly
  112. // valid pointer for some very large requested size.
  113. if (impl_->base_sgmt_->get_free_memory() >= size) {
  114. void* ptr = impl_->base_sgmt_->allocate(size, std::nothrow);
  115. if (ptr) {
  116. return (ptr);
  117. }
  118. }
  119. // Grow the mapped segment doubling the size until we have sufficient
  120. // free memory in the revised segment for the requested size.
  121. do {
  122. impl_->growSegment();
  123. } while (impl_->base_sgmt_->get_free_memory() < size);
  124. isc_throw(MemorySegmentGrown, "mapped memory segment grown, size: "
  125. << impl_->base_sgmt_->get_size() << ", free size: "
  126. << impl_->base_sgmt_->get_free_memory());
  127. }
  128. void
  129. MemorySegmentMapped::deallocate(void* ptr, size_t) {
  130. if (impl_->read_only_) {
  131. isc_throw(InvalidOperation, "allocate attempt on read-only segment");
  132. }
  133. // the underlying deallocate() would deal with the case where ptr == 0,
  134. // but it's an undocumented behavior, so we handle it ourselves for safety.
  135. if (!ptr) {
  136. return;
  137. }
  138. impl_->base_sgmt_->deallocate(ptr);
  139. }
  140. bool
  141. MemorySegmentMapped::allMemoryDeallocated() const {
  142. return (impl_->base_sgmt_->all_memory_deallocated());
  143. }
  144. void*
  145. MemorySegmentMapped::getNamedAddress(const char* name) {
  146. offset_ptr<void>* storage =
  147. impl_->base_sgmt_->find<offset_ptr<void> >(name).first;
  148. if (storage) {
  149. return (storage->get());
  150. }
  151. return (0);
  152. }
  153. bool
  154. MemorySegmentMapped::setNamedAddress(const char* name, void* addr) {
  155. if (impl_->read_only_) {
  156. isc_throw(InvalidOperation, "setNamedAddress on read-only segment");
  157. }
  158. if (addr && !impl_->base_sgmt_->belongs_to_segment(addr)) {
  159. isc_throw(MemorySegmentError, "out of segment address: " << addr);
  160. }
  161. bool grown = false;
  162. while (true) {
  163. offset_ptr<void>* storage =
  164. impl_->base_sgmt_->find_or_construct<offset_ptr<void> >(
  165. name, std::nothrow)();
  166. if (storage) {
  167. *storage = addr;
  168. return (grown);
  169. }
  170. impl_->growSegment();
  171. grown = true;
  172. }
  173. }
  174. bool
  175. MemorySegmentMapped::clearNamedAddress(const char* name) {
  176. if (impl_->read_only_) {
  177. isc_throw(InvalidOperation, "clearNamedAddress on read-only segment");
  178. }
  179. return (impl_->base_sgmt_->destroy<offset_ptr<void> >(name));
  180. }
  181. void
  182. MemorySegmentMapped::shrinkToFit() {
  183. if (impl_->read_only_) {
  184. isc_throw(InvalidOperation, "shrinkToFit on read-only segment");
  185. }
  186. // It appears an assertion failure is triggered within Boost if the size
  187. // is too small. To work this around we'll make it no-op if the size is
  188. // already reasonably small.
  189. if (getSize() < INITIAL_SIZE) {
  190. return;
  191. }
  192. impl_->base_sgmt_.reset();
  193. managed_mapped_file::shrink_to_fit(impl_->filename_.c_str());
  194. try {
  195. // Remap the grown file; this should succeed, but it's not 100%
  196. // guaranteed. If it fails we treat it as if we fail to create
  197. // the new segment.
  198. impl_->base_sgmt_.reset(
  199. new managed_mapped_file(open_only, impl_->filename_.c_str()));
  200. } catch (const boost::interprocess::interprocess_exception& ex) {
  201. isc_throw(MemorySegmentError,
  202. "remap after shrink failed; segment is now unusable");
  203. }
  204. }
  205. size_t
  206. MemorySegmentMapped::getSize() const {
  207. return (impl_->base_sgmt_->get_size());
  208. }
  209. size_t
  210. MemorySegmentMapped::getCheckSum() const {
  211. const size_t page_sz = boost::interprocess::mapped_region::get_page_size();
  212. const uint8_t* const cp_beg = static_cast<const uint8_t*>(
  213. impl_->base_sgmt_->get_address());
  214. const uint8_t* const cp_end = cp_beg + impl_->base_sgmt_->get_size();
  215. size_t sum = 0;
  216. for (const uint8_t* cp = cp_beg; cp < cp_end; cp += page_sz) {
  217. sum += *cp;
  218. }
  219. return (sum);
  220. }
  221. } // namespace util
  222. } // namespace isc