Loading [MathJax]/extensions/TeX/newcommand.js
 deal.II version GIT relicensing-2684-gc61376a70f 2025-02-22 15:30:00+00:00
\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\}}
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
memory_consumption.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2000 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_memory_consumption_h
16#define dealii_memory_consumption_h
17
18
19#include <deal.II/base/config.h>
20
22
23#include <array>
24#include <complex>
25#include <cstddef>
26#include <cstring>
27#include <memory>
28#include <optional>
29#include <string>
30#include <type_traits>
31#include <vector>
32
34
87{
93 template <typename T>
94 inline std::enable_if_t<std::is_fundamental_v<T>, std::size_t>
95 memory_consumption(const T &t);
96
103 template <typename T>
104 inline std::enable_if_t<!(std::is_fundamental_v<T> || std::is_pointer_v<T>),
105 std::size_t>
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
229 template <typename A>
230 inline std::size_t
231 memory_consumption(const std::optional<A> &o);
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
269namespace MemoryConsumption
270{
271 template <typename T>
272 inline std::enable_if_t<std::is_fundamental_v<T>, std::size_t>
274 {
275 return sizeof(T);
276 }
277
278
279
280 inline std::size_t
281 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 std::size_t
297 memory_consumption(const std::complex<T> &)
298 {
299 return sizeof(std::complex<T>);
300 }
301
302
303
304 template <typename T, std::size_t width>
305 inline std::size_t
310
311
312
313 inline std::size_t
314 memory_consumption(const std::string &s)
315 {
316 return sizeof(s) + s.size();
317 }
318
319
320
321 template <typename T>
322 std::size_t
323 memory_consumption(const std::vector<T> &v)
324 {
325 // shortcut for types that do not allocate memory themselves
326 if (std::is_fundamental_v<T> || std::is_pointer_v<T>)
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
346 memory_consumption(const std::array<T, N> &v)
347 {
348 // shortcut for types that do not allocate memory themselves
349 if (std::is_fundamental_v<T> || std::is_pointer_v<T>)
350 {
351 return sizeof(v);
352 }
353 else
354 {
355 std::size_t mem = 0;
356 for (std::size_t i = 0; i != N; ++i)
357 mem += memory_consumption(v[i]);
358 return mem;
359 }
360 }
361
362
363
364 template <typename T, int N>
365 std::size_t
366 memory_consumption(const T (&v)[N])
367 {
368 std::size_t mem = 0;
369 for (unsigned int i = 0; i < N; ++i)
370 mem += memory_consumption(v[i]);
371 return mem;
372 }
373
374
375
376 inline std::size_t
377 memory_consumption(const std::vector<bool> &v)
378 {
379 return v.capacity() / 8 + sizeof(v);
380 }
381
382
383
384 template <typename A, typename B>
385 inline std::size_t
386 memory_consumption(const std::pair<A, B> &p)
387 {
388 return (memory_consumption(p.first) + memory_consumption(p.second));
389 }
390
391
392
393 template <typename A>
394 inline std::size_t
395 memory_consumption(const std::optional<A> &o)
396 {
397 if (o.has_value())
398 {
399 //
400 // If the optional carries a value then we query the contained
401 // value for memory consumption and estimate the size of the
402 // control overhead.
403 //
404 return memory_consumption(o.value()) + sizeof(o) - sizeof(A);
405 }
406 else
407 {
408 //
409 // The optional contains no value, so simply return its plain size
410 // (consisting of space for the value and control overhead):
411 //
412 return sizeof(o);
413 }
414 }
415
416
417
418 template <typename T>
419 inline std::size_t
420 memory_consumption(const T *const)
421 {
422 return sizeof(T *);
423 }
424
425
426
427 template <typename T>
428 inline std::size_t
429 memory_consumption(const std::shared_ptr<T> &)
430 {
431 return sizeof(std::shared_ptr<T>);
432 }
433
434
435
436 template <typename T>
437 inline std::size_t
438 memory_consumption(const std::unique_ptr<T> &)
439 {
440 return sizeof(std::unique_ptr<T>);
441 }
442
443
444
445 template <typename T>
446 inline std::enable_if_t<!(std::is_fundamental_v<T> || std::is_pointer_v<T>),
447 std::size_t>
449 {
450 return t.memory_consumption();
451 }
452} // namespace MemoryConsumption
453
455
456#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:522
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:523
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)