Reference documentation for deal.II version Git 0ec2b6e 2017-02-26 08:36:35 -0700
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 #include <deal.II/base/std_cxx11/array.h>
25 
26 #include <string>
27 #include <complex>
28 #include <vector>
29 #include <cstddef>
30 #include <cstring>
31 
32 DEAL_II_NAMESPACE_OPEN
33 
34 
35 // forward declaration
36 template <typename T> class VectorizedArray;
37 
38 
93 {
99  template <typename T>
100  inline
101  typename std_cxx11::enable_if<std_cxx11::is_fundamental<T>::value, std::size_t>::type
102  memory_consumption (const T &t);
103 
110  template <typename T>
111  inline
112  typename std_cxx11::enable_if<!(std_cxx11::is_fundamental<T>::value || std_cxx11::is_pointer<T>::value), std::size_t>::type
113  memory_consumption (const T &t);
114 
121  inline
122  std::size_t memory_consumption (const char *string);
123 
128  template <typename T>
129  inline
130  std::size_t memory_consumption (const std::complex<T> &);
131 
136  template <typename T>
137  inline
138  std::size_t memory_consumption (const VectorizedArray<T> &);
139 
144  inline
145  std::size_t memory_consumption (const std::string &s);
146 
174  template <typename T>
175  inline
176  std::size_t memory_consumption (const std::vector<T> &v);
177 
198  template <typename T, std::size_t N>
199  inline
200  std::size_t memory_consumption (const std_cxx11::array<T,N> &v);
201 
210  template <typename T, int N>
211  inline
212  std::size_t memory_consumption (const T (&v)[N]);
213 
221  inline
222  std::size_t memory_consumption (const std::vector<bool> &v);
223 
228  template <typename A, typename B>
229  inline
230  std::size_t memory_consumption (const std::pair<A,B> &p);
231 
241  template<typename T>
242  inline
243  std::size_t
244  memory_consumption (const T *const);
245 
251  template <typename T>
252  inline
253  std::size_t memory_consumption (const std_cxx11::shared_ptr<T> &);
254 
260  template <typename T>
261  inline
262  std::size_t memory_consumption (const std_cxx11::unique_ptr<T> &);
263 }
264 
265 
266 
267 // now comes the implementation of these functions
268 
269 namespace MemoryConsumption
270 {
271  template <typename T>
272  inline
273  typename std_cxx11::enable_if<std_cxx11::is_fundamental<T>::value, std::size_t>::type
275  {
276  return sizeof(T);
277  }
278 
279 
280 
281  inline
282  std::size_t memory_consumption (const char *string)
283  {
284  if (string == NULL)
285  {
286  return 0;
287  }
288  else
289  {
290  return sizeof(char)*(strlen(string) /*Remember the NUL*/ + 1);
291  }
292  }
293 
294 
295 
296  template <typename T>
297  inline
298  std::size_t memory_consumption (const std::complex<T> &)
299  {
300  return sizeof(std::complex<T>);
301  }
302 
303 
304 
305  template <typename T>
306  inline
308  {
309  return sizeof(VectorizedArray<T>);
310  }
311 
312 
313 
314  inline
315  std::size_t memory_consumption (const std::string &s)
316  {
317  return sizeof(s) + s.length();
318  }
319 
320 
321 
322  template <typename T>
323  std::size_t memory_consumption (const std::vector<T> &v)
324  {
325  // shortcut for types that do not allocate memory themselves
326  if (std_cxx11::is_fundamental<T>::value || std_cxx11::is_pointer<T>::value)
327  {
328  return v.capacity()*sizeof(T) + sizeof(v);
329  }
330  else
331  {
332  std::size_t mem = sizeof(std::vector<T>);
333  for (unsigned int i=0; i<v.size(); ++i)
334  {
335  mem += memory_consumption(v[i]);
336  }
337  mem += (v.capacity() - v.size())*sizeof(T);
338  return mem;
339  }
340  }
341 
342 
343 
344  template <typename T, std::size_t N>
345  std::size_t memory_consumption (const std_cxx11::array<T,N> &v)
346  {
347  // shortcut for types that do not allocate memory themselves
348  if (std_cxx11::is_fundamental<T>::value || std_cxx11::is_pointer<T>::value)
349  {
350  return sizeof(v);
351  }
352  else
353  {
354  std::size_t mem = 0;
355  for (std::size_t i=0; i!=N; ++i)
356  mem += memory_consumption(v[i]);
357  return mem;
358  }
359  }
360 
361 
362 
363  template <typename T, int N>
364  std::size_t memory_consumption (const T (&v)[N])
365  {
366  std::size_t mem = 0;
367  for (unsigned int i=0; i<N; ++i)
368  mem += memory_consumption(v[i]);
369  return mem;
370  }
371 
372 
373 
374  inline
375  std::size_t memory_consumption (const std::vector<bool> &v)
376  {
377  return v.capacity() / 8 + sizeof(v);
378  }
379 
380 
381 
382  template <typename A, typename B>
383  inline
384  std::size_t memory_consumption (const std::pair<A,B> &p)
385  {
386  return (memory_consumption(p.first) +
387  memory_consumption(p.second));
388  }
389 
390 
391 
392  template<typename T>
393  inline
394  std::size_t
395  memory_consumption(const T *const)
396  {
397  return sizeof(T *);
398  }
399 
400 
401 
402  template <typename T>
403  inline
404  std::size_t
405  memory_consumption (const std_cxx11::shared_ptr<T> &)
406  {
407  return sizeof(std_cxx11::shared_ptr<T>);
408  }
409 
410 
411 
412  template <typename T>
413  inline
414  std::size_t
415  memory_consumption (const std_cxx11::unique_ptr<T> &)
416  {
417  return sizeof(std_cxx11::unique_ptr<T>);
418  }
419 
420 
421 
422  template <typename T>
423  inline
424  typename std_cxx11::enable_if<!(std_cxx11::is_fundamental<T>::value || std_cxx11::is_pointer<T>::value), std::size_t>::type
425  memory_consumption (const T &t)
426  {
427  return t.memory_consumption();
428  }
429 }
430 
431 DEAL_II_NAMESPACE_CLOSE
432 
433 #endif
std_cxx11::enable_if< std_cxx11::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)