Commit de6e68cf authored by Mukund Sivaraman's avatar Mukund Sivaraman
Browse files

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

parent 3c98ade5
......@@ -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.
///
......
......@@ -47,7 +47,7 @@ MemorySegmentLocal::deallocate(void* ptr, size_t size) {
}
bool
MemorySegmentLocal::allMemoryDeallocated() const {
MemorySegmentLocal::allMemoryDeallocated() {
return (allocated_size_ == 0 && named_addrs_.empty());
}
......
......@@ -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.
///
......
......@@ -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
......
......@@ -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.
///
......
......@@ -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));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment