/// \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.
///
}
bool
-MemorySegmentLocal::allMemoryDeallocated() const {
+MemorySegmentLocal::allMemoryDeallocated() {
return (allocated_size_ == 0 && named_addrs_.empty());
}
///
/// \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.
///
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();
+ }
}
}
}
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
/// 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.
///
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));
}