Reference documentation for deal.II version 8.4.1
memory_consumption.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2016 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__memory_consumption_h
17 #define dealii__memory_consumption_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/std_cxx11/shared_ptr.h>
22 #include <deal.II/base/std_cxx11/type_traits.h>
23 #include <deal.II/base/std_cxx11/unique_ptr.h>
24 
25 #include <string>
26 #include <complex>
27 #include <vector>
28 #include <cstddef>
29 #include <cstring>
30 
31 DEAL_II_NAMESPACE_OPEN
32 
33 
34 // forward declaration
35 template <typename T> class VectorizedArray;
36 
37 
92 {
98  template <typename T>
99  inline
100  typename std_cxx11::enable_if<std_cxx11::is_fundamental<T>::value, std::size_t>::type
101  memory_consumption (const T &t);
102 
109  template <typename T>
110  inline
111  typename std_cxx11::enable_if<!(std_cxx11::is_fundamental<T>::value || std_cxx11::is_pointer<T>::value), std::size_t>::type
112  memory_consumption (const T &t);
113 
120  inline
121  std::size_t memory_consumption (const char *string);
122 
127  template <typename T>
128  inline
129  std::size_t memory_consumption (const std::complex<T> &);
130 
135  template <typename T>
136  inline
137  std::size_t memory_consumption (const VectorizedArray<T> &);
138 
143  inline
144  std::size_t memory_consumption (const std::string &s);
145 
173  template <typename T>
174  inline
175  std::size_t memory_consumption (const std::vector<T> &v);
176 
185  template <typename T, int N>
186  inline
187  std::size_t memory_consumption (const T (&v)[N]);
188 
196  inline
197  std::size_t memory_consumption (const std::vector<bool> &v);
198 
203  template <typename A, typename B>
204  inline
205  std::size_t memory_consumption (const std::pair<A,B> &p);
206 
216  template<typename T>
217  inline
218  std::size_t
219  memory_consumption (const T *const);
220 
226  template <typename T>
227  inline
228  std::size_t memory_consumption (const std_cxx11::shared_ptr<T> &);
229 
235  template <typename T>
236  inline
237  std::size_t memory_consumption (const std_cxx11::unique_ptr<T> &);
238 }
239 
240 
241 
242 // now comes the implementation of these functions
243 
244 namespace MemoryConsumption
245 {
246  template <typename T>
247  inline
248  typename std_cxx11::enable_if<std_cxx11::is_fundamental<T>::value, std::size_t>::type
250  {
251  return sizeof(T);
252  }
253 
254 
255 
256  inline
257  std::size_t memory_consumption (const char *string)
258  {
259  if (string == NULL)
260  {
261  return 0;
262  }
263  else
264  {
265  return sizeof(char)*(strlen(string) /*Remember the NUL*/ + 1);
266  }
267  }
268 
269 
270 
271  template <typename T>
272  inline
273  std::size_t memory_consumption (const std::complex<T> &)
274  {
275  return sizeof(std::complex<T>);
276  }
277 
278 
279 
280  template <typename T>
281  inline
283  {
284  return sizeof(VectorizedArray<T>);
285  }
286 
287 
288 
289  inline
290  std::size_t memory_consumption (const std::string &s)
291  {
292  return sizeof(s) + s.length();
293  }
294 
295 
296 
297  template <typename T>
298  std::size_t memory_consumption (const std::vector<T> &v)
299  {
300  // shortcut for types that do not allocate memory themselves
301  if (std_cxx11::is_fundamental<T>::value || std_cxx11::is_pointer<T>::value)
302  {
303  return v.capacity()*sizeof(T) + sizeof(v);
304  }
305  else
306  {
307  std::size_t mem = sizeof(std::vector<T>);
308  for (unsigned int i=0; i<v.size(); ++i)
309  {
310  mem += memory_consumption(v[i]);
311  }
312  mem += (v.capacity() - v.size())*sizeof(T);
313  return mem;
314  }
315  }
316 
317 
318 
319  template <typename T, int N>
320  std::size_t memory_consumption (const T (&v)[N])
321  {
322  std::size_t mem = 0;
323  for (unsigned int i=0; i<N; ++i)
324  mem += memory_consumption(v[i]);
325  return mem;
326  }
327 
328 
329 
330  inline
331  std::size_t memory_consumption (const std::vector<bool> &v)
332  {
333  return v.capacity() / 8 + sizeof(v);
334  }
335 
336 
337 
338  template <typename A, typename B>
339  inline
340  std::size_t memory_consumption (const std::pair<A,B> &p)
341  {
342  return (memory_consumption(p.first) +
343  memory_consumption(p.second));
344  }
345 
346 
347 
348  template<typename T>
349  inline
350  std::size_t
351  memory_consumption(const T *const)
352  {
353  return sizeof(T *);
354  }
355 
356 
357 
358  template <typename T>
359  inline
360  std::size_t
361  memory_consumption (const std_cxx11::shared_ptr<T> &)
362  {
363  return sizeof(std_cxx11::shared_ptr<T>);
364  }
365 
366 
367 
368  template <typename T>
369  inline
370  std::size_t
371  memory_consumption (const std_cxx11::unique_ptr<T> &)
372  {
373  return sizeof(std_cxx11::unique_ptr<T>);
374  }
375 
376 
377 
378  template <typename T>
379  inline
380  typename std_cxx11::enable_if<!(std_cxx11::is_fundamental<T>::value || std_cxx11::is_pointer<T>::value), std::size_t>::type
381  memory_consumption (const T &t)
382  {
383  return t.memory_consumption();
384  }
385 }
386 
387 DEAL_II_NAMESPACE_CLOSE
388 
389 #endif
std_cxx11::enable_if< std_cxx11::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)