Reference documentation for deal.II version 9.2.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
memory_consumption.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2020 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 
32 
87 {
93  template <typename T>
94  inline
95  typename std::enable_if<std::is_fundamental<T>::value, std::size_t>::type
96  memory_consumption(const T &t);
97 
104  template <typename T>
107  std::size_t>::type
108  memory_consumption(const T &t);
109 
116  inline std::size_t
117  memory_consumption(const char *string);
118 
123  template <typename T>
124  inline std::size_t
125  memory_consumption(const std::complex<T> &);
126 
131  template <typename T, std::size_t width>
132  inline std::size_t
134 
139  inline std::size_t
140  memory_consumption(const std::string &s);
141 
169  template <typename T>
170  inline std::size_t
171  memory_consumption(const std::vector<T> &v);
172 
193  template <typename T, std::size_t N>
194  inline std::size_t
195  memory_consumption(const std::array<T, N> &v);
196 
205  template <typename T, int N>
206  inline std::size_t
207  memory_consumption(const T (&v)[N]);
208 
216  inline std::size_t
217  memory_consumption(const std::vector<bool> &v);
218 
223  template <typename A, typename B>
224  inline std::size_t
225  memory_consumption(const std::pair<A, B> &p);
226 
236  template <typename T>
237  inline std::size_t
238  memory_consumption(const T *const);
239 
245  template <typename T>
246  inline std::size_t
247  memory_consumption(const std::shared_ptr<T> &);
248 
254  template <typename T>
255  inline std::size_t
256  memory_consumption(const std::unique_ptr<T> &);
257 } // namespace MemoryConsumption
258 
259 
260 
261 // now comes the implementation of these functions
262 
263 namespace MemoryConsumption
264 {
265  template <typename T>
266  inline
267  typename std::enable_if<std::is_fundamental<T>::value, std::size_t>::type
269  {
270  return sizeof(T);
271  }
272 
273 
274 
275  inline std::size_t
276  memory_consumption(const char *string)
277  {
278  if (string == nullptr)
279  {
280  return 0;
281  }
282  else
283  {
284  return sizeof(char) * (strlen(string) /*Remember the NUL*/ + 1);
285  }
286  }
287 
288 
289 
290  template <typename T>
291  inline std::size_t
292  memory_consumption(const std::complex<T> &)
293  {
294  return sizeof(std::complex<T>);
295  }
296 
297 
298 
299  template <typename T, std::size_t width>
300  inline std::size_t
302  {
303  return sizeof(VectorizedArray<T, width>);
304  }
305 
306 
307 
308  inline std::size_t
309  memory_consumption(const std::string &s)
310  {
311  return sizeof(s) + s.length();
312  }
313 
314 
315 
316  template <typename T>
317  std::size_t
318  memory_consumption(const std::vector<T> &v)
319  {
320  // shortcut for types that do not allocate memory themselves
322  {
323  return v.capacity() * sizeof(T) + sizeof(v);
324  }
325  else
326  {
327  std::size_t mem = sizeof(std::vector<T>);
328  for (unsigned int i = 0; i < v.size(); ++i)
329  {
330  mem += memory_consumption(v[i]);
331  }
332  mem += (v.capacity() - v.size()) * sizeof(T);
333  return mem;
334  }
335  }
336 
337 
338 
339  template <typename T, std::size_t N>
340  std::size_t
341  memory_consumption(const std::array<T, N> &v)
342  {
343  // shortcut for types that do not allocate memory themselves
345  {
346  return sizeof(v);
347  }
348  else
349  {
350  std::size_t mem = 0;
351  for (std::size_t i = 0; i != N; ++i)
352  mem += memory_consumption(v[i]);
353  return mem;
354  }
355  }
356 
357 
358 
359  template <typename T, int N>
360  std::size_t
361  memory_consumption(const T (&v)[N])
362  {
363  std::size_t mem = 0;
364  for (unsigned int i = 0; i < N; ++i)
365  mem += memory_consumption(v[i]);
366  return mem;
367  }
368 
369 
370 
371  inline std::size_t
372  memory_consumption(const std::vector<bool> &v)
373  {
374  return v.capacity() / 8 + sizeof(v);
375  }
376 
377 
378 
379  template <typename A, typename B>
380  inline std::size_t
381  memory_consumption(const std::pair<A, B> &p)
382  {
383  return (memory_consumption(p.first) + memory_consumption(p.second));
384  }
385 
386 
387 
388  template <typename T>
389  inline std::size_t
390  memory_consumption(const T *const)
391  {
392  return sizeof(T *);
393  }
394 
395 
396 
397  template <typename T>
398  inline std::size_t
399  memory_consumption(const std::shared_ptr<T> &)
400  {
401  return sizeof(std::shared_ptr<T>);
402  }
403 
404 
405 
406  template <typename T>
407  inline std::size_t
408  memory_consumption(const std::unique_ptr<T> &)
409  {
410  return sizeof(std::unique_ptr<T>);
411  }
412 
413 
414 
415  template <typename T>
418  std::size_t>::type
420  {
421  return t.memory_consumption();
422  }
423 } // namespace MemoryConsumption
424 
426 
427 #endif
static const char T
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
static const char N
static const bool value
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)