Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(833)

Side by Side Diff: Source/wtf/MemoryInstrumentation.h

Issue 13973026: remove memoryinstrumentation Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: remove the rest part of MemoryInstrumentation Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/wtf/ListHashSet.h ('k') | Source/wtf/MemoryInstrumentation.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef MemoryInstrumentation_h
32 #define MemoryInstrumentation_h
33
34 #include <wtf/OwnPtr.h>
35 #include <wtf/PassOwnPtr.h>
36 #include <wtf/RefPtr.h>
37
38 #define DEBUG_POINTER_INSTRUMENTATION 0
39
40 namespace WTF {
41
42 class MemoryClassInfo;
43 class MemoryObjectInfo;
44 class MemoryInstrumentation;
45
46 typedef const char* MemoryObjectType;
47
48 enum MemberType {
49 PointerMember,
50 ReferenceMember,
51 RetainingPointer,
52 LastMemberTypeEntry
53 };
54
55 template<typename T> void reportMemoryUsage(const T*, MemoryObjectInfo*);
56
57 class MemoryInstrumentationClient {
58 public:
59 virtual ~MemoryInstrumentationClient() { }
60 virtual void countObjectSize(const void*, MemoryObjectType, size_t) = 0;
61 virtual bool visited(const void*) = 0;
62 virtual bool checkCountedObject(const void*) = 0;
63
64 virtual void reportNode(const MemoryObjectInfo&) = 0;
65 virtual void reportEdge(const void* target, const char* edgeName, MemberType ) = 0;
66 virtual void reportLeaf(const MemoryObjectInfo&, const char* edgeName) = 0;
67 virtual void reportBaseAddress(const void* base, const void* real) = 0;
68 virtual int registerString(const char*) = 0;
69 };
70
71 class MemoryInstrumentation {
72 public:
73 WTF_EXPORT_PRIVATE explicit MemoryInstrumentation(MemoryInstrumentationClien t*);
74 WTF_EXPORT_PRIVATE virtual ~MemoryInstrumentation();
75
76 template <typename T> void addRootObject(const T& t, MemoryObjectType object Type = 0)
77 {
78 MemberTypeTraits<T>::addRootObject(this, t, objectType);
79 processDeferredObjects();
80 }
81
82 template <typename T> void addRootObject(const OwnPtr<T>&, MemoryObjectType = 0); // Link time guard.
83 template <typename T> void addRootObject(const RefPtr<T>&, MemoryObjectType = 0); // Link time guard.
84
85 protected:
86 class WrapperBase {
87 public:
88 WTF_EXPORT_PRIVATE WrapperBase(MemoryObjectType, const void* pointer);
89 virtual ~WrapperBase() { }
90 WTF_EXPORT_PRIVATE void process(MemoryInstrumentation*);
91 WTF_EXPORT_PRIVATE void processPointer(MemoryInstrumentation*, bool isRo ot);
92 WTF_EXPORT_PRIVATE void processRootObjectRef(MemoryInstrumentation*);
93
94 protected:
95 virtual void callReportMemoryUsage(MemoryObjectInfo*) = 0;
96 const void* m_pointer;
97 const MemoryObjectType m_ownerObjectType;
98
99 private:
100 #if DEBUG_POINTER_INSTRUMENTATION
101 static const int s_maxCallStackSize = 32;
102 void* m_callStack[s_maxCallStackSize];
103 int m_callStackSize;
104 #endif
105 };
106
107 private:
108 void countObjectSize(const void* object, MemoryObjectType objectType, size_t size) { m_client->countObjectSize(object, objectType, size); }
109 bool visited(const void* pointer) { return m_client->visited(pointer); }
110 bool checkCountedObject(const void* pointer) { return m_client->checkCounted Object(pointer); }
111
112 WTF_EXPORT_PRIVATE void reportEdge(const void* target, const char* edgeName, MemberType);
113
114 virtual void deferObject(PassOwnPtr<WrapperBase>) = 0;
115 virtual void processDeferredObjects() = 0;
116
117 WTF_EXPORT_PRIVATE static MemoryObjectType getObjectType(MemoryObjectInfo*);
118
119 friend class MemoryObjectInfo;
120 friend class MemoryClassInfo;
121 template<typename T> friend void reportMemoryUsage(const T*, MemoryObjectInf o*);
122
123 template <typename Type>
124 class IsInstrumented {
125 class yes {
126 char m;
127 };
128
129 class no {
130 yes m[2];
131 };
132
133 struct BaseMixin {
134 void reportMemoryUsage(MemoryObjectInfo*) const { }
135 };
136
137 struct Base : public Type, public BaseMixin {
138 // Provide expicit destructor without definition to avoid MSVC warni ng "destructor could
139 // not be generated because a base class destructor is inaccessible. "
140 ~Base();
141 };
142
143 template <typename T, T t> class Helper { };
144
145 template <typename U> static no deduce(U*, Helper<void (BaseMixin::*)(Me moryObjectInfo*) const, &U::reportMemoryUsage>* = 0);
146 static yes deduce(...);
147
148 public:
149 static const bool result = sizeof(yes) == sizeof(deduce((Base*)(0)));
150
151 };
152
153 template <int>
154 struct InstrumentationSelector {
155 template <typename T> static void reportObjectMemoryUsage(const T*, Memo ryObjectInfo*);
156 };
157
158 template<typename T> class Wrapper : public WrapperBase {
159 public:
160 Wrapper(const T* pointer, MemoryObjectType);
161
162 protected:
163 virtual void callReportMemoryUsage(MemoryObjectInfo*) OVERRIDE;
164 };
165
166 template<typename T> void addObject(const T& t, MemoryObjectInfo* ownerObjec tInfo, const char* edgeName, MemberType memberType)
167 {
168 MemberTypeTraits<T>::addObject(this, t, ownerObjectInfo, edgeName, membe rType);
169 }
170 void addRawBuffer(const void* buffer, MemoryObjectType ownerObjectType, size _t size, const char* className = 0, const char* edgeName = 0)
171 {
172 if (!buffer || visited(buffer))
173 return;
174 countObjectSize(buffer, ownerObjectType, size);
175 reportLinkToBuffer(buffer, ownerObjectType, size, className, edgeName);
176 }
177 WTF_EXPORT_PRIVATE void reportLinkToBuffer(const void* buffer, MemoryObjectT ype ownerObjectType, size_t, const char* nodeName, const char* edgeName);
178
179 template<typename T>
180 struct MemberTypeTraits { // Default ReferenceMember implementation.
181 static void addObject(MemoryInstrumentation* instrumentation, const T& t , MemoryObjectInfo* ownerObjectInfo, const char* edgeName, MemberType)
182 {
183 instrumentation->addObjectImpl(&t, ownerObjectInfo, ReferenceMember, edgeName);
184 }
185
186 static void addRootObject(MemoryInstrumentation* instrumentation, const T& t, MemoryObjectType objectType)
187 {
188 Wrapper<T>(&t, objectType).processRootObjectRef(instrumentation);
189 }
190 };
191
192 template<typename T>
193 struct MemberTypeTraits<T*> { // Custom PointerMember implementation.
194 static void addObject(MemoryInstrumentation* instrumentation, const T* c onst& t, MemoryObjectInfo* ownerObjectInfo, const char* edgeName, MemberType mem berType)
195 {
196 instrumentation->addObjectImpl(t, ownerObjectInfo, memberType != Las tMemberTypeEntry ? memberType : PointerMember, edgeName);
197 }
198
199 static void addRootObject(MemoryInstrumentation* instrumentation, const T* const& t, MemoryObjectType objectType)
200 {
201 if (t && !instrumentation->visited(t))
202 Wrapper<T>(t, objectType).processPointer(instrumentation, true);
203 }
204 };
205
206 template<typename T> void addObjectImpl(const T*, MemoryObjectInfo*, MemberT ype, const char* edgeName);
207 template<typename T> void addObjectImpl(const OwnPtr<T>*, MemoryObjectInfo*, MemberType, const char* edgeName);
208 template<typename T> void addObjectImpl(const RefPtr<T>*, MemoryObjectInfo*, MemberType, const char* edgeName);
209
210 MemoryInstrumentationClient* m_client;
211 };
212
213 // We are trying to keep the signature of the function as small as possible
214 // because it significantly affects the binary size.
215 // We caluclates class name for 624 classes at the moment.
216 // So one extra byte of the function signature increases the binary size to 624 extra bytes.
217 #if COMPILER(MSVC)
218 template <typename T> struct FN {
219 static char* fn() { return const_cast<char*>(__FUNCTION__); }
220 };
221
222 template <typename T> char* fn() { return FN<T>::fn(); }
223 #else
224 template <typename T> char* fn() { return const_cast<char*>(__PRETTY_FUNCTION__) ; }
225 #endif
226
227 class MemoryClassInfo {
228 public:
229 template<typename T>
230 MemoryClassInfo(MemoryObjectInfo* memoryObjectInfo, const T* pointer, Memory ObjectType objectType = 0, size_t actualSize = sizeof(T))
231 : m_memoryObjectInfo(memoryObjectInfo)
232 , m_memoryInstrumentation(0)
233 , m_objectType(0)
234 , m_skipMembers(false)
235 {
236 init(pointer, fn<T>(), objectType, actualSize);
237 }
238
239 template<typename M> void addMember(const M& member, const char* edgeName = 0, MemberType memberType = LastMemberTypeEntry)
240 {
241 if (!m_skipMembers)
242 m_memoryInstrumentation->addObject(member, m_memoryObjectInfo, edgeN ame, memberType);
243 }
244
245 WTF_EXPORT_PRIVATE void addRawBuffer(const void* buffer, size_t, const char* className = 0, const char* edgeName = 0);
246 WTF_EXPORT_PRIVATE void addPrivateBuffer(size_t, MemoryObjectType ownerObjec tType = 0, const char* className = 0, const char* edgeName = 0);
247 WTF_EXPORT_PRIVATE void setCustomAllocation(bool);
248
249 void addWeakPointer(void*) { }
250 template<typename M> void ignoreMember(const M&) { }
251
252 WTF_EXPORT_PRIVATE static void callReportObjectInfo(MemoryObjectInfo*, const void* pointer, const char* stringWithClassName, MemoryObjectType, size_t actual Size);
253
254 private:
255 WTF_EXPORT_PRIVATE void init(const void* pointer, const char* stringWithClas sName, MemoryObjectType, size_t actualSize);
256
257 MemoryObjectInfo* m_memoryObjectInfo;
258 MemoryInstrumentation* m_memoryInstrumentation;
259 MemoryObjectType m_objectType;
260 bool m_skipMembers;
261 };
262
263 template <>
264 template <typename T>
265 void MemoryInstrumentation::InstrumentationSelector<true>::reportObjectMemoryUsa ge(const T* object, MemoryObjectInfo* memoryObjectInfo)
266 {
267 object->reportMemoryUsage(memoryObjectInfo);
268 }
269
270 template <>
271 template <typename T>
272 void MemoryInstrumentation::InstrumentationSelector<false>::reportObjectMemoryUs age(const T* object, MemoryObjectInfo* memoryObjectInfo)
273 {
274 MemoryClassInfo::callReportObjectInfo(memoryObjectInfo, object, fn<T>(), 0, sizeof(T));
275 }
276
277 template<typename T>
278 void reportMemoryUsage(const T* object, MemoryObjectInfo* memoryObjectInfo)
279 {
280 MemoryInstrumentation::InstrumentationSelector<MemoryInstrumentation::IsInst rumented<T>::result>::reportObjectMemoryUsage(object, memoryObjectInfo);
281 }
282
283 template<typename T>
284 void MemoryInstrumentation::addObjectImpl(const T* object, MemoryObjectInfo* own erObjectInfo, MemberType memberType, const char* edgeName)
285 {
286 if (memberType == PointerMember)
287 return;
288 if (memberType == ReferenceMember)
289 reportMemoryUsage(object, ownerObjectInfo);
290 else {
291 if (!object)
292 return;
293 reportEdge(object, edgeName, memberType);
294 if (visited(object))
295 return;
296 deferObject(adoptPtr(new Wrapper<T>(object, getObjectType(ownerObjectInf o))));
297 }
298 }
299
300 template<typename T>
301 void MemoryInstrumentation::addObjectImpl(const OwnPtr<T>* object, MemoryObjectI nfo* ownerObjectInfo, MemberType memberType, const char* edgeName)
302 {
303 if (memberType == PointerMember && !visited(object))
304 countObjectSize(object, getObjectType(ownerObjectInfo), sizeof(*object)) ;
305 addObjectImpl(object->get(), ownerObjectInfo, RetainingPointer, edgeName);
306 }
307
308 template<typename T>
309 void MemoryInstrumentation::addObjectImpl(const RefPtr<T>* object, MemoryObjectI nfo* ownerObjectInfo, MemberType memberType, const char* edgeName)
310 {
311 if (memberType == PointerMember && !visited(object))
312 countObjectSize(object, getObjectType(ownerObjectInfo), sizeof(*object)) ;
313 addObjectImpl(object->get(), ownerObjectInfo, RetainingPointer, edgeName);
314 }
315
316 template<typename T>
317 MemoryInstrumentation::Wrapper<T>::Wrapper(const T* pointer, MemoryObjectType ow nerObjectType)
318 : WrapperBase(ownerObjectType, pointer)
319 {
320 }
321
322 template<typename T>
323 void MemoryInstrumentation::Wrapper<T>::callReportMemoryUsage(MemoryObjectInfo* memoryObjectInfo)
324 {
325 reportMemoryUsage(static_cast<const T*>(m_pointer), memoryObjectInfo);
326 }
327
328 // Link time guard for classes with external memory instrumentation.
329 template<typename T, size_t inlineCapacity> class Vector;
330 template<typename T, size_t inlineCapacity> void reportMemoryUsage(const Vector< T, inlineCapacity>*, MemoryObjectInfo*);
331
332 template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTrai tsArg, typename MappedTraitsArg> class HashMap;
333 template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTrai tsArg, typename MappedTraitsArg> void reportMemoryUsage(const HashMap<KeyArg, Ma ppedArg, HashArg, KeyTraitsArg, MappedTraitsArg>*, MemoryObjectInfo*);
334
335 template<typename ValueArg, typename HashArg, typename TraitsArg> class HashCoun tedSet;
336 template<typename ValueArg, typename HashArg, typename TraitsArg> void reportMem oryUsage(const HashCountedSet<ValueArg, HashArg, TraitsArg>*, MemoryObjectInfo*) ;
337
338 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListH ashSet;
339 template<typename ValueArg, size_t inlineCapacity, typename HashArg> void report MemoryUsage(const ListHashSet<ValueArg, inlineCapacity, HashArg>*, MemoryObjectI nfo*);
340
341 class String;
342 void reportMemoryUsage(const String*, MemoryObjectInfo*);
343
344 class StringImpl;
345 void reportMemoryUsage(const StringImpl*, MemoryObjectInfo*);
346
347 class AtomicString;
348 void reportMemoryUsage(const AtomicString*, MemoryObjectInfo*);
349
350 class CString;
351 void reportMemoryUsage(const CString*, MemoryObjectInfo*);
352
353 class CStringBuffer;
354 void reportMemoryUsage(const CStringBuffer*, MemoryObjectInfo*);
355
356 class ParsedURL;
357 void reportMemoryUsage(const ParsedURL*, MemoryObjectInfo*);
358
359 class URLString;
360 void reportMemoryUsage(const URLString*, MemoryObjectInfo*);
361
362 } // namespace WTF
363
364 #endif // !defined(MemoryInstrumentation_h)
OLDNEW
« no previous file with comments | « Source/wtf/ListHashSet.h ('k') | Source/wtf/MemoryInstrumentation.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698