]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
new class GrowingVectorMemory
authorkanschat <kanschat@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 3 Nov 1999 15:10:04 +0000 (15:10 +0000)
committerkanschat <kanschat@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 3 Nov 1999 15:10:04 +0000 (15:10 +0000)
git-svn-id: https://svn.dealii.org/trunk@1821 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/lac/include/lac/vector_memory.h

index f6e989c5306b34925851472e1efa92f651abd23e..6cafcaf64e7ea338082ceef4e27d279fc5762c55 100644 (file)
@@ -1,12 +1,12 @@
 /*----------------------------   vector_memory.h     ---------------------------*/
 /*      $Id$                 */
-#ifndef __vector_memory_H
-#define __vector_memory_H
+#ifndef __lac__vector_memory_H
+#define __lac__vector_memory_H
 /*----------------------------   vector_memory.h     ---------------------------*/
 
 
 #include <base/subscriptor.h>
-
+#include <base/logstream.h>
 
 
 /**
@@ -43,6 +43,15 @@ class VectorMemory : public Subscriptor
                                      * for later use.
                                      */
     virtual void free(const Vector*) = 0;
+                                    /**
+                                     * No more available vectors.
+                                     */
+    DeclException0(ExcNoMoreVectors);
+                                    /**
+                                     * Vector was not allocated from
+                                     * this memory pool.
+                                     */
+    DeclException0(ExcNotAllocatedHere);
 };
 
 
@@ -56,29 +65,165 @@ class VectorMemory : public Subscriptor
  * specially adapted actions to the purpose of this class.
  */
 template<class Vector>
-class PrimitiveVectorMemory : public VectorMemory<Vector> {
+class PrimitiveVectorMemory : public VectorMemory<Vector>
+{
   public:
                                     /**
                                      * Constructor.
                                      */
-    PrimitiveVectorMemory () {};
+    PrimitiveVectorMemory () {}
 
                                     /**
                                      * Allocate a vector
                                      * from the global heap.
                                      */
-    virtual Vector* alloc() {
-      return new Vector();
-    };
+    virtual Vector* alloc()
+      {
+       return new Vector();
+      }
     
                                     /**
                                      * Return a vector to the global heap.
                                      */
-    virtual void free (const Vector* v) {
-      delete v;
-    };
+    virtual void free (const Vector* v)
+      {
+       delete v;
+      }
+};
+
+
+/**
+ * Memory keeping allocated vectors.  This #VectorMemory# is able to
+ * grow infinitely (according to computer memory).  A vector once
+ * allocated will stay in the memory pool, though, and it will be
+ * reused in later allocation.
+ * 
+ * @author Guido Kanschat, 1999
+ */
+
+template<class Vector>
+class GrowingVectorMemory : public VectorMemory<Vector>
+{
+  public:
+                                    /**
+                                     * Constructor.
+                                     * The argument allows to
+                                     * preallocate a certain number
+                                     * of vectors.
+                                     */
+    GrowingVectorMemory(unsigned int initial_size = 0);
+                                    /**
+                                     * Destructor.
+                                     * Release all vectors.
+                                     * This destructor also offers
+                                     * some statistic on the number
+                                     * of allocated vectors.
+                                     *
+                                     * The log file will also contain
+                                     * a warning message, if there
+                                     * are allocated vectors left.
+                                     */
+    ~GrowingVectorMemory();
+                                    /**
+                                     * Return new vector from the pool.
+                                     */
+    virtual Vector* alloc();
+    
+                                    /**
+                                     * Return a vector into the pool
+                                     * for later use.
+                                     */
+    virtual void free(const Vector*);
+  private:
+                                    /**
+                                     * Type to enter into the
+                                     * array. First component will be
+                                     * a flag telling whether the
+                                     * vector is used, second the
+                                     * vector itself.
+                                     */
+    typedef pair<bool, Vector* > entry_type;
+                                    /**
+                                     * Array of allocated vectors.
+                                     */
+    vector<entry_type> pool;
+                                    /**
+                                     * Overall number of allocations.
+                                     */
+    unsigned int n_alloc;
 };
 
+template<typename Vector>
+GrowingVectorMemory<Vector>::GrowingVectorMemory(unsigned int
+                                                initial_size)
+               : pool(initial_size)
+{
+  for (vector<entry_type>::iterator i=pool.begin();i != pool.end()
+                                     ;++i)
+    {
+      i->first = false;
+      i->second = new Vector;
+    }
+  n_alloc = 0;
+}
+
+template<typename Vector>
+GrowingVectorMemory<Vector>::~GrowingVectorMemory()
+{
+  unsigned int n = 0;
+  for (vector<entry_type>::iterator i=pool.begin();i != pool.end() ;++i)
+    {
+      if (i->first == true)
+       ++n;
+      delete i->second;
+    }
+  deallog << "GrowingVectorMemory:Overall allocated vectors: "
+         << n_alloc << endl;
+  deallog << "GrowingVectorMemory:Maximum allocated vectors: "
+         << pool.size() << endl;
+  if (n!=0)
+    deallog << "GrowingVectorMemory:Vectors not deallocated: "
+           << n << " Memory leak!" << endl;
+}
+
+
+
+template<typename Vector>
+Vector*
+GrowingVectorMemory<Vector>::alloc()
+{
+  ++n_alloc;
+  for (vector<entry_type>::iterator i=pool.begin();i != pool.end() ;++i)
+    {
+      if (i->first == false)
+       {
+         i->first = true;
+         return (i->second);
+       }
+    }
+  entry_type t;
+  t.first = true;
+  t.second = new Vector;
+  pool.push_back(t);
+  return t.second;
+}
+
+  
+  
+template<typename Vector>
+void
+GrowingVectorMemory<Vector>::free(const Vector* const v)
+{
+  for (vector<entry_type>::iterator i=pool.begin();i != pool.end() ;++i)
+    {
+      if (v == (i->second))
+       {
+         i->first = false;
+         return;
+       }
+    }
+  Assert(false, ExcNotAllocatedHere());
+}
 
 
 /*----------------------------   vector_memory.h     ---------------------------*/

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.