Reference documentation for deal.II version 9.1.0-pre
memory_consumption.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 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.md at
12 // the top level directory of deal.II.
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 <array>
23 #include <complex>
24 #include <cstddef>
25 #include <cstring>
26 #include <memory>
27 #include <string>
28 #include <type_traits>
29 #include <vector>
30 
31 DEAL_II_NAMESPACE_OPEN
32 
33 
34 // forward declaration
35 template <typename T>
37 
38 
93 {
99  template <typename T>
100  inline
101  typename std::enable_if<std::is_fundamental<T>::value, std::size_t>::type
102  memory_consumption(const T &t);
103 
110  template <typename T>
111  inline typename std::enable_if<!(std::is_fundamental<T>::value ||
112  std::is_pointer<T>::value),
113  std::size_t>::type
114  memory_consumption(const T &t);
115 
122  inline std::size_t
123  memory_consumption(const char *string);
124 
129  template <typename T>
130  inline std::size_t
131  memory_consumption(const std::complex<T> &);
132 
137  template <typename T>
138  inline std::size_t
140 
145  inline std::size_t
146  memory_consumption(const std::string &s);
147 
175  template <typename T>
176  inline std::size_t
177  memory_consumption(const std::vector<T> &v);
178 
199  template <typename T, std::size_t N>
200  inline std::size_t
201  memory_consumption(const std::array<T, N> &v);
202 
211  template <typename T, int N>
212  inline std::size_t
213  memory_consumption(const T (&v)[N]);
214 
222  inline std::size_t
223  memory_consumption(const std::vector<bool> &v);
224 
229  template <typename A, typename B>
230  inline std::size_t
231  memory_consumption(const std::pair<A, B> &p);
232 
242  template <typename T>
243  inline std::size_t
244  memory_consumption(const T *const);
245 
251  template <typename T>
252  inline std::size_t
253  memory_consumption(const std::shared_ptr<T> &);
254 
260  template <typename T>
261  inline std::size_t
262  memory_consumption(const std::unique_ptr<T> &);
263 } // namespace MemoryConsumption
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::enable_if<std::is_fundamental<T>::value, std::size_t>::type
275  {
276  return sizeof(T);
277  }
278 
279 
280 
281  inline std::size_t
282  memory_consumption(const char *string)
283  {
284  if (string == nullptr)
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 std::size_t
298  memory_consumption(const std::complex<T> &)
299  {
300  return sizeof(std::complex<T>);
301  }
302 
303 
304 
305  template <typename T>
306  inline std::size_t
308  {
309  return sizeof(VectorizedArray<T>);
310  }
311 
312 
313 
314  inline std::size_t
315  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
324  memory_consumption(const std::vector<T> &v)
325  {
326  // shortcut for types that do not allocate memory themselves
327  if (std::is_fundamental<T>::value || std::is_pointer<T>::value)
328  {
329  return v.capacity() * sizeof(T) + sizeof(v);
330  }
331  else
332  {
333  std::size_t mem = sizeof(std::vector<T>);
334  for (unsigned int i = 0; i < v.size(); ++i)
335  {
336  mem += memory_consumption(v[i]);
337  }
338  mem += (v.capacity() - v.size()) * sizeof(T);
339  return mem;
340  }
341  }
342 
343 
344 
345  template <typename T, std::size_t N>
346  std::size_t
347  memory_consumption(const std::array<T, N> &v)
348  {
349  // shortcut for types that do not allocate memory themselves
350  if (std::is_fundamental<T>::value || std::is_pointer<T>::value)
351  {
352  return sizeof(v);
353  }
354  else
355  {
356  std::size_t mem = 0;
357  for (std::size_t i = 0; i != N; ++i)
358  mem += memory_consumption(v[i]);
359  return mem;
360  }
361  }
362 
363 
364 
365  template <typename T, int N>
366  std::size_t
367  memory_consumption(const T (&v)[N])
368  {
369  std::size_t mem = 0;
370  for (unsigned int i = 0; i < N; ++i)
371  mem += memory_consumption(v[i]);
372  return mem;
373  }
374 
375 
376 
377  inline std::size_t
378  memory_consumption(const std::vector<bool> &v)
379  {
380  return v.capacity() / 8 + sizeof(v);
381  }
382 
383 
384 
385  template <typename A, typename B>
386  inline std::size_t
387  memory_consumption(const std::pair<A, B> &p)
388  {
389  return (memory_consumption(p.first) + memory_consumption(p.second));
390  }
391 
392 
393 
394  template <typename T>
395  inline std::size_t
396  memory_consumption(const T *const)
397  {
398  return sizeof(T *);
399  }
400 
401 
402 
403  template <typename T>
404  inline std::size_t
405  memory_consumption(const std::shared_ptr<T> &)
406  {
407  return sizeof(std::shared_ptr<T>);
408  }
409 
410 
411 
412  template <typename T>
413  inline 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 typename std::enable_if<!(std::is_fundamental<T>::value ||
423  std::is_pointer<T>::value),
424  std::size_t>::type
425  memory_consumption(const T &t)
426  {
427  return t.memory_consumption();
428  }
429 } // namespace MemoryConsumption
430 
431 DEAL_II_NAMESPACE_CLOSE
432 
433 #endif
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)