memory_segment_mapped.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <cassert>
  19. #include <string>
  20. #include <new>
  21. using boost::interprocess::managed_mapped_file;
  22. using boost::interprocess::open_or_create;
  23. using boost::interprocess::open_only;
  24. namespace isc {
  25. namespace util {
  26. struct MemorySegmentMapped::Impl {
  27. Impl(const std::string& filename, size_t initial_size) :
  28. filename_(filename),
  29. base_sgmt_(new managed_mapped_file(open_or_create, filename.c_str(),
  30. initial_size))
  31. {}
  32. Impl(const std::string& filename) :
  33. filename_(filename),
  34. base_sgmt_(new managed_mapped_file(open_only, filename.c_str()))
  35. {}
  36. // mapped file; remember it in case we need to grow it.
  37. const std::string filename_;
  38. // actual Boost implementation of mapped segment.
  39. boost::scoped_ptr<managed_mapped_file> base_sgmt_;
  40. };
  41. MemorySegmentMapped::MemorySegmentMapped(const std::string& filename) :
  42. impl_(0)
  43. {
  44. try {
  45. impl_ = new Impl(filename);
  46. } catch (const boost::interprocess::interprocess_exception& ex) {
  47. isc_throw(MemorySegmentOpenError,
  48. "failed to open mapped memory segment for " << filename
  49. << ": " << ex.what());
  50. }
  51. }
  52. MemorySegmentMapped::MemorySegmentMapped(const std::string& filename,
  53. bool create, size_t initial_size) :
  54. impl_(0)
  55. {
  56. try {
  57. if (create) {
  58. impl_ = new Impl(filename, initial_size);
  59. } else {
  60. impl_ = new Impl(filename);
  61. }
  62. } catch (const boost::interprocess::interprocess_exception& ex) {
  63. isc_throw(MemorySegmentOpenError,
  64. "failed to open mapped memory segment for " << filename
  65. << ": " << ex.what());
  66. }
  67. }
  68. MemorySegmentMapped::~MemorySegmentMapped() {
  69. delete impl_;
  70. }
  71. void*
  72. MemorySegmentMapped::allocate(size_t size) {
  73. // We explicitly check the free memory size; it appears
  74. // managed_mapped_file::allocate() could incorrectly return a seemingly
  75. // valid pointer for some very large requested size.
  76. if (impl_->base_sgmt_->get_free_memory() >= size) {
  77. void* ptr = impl_->base_sgmt_->allocate(size, std::nothrow);
  78. if (ptr) {
  79. return (ptr);
  80. }
  81. }
  82. // Grow the mapped segment doubling the size until we have sufficient
  83. // free memory in the revised segment for the requested size.
  84. do {
  85. // We first need to unmap it before calling grow().
  86. const size_t prev_size = impl_->base_sgmt_->get_size();
  87. impl_->base_sgmt_.reset();
  88. const size_t new_size = prev_size * 2;
  89. assert(new_size != 0); // assume grow fails before size overflow
  90. if (!managed_mapped_file::grow(impl_->filename_.c_str(),
  91. new_size - prev_size))
  92. {
  93. throw std::bad_alloc();
  94. }
  95. try {
  96. // Remap the grown file; this should succeed, but it's not 100%
  97. // guaranteed. If it fails we treat it as if we fail to create
  98. // the new segment.
  99. impl_->base_sgmt_.reset(
  100. new managed_mapped_file(open_only, impl_->filename_.c_str()));
  101. } catch (const boost::interprocess::interprocess_exception& ex) {
  102. throw std::bad_alloc();
  103. }
  104. } while (impl_->base_sgmt_->get_free_memory() < size);
  105. isc_throw(MemorySegmentGrown, "mapped memory segment grown, size: "
  106. << impl_->base_sgmt_->get_size() << ", free size: "
  107. << impl_->base_sgmt_->get_free_memory());
  108. }
  109. void
  110. MemorySegmentMapped::deallocate(void* ptr, size_t /*size*/) {
  111. impl_->base_sgmt_->deallocate(ptr);
  112. }
  113. bool
  114. MemorySegmentMapped::allMemoryDeallocated() const {
  115. return (impl_->base_sgmt_->all_memory_deallocated());
  116. }
  117. size_t
  118. MemorySegmentMapped::getSize() const {
  119. return (impl_->base_sgmt_->get_size());
  120. }
  121. } // namespace util
  122. } // namespace isc