Browse Source

[2850] Fix MemorySegmentMapped::allMemoryDeallocated() and make it non-const

Mukund Sivaraman 12 years ago
parent
commit
de6e68cf25

+ 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() const = 0;
+    virtual bool allMemoryDeallocated() = 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() const {
+MemorySegmentLocal::allMemoryDeallocated() {
     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() const;
+    virtual bool allMemoryDeallocated();
 
     /// \brief Local segment version of getNamedAddress.
     ///

+ 22 - 7
src/lib/util/memory_segment_mapped.cc

@@ -139,11 +139,22 @@ struct MemorySegmentMapped::Impl {
 
     void reserveMemory() {
         if (!read_only_) {
-            // Reserve a named address for use during setNamedAddress().
-            const offset_ptr<void>* reserved_storage =
-                base_sgmt_->find_or_construct<offset_ptr<void> >(
-                    RESERVED_NAMED_ADDRESS_STORAGE_NAME, std::nothrow)();
-            assert(reserved_storage);
+            // Reserve a named address for use during
+            // setNamedAddress(). Though this will almost always succeed
+            // during construction, it may fail later during a call from
+            // allMemoryDeallocated() when the segment has been in use
+            // for a while.
+            while (true) {
+                const offset_ptr<void>* reserved_storage =
+                    base_sgmt_->find_or_construct<offset_ptr<void> >(
+                        RESERVED_NAMED_ADDRESS_STORAGE_NAME, std::nothrow)();
+
+                if (reserved_storage) {
+                    break;
+                }
+
+                growSegment();
+            }
         }
     }
 
@@ -306,8 +317,12 @@ MemorySegmentMapped::deallocate(void* ptr, size_t) {
 }
 
 bool
-MemorySegmentMapped::allMemoryDeallocated() const {
-    return (impl_->base_sgmt_->all_memory_deallocated());
+MemorySegmentMapped::allMemoryDeallocated() {
+    impl_->freeReservedMemory();
+    const bool result = impl_->base_sgmt_->all_memory_deallocated();
+    impl_->reserveMemory();
+
+    return (result);
 }
 
 MemorySegment::NamedAddressResult

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

@@ -175,7 +175,7 @@ public:
     /// read-only mode; in that case MemorySegmentError will be thrown.
     virtual void deallocate(void* ptr, size_t size);
 
-    virtual bool allMemoryDeallocated() const;
+    virtual bool allMemoryDeallocated();
 
     /// \brief Mapped segment version of setNamedAddress.
     ///

+ 8 - 1
src/lib/util/tests/memory_segment_mapped_unittest.cc

@@ -467,7 +467,14 @@ TEST_F(MemorySegmentMappedTest, shrink) {
     EXPECT_EQ(shrinked_size, segment_->getSize());
 
     // Check that the segment is still usable after shrink.
-    void* p = segment_->allocate(sizeof(uint32_t));
+    void *p = NULL;
+    while (!p) {
+        try {
+            p = segment_->allocate(sizeof(uint32_t));
+        } catch (const MemorySegmentGrown&) {
+            // Do nothing. Just try again.
+        }
+    }
     segment_->deallocate(p, sizeof(uint32_t));
 }