Reference documentation for deal.II version 8.4.1
vector_memory.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2015 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__vector_memory_h
17 #define dealii__vector_memory_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/smartpointer.h>
22 #include <deal.II/base/logstream.h>
23 #include <deal.II/base/thread_management.h>
24 #include <deal.II/lac/vector.h>
25 
26 #include <vector>
27 #include <iostream>
28 
29 DEAL_II_NAMESPACE_OPEN
30 
31 
34 
70 template<typename VectorType = ::Vector<double> >
71 class VectorMemory : public Subscriptor
72 {
73 public:
74 
79  virtual ~VectorMemory () {}
80 
87  virtual VectorType *alloc () = 0;
88 
93  virtual void free (const VectorType *const) = 0;
94 
103  DeclException0(ExcNoMoreVectors);
107  DeclException0(ExcNotAllocatedHere);
108 
110 
118  class Pointer
119  {
120  public:
128  ~Pointer();
129 
133  operator VectorType *() const;
134 
138  VectorType &operator * () const;
139 
143  VectorType *operator -> () const;
144  private:
152  VectorType *v;
153  };
154 };
155 
156 
157 
165 template<typename VectorType = ::Vector<double> >
166 class PrimitiveVectorMemory : public VectorMemory<VectorType>
167 {
168 public:
173 
183  virtual VectorType *alloc ()
184  {
185  return new VectorType();
186  }
187 
196  virtual void free (const VectorType *const v)
197  {
198  delete v;
199  }
200 };
201 
202 
203 
228 template<typename VectorType = ::Vector<double> >
229 class GrowingVectorMemory : public VectorMemory<VectorType>
230 {
231 public:
236 
241  GrowingVectorMemory (const size_type initial_size = 0,
242  const bool log_statistics = false);
243 
251  virtual ~GrowingVectorMemory();
252 
259  virtual VectorType *alloc ();
260 
268  virtual void free (const VectorType *const);
269 
273  static void release_unused_memory ();
274 
278  virtual std::size_t memory_consumption() const;
279 
280 private:
285  typedef std::pair<bool, VectorType *> entry_type;
286 
296  struct Pool
297  {
301  Pool();
305  ~Pool();
309  void initialize(const size_type size);
313  std::vector<entry_type> *data;
314  };
315 
319  static Pool pool;
320 
325  size_type total_alloc;
330  size_type current_alloc;
331 
336 
342 };
343 
346 #ifndef DOXYGEN
347 /* --------------------- inline functions ---------------------- */
348 
349 
350 template <typename VectorType>
351 inline
353  :
354  pool(&mem, typeid(*this).name()), v(0)
355 {
356  v = pool->alloc();
357 }
358 
359 
360 template <typename VectorType>
361 inline
363 {
364  pool->free(v);
365 }
366 
367 
368 template <typename VectorType>
369 inline
371 {
372  return v;
373 }
374 
375 
376 template <typename VectorType>
377 inline
379 {
380  return *v;
381 }
382 
383 
384 template <typename VectorType>
385 inline
387 {
388  return v;
389 }
390 
391 
392 #endif // DOXYGEN
393 
394 DEAL_II_NAMESPACE_CLOSE
395 
396 #endif
virtual VectorType * alloc()=0
static void release_unused_memory()
virtual VectorType * alloc()
VectorType * operator->() const
VectorType & operator*() const
SmartPointer< VectorMemory< VectorType >, Pointer > pool
DeclException0(ExcNoMoreVectors)
void initialize(const size_type size)
std::vector< entry_type > * data
virtual void free(const VectorType *const)=0
std::pair< bool, VectorType * > entry_type
GrowingVectorMemory(const size_type initial_size=0, const bool log_statistics=false)
unsigned int global_dof_index
Definition: types.h:88
virtual VectorType * alloc()
static Threads::Mutex mutex
virtual void free(const VectorType *const)
virtual ~GrowingVectorMemory()
virtual std::size_t memory_consumption() const
virtual void free(const VectorType *const v)
Pointer(VectorMemory< VectorType > &mem)
types::global_dof_index size_type
virtual ~VectorMemory()
Definition: vector_memory.h:79