Browse Source

[2850] Constify allMemoryDeallocated() again

Mukund Sivaraman 12 years ago
parent
commit
214d50fb38

+ 1 - 1
src/lib/util/memory_segment.h

@@ -157,7 +157,7 @@ public:
     /// \return Returns <code>true</code> if all allocated memory (including
     /// names associated by memory addresses by \c setNamedAddress()) was
     /// deallocated, <code>false</code> otherwise.
-    virtual bool allMemoryDeallocated() = 0;
+    virtual bool allMemoryDeallocated() const = 0;
 
     /// \brief Associate specified address in the segment with a given name.
     ///

+ 1 - 1
src/lib/util/memory_segment_local.cc

@@ -47,7 +47,7 @@ MemorySegmentLocal::deallocate(void* ptr, size_t size) {
 }
 
 bool
-MemorySegmentLocal::allMemoryDeallocated() {
+MemorySegmentLocal::allMemoryDeallocated() const {
     return (allocated_size_ == 0 && named_addrs_.empty());
 }
 

+ 1 - 1
src/lib/util/memory_segment_local.h

@@ -64,7 +64,7 @@ public:
     ///
     /// \return Returns <code>true</code> if all allocated memory was
     /// deallocated, <code>false</code> otherwise.
-    virtual bool allMemoryDeallocated();
+    virtual bool allMemoryDeallocated() const;
 
     /// \brief Local segment version of getNamedAddress.
     ///

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

@@ -236,7 +236,8 @@ private:
 };
 
 MemorySegmentMapped::MemorySegmentMapped(const std::string& filename) :
-    impl_(NULL)
+    impl_(NULL),
+    allocated_size_(0)
 {
     try {
         impl_ = new Impl(filename, true);
@@ -249,7 +250,8 @@ MemorySegmentMapped::MemorySegmentMapped(const std::string& filename) :
 
 MemorySegmentMapped::MemorySegmentMapped(const std::string& filename,
                                          OpenMode mode, size_t initial_size) :
-    impl_(NULL)
+    impl_(NULL),
+    allocated_size_(0)
 {
     try {
         switch (mode) {
@@ -293,6 +295,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;
             return (ptr);
         }
     }
@@ -308,7 +311,7 @@ MemorySegmentMapped::allocate(size_t size) {
 }
 
 void
-MemorySegmentMapped::deallocate(void* ptr, size_t) {
+MemorySegmentMapped::deallocate(void* ptr, size_t size) {
     if (impl_->read_only_) {
         isc_throw(MemorySegmentError,
                   "deallocate attempt on read-only segment");
@@ -321,15 +324,15 @@ MemorySegmentMapped::deallocate(void* ptr, size_t) {
     }
 
     impl_->base_sgmt_->deallocate(ptr);
+    allocated_size_ -= size;
 }
 
 bool
-MemorySegmentMapped::allMemoryDeallocated() {
-    impl_->freeReservedMemory();
-    const bool result = impl_->base_sgmt_->all_memory_deallocated();
-    impl_->reserveMemory();
-
-    return (result);
+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) &&
+             (num_named_objs == expected_num_named_objs));
 }
 
 MemorySegment::NamedAddressResult

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

@@ -182,7 +182,7 @@ public:
     /// read-only mode; in that case MemorySegmentError will be thrown.
     virtual void deallocate(void* ptr, size_t size);
 
-    virtual bool allMemoryDeallocated();
+    virtual bool allMemoryDeallocated() const;
 
     /// \brief Mapped segment version of setNamedAddress.
     ///
@@ -256,6 +256,7 @@ public:
 private:
     struct Impl;
     Impl* impl_;
+    size_t allocated_size_;
 };
 
 } // namespace util

+ 4 - 2
src/lib/util/tests/memory_segment_mapped_unittest.cc

@@ -276,11 +276,13 @@ TEST_F(MemorySegmentMappedTest, badDeallocate) {
         resetSegment();
     }
 
-    // Invalid size; this implementation doesn't detect such errors.
+    // Invalid size; this implementation doesn't detect such errors and
+    // will free the memory, but \c allMemoryDeallocated() will detect
+    // it.
     ptr = segment_->allocate(4);
     EXPECT_NE(static_cast<void*>(NULL), ptr);
     segment_->deallocate(ptr, 8);
-    EXPECT_TRUE(segment_->allMemoryDeallocated());
+    EXPECT_FALSE(segment_->allMemoryDeallocated());
 }
 
 // A helper of namedAddress.