All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
refPtrTracker.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_REF_PTR_TRACKER_H
8#define PXR_BASE_TF_REF_PTR_TRACKER_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/base/tf/api.h"
15#include "pxr/base/tf/hash.h"
16#include "pxr/base/tf/hashmap.h"
19#include <iosfwd>
20#include <mutex>
21#include <vector>
22
23PXR_NAMESPACE_OPEN_SCOPE
24
25class TfRefBase;
26template <class T> class TfRefPtr;
27
80 TfRefPtrTracker(const TfRefPtrTracker&) = delete;
81 TfRefPtrTracker& operator=(const TfRefPtrTracker&) = delete;
82public:
83 enum TraceType { Add, Assign };
84
85 TF_API static TfRefPtrTracker& GetInstance()
86 {
88 }
89
91 TF_API
92 size_t GetStackTraceMaxDepth() const;
93
95 TF_API
97
99 struct Trace {
101 std::vector<uintptr_t> trace;
102
105
107 TraceType type;
108 };
109
111 typedef TfHashMap<const void*, Trace, TfHash> OwnerTraces;
112
116 typedef TfHashMap<const TfRefBase*, size_t, TfHash> WatchedCounts;
117
120 TF_API
122
124 TF_API
126
129 TF_API
130 void ReportAllWatchedCounts(std::ostream& stream) const;
131
133 TF_API
134 void ReportAllTraces(std::ostream& stream) const;
135
137 TF_API
138 void ReportTracesForWatched(std::ostream& stream,
139 const TfRefBase* watched) const;
140
146 static bool WatchNone(const void*)
147 {
148 return false;
149 }
150
153 static bool WatchAll(const void*)
154 {
155 return true;
156 }
157
158private:
161
163 void _Watch(const TfRefBase* obj);
164
166 void _Unwatch(const TfRefBase* obj);
167
170 void _AddTrace(const void* owner, const TfRefBase* obj, TraceType = Add);
171
173 void _RemoveTraces(const void* owner);
174
175private:
176 typedef std::mutex _Mutex;
177 typedef std::lock_guard<std::mutex> _Lock;
178 mutable _Mutex _mutex;
179 size_t _maxDepth;
180 WatchedCounts _watched;
181 OwnerTraces _traces;
182
183 friend class Tf_RefPtrTrackerUtil;
184 friend class TfSingleton<TfRefPtrTracker>;
185};
186
187TF_API_TEMPLATE_CLASS(TfSingleton<TfRefPtrTracker>);
188
189// For internal use only.
190class Tf_RefPtrTrackerUtil {
191public:
193 static void Watch(const TfRefBase* obj)
194 {
195 TfRefPtrTracker::GetInstance()._Watch(obj);
196 }
197
199 static void Unwatch(const TfRefBase* obj)
200 {
201 TfRefPtrTracker::GetInstance()._Unwatch(obj);
202 }
203
206 static void AddTrace(const void* owner, const TfRefBase* obj,
207 TfRefPtrTracker::TraceType type = TfRefPtrTracker::Add)
208 {
209 TfRefPtrTracker::GetInstance()._AddTrace(owner, obj, type);
210 }
211
213 static void RemoveTraces(const void* owner)
214 {
215 TfRefPtrTracker::GetInstance()._RemoveTraces(owner);
216 }
217};
218
219#define TF_DECLARE_REFPTR_TRACK(T) \
220 inline void \
221 Tf_RefPtrTracker_FirstRef(const void*, const TfRefBase *obj, T*); \
222 inline void \
223 Tf_RefPtrTracker_LastRef(const void*, const TfRefBase *obj, T*); \
224 inline void \
225 Tf_RefPtrTracker_New(const void* owner, const TfRefBase *obj, T*); \
226 inline void \
227 Tf_RefPtrTracker_Delete(const void* owner, const TfRefBase *obj, T*); \
228 inline void \
229 Tf_RefPtrTracker_Assign(const void* owner, \
230 const TfRefBase *obj, const TfRefBase *oldObj, T*);
231
232#define TF_DEFINE_REFPTR_TRACK(T, COND) \
233 inline void \
234 Tf_RefPtrTracker_FirstRef(const void*, const TfRefBase *obj, T*) { \
235 if (obj && COND(obj)) Tf_RefPtrTrackerUtil::Watch(obj); \
236 } \
237 inline void \
238 Tf_RefPtrTracker_LastRef(const void*, const TfRefBase *obj, T*) { \
239 Tf_RefPtrTrackerUtil::Unwatch(obj); \
240 } \
241 inline void \
242 Tf_RefPtrTracker_New(const void* owner, const TfRefBase *obj, T*) { \
243 Tf_RefPtrTrackerUtil::AddTrace(owner, obj); \
244 } \
245 inline void \
246 Tf_RefPtrTracker_Delete(const void* owner, const TfRefBase *obj, T*) { \
247 Tf_RefPtrTrackerUtil::RemoveTraces(owner); \
248 } \
249 inline void \
250 Tf_RefPtrTracker_Assign(const void* owner, const TfRefBase *obj, \
251 const TfRefBase *oldObj, T*) { \
252 if (oldObj != obj) { \
253 Tf_RefPtrTrackerUtil::AddTrace( \
254 owner, obj, TfRefPtrTracker::Assign); \
255 } \
256 }
257
258PXR_NAMESPACE_CLOSE_SCOPE
259
260#endif
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:56
Reference-counted smart pointer utility class.
Definition: refPtr.h:590
Provides tracking of TfRefPtr objects to particular objects.
Definition: refPtrTracker.h:79
const TfRefBase * obj
The object being pointed to.
TF_API OwnerTraces GetAllTraces() const
Returns traces for all owners. Returns a copy for thread safety.
TF_API void ReportAllWatchedCounts(std::ostream &stream) const
Writes all watched objects and the number of owners of each to stream.
TF_API void ReportAllTraces(std::ostream &stream) const
Writes all traces to stream.
TF_API size_t GetStackTraceMaxDepth() const
Returns the maximum stack trace depth.
TfHashMap< const void *, Trace, TfHash > OwnerTraces
Maps a TfRefPtr address to the most recent trace for it.
std::vector< uintptr_t > trace
The stack trace when the TfRefPtr was created or assigned to.
TfHashMap< const TfRefBase *, size_t, TfHash > WatchedCounts
Maps a TfRefBase object pointer to the number of TfRefPtr objects using it.
TF_API WatchedCounts GetWatchedCounts() const
Returns the watched objects and the number of owners of each.
TF_API void ReportTracesForWatched(std::ostream &stream, const TfRefBase *watched) const
Writes traces for all owners of watched.
TraceType type
Whether the TfRefPtr was created or assigned to.
TF_API void SetStackTraceMaxDepth(size_t)
Sets the maximum stack trace depth.
static bool WatchAll(const void *)
Handy function to pass as second argument to TF_DEFINE_REFPTR_TRACK.
static bool WatchNone(const void *)
Handy function to pass as second argument to TF_DEFINE_REFPTR_TRACK.
Manage a single instance of an object (see.
Definition: singleton.h:105
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:120
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:124
Manage a single instance of an object.