1+ // Copyright Contributors to the OpenImageIO project.
2+ // SPDX-License-Identifier: Apache-2.0
3+ // https://github.com/AcademySoftwareFoundation/OpenImageIO
4+
5+
6+ // ///////////////////////////////////////////////////////////////////////
7+ // / @file memory.h
8+ // /
9+ // / @brief Utilities for memory tracking.
10+ // ///////////////////////////////////////////////////////////////////////
11+
12+
13+ #pragma once
14+
15+ #define OPENIMAGEIO_MEMORY_H
16+
17+ #include < cstring>
18+ #include < memory>
19+ #include < vector>
20+
21+ OIIO_NAMESPACE_BEGIN
22+
23+ namespace pvt {
24+
25+ // / Return the total heap memory allocated by `object`.
26+ // / The template specialization can be used to give improved results for non trivial types
27+ // / that perform heap allocation, and to include members allocations recursively.
28+ template <typename T>
29+ inline size_t
30+ heapsize (const T& t)
31+ {
32+ return 0 ;
33+ }
34+
35+ // / Return the total memory footprint of `object`. If possible, including any heap
36+ // / allocations done by any constituent parts. The default implementation just reduces
37+ // / to sizeof(object), given that heapsize(object) would return 0.
38+ // / The template specialization can be used to give improved results for non trivial types
39+ // / that perform heap allocation.
40+ template <typename T>
41+ inline size_t
42+ footprint (const T& t)
43+ {
44+ return sizeof (T) + heapsize (t);
45+ }
46+
47+ template <typename T>
48+ inline size_t
49+ footprint (const T* t)
50+ {
51+ return sizeof (T) + (t ? footprint (*t) : 0 );
52+ }
53+
54+ // / Specializations for common STL types
55+
56+
57+ // heapsize specialization for std::string
58+ template <>
59+ inline size_t
60+ heapsize<std::string>(const std::string& s)
61+ {
62+ // accounts for small string optimization that does not
63+ // use any heap allocations
64+ const char * const sbegin = (const char *)&s;
65+ const char * const send = sbegin + sizeof (std::string);
66+ const char * const sdata = s.data ();
67+ const bool is_small = sdata >= sbegin && sdata < send;
68+ return is_small ? 0 : s.capacity ();
69+ }
70+
71+ // heapsize specialization for std::vector
72+ template <typename T>
73+ inline size_t
74+ heapsize (const std::vector<T>& vec)
75+ {
76+ size_t size = 0 ;
77+ for (const T& elem : vec)
78+ size += footprint (elem);
79+ return size;
80+ }
81+
82+ // heapsize specialization for std::shared_ptr
83+ template <typename T>
84+ inline size_t
85+ heapsize (const std::shared_ptr<T>& ref)
86+ {
87+ return ref ? footprint (*ref.get ()) : 0 ;
88+ }
89+
90+ // footprint specialization for std::shared_ptr
91+ template <typename T>
92+ inline size_t
93+ footprint (const std::shared_ptr<T>& ref)
94+ {
95+ return sizeof (std::shared_ptr<T>) + heapsize (ref);
96+ }
97+
98+ // heapsize specialization for std::unique_ptr
99+ template <typename T>
100+ inline size_t
101+ heapsize (const std::unique_ptr<T>& ref)
102+ {
103+ return ref ? footprint (*ref.get ()) : 0 ;
104+ }
105+
106+ // footprint specialization for std::unique_ptr
107+ template <typename T>
108+ inline size_t
109+ footprint (const std::unique_ptr<T>& ref)
110+ {
111+ return sizeof (std::unique_ptr<T>) + heapsize (ref);
112+ }
113+
114+ } // namespace pvt
115+
116+
117+ OIIO_NAMESPACE_END
0 commit comments