Browse Source

[2850] Move allocate_size_ to private class

Mukund Sivaraman 12 years ago
parent
commit
62b6680e17
2 changed files with 12 additions and 9 deletions
  1. 12 8
      src/lib/util/memory_segment_mapped.cc
  2. 0 1
      src/lib/util/memory_segment_mapped.h

+ 12 - 8
src/lib/util/memory_segment_mapped.cc

@@ -74,7 +74,7 @@ struct MemorySegmentMapped::Impl {
     // to detect possible conflict with other readers or writers using
     // file lock.
     Impl(const std::string& filename, create_only_t, size_t initial_size) :
-        read_only_(false), filename_(filename)
+        read_only_(false), filename_(filename), allocated_size_(0)
     {
         try {
             // First, try opening it in boost create_only mode; it fails if
@@ -115,6 +115,7 @@ struct MemorySegmentMapped::Impl {
         read_only_(false), filename_(filename),
         base_sgmt_(new BaseSegment(open_or_create, filename.c_str(),
                                    initial_size)),
+        allocated_size_(0),
         lock_(new boost::interprocess::file_lock(filename.c_str()))
     {
         checkWriter();
@@ -127,6 +128,7 @@ struct MemorySegmentMapped::Impl {
         base_sgmt_(read_only_ ?
                    new BaseSegment(open_read_only, filename.c_str()) :
                    new BaseSegment(open_only, filename.c_str())),
+        allocated_size_(0),
         lock_(new boost::interprocess::file_lock(filename.c_str()))
     {
         if (read_only_) {
@@ -210,6 +212,10 @@ struct MemorySegmentMapped::Impl {
     // actual Boost implementation of mapped segment.
     boost::scoped_ptr<BaseSegment> base_sgmt_;
 
+    // number of bytes of memory currently allocated in this segment
+    // through allocate().
+    size_t allocated_size_;
+
 private:
     // helper methods and member to detect any reader-writer conflict at
     // the time of construction using an advisory file lock.  The lock will
@@ -236,8 +242,7 @@ private:
 };
 
 MemorySegmentMapped::MemorySegmentMapped(const std::string& filename) :
-    impl_(NULL),
-    allocated_size_(0)
+    impl_(NULL)
 {
     try {
         impl_ = new Impl(filename, true);
@@ -250,8 +255,7 @@ MemorySegmentMapped::MemorySegmentMapped(const std::string& filename) :
 
 MemorySegmentMapped::MemorySegmentMapped(const std::string& filename,
                                          OpenMode mode, size_t initial_size) :
-    impl_(NULL),
-    allocated_size_(0)
+    impl_(NULL)
 {
     try {
         switch (mode) {
@@ -295,7 +299,7 @@ MemorySegmentMapped::allocate(size_t size) {
     if (impl_->base_sgmt_->get_free_memory() >= size) {
         void* ptr = impl_->base_sgmt_->allocate(size, std::nothrow);
         if (ptr) {
-            allocated_size_ += size;
+            impl_->allocated_size_ += size;
             return (ptr);
         }
     }
@@ -324,14 +328,14 @@ MemorySegmentMapped::deallocate(void* ptr, size_t size) {
     }
 
     impl_->base_sgmt_->deallocate(ptr);
-    allocated_size_ -= size;
+    impl_->allocated_size_ -= size;
 }
 
 bool
 MemorySegmentMapped::allMemoryDeallocated() const {
      const size_t expected_num_named_objs = impl_->read_only_ ? 0 : 1;
      const size_t num_named_objs = impl_->base_sgmt_->get_num_named_objects();
-     return ((allocated_size_ == 0) &&
+     return ((impl_->allocated_size_ == 0) &&
              (num_named_objs == expected_num_named_objs));
 }
 

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

@@ -256,7 +256,6 @@ public:
 private:
     struct Impl;
     Impl* impl_;
-    size_t allocated_size_;
 };
 
 } // namespace util