MeshLib C++ Docs
Loading...
Searching...
No Matches
MRHeapBytes.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include "MRMacros.h"
5#include <vector>
6#include <memory>
7
8namespace MR
9{
10
13
15template<typename T>
16[[nodiscard]] inline size_t heapBytes( const std::vector<T> & vec )
17{
18 constexpr bool hasHeapBytes = requires( const T& t ) { t.heapBytes(); };
19 if constexpr ( hasHeapBytes )
20 {
21 size_t res = vec.capacity() * sizeof( T );
22 for ( const T & t : vec )
23 res += t.heapBytes();
24 return res;
25 }
26 else
27 {
28 return vec.capacity() * sizeof( T );
29 }
30}
31
32template<typename T, typename U>
33[[nodiscard]] inline size_t heapBytes( const Vector<T, U>& vec )
34{
35 constexpr bool hasHeapBytes = requires( const T & t ) { t.heapBytes(); };
36 if constexpr ( hasHeapBytes )
37 {
38 size_t res = vec.size() * sizeof( T );
39 for ( const T & t : vec )
40 res += t.heapBytes();
41 return res;
42 }
43 else
44 {
45 return vec.size() * sizeof( T );
46 }
47}
48
50template<typename T>
51[[nodiscard]] inline size_t heapBytes( const std::unique_ptr<T> & ptr )
52{
53 if ( !ptr )
54 return 0;
55 return sizeof( T ) + ptr->heapBytes();
56}
57
59template<typename T>
60[[nodiscard]] inline size_t heapBytes( const std::shared_ptr<T> & ptr )
61{
62 if ( !ptr )
63 return 0;
64 return sizeof( T ) + ptr->heapBytes();
65}
66
69template<typename T> MR_REQUIRES_IF_SUPPORTED( std::is_function_v<T> )
70[[nodiscard]] inline size_t heapBytes( const std::function<T> & )
71{
72 return 0;
73}
74
76template<typename ...Ts>
77[[nodiscard]] inline size_t heapBytes( const phmap::flat_hash_map<Ts...>& hashMap )
78{
79 // from parallel_hashmap/phmap.h:
80 // The control state and slot array are stored contiguously in a shared heap
81 // allocation. The layout of this allocation is: `capacity()` control bytes,
82 // one sentinel control byte, `Group::kWidth - 1` cloned control bytes,
83 // <possible padding>, `capacity()` slots
84 const auto cap = hashMap.capacity();
85 constexpr size_t kWidth = 16; // the usage of phmap::priv::Group::kWidth here will require inclusion of phmap.h
86 return cap + kWidth + cap * sizeof( typename phmap::flat_hash_map<Ts...>::slot_type );
87}
88
90
91} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:31
std::vector<T>-like container that requires specific indexing type,
Definition MRMesh/MRVector.h:19
std::size_t size() const
Definition MRMesh/MRVector.h:51
size_t heapBytes(const BitSet &bs)
returns the amount of memory given BitSet occupies on heap
Definition MRMesh/MRBitSet.h:226
Definition MRCameraOrientationPlugin.h:8