include/deal.II/lac/vector_memory.h

00001 //---------------------------------------------------------------------------
00002 //    @f$Id: vector_memory.h 25345 2012-03-31 08:37:04Z bangerth @f$
00003 //
00004 //    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 by the deal.II authors
00005 //
00006 //    This file is subject to QPL and may not be  distributed
00007 //    without copyright and license information. Please refer
00008 //    to the file deal.II/doc/license.html for the  text  and
00009 //    further information on this license.
00010 //
00011 //---------------------------------------------------------------------------
00012 #ifndef __deal2__vector_memory_h
00013 #define __deal2__vector_memory_h
00014 
00015 
00016 #include <deal.II/base/config.h>
00017 #include <deal.II/base/smartpointer.h>
00018 #include <deal.II/base/logstream.h>
00019 #include <deal.II/base/thread_management.h>
00020 #include <deal.II/lac/vector.h>
00021 
00022 #include <vector>
00023 #include <iostream>
00024 
00025 DEAL_II_NAMESPACE_OPEN
00026 
00027 
00030 
00068 template<class VECTOR = ::Vector<double> >
00069 class VectorMemory : public Subscriptor
00070 {
00071   public:
00072 
00078     virtual ~VectorMemory() {}
00079 
00091     virtual VECTOR* alloc () = 0;
00092 
00100     virtual void free (const VECTOR * const) = 0;
00101 
00108     DeclException0(ExcNoMoreVectors);
00113     DeclException0(ExcNotAllocatedHere);
00114 
00116 
00124     class Pointer
00125     {
00126       public:
00132         Pointer(VectorMemory<VECTOR>& mem);
00138         ~Pointer();
00139 
00143         operator VECTOR* () const;
00144 
00148         VECTOR& operator * () const;
00149 
00153         VECTOR * operator -> () const;
00154       private:
00158         SmartPointer<VectorMemory<VECTOR>,Pointer> pool;
00162         VECTOR* v;
00163     };
00164 };
00165 
00166 
00167 
00176 template<class VECTOR = ::Vector<double> >
00177 class PrimitiveVectorMemory : public VectorMemory<VECTOR>
00178 {
00179   public:
00183     PrimitiveVectorMemory () {}
00184 
00201     virtual VECTOR* alloc ()
00202       {
00203         return new VECTOR();
00204       }
00205 
00218     virtual void free (const VECTOR * const v)
00219       {
00220         delete v;
00221       }
00222 };
00223 
00224 
00225 
00252 template<class VECTOR = ::Vector<double> >
00253 class GrowingVectorMemory : public VectorMemory<VECTOR>
00254 {
00255   public:
00262     GrowingVectorMemory (const unsigned int initial_size = 0,
00263                          const bool log_statistics = false);
00264 
00276     ~GrowingVectorMemory();
00277 
00289     virtual VECTOR* alloc ();
00290 
00303     virtual void free (const VECTOR * const);
00304 
00309     static void release_unused_memory ();
00310 
00316     virtual std::size_t memory_consumption() const;
00317 
00318   private:
00326     typedef std::pair<bool, VECTOR*> entry_type;
00327 
00341     struct Pool
00342     {
00347         Pool();
00353         ~Pool();
00359         void initialize(const unsigned int size);
00364         std::vector<entry_type>* data;
00365     };
00366 
00370     static Pool pool;
00371 
00379     unsigned int total_alloc;
00385     unsigned int current_alloc;
00386 
00392     bool log_statistics;
00393 
00399     static Threads::ThreadMutex mutex;
00400 };
00401 
00404 #ifndef DOXYGEN
00405 /* --------------------- inline functions ---------------------- */
00406 
00407 
00408 template <typename VECTOR>
00409 inline
00410 VectorMemory<VECTOR>::Pointer::Pointer(VectorMemory<VECTOR>& mem)
00411                 :
00412                 pool(&mem, typeid(*this).name()), v(0)
00413 {
00414   v = pool->alloc();
00415 }
00416 
00417 
00418 template <typename VECTOR>
00419 inline
00420 VectorMemory<VECTOR>::Pointer::~Pointer()
00421 {
00422   pool->free(v);
00423 }
00424 
00425 
00426 template <typename VECTOR>
00427 inline
00428 VectorMemory<VECTOR>::Pointer::operator VECTOR* () const
00429 {
00430   return v;
00431 }
00432 
00433 
00434 template <typename VECTOR>
00435 inline
00436 VECTOR & VectorMemory<VECTOR>::Pointer::operator * () const
00437 {
00438   return *v;
00439 }
00440 
00441 
00442 template <typename VECTOR>
00443 inline
00444 VECTOR * VectorMemory<VECTOR>::Pointer::operator -> () const
00445 {
00446   return v;
00447 }
00448 
00449 
00450 #endif // DOXYGEN
00451 
00452 DEAL_II_NAMESPACE_CLOSE
00453 
00454 #endif
00455 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

deal.II documentation generated on Tue May 22 2012 12:06:02 by doxygen 1.7.3