]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Use std::unique_ptr instead of raw pointers in GrowingVectorMemory.
authorWolfgang Bangerth <bangerth@colostate.edu>
Tue, 22 Aug 2017 18:45:43 +0000 (12:45 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Tue, 22 Aug 2017 18:45:43 +0000 (12:45 -0600)
include/deal.II/lac/vector_memory.h
include/deal.II/lac/vector_memory.templates.h

index ee8b4c344d46df61c98dcedfd0b80510276b785a..ea9717365fe90b7da04391fead72bf0b026a0cec 100644 (file)
@@ -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<bool, VectorType *> entry_type;
+  typedef std::pair<bool, std::unique_ptr<VectorType> > 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.
index 02f64a1cfff7c78701911f1fcc32cb61ad923831..4bd60279c3e2a118cc7efeabd3946e5d15d500f7 100644 (file)
@@ -18,6 +18,7 @@
 
 
 #include <deal.II/lac/vector_memory.h>
+#include <deal.II/base/std_cxx14/memory.h>
 
 DEAL_II_NAMESPACE_OPEN
 
@@ -42,18 +43,12 @@ inline
 GrowingVectorMemory<VectorType>::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<entry_type>::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<VectorType>::Pool::initialize(const size_type size)
            ++i)
         {
           i->first = false;
-          i->second = new VectorType;
+          i->second = std_cxx14::make_unique<VectorType>();
         }
     }
 }
@@ -116,6 +111,7 @@ VectorType *
 GrowingVectorMemory<VectorType>::alloc ()
 {
   Threads::Mutex::ScopedLock lock(mutex);
+
   ++total_alloc;
   ++current_alloc;
   // see if there is a free vector
@@ -126,16 +122,14 @@ GrowingVectorMemory<VectorType>::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<VectorType>()));
 
-  return t.second;
+  return pool.data->back().second.get();
 }
 
 
@@ -146,10 +140,11 @@ void
 GrowingVectorMemory<VectorType>::free(const VectorType *const v)
 {
   Threads::Mutex::ScopedLock lock(mutex);
+
   for (typename std::vector<entry_type>::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<VectorType>::release_unused_memory ()
 {
   Threads::Mutex::ScopedLock lock(mutex);
 
-  std::vector<entry_type> new_data;
-
   if (pool.data != nullptr)
-    {
-      const typename std::vector<entry_type>::const_iterator
-      end = pool.data->end();
-      for (typename std::vector<entry_type>::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();
 }
 
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.