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