From: Wolfgang Bangerth Date: Tue, 31 Oct 2023 16:27:00 +0000 (-0600) Subject: Minor code updates to GrowingVectorMemory. X-Git-Tag: relicensing~330^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d5c8db6e2f79a17b1c65c7481ae017b4316c84fc;p=dealii.git Minor code updates to GrowingVectorMemory. --- diff --git a/include/deal.II/lac/vector_memory.templates.h b/include/deal.II/lac/vector_memory.templates.h index 70fa3ab7f6..f19f20d961 100644 --- a/include/deal.II/lac/vector_memory.templates.h +++ b/include/deal.II/lac/vector_memory.templates.h @@ -107,6 +107,11 @@ inline GrowingVectorMemory::GrowingVectorMemory( , current_alloc(0) , log_statistics(log_statistics) { + // Constructors generally cannot be called in parallel and so it is not + // necessary to guard access to member variables with a mutex -- unless + // the member variable being accessed is 'static', which is the case + // for the things get_pool() returns, and in that case the mutex itself + // must also be 'static' as it is here. std::lock_guard lock(mutex); get_pool().initialize(initial_size); } @@ -136,23 +141,23 @@ GrowingVectorMemory::alloc() ++total_alloc; ++current_alloc; - // see if there is a free vector - // available in our list - for (typename std::vector::iterator i = get_pool().data->begin(); - i != get_pool().data->end(); - ++i) + + // See if there is a currently unused vector available in our list + for (entry_type &i : *get_pool().data) { - if (i->first == false) + if (i.first == false) { - i->first = true; - return i->second.get(); + i.first = true; + return i.second.get(); } } - // no free vector found, so let's just allocate a new one - get_pool().data->emplace_back(true, std::make_unique()); + // No currently unused vector found, so let's just allocate a new one + // and return it: + const auto &new_entry = + get_pool().data->emplace_back(true, std::make_unique()); - return get_pool().data->back().second.get(); + return new_entry.second.get(); } @@ -163,17 +168,19 @@ GrowingVectorMemory::free(const VectorType *const v) { std::lock_guard lock(mutex); - for (typename std::vector::iterator i = get_pool().data->begin(); - i != get_pool().data->end(); - ++i) + // Find the vector to be de-allocated and mark it as now unused: + for (entry_type &i : *get_pool().data) { - if (v == i->second.get()) + if (v == i.second.get()) { - i->first = false; + i.first = false; --current_alloc; return; } } + + // If we got here, someone is trying to free a vector that has not + // been allocated! Assert(false, typename VectorMemory::ExcNotAllocatedHere()); } @@ -197,14 +204,10 @@ GrowingVectorMemory::memory_consumption() const { std::lock_guard lock(mutex); - std::size_t result = sizeof(*this); - const typename std::vector::const_iterator end = - get_pool().data->end(); - for (typename std::vector::const_iterator i = - get_pool().data->begin(); - i != end; - ++i) - result += sizeof(*i) + MemoryConsumption::memory_consumption(i->second); + std::size_t result = sizeof(*this); + for (const entry_type &i : *get_pool().data) + result += + sizeof(entry_type) + MemoryConsumption::memory_consumption(i.second); return result; }