Chromium Code Reviews| Index: third_party/WebKit/Source/wtf/LeakAnnotations.h |
| diff --git a/third_party/WebKit/Source/wtf/LeakAnnotations.h b/third_party/WebKit/Source/wtf/LeakAnnotations.h |
| index f330e170ad2ca939361de7de92b6bff8eec52274..90173c2023b3bf94ab5d162f4ac836111e84bc9b 100644 |
| --- a/third_party/WebKit/Source/wtf/LeakAnnotations.h |
| +++ b/third_party/WebKit/Source/wtf/LeakAnnotations.h |
| @@ -32,22 +32,17 @@ |
| #ifndef WTF_LeakAnnotations_h |
| #define WTF_LeakAnnotations_h |
| -// This file defines macros which can be used to annotate intentional memory |
| -// leaks. Support for annotations is implemented in HeapChecker and |
| -// LeakSanitizer. Annotated objects will be treated as a source of live |
| -// pointers, i.e. any heap objects reachable by following pointers from an |
| -// annotated object will not be reported as leaks. |
| +// This file defines macros for working with LeakSanitizer, allowing memory |
| +// and allocations to be registered as exempted from LSan consideration. |
| // |
| -// WTF_ANNOTATE_SCOPED_MEMORY_LEAK: all allocations made in the current scope |
| -// will be annotated as leaks. |
| -// WTF_ANNOTATE_LEAKING_OBJECT_PTR(X): the heap object referenced by pointer X |
| -// will be annotated as a leak. |
| +// LSan exempted memory will be treated as a source of live pointers, |
| +// i.e. heap objects reachable by following pointers from an exempted |
| +// object will not be reported as leaks. |
| // |
| -// Note that HeapChecker will report a fatal error if an object which has been |
| -// annotated with ANNOTATE_LEAKING_OBJECT_PTR is later deleted (but |
| -// LeakSanitizer won't). |
| - |
| #include "wtf/Noncopyable.h" |
| +#if USE(LEAK_SANITIZER) |
| +#include "wtf/TypeTraits.h" |
| +#endif |
| namespace WTF { |
| @@ -72,17 +67,73 @@ public: |
| } |
| }; |
| -#define WTF_ANNOTATE_SCOPED_MEMORY_LEAK \ |
| +// LEAK_MEMORY_LEAK_SCOPE: all allocations made in the current scope |
| +// will be exempted from LSan consideration. |
| +#define WTF_ANNOTATE_LEAK_SCOPE \ |
| WTF::LeakSanitizerDisabler leakSanitizerDisabler; static_cast<void>(0) |
| -#define WTF_ANNOTATE_LEAKING_OBJECT_PTR(X) \ |
| - WTF::__lsan_ignore_object(X) |
| +// WTF_ANNOTATE_LEAK_IGNORE_OBJECT(X): the heap object referenced by pointer X |
| +// will be ignored by LSan. |
| +// |
| +#define WTF_ANNOTATE_LEAK_IGNORE_OBJECT(X) WTF::__lsan_ignore_object(X) |
| + |
| +// If the object pointed to by the static local is on the Oilpan heap, a strong |
| +// Persistent<> is created to keep the pointed-to heap object alive. This makes |
| +// both the Persistent<> and the heap object _reachable_ by LeakSanitizer's leak |
| +// detection pass. We do not want these intentional leaks to be reported by LSan, |
| +// hence the static local is registered with Oilpan |
| +// (see RegisterStaticLocalReference<> below.) |
| +// |
| +// Upon Blink shutdown, all the registered statics are released and a final round |
| +// of GCs are performed to sweep out their now-unreachable object graphs. The end |
| +// result being a tidied heap that the LeakSanitizer can then scan to report real leaks. |
| +// |
| +// The CanRegisterStaticLocalReference<> and RegisterStaticLocalReference<> templates |
| +// arrange for this -- for a class type T, a registerStatic() implementation is |
| +// provided if "T* T::registerAsStaticReference(T*)" is a method on T |
| +// (inherited or otherwise.) |
| +// |
| +// An empty, trivial registerStatic() method is provided for all other class types T. |
| +template<typename T> |
| +class CanRegisterStaticLocalReference { |
| + typedef char YesType; |
| + typedef struct NoType { |
| + char padding[8]; |
| + } NoType; |
| + |
| + // Check if class T has public method "T* registerAsStaticReference()". |
| + template<typename V> static YesType checkHasRegisterAsStaticReferenceMethod(V* p, typename EnableIf<IsSubclass<V, typename std::remove_pointer<decltype(p->registerAsStaticReference())>::type>::value>::Type* = 0); |
| + template<typename V> static NoType checkHasRegisterAsStaticReferenceMethod(...); |
| + |
| +public: |
| + static const bool value = sizeof(YesType) + sizeof(T) == sizeof(checkHasRegisterAsStaticReferenceMethod<T>(nullptr)) + sizeof(T); |
| +}; |
| + |
| +template<typename T, bool = CanRegisterStaticLocalReference<T>::value> |
| +class RegisterStaticLocalReference { |
| +public: |
| + static T* registerStatic(T* ptr) |
| + { |
| + return ptr; |
| + } |
| +}; |
| + |
| +template<typename T> |
| +class RegisterStaticLocalReference<T, true> { |
| +public: |
| + static T* registerStatic(T* ptr) |
| + { |
| + return static_cast<T*>(ptr->registerAsStaticReference()); |
| + } |
| +}; |
| + |
| +#define WTF_ANNOTATE_LEAK_REGISTER_STATIC_LOCAL(Type, Object) WTF::RegisterStaticLocalReference<Type>::registerStatic(Object) |
| -#else // USE(LEAK_SANITIZER) |
| +#else |
| -// If Leak Sanitizer is not being used, the annotations should be no-ops. |
| -#define WTF_ANNOTATE_SCOPED_MEMORY_LEAK |
| -#define WTF_ANNOTATE_LEAKING_OBJECT_PTR(X) |
| +#define WTF_ANNOTATE_LEAK_SCOPE |
| +#define WTF_ANNOTATE_LEAK_IGNORE_OBJECT(X) |
| +#define WTF_ANNOTATE_LEAK_REGISTER_STATIC_LOCAL(Type, Object) Object |
|
haraken
2015/12/06 23:56:21
It's a bit confusing to have four macros to annota
|
| #endif // USE(LEAK_SANITIZER) |