deal.II version GIT relicensing-2167-g9622207b8f 2024-11-21 12:40: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\}}\)
Loading...
Searching...
No Matches
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
21#include <array>
22#include <complex>
23#include <cstddef>
24#include <cstring>
25#include <memory>
26#include <optional>
27#include <string>
28#include <type_traits>
29#include <vector>
30
32
85{
91 template <typename T>
92 inline std::enable_if_t<std::is_fundamental_v<T>, std::size_t>
93 memory_consumption(const T &t);
94
101 template <typename T>
102 inline std::enable_if_t<!(std::is_fundamental_v<T> || std::is_pointer_v<T>),
103 std::size_t>
104 memory_consumption(const T &t);
105
112 inline std::size_t
113 memory_consumption(const char *string);
114
119 template <typename T>
120 inline std::size_t
121 memory_consumption(const std::complex<T> &);
122
127 template <typename T, std::size_t width>
128 inline std::size_t
130
135 inline std::size_t
136 memory_consumption(const std::string &s);
137
165 template <typename T>
166 inline std::size_t
167 memory_consumption(const std::vector<T> &v);
168
189 template <typename T, std::size_t N>
190 inline std::size_t
191 memory_consumption(const std::array<T, N> &v);
192
201 template <typename T, int N>
202 inline std::size_t
203 memory_consumption(const T (&v)[N]);
204
212 inline std::size_t
213 memory_consumption(const std::vector<bool> &v);
214
219 template <typename A, typename B>
220 inline std::size_t
221 memory_consumption(const std::pair<A, B> &p);
222
227 template <typename A>
228 inline std::size_t
229 memory_consumption(const std::optional<A> &o);
230
240 template <typename T>
241 inline std::size_t
242 memory_consumption(const T *const);
243
249 template <typename T>
250 inline std::size_t
251 memory_consumption(const std::shared_ptr<T> &);
252
258 template <typename T>
259 inline std::size_t
260 memory_consumption(const std::unique_ptr<T> &);
261} // namespace MemoryConsumption
262
263
264
265// now comes the implementation of these functions
266
267namespace MemoryConsumption
268{
269 template <typename T>
270 inline std::enable_if_t<std::is_fundamental_v<T>, std::size_t>
272 {
273 return sizeof(T);
274 }
275
276
277
278 inline std::size_t
279 memory_consumption(const char *string)
280 {
281 if (string == nullptr)
282 {
283 return 0;
284 }
285 else
286 {
287 return sizeof(char) * (strlen(string) /*Remember the NUL*/ + 1);
288 }
289 }
290
291
292
293 template <typename T>
294 inline std::size_t
295 memory_consumption(const std::complex<T> &)
296 {
297 return sizeof(std::complex<T>);
298 }
299
300
301
302 template <typename T, std::size_t width>
303 inline std::size_t
308
309
310
311 inline std::size_t
312 memory_consumption(const std::string &s)
313 {
314 return sizeof(s) + s.size();
315 }
316
317
318
319 template <typename T>
320 std::size_t
321 memory_consumption(const std::vector<T> &v)
322 {
323 // shortcut for types that do not allocate memory themselves
324 if (std::is_fundamental_v<T> || std::is_pointer_v<T>)
325 {
326 return v.capacity() * sizeof(T) + sizeof(v);
327 }
328 else
329 {
330 std::size_t mem = sizeof(std::vector<T>);
331 for (unsigned int i = 0; i < v.size(); ++i)
332 {
333 mem += memory_consumption(v[i]);
334 }
335 mem += (v.capacity() - v.size()) * sizeof(T);
336 return mem;
337 }
338 }
339
340
341
342 template <typename T, std::size_t N>
343 std::size_t
344 memory_consumption(const std::array<T, N> &v)
345 {
346 // shortcut for types that do not allocate memory themselves
347 if (std::is_fundamental_v<T> || std::is_pointer_v<T>)
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
364 memory_consumption(const T (&v)[N])
365 {
366 std::size_t mem = 0;
367 for (unsigned int i = 0; i < N; ++i)
368 mem += memory_consumption(v[i]);
369 return mem;
370 }
371
372
373
374 inline std::size_t
375 memory_consumption(const std::vector<bool> &v)
376 {
377 return v.capacity() / 8 + sizeof(v);
378 }
379
380
381
382 template <typename A, typename B>
383 inline std::size_t
384 memory_consumption(const std::pair<A, B> &p)
385 {
386 return (memory_consumption(p.first) + memory_consumption(p.second));
387 }
388
389
390
391 template <typename A>
392 inline std::size_t
393 memory_consumption(const std::optional<A> &o)
394 {
395 if (o.has_value())
396 {
397 //
398 // If the optional carries a value then we query the contained
399 // value for memory consumption and estimate the size of the
400 // control overhead.
401 //
402 return memory_consumption(o.value()) + sizeof(o) - sizeof(A);
403 }
404 else
405 {
406 //
407 // The optional contains no value, so simply return its plain size
408 // (consisting of space for the value and control overhead):
409 //
410 return sizeof(o);
411 }
412 }
413
414
415
416 template <typename T>
417 inline std::size_t
418 memory_consumption(const T *const)
419 {
420 return sizeof(T *);
421 }
422
423
424
425 template <typename T>
426 inline std::size_t
427 memory_consumption(const std::shared_ptr<T> &)
428 {
429 return sizeof(std::shared_ptr<T>);
430 }
431
432
433
434 template <typename T>
435 inline std::size_t
436 memory_consumption(const std::unique_ptr<T> &)
437 {
438 return sizeof(std::unique_ptr<T>);
439 }
440
441
442
443 template <typename T>
444 inline std::enable_if_t<!(std::is_fundamental_v<T> || std::is_pointer_v<T>),
445 std::size_t>
447 {
448 return t.memory_consumption();
449 }
450} // namespace MemoryConsumption
451
453
454#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)