Browse Source

[2831] make sure flushing memory before unmapping.

JINMEI Tatuya 12 years ago
parent
commit
bb5479f56b
2 changed files with 10 additions and 1 deletions
  1. 4 0
      src/lib/util/memory_segment_mapped.cc
  2. 6 1
      src/lib/util/memory_segment_mapped.h

+ 4 - 0
src/lib/util/memory_segment_mapped.cc

@@ -51,6 +51,7 @@ struct MemorySegmentMapped::Impl {
     void growSegment() {
         // We first need to unmap it before calling grow().
         const size_t prev_size = base_sgmt_->get_size();
+        base_sgmt_->flush();
         base_sgmt_.reset();
 
         const size_t new_size = prev_size * 2;
@@ -113,6 +114,9 @@ MemorySegmentMapped::MemorySegmentMapped(const std::string& filename,
 }
 
 MemorySegmentMapped::~MemorySegmentMapped() {
+    if (impl_->base_sgmt_ && !impl_->read_only_) {
+        impl_->base_sgmt_->flush(); // note: this is exception free
+    }
     delete impl_;
 }
 

+ 6 - 1
src/lib/util/memory_segment_mapped.h

@@ -91,7 +91,12 @@ public:
     MemorySegmentMapped(const std::string& filename, bool create,
                         size_t initial_size = INITIAL_SIZE);
 
-    /// \brief Destructor
+    /// \brief Destructor.
+    ///
+    /// If the object was constructed in the read-write mode and the underlying
+    /// memory segment wasn't broken due to an exceptional event, the
+    /// destructor ensures the content of the mapped memory is written back to
+    /// the corresponding file.
     virtual ~MemorySegmentMapped();
 
     /// \brief Allocate/acquire a segment of memory.