Reference documentation for deal.II version 8.1.0
vector_memory.h
1 // ---------------------------------------------------------------------
2 // @f$Id: vector_memory.h 31386 2013-10-22 18:01:43Z maier @f$
3 //
4 // Copyright (C) 1998 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__vector_memory_h
18 #define __deal2__vector_memory_h
19 
20 
21 #include <deal.II/base/config.h>
22 #include <deal.II/base/smartpointer.h>
23 #include <deal.II/base/logstream.h>
24 #include <deal.II/base/thread_management.h>
25 #include <deal.II/lac/vector.h>
26 
27 #include <vector>
28 #include <iostream>
29 
31 
32 
35 
73 template<class VECTOR = ::Vector<double> >
74 class VectorMemory : public Subscriptor
75 {
76 public:
77 
83  virtual ~VectorMemory () {}
84 
96  virtual VECTOR *alloc () = 0;
97 
105  virtual void free (const VECTOR *const) = 0;
106 
113  DeclException0(ExcNoMoreVectors);
118  DeclException0(ExcNotAllocatedHere);
119 
121 
129  class Pointer
130  {
131  public:
143  ~Pointer();
144 
148  operator VECTOR *() const;
149 
153  VECTOR &operator * () const;
154 
158  VECTOR *operator -> () const;
159  private:
167  VECTOR *v;
168  };
169 };
170 
171 
172 
181 template<class VECTOR = ::Vector<double> >
182 class PrimitiveVectorMemory : public VectorMemory<VECTOR>
183 {
184 public:
189 
206  virtual VECTOR *alloc ()
207  {
208  return new VECTOR();
209  }
210 
223  virtual void free (const VECTOR *const v)
224  {
225  delete v;
226  }
227 };
228 
229 
230 
257 template<class VECTOR = ::Vector<double> >
258 class GrowingVectorMemory : public VectorMemory<VECTOR>
259 {
260 public:
265 
272  GrowingVectorMemory (const size_type initial_size = 0,
273  const bool log_statistics = false);
274 
286  virtual ~GrowingVectorMemory();
287 
299  virtual VECTOR *alloc ();
300 
313  virtual void free (const VECTOR *const);
314 
319  static void release_unused_memory ();
320 
326  virtual std::size_t memory_consumption() const;
327 
328 private:
336  typedef std::pair<bool, VECTOR *> entry_type;
337 
351  struct Pool
352  {
357  Pool();
363  ~Pool();
369  void initialize(const size_type size);
374  std::vector<entry_type> *data;
375  };
376 
380  static Pool pool;
381 
389  size_type total_alloc;
395  size_type current_alloc;
396 
403 
410 };
411 
414 #ifndef DOXYGEN
415 /* --------------------- inline functions ---------------------- */
416 
417 
418 template <typename VECTOR>
419 inline
421  :
422  pool(&mem, typeid(*this).name()), v(0)
423 {
424  v = pool->alloc();
425 }
426 
427 
428 template <typename VECTOR>
429 inline
431 {
432  pool->free(v);
433 }
434 
435 
436 template <typename VECTOR>
437 inline
439 {
440  return v;
441 }
442 
443 
444 template <typename VECTOR>
445 inline
447 {
448  return *v;
449 }
450 
451 
452 template <typename VECTOR>
453 inline
455 {
456  return v;
457 }
458 
459 
460 #endif // DOXYGEN
461 
462 DEAL_II_NAMESPACE_CLOSE
463 
464 #endif
std::pair< bool, VECTOR * > entry_type
std::vector< entry_type > * data
virtual ~VectorMemory()
Definition: vector_memory.h:83
SmartPointer< VectorMemory< VECTOR >, Pointer > pool
virtual VECTOR * alloc()=0
virtual VECTOR * alloc()
VECTOR & operator*() const
static Threads::Mutex mutex
unsigned int global_dof_index
Definition: types.h:100
DeclException0(ExcNoMoreVectors)
virtual void free(const VECTOR *const)=0
types::global_dof_index size_type
Pointer(VectorMemory< VECTOR > &mem)
virtual void free(const VECTOR *const v)
VECTOR * operator->() const