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

Side by Side Diff: third_party/WebKit/Source/wtf/StdLibExtras.h

Issue 1850413002: Improve DEFINE_STATIC_LOCAL()'s handling of Blink GCed objects. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address compilation failure Created 4 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
« no previous file with comments | « third_party/WebKit/Source/web/WebPagePopupImpl.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 12 matching lines...) Expand all
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #ifndef WTF_StdLibExtras_h 26 #ifndef WTF_StdLibExtras_h
27 #define WTF_StdLibExtras_h 27 #define WTF_StdLibExtras_h
28 28
29 #include "base/numerics/safe_conversions.h" 29 #include "base/numerics/safe_conversions.h"
30 #include "wtf/Assertions.h" 30 #include "wtf/Assertions.h"
31 #include "wtf/CPU.h" 31 #include "wtf/CPU.h"
32 #include "wtf/LeakAnnotations.h" 32 #include "wtf/LeakAnnotations.h"
33 #include "wtf/TypeTraits.h"
33 #include <cstddef> 34 #include <cstddef>
34 35
35 #if ENABLE(ASSERT) 36 #if ENABLE(ASSERT)
36 #include "wtf/Noncopyable.h" 37 #include "wtf/Noncopyable.h"
37 #include "wtf/Threading.h" 38 #include "wtf/Threading.h"
38 39
39 class WTF_EXPORT StaticLocalVerifier { 40 class WTF_EXPORT StaticLocalVerifier {
40 WTF_MAKE_NONCOPYABLE(StaticLocalVerifier); 41 WTF_MAKE_NONCOPYABLE(StaticLocalVerifier);
41 public: 42 public:
42 StaticLocalVerifier() 43 StaticLocalVerifier()
43 : m_safelyInitialized(WTF::isBeforeThreadCreated()) 44 : m_safelyInitialized(WTF::isBeforeThreadCreated())
44 , m_thread(WTF::currentThread()) 45 , m_thread(WTF::currentThread())
45 { 46 {
46 } 47 }
47 48
48 bool isNotRacy() 49 bool isNotRacy()
49 { 50 {
50 // Make sure that this 1) is safely initialized, 2) keeps being called 51 // Make sure that this 1) is safely initialized, 2) keeps being called
51 // on the same thread, or 3) is called within 52 // on the same thread, or 3) is called within
52 // AtomicallyInitializedStatic (i.e. with a lock held). 53 // AtomicallyInitializedStatic (i.e. with a lock held).
53 return m_safelyInitialized || m_thread == WTF::currentThread() || WTF::i sAtomicallyInitializedStaticMutexLockHeld(); 54 return m_safelyInitialized || m_thread == WTF::currentThread() || WTF::i sAtomicallyInitializedStaticMutexLockHeld();
54 } 55 }
55 56
56 private: 57 private:
57 bool m_safelyInitialized; 58 bool m_safelyInitialized;
58 ThreadIdentifier m_thread; 59 ThreadIdentifier m_thread;
59 }; 60 };
60 #endif 61 #endif
61 62
62 // A direct static local to a Blink garbage collected objects isn't allowed; 63 namespace blink {
63 // must be wrapped up with a persistent reference. 64 template<typename T>class Persistent;
64 #define STATIC_ASSERT_FOR_LOCAL_WITH_GARBAGE_COLLECTED_TYPE(Name, Type) \ 65 };
65 using Name##NoConstType = std::remove_const<Type>::type; \
66 using Name##NoPointerType = std::remove_pointer<Name##NoConstType>::type; \
67 using Name##NoReferenceType = std::remove_reference<Name##NoPointerType>::ty pe; \
68 static_assert(!WTF::IsGarbageCollectedType<Name##NoReferenceType>::value || WTF::IsPersistentReferenceType<Name##NoReferenceType>::value, "Garbage collected static local needs to be wrapped up with a persistent reference")
69 66
70 // Use DEFINE_STATIC_LOCAL() to declare and define a static local variable (stat ic T;) 67 template<typename T, bool = WTF::IsGarbageCollectedType<T>::value && !WTF::IsPer sistentReferenceType<T>::value>
71 // so that it is leaked and its destructors are not called at exit. 68 class StaticLocalWrapper {
69 public:
70 using WrapType = T;
71
72 static T& unwrap(T* singleton)
73 {
74 return *singleton;
75 }
76 };
77
78 template<typename T>
79 class StaticLocalWrapper<T, true> {
80 public:
81 using WrapType = blink::Persistent<T>;
82
83 static T& unwrap(blink::Persistent<T>* singleton)
84 {
85 ASSERT(singleton);
86 // If this assert triggers, you're supplying an empty ("()") 'Arguments' argument
87 // to DEFINE_STATIC_LOCAL() - it must be the heap object you wish to cre ate
88 // as a static singleton and wrapped up with a Persistent reference.
89 ASSERT(*singleton);
90 return **singleton;
91 }
92 };
93
94 #if ENABLE(ASSERT)
95 #define DEFINE_STATIC_LOCAL_CHECK_THREADSAFE_ACCESS(Name) \
96 static StaticLocalVerifier Name##StaticLocalVerifier; \
97 ASSERT(Name##StaticLocalVerifier.isNotRacy())
98 #else
99 #define DEFINE_STATIC_LOCAL_CHECK_THREADSAFE_ACCESS(Name)
100 #endif
101
102 // Use DEFINE_STATIC_LOCAL() to declare and define a static local variable
103 // (static T;) so that it is leaked and its destructors are not called at exit.
104 // T may also be a Blink garbage collected object, in which case it is
105 // wrapped up by an off-heap Persistent<T> reference to the object, keeping
106 // it alive across GCs.
72 // 107 //
73 // To cooperate with leak detection, the objects held onto by these local static s 108 // To cooperate with leak detection(LSan) for Blink garbage collected objects,
74 // will in some cases have to be finalized prior to leak checking. This only app lies 109 // the objects owned by persistent local statics will in some cases have to be
75 // to static references to Oilpan heap objects and what they transitively hold o n to. 110 // finalized prior to leak checking. This only applies to static references to
76 // LEAK_SANITIZER_REGISTER_STATIC_LOCAL() takes care of the details. 111 // Blink heap objects and what they transitively hold on to. Hence the
112 // LEAK_SANITIZER_REGISTER_STATIC_LOCAL() use, it taking care of the grungy
113 // details.
77 // 114 //
78 #if ENABLE(ASSERT) 115 #define DEFINE_STATIC_LOCAL(Type, Name, Arguments) \
79 #define DEFINE_STATIC_LOCAL(Type, Name, Arguments) \ 116 DEFINE_STATIC_LOCAL_CHECK_THREADSAFE_ACCESS(Name); \
80 STATIC_ASSERT_FOR_LOCAL_WITH_GARBAGE_COLLECTED_TYPE(Name, Type); \ 117 using WrappedTypeFor##Name = StaticLocalWrapper<Type>::WrapType; \
81 static StaticLocalVerifier Name##StaticLocalVerifier; \ 118 static Type& Name = StaticLocalWrapper<Type>::unwrap(LEAK_SANITIZER_REGISTER _STATIC_LOCAL(WrappedTypeFor##Name, new WrappedTypeFor##Name Arguments))
82 ASSERT(Name##StaticLocalVerifier.isNotRacy()); \
83 static Type& Name = *LEAK_SANITIZER_REGISTER_STATIC_LOCAL(Type, new Type Arg uments)
84 #else
85 #define DEFINE_STATIC_LOCAL(Type, Name, Arguments) \
86 STATIC_ASSERT_FOR_LOCAL_WITH_GARBAGE_COLLECTED_TYPE(Name, Type); \
87 static Type& Name = *LEAK_SANITIZER_REGISTER_STATIC_LOCAL(Type, new Type Arg uments)
88 #endif
89 119
90 // Use this to declare and define a static local pointer to a ref-counted object so that 120 // Use this to declare and define a static local pointer to a ref-counted object so that
91 // it is leaked so that the object's destructors are not called at exit. 121 // it is leaked so that the object's destructors are not called at exit.
92 // This macro should be used with ref-counted objects rather than DEFINE_STATIC_ LOCAL macro, 122 // This macro should be used with ref-counted objects rather than DEFINE_STATIC_ LOCAL macro,
93 // as this macro does not lead to an extra memory allocation. 123 // as this macro does not lead to an extra memory allocation.
94 #define DEFINE_STATIC_REF(type, name, arguments) \ 124 #define DEFINE_STATIC_REF(type, name, arguments) \
95 static type* name = PassRefPtr<type>(arguments).leakRef(); 125 static type* name = PassRefPtr<type>(arguments).leakRef();
96 126
97 /* 127 /*
98 * The reinterpret_cast<Type1*>([pointer to Type2]) expressions - where 128 * The reinterpret_cast<Type1*>([pointer to Type2]) expressions - where
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 inline void* operator new(size_t, NotNullTag, void* location) 231 inline void* operator new(size_t, NotNullTag, void* location)
202 { 232 {
203 ASSERT(location); 233 ASSERT(location);
204 return location; 234 return location;
205 } 235 }
206 236
207 using WTF::bitwise_cast; 237 using WTF::bitwise_cast;
208 using WTF::safeCast; 238 using WTF::safeCast;
209 239
210 #endif // WTF_StdLibExtras_h 240 #endif // WTF_StdLibExtras_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/web/WebPagePopupImpl.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698