Reference documentation for deal.II version 9.4.1
\(\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\}}\)
Loading...
Searching...
No Matches
memory_consumption.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (C) 2000 - 2022 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
85{
91 template <typename T>
92 inline
93 typename std::enable_if<std::is_fundamental<T>::value, std::size_t>::type
94 memory_consumption(const T &t);
95
102 template <typename T>
103 inline typename std::enable_if<!(std::is_fundamental<T>::value ||
104 std::is_pointer<T>::value),
105 std::size_t>::type
106 memory_consumption(const T &t);
107
114 inline std::size_t
115 memory_consumption(const char *string);
116
121 template <typename T>
122 inline std::size_t
123 memory_consumption(const std::complex<T> &);
124
129 template <typename T, std::size_t width>
130 inline std::size_t
132
137 inline std::size_t
138 memory_consumption(const std::string &s);
139
167 template <typename T>
168 inline std::size_t
169 memory_consumption(const std::vector<T> &v);
170
191 template <typename T, std::size_t N>
192 inline std::size_t
193 memory_consumption(const std::array<T, N> &v);
194
203 template <typename T, int N>
204 inline std::size_t
205 memory_consumption(const T (&v)[N]);
206
214 inline std::size_t
215 memory_consumption(const std::vector<bool> &v);
216
221 template <typename A, typename B>
222 inline std::size_t
223 memory_consumption(const std::pair<A, B> &p);
224
234 template <typename T>
235 inline std::size_t
236 memory_consumption(const T *const);
237
243 template <typename T>
244 inline std::size_t
245 memory_consumption(const std::shared_ptr<T> &);
246
252 template <typename T>
253 inline std::size_t
254 memory_consumption(const std::unique_ptr<T> &);
255} // namespace MemoryConsumption
256
257
258
259// now comes the implementation of these functions
260
261namespace MemoryConsumption
262{
263 template <typename T>
264 inline
265 typename std::enable_if<std::is_fundamental<T>::value, std::size_t>::type
267 {
268 return sizeof(T);
269 }
270
271
272
273 inline std::size_t
274 memory_consumption(const char *string)
275 {
276 if (string == nullptr)
277 {
278 return 0;
279 }
280 else
281 {
282 return sizeof(char) * (strlen(string) /*Remember the NUL*/ + 1);
283 }
284 }
285
286
287
288 template <typename T>
289 inline std::size_t
290 memory_consumption(const std::complex<T> &)
291 {
292 return sizeof(std::complex<T>);
293 }
294
295
296
297 template <typename T, std::size_t width>
298 inline std::size_t
300 {
301 return sizeof(VectorizedArray<T, width>);
302 }
303
304
305
306 inline std::size_t
307 memory_consumption(const std::string &s)
308 {
309 return sizeof(s) + s.size();
310 }
311
312
313
314 template <typename T>
315 std::size_t
316 memory_consumption(const std::vector<T> &v)
317 {
318 // shortcut for types that do not allocate memory themselves
319 if (std::is_fundamental<T>::value || std::is_pointer<T>::value)
320 {
321 return v.capacity() * sizeof(T) + sizeof(v);
322 }
323 else
324 {
325 std::size_t mem = sizeof(std::vector<T>);
326 for (unsigned int i = 0; i < v.size(); ++i)
327 {
328 mem += memory_consumption(v[i]);
329 }
330 mem += (v.capacity() - v.size()) * sizeof(T);
331 return mem;
332 }
333 }
334
335
336
337 template <typename T, std::size_t N>
338 std::size_t
339 memory_consumption(const std::array<T, N> &v)
340 {
341 // shortcut for types that do not allocate memory themselves
342 if (std::is_fundamental<T>::value || std::is_pointer<T>::value)
343 {
344 return sizeof(v);
345 }
346 else
347 {
348 std::size_t mem = 0;
349 for (std::size_t i = 0; i != N; ++i)
350 mem += memory_consumption(v[i]);
351 return mem;
352 }
353 }
354
355
356
357 template <typename T, int N>
358 std::size_t
359 memory_consumption(const T (&v)[N])
360 {
361 std::size_t mem = 0;
362 for (unsigned int i = 0; i < N; ++i)
363 mem += memory_consumption(v[i]);
364 return mem;
365 }
366
367
368
369 inline std::size_t
370 memory_consumption(const std::vector<bool> &v)
371 {
372 return v.capacity() / 8 + sizeof(v);
373 }
374
375
376
377 template <typename A, typename B>
378 inline std::size_t
379 memory_consumption(const std::pair<A, B> &p)
380 {
381 return (memory_consumption(p.first) + memory_consumption(p.second));
382 }
383
384
385
386 template <typename T>
387 inline std::size_t
388 memory_consumption(const T *const)
389 {
390 return sizeof(T *);
391 }
392
393
394
395 template <typename T>
396 inline std::size_t
397 memory_consumption(const std::shared_ptr<T> &)
398 {
399 return sizeof(std::shared_ptr<T>);
400 }
401
402
403
404 template <typename T>
405 inline std::size_t
406 memory_consumption(const std::unique_ptr<T> &)
407 {
408 return sizeof(std::unique_ptr<T>);
409 }
410
411
412
413 template <typename T>
414 inline typename std::enable_if<!(std::is_fundamental<T>::value ||
415 std::is_pointer<T>::value),
416 std::size_t>::type
418 {
419 return t.memory_consumption();
420 }
421} // namespace MemoryConsumption
422
424
425#endif
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)