Skip to content

Commit bdd5221

Browse files
sipafurszy
authored andcommitted
Add support for unique_ptr and shared_ptr to memusage
1 parent 017396d commit bdd5221

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/memusage.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ struct stl_tree_node
8080
X x;
8181
};
8282

83+
struct stl_shared_counter
84+
{
85+
/* Various platforms use different sized counters here.
86+
* Conservatively assume that they won't be larger than size_t. */
87+
void* class_type;
88+
size_t use_count;
89+
size_t weak_count;
90+
};
91+
8392
template<typename X>
8493
static inline size_t DynamicUsage(const std::vector<X>& v)
8594
{
@@ -158,6 +167,21 @@ static inline size_t RecursiveDynamicUsage(const std::pair<X, Y>& v)
158167
return RecursiveDynamicUsage(v.first) + RecursiveDynamicUsage(v.second);
159168
}
160169

170+
template<typename X>
171+
static inline size_t DynamicUsage(const std::unique_ptr<X>& p)
172+
{
173+
return p ? MallocUsage(sizeof(X)) : 0;
174+
}
175+
176+
template<typename X>
177+
static inline size_t DynamicUsage(const std::shared_ptr<X>& p)
178+
{
179+
// A shared_ptr can either use a single continuous memory block for both
180+
// the counter and the storage (when using std::make_shared), or separate.
181+
// We can't observe the difference, however, so assume the worst.
182+
return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) : 0;
183+
}
184+
161185
// Boost data structures
162186

163187
template<typename X>

0 commit comments

Comments
 (0)