OLD | NEW |
---|---|
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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 // AtomicallyInitializedStatic (i.e. with a lock held). | 52 // AtomicallyInitializedStatic (i.e. with a lock held). |
53 return m_safelyInitialized || m_thread == WTF::currentThread() || WTF::i sAtomicallyInitializedStaticMutexLockHeld(); | 53 return m_safelyInitialized || m_thread == WTF::currentThread() || WTF::i sAtomicallyInitializedStaticMutexLockHeld(); |
54 } | 54 } |
55 | 55 |
56 private: | 56 private: |
57 bool m_safelyInitialized; | 57 bool m_safelyInitialized; |
58 ThreadIdentifier m_thread; | 58 ThreadIdentifier m_thread; |
59 }; | 59 }; |
60 #endif | 60 #endif |
61 | 61 |
62 // A direct static local to a Blink garbage collected objects isn't allowed; | 62 namespace blink { |
63 // must be wrapped up with a persistent reference. | 63 template<typename T>class Persistent; |
64 #define STATIC_ASSERT_FOR_LOCAL_WITH_GARBAGE_COLLECTED_TYPE(Name, Type) \ | 64 }; |
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 | 65 |
70 // Use DEFINE_STATIC_LOCAL() to declare and define a static local variable (stat ic T;) | 66 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. | 67 class StaticLocalWrapper { |
68 public: | |
69 using WrapType = T; | |
70 | |
71 static T& unwrap(T* singleton) | |
72 { | |
73 return *singleton; | |
74 } | |
75 }; | |
76 | |
77 template<typename T> | |
78 class StaticLocalWrapper<T, true> { | |
79 public: | |
80 using WrapType = blink::Persistent<T>; | |
81 | |
82 static T& unwrap(blink::Persistent<T>* singleton) | |
83 { | |
84 ASSERT(singleton); | |
85 // If this assert triggers, you're supplying an empty ("()") 'Arguments' argument | |
86 // to DEFINE_STATIC_LOCAL() - it must be the heap object you wish to cre ate | |
87 // as a static singleton and wrapped up with a Persistent reference. | |
88 ASSERT(*singleton); | |
sof
2016/04/04 07:09:00
Added this assert to catch out the accidental crea
| |
89 return **singleton; | |
90 } | |
91 }; | |
92 | |
93 #if ENABLE(ASSERT) | |
94 #define DEFINE_STATIC_LOCAL_CHECK_THREADSAFE_ACCESS(Name) \ | |
95 static StaticLocalVerifier Name##StaticLocalVerifier; \ | |
96 ASSERT(Name##StaticLocalVerifier.isNotRacy()) | |
97 #else | |
98 #define DEFINE_STATIC_LOCAL_CHECK_THREADSAFE_ACCESS(Name) | |
99 #endif | |
100 | |
101 // Use DEFINE_STATIC_LOCAL() to declare and define a static local variable | |
102 // (static T;) so that it is leaked and its destructors are not called at exit. | |
103 // T may also be a Blink garbage collected object, in which case it is | |
104 // wrapped up by an off-heap Persistent<T> reference to the object, keeping | |
105 // it alive across GCs. | |
72 // | 106 // |
73 // To cooperate with leak detection, the objects held onto by these local static s | 107 // 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 | 108 // 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. | 109 // finalized prior to leak checking. This only applies to static references to |
76 // LEAK_SANITIZER_REGISTER_STATIC_LOCAL() takes care of the details. | 110 // Blink heap objects and what they transitively hold on to. Hence the |
111 // LEAK_SANITIZER_REGISTER_STATIC_LOCAL() use, it taking care of the grungy | |
112 // details. | |
77 // | 113 // |
78 #if ENABLE(ASSERT) | 114 #define DEFINE_STATIC_LOCAL(Type, Name, Arguments) \ |
79 #define DEFINE_STATIC_LOCAL(Type, Name, Arguments) \ | 115 DEFINE_STATIC_LOCAL_CHECK_THREADSAFE_ACCESS(Name); \ |
80 STATIC_ASSERT_FOR_LOCAL_WITH_GARBAGE_COLLECTED_TYPE(Name, Type); \ | 116 using WrappedTypeFor##Name = StaticLocalWrapper<Type>::WrapType; \ |
81 static StaticLocalVerifier Name##StaticLocalVerifier; \ | 117 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 | 118 |
90 // Use this to declare and define a static local pointer to a ref-counted object so that | 119 // 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. | 120 // 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, | 121 // 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. | 122 // as this macro does not lead to an extra memory allocation. |
94 #define DEFINE_STATIC_REF(type, name, arguments) \ | 123 #define DEFINE_STATIC_REF(type, name, arguments) \ |
95 static type* name = PassRefPtr<type>(arguments).leakRef(); | 124 static type* name = PassRefPtr<type>(arguments).leakRef(); |
96 | 125 |
97 /* | 126 /* |
98 * The reinterpret_cast<Type1*>([pointer to Type2]) expressions - where | 127 * The reinterpret_cast<Type1*>([pointer to Type2]) expressions - where |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 inline void* operator new(size_t, NotNullTag, void* location) | 230 inline void* operator new(size_t, NotNullTag, void* location) |
202 { | 231 { |
203 ASSERT(location); | 232 ASSERT(location); |
204 return location; | 233 return location; |
205 } | 234 } |
206 | 235 |
207 using WTF::bitwise_cast; | 236 using WTF::bitwise_cast; |
208 using WTF::safeCast; | 237 using WTF::safeCast; |
209 | 238 |
210 #endif // WTF_StdLibExtras_h | 239 #endif // WTF_StdLibExtras_h |
OLD | NEW |