From: Wolfgang Bangerth Date: Tue, 22 Aug 2017 18:45:43 +0000 (-0600) Subject: Use std::unique_ptr instead of raw pointers in GrowingVectorMemory. X-Git-Tag: v9.0.0-rc1~1175^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eda5626b8fad9b66ed515e0ea44cdfc8d2549806;p=dealii.git Use std::unique_ptr instead of raw pointers in GrowingVectorMemory. --- diff --git a/include/deal.II/lac/vector_memory.h b/include/deal.II/lac/vector_memory.h index ee8b4c344d..ea9717365f 100644 --- a/include/deal.II/lac/vector_memory.h +++ b/include/deal.II/lac/vector_memory.h @@ -260,10 +260,12 @@ public: private: /** - * Type to enter into the array. First component will be a flag telling - * whether the vector is used, second the vector itself. + * A type that describes this entries of an array that represents + * the vectors stored by this object. The first component of the pair + * is be a flag telling whether the vector is used, the second + * a pointer to the vector itself. */ - typedef std::pair entry_type; + typedef std::pair > entry_type; /** * The class providing the actual storage for the memory pool. @@ -272,7 +274,7 @@ private: * Only one of these pools is used for each vector type, thus allocating all * vectors from the same storage. * - * @author Guido Kanschat, 2007 + * @author Guido Kanschat, 2007, Wolfgang Bangerth 2017. */ struct Pool { @@ -280,14 +282,17 @@ private: * Standard constructor creating an empty pool */ Pool(); + /** - * Destructor. Frees memory and warns about memory leaks + * Destructor. */ ~Pool(); + /** * Create data vector; does nothing after first initialization */ void initialize(const size_type size); + /** * Pointer to the storage object */ @@ -304,6 +309,7 @@ private: * output at the end of an object's lifetime. */ size_type total_alloc; + /** * Number of vectors currently allocated in this object; used for detecting * memory leaks. diff --git a/include/deal.II/lac/vector_memory.templates.h b/include/deal.II/lac/vector_memory.templates.h index 02f64a1cff..4bd60279c3 100644 --- a/include/deal.II/lac/vector_memory.templates.h +++ b/include/deal.II/lac/vector_memory.templates.h @@ -18,6 +18,7 @@ #include +#include DEAL_II_NAMESPACE_OPEN @@ -42,18 +43,12 @@ inline GrowingVectorMemory::Pool::~Pool() { // Nothing to do if memory was unused. - if (data == nullptr) return; - - // First, delete all remaining - // vectors. Actually, there should - // be none, if there is no memory - // leak - for (typename std::vector::iterator i=data->begin(); - i != data->end(); - ++i) - { - delete i->second; - } + if (data == nullptr) + return; + + // delete the 'data' object. this also releases all vectors + // that are pointed to by the std::unique_ptrs + data->clear(); delete data; } @@ -72,7 +67,7 @@ GrowingVectorMemory::Pool::initialize(const size_type size) ++i) { i->first = false; - i->second = new VectorType; + i->second = std_cxx14::make_unique(); } } } @@ -116,6 +111,7 @@ VectorType * GrowingVectorMemory::alloc () { Threads::Mutex::ScopedLock lock(mutex); + ++total_alloc; ++current_alloc; // see if there is a free vector @@ -126,16 +122,14 @@ GrowingVectorMemory::alloc () if (i->first == false) { i->first = true; - return (i->second); + return i->second.get(); } } - // no free vector found, so let's - // just allocate a new one - const entry_type t (true, new VectorType); - pool.data->push_back(t); + // no free vector found, so let's just allocate a new one + pool.data->emplace_back(entry_type (true, std_cxx14::make_unique())); - return t.second; + return pool.data->back().second.get(); } @@ -146,10 +140,11 @@ void GrowingVectorMemory::free(const VectorType *const v) { Threads::Mutex::ScopedLock lock(mutex); + for (typename std::vector::iterator i=pool.data->begin(); i != pool.data->end(); ++i) { - if (v == (i->second)) + if (v == i->second.get()) { i->first = false; --current_alloc; @@ -168,21 +163,8 @@ GrowingVectorMemory::release_unused_memory () { Threads::Mutex::ScopedLock lock(mutex); - std::vector new_data; - if (pool.data != nullptr) - { - const typename std::vector::const_iterator - end = pool.data->end(); - for (typename std::vector::const_iterator - i = pool.data->begin(); i != end ; ++i) - if (i->first == false) - delete i->second; - else - new_data.push_back (*i); - - *pool.data = new_data; - } + pool.data->clear(); }