Reference documentation for deal.II version 8.5.1
vector_memory.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2017 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 
104  "You are trying to deallocate a vector from a memory pool, but this "
105  "vector has not actually been allocated by the same pool before.");
106 
108 
123  class Pointer
124  {
125  public:
133  ~Pointer();
134 
138  operator VectorType *() const;
139 
143  VectorType &operator * () const;
144 
148  VectorType *operator -> () const;
149 
150  private:
155 
159  VectorType *v;
160  };
161 };
162 
163 
164 
172 template<typename VectorType = ::Vector<double> >
173 class PrimitiveVectorMemory : public VectorMemory<VectorType>
174 {
175 public:
180 
190  virtual VectorType *alloc ()
191  {
192  return new VectorType();
193  }
194 
203  virtual void free (const VectorType *const v)
204  {
205  delete v;
206  }
207 };
208 
209 
210 
235 template<typename VectorType = ::Vector<double> >
236 class GrowingVectorMemory : public VectorMemory<VectorType>
237 {
238 public:
243 
248  GrowingVectorMemory (const size_type initial_size = 0,
249  const bool log_statistics = false);
250 
258  virtual ~GrowingVectorMemory();
259 
266  virtual VectorType *alloc ();
267 
275  virtual void free (const VectorType *const);
276 
280  static void release_unused_memory ();
281 
285  virtual std::size_t memory_consumption() const;
286 
287 private:
292  typedef std::pair<bool, VectorType *> entry_type;
293 
303  struct Pool
304  {
308  Pool();
312  ~Pool();
316  void initialize(const size_type size);
320  std::vector<entry_type> *data;
321  };
322 
326  static Pool pool;
327 
338 
343 
349 };
350 
351 
352 
353 namespace internal
354 {
355  namespace GrowingVectorMemory
356  {
357  void release_all_unused_memory();
358  }
359 }
360 
363 #ifndef DOXYGEN
364 /* --------------------- inline functions ---------------------- */
365 
366 
367 template <typename VectorType>
368 inline
370  :
371  pool(&mem, typeid(*this).name()), v(0)
372 {
373  v = pool->alloc();
374 }
375 
376 
377 template <typename VectorType>
378 inline
380 {
381  try
382  {
383  pool->free(v);
384  }
385  catch (...)
386  {}
387 }
388 
389 
390 template <typename VectorType>
391 inline
393 {
394  return v;
395 }
396 
397 
398 template <typename VectorType>
399 inline
401 {
402  return *v;
403 }
404 
405 
406 template <typename VectorType>
407 inline
409 {
410  return v;
411 }
412 
413 
414 #endif // DOXYGEN
415 
416 DEAL_II_NAMESPACE_CLOSE
417 
418 #endif
virtual VectorType * alloc()=0
static void release_unused_memory()
virtual VectorType * alloc()
SmartPointer< VectorMemory< VectorType >, Pointer > pool
void initialize(const size_type size)
std::vector< entry_type > * data
virtual void free(const VectorType *const)=0
std::pair< bool, VectorType * > entry_type
static ::ExceptionBase & ExcNotAllocatedHere()
GrowingVectorMemory(const size_type initial_size=0, const bool log_statistics=false)
VectorType & operator*() const
unsigned int global_dof_index
Definition: types.h:88
virtual VectorType * alloc()
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:553
static Threads::Mutex mutex
virtual void free(const VectorType *const)
virtual ~GrowingVectorMemory()
VectorType * operator->() const
virtual void free(const VectorType *const v)
Pointer(VectorMemory< VectorType > &mem)
virtual std::size_t memory_consumption() const
types::global_dof_index size_type
virtual ~VectorMemory()
Definition: vector_memory.h:79