From be3f7f27f82e1b41831b326a1923d79a58e903d4 Mon Sep 17 00:00:00 2001 From: kanschat Date: Wed, 3 Nov 1999 15:10:04 +0000 Subject: [PATCH] new class GrowingVectorMemory git-svn-id: https://svn.dealii.org/trunk@1821 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/lac/include/lac/vector_memory.h | 167 ++++++++++++++++++++++-- 1 file changed, 156 insertions(+), 11 deletions(-) diff --git a/deal.II/lac/include/lac/vector_memory.h b/deal.II/lac/include/lac/vector_memory.h index f6e989c530..6cafcaf64e 100644 --- a/deal.II/lac/include/lac/vector_memory.h +++ b/deal.II/lac/include/lac/vector_memory.h @@ -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 - +#include /** @@ -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 PrimitiveVectorMemory : public VectorMemory { +class PrimitiveVectorMemory : public VectorMemory +{ 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 GrowingVectorMemory : public VectorMemory +{ + 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 entry_type; + /** + * Array of allocated vectors. + */ + vector pool; + /** + * Overall number of allocations. + */ + unsigned int n_alloc; }; +template +GrowingVectorMemory::GrowingVectorMemory(unsigned int + initial_size) + : pool(initial_size) +{ + for (vector::iterator i=pool.begin();i != pool.end() + ;++i) + { + i->first = false; + i->second = new Vector; + } + n_alloc = 0; +} + +template +GrowingVectorMemory::~GrowingVectorMemory() +{ + unsigned int n = 0; + for (vector::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 +Vector* +GrowingVectorMemory::alloc() +{ + ++n_alloc; + for (vector::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 +void +GrowingVectorMemory::free(const Vector* const v) +{ + for (vector::iterator i=pool.begin();i != pool.end() ;++i) + { + if (v == (i->second)) + { + i->first = false; + return; + } + } + Assert(false, ExcNotAllocatedHere()); +} /*---------------------------- vector_memory.h ---------------------------*/ -- 2.39.5