memory_segment_mapped.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. void* ptr = impl_->base_sgmt_->allocate(size, std::nothrow);
  74. if (ptr) {
  75. return (ptr);
  76. }
  77. // Grow the mapped segment doubling the size until we have sufficient
  78. // free memory in the revised segment for the requested size.
  79. while (impl_->base_sgmt_->get_free_memory() < size) {
  80. // We first need to unmap it before calling grow().
  81. const size_t prev_size = impl_->base_sgmt_->get_size();
  82. impl_->base_sgmt_.reset();
  83. const size_t new_size = prev_size * 2;
  84. assert(new_size != 0); // assume grow fails before size overflow
  85. // TBD error handling
  86. managed_mapped_file::grow(impl_->filename_.c_str(),
  87. new_size - prev_size);
  88. impl_->base_sgmt_.reset(
  89. new managed_mapped_file(open_only, impl_->filename_.c_str()));
  90. }
  91. isc_throw(MemorySegmentGrown, "mapped memory segment grown, size: "
  92. << impl_->base_sgmt_->get_size() << ", free size: "
  93. << impl_->base_sgmt_->get_free_memory());
  94. }
  95. void
  96. MemorySegmentMapped::deallocate(void* ptr, size_t /*size*/) {
  97. impl_->base_sgmt_->deallocate(ptr);
  98. }
  99. bool
  100. MemorySegmentMapped::allMemoryDeallocated() const {
  101. return (impl_->base_sgmt_->all_memory_deallocated());
  102. }
  103. size_t
  104. MemorySegmentMapped::getSize() const {
  105. return (impl_->base_sgmt_->get_size());
  106. }
  107. } // namespace util
  108. } // namespace isc