Skip to content

Commit 5a21764

Browse files
Enhance Ref and RefPtr to be able to work with smart pointers.
https://bugs.webkit.org/show_bug.cgi?id=180762 <rdar://problem/36027122> Reviewed by JF Bastien and Darin Adler. Source/WTF: This is so we can use them with ConstExprPoisoned pointers to make PoisonedRef and PoisonedRefPtr. * WTF.xcodeproj/project.pbxproj: * wtf/CMakeLists.txt: * wtf/DumbPtrTraits.h: Added. (WTF::DumbPtrTraits::exchange): (WTF::DumbPtrTraits::swap): (WTF::DumbPtrTraits::unwrap): * wtf/Forward.h: * wtf/Poisoned.h: (WTF::ConstExprPoisonedPtrTraits::exchange): (WTF::ConstExprPoisonedPtrTraits::swap): (WTF::ConstExprPoisonedPtrTraits::unwrap): * wtf/Ref.h: (WTF::Ref::~Ref): (WTF::Ref::Ref): (WTF::Ref::ptrAllowingHashTableEmptyValue const): (WTF::Ref::ptrAllowingHashTableEmptyValue): (WTF::Ref::operator-> const): (WTF::Ref::get const): (WTF::Ref::operator T& const): (WTF::=): (WTF::U>::swap): (WTF::swap): (WTF::U>::replace): (WTF::static_reference_cast): (WTF::adoptRef): (WTF::is): (WTF::Ref<T>::swap): Deleted. (WTF::Ref<T>::replace): Deleted. (WTF::GetPtrHelper<Ref<T>>::getPtr): Deleted. * wtf/RefPtr.cpp: Added. * wtf/RefPtr.h: (WTF::RefPtr::RefPtr): (WTF::RefPtr::~RefPtr): (WTF::RefPtr::get const): (WTF::RefPtr::operator* const): (WTF::RefPtr::operator-> const): (WTF::U>::RefPtr): (WTF::U>::leakRef): (WTF::=): (WTF::U>::swap): (WTF::swap): (WTF::operator==): (WTF::operator!=): (WTF::static_pointer_cast): (WTF::adoptRef): (WTF::is): (WTF::RefPtr<T>::RefPtr): Deleted. (WTF::RefPtr<T>::leakRef): Deleted. (WTF::RefPtr<T>::swap): Deleted. Tools: * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/ConstExprPoisoned.cpp: (TestWebKitAPI::TEST): * TestWebKitAPI/Tests/WTF/Poisoned.cpp: (TestWebKitAPI::TEST): * TestWebKitAPI/Tests/WTF/PoisonedRef.cpp: Added. (TestWebKitAPI::TEST): (TestWebKitAPI::passWithRef): (TestWebKitAPI::PoisonedRefCheckingRefLogger::PoisonedRefCheckingRefLogger): (TestWebKitAPI::PoisonedRefCheckingRefLogger::ref): (TestWebKitAPI::PoisonedRefCheckingRefLogger::deref): (TestWebKitAPI::DerivedPoisonedRefCheckingRefLogger::DerivedPoisonedRefCheckingRefLogger): * TestWebKitAPI/Tests/WTF/PoisonedRefPtr.cpp: Added. (TestWebKitAPI::TEST): (TestWebKitAPI::f1): (TestWebKitAPI::ConstRefCounted::create): (TestWebKitAPI::returnConstRefCountedRef): (TestWebKitAPI::returnRefCountedRef): (TestWebKitAPI::PoisonedRefPtrCheckingRefLogger::PoisonedRefPtrCheckingRefLogger): (TestWebKitAPI::loggerName): (TestWebKitAPI::PoisonedRefPtrCheckingRefLogger::ref): (TestWebKitAPI::PoisonedRefPtrCheckingRefLogger::deref): git-svn-id: http://svn.webkit.org/repository/webkit/trunk@226015 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 45152af commit 5a21764

File tree

16 files changed

+1231
-93
lines changed

16 files changed

+1231
-93
lines changed

Source/WTF/ChangeLog

+61
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,64 @@
1+
2017-12-17 Mark Lam <[email protected]>
2+
3+
Enhance Ref and RefPtr to be able to work with smart pointers.
4+
https://bugs.webkit.org/show_bug.cgi?id=180762
5+
<rdar://problem/36027122>
6+
7+
Reviewed by JF Bastien and Darin Adler.
8+
9+
This is so we can use them with ConstExprPoisoned pointers to make PoisonedRef
10+
and PoisonedRefPtr.
11+
12+
* WTF.xcodeproj/project.pbxproj:
13+
* wtf/CMakeLists.txt:
14+
* wtf/DumbPtrTraits.h: Added.
15+
(WTF::DumbPtrTraits::exchange):
16+
(WTF::DumbPtrTraits::swap):
17+
(WTF::DumbPtrTraits::unwrap):
18+
* wtf/Forward.h:
19+
* wtf/Poisoned.h:
20+
(WTF::ConstExprPoisonedPtrTraits::exchange):
21+
(WTF::ConstExprPoisonedPtrTraits::swap):
22+
(WTF::ConstExprPoisonedPtrTraits::unwrap):
23+
* wtf/Ref.h:
24+
(WTF::Ref::~Ref):
25+
(WTF::Ref::Ref):
26+
(WTF::Ref::ptrAllowingHashTableEmptyValue const):
27+
(WTF::Ref::ptrAllowingHashTableEmptyValue):
28+
(WTF::Ref::operator-> const):
29+
(WTF::Ref::get const):
30+
(WTF::Ref::operator T& const):
31+
(WTF::=):
32+
(WTF::U>::swap):
33+
(WTF::swap):
34+
(WTF::U>::replace):
35+
(WTF::static_reference_cast):
36+
(WTF::adoptRef):
37+
(WTF::is):
38+
(WTF::Ref<T>::swap): Deleted.
39+
(WTF::Ref<T>::replace): Deleted.
40+
(WTF::GetPtrHelper<Ref<T>>::getPtr): Deleted.
41+
* wtf/RefPtr.cpp: Added.
42+
* wtf/RefPtr.h:
43+
(WTF::RefPtr::RefPtr):
44+
(WTF::RefPtr::~RefPtr):
45+
(WTF::RefPtr::get const):
46+
(WTF::RefPtr::operator* const):
47+
(WTF::RefPtr::operator-> const):
48+
(WTF::U>::RefPtr):
49+
(WTF::U>::leakRef):
50+
(WTF::=):
51+
(WTF::U>::swap):
52+
(WTF::swap):
53+
(WTF::operator==):
54+
(WTF::operator!=):
55+
(WTF::static_pointer_cast):
56+
(WTF::adoptRef):
57+
(WTF::is):
58+
(WTF::RefPtr<T>::RefPtr): Deleted.
59+
(WTF::RefPtr<T>::leakRef): Deleted.
60+
(WTF::RefPtr<T>::swap): Deleted.
61+
162
2017-12-16 Yusuke Suzuki <[email protected]>
263

364
Remove unnecessary boolean result of start() functions

Source/WTF/WTF.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
E3A32BC41FC830E2007D7E76 /* JSValueMalloc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E3A32BC21FC830E2007D7E76 /* JSValueMalloc.cpp */; };
151151
E4A0AD391A96245500536DF6 /* WorkQueue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4A0AD371A96245500536DF6 /* WorkQueue.cpp */; };
152152
E4A0AD3D1A96253C00536DF6 /* WorkQueueCocoa.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4A0AD3C1A96253C00536DF6 /* WorkQueueCocoa.cpp */; };
153+
FE05FAFF1FE5007500093230 /* RefPtr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE05FAFE1FE5007500093230 /* RefPtr.cpp */; };
153154
FE85416E1FBE285D008DA5DA /* Poisoned.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE85416C1FBE285B008DA5DA /* Poisoned.cpp */; };
154155
FEDACD3D1630F83F00C69634 /* StackStats.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FEDACD3B1630F83F00C69634 /* StackStats.cpp */; };
155156
/* End PBXBuildFile section */
@@ -616,6 +617,8 @@
616617
EB95E1EF161A72410089A2F5 /* ByteOrder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ByteOrder.h; sourceTree = "<group>"; };
617618
EF7D6CD59D8642A8A0DA86AD /* StackTrace.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StackTrace.h; sourceTree = "<group>"; };
618619
F72BBDB107FA424886178B9E /* SymbolImpl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SymbolImpl.cpp; sourceTree = "<group>"; };
620+
FE05FAE61FDB214300093230 /* DumbPtrTraits.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DumbPtrTraits.h; sourceTree = "<group>"; };
621+
FE05FAFE1FE5007500093230 /* RefPtr.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RefPtr.cpp; sourceTree = "<group>"; };
619622
FE8225301B2A1E5B00BA68FD /* NakedPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NakedPtr.h; sourceTree = "<group>"; };
620623
FE85416C1FBE285B008DA5DA /* Poisoned.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Poisoned.cpp; sourceTree = "<group>"; };
621624
FE85416D1FBE285C008DA5DA /* Poisoned.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Poisoned.h; sourceTree = "<group>"; };
@@ -848,6 +851,7 @@
848851
A8A4727F151A825A004123FF /* DispatchPtr.h */,
849852
0F4570421BE5B58F0062A629 /* Dominators.h */,
850853
A8A47280151A825A004123FF /* DoublyLinkedList.h */,
854+
FE05FAE61FDB214300093230 /* DumbPtrTraits.h */,
851855
A8A47297151A825A004123FF /* dtoa.cpp */,
852856
A8A47298151A825A004123FF /* dtoa.h */,
853857
1AEA88E11D6BBCF400E5AD64 /* EnumTraits.h */,
@@ -1004,6 +1008,7 @@
10041008
A8A47301151A825B004123FF /* RefCountedLeakCounter.cpp */,
10051009
A8A47302151A825B004123FF /* RefCountedLeakCounter.h */,
10061010
86F46F5F1A2840EE00CCBF22 /* RefCounter.h */,
1011+
FE05FAFE1FE5007500093230 /* RefPtr.cpp */,
10071012
A8A47303151A825B004123FF /* RefPtr.h */,
10081013
A8A47305151A825B004123FF /* RetainPtr.h */,
10091014
2CDED0F118115C85004DBA70 /* RunLoop.cpp */,
@@ -1411,6 +1416,7 @@
14111416
0F66B28A1DC97BAB004A1D3F /* ClockType.cpp in Sources */,
14121417
A8A47460151A825B004123FF /* CollatorDefault.cpp in Sources */,
14131418
A8A47463151A825B004123FF /* CollatorICU.cpp in Sources */,
1419+
FE05FAFF1FE5007500093230 /* RefPtr.cpp in Sources */,
14141420
0F8F2B92172E0103007DBDA5 /* CompilationThread.cpp in Sources */,
14151421
E38C41281EB4E0680042957D /* CPUTime.cpp in Sources */,
14161422
E38C41251EB4E04C0042957D /* CPUTimeCocoa.mm in Sources */,

Source/WTF/wtf/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set(WTF_HEADERS
3737
Deque.h
3838
DisallowCType.h
3939
DoublyLinkedList.h
40+
DumbPtrTraits.h
4041
FastMalloc.h
4142
FastTLS.h
4243
FeatureDefines.h
@@ -268,6 +269,7 @@ set(WTF_SOURCES
268269
RandomNumber.cpp
269270
ReadWriteLock.cpp
270271
RefCountedLeakCounter.cpp
272+
RefPtr.cpp
271273
RunLoop.cpp
272274
SHA1.cpp
273275
Seconds.cpp

Source/WTF/wtf/DumbPtrTraits.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2017 Apple 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
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#pragma once
27+
28+
#include <utility>
29+
30+
namespace WTF {
31+
32+
template<typename T>
33+
struct DumbPtrTraits {
34+
using StorageType = T*;
35+
36+
template<typename U>
37+
static ALWAYS_INLINE T* exchange(StorageType& ptr, U&& newValue) { return std::exchange(ptr, newValue); }
38+
39+
static ALWAYS_INLINE void swap(StorageType& a, StorageType& b) { std::swap(a, b); }
40+
static ALWAYS_INLINE T* unwrap(const StorageType& ptr) { return ptr; }
41+
};
42+
43+
} // namespace WTF
44+

Source/WTF/wtf/Forward.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ class TextStream;
4848
struct FastMalloc;
4949

5050
template<typename> class CompletionHandler;
51+
template<typename T> struct DumbPtrTraits;
5152
template<typename> class Function;
5253
template<typename> class LazyNeverDestroyed;
5354
template<typename> class NeverDestroyed;
5455
template<typename> class OptionSet;
55-
template<typename> class Ref;
56-
template<typename> class RefPtr;
56+
template<typename T, typename = DumbPtrTraits<T>> class Ref;
57+
template<typename T, typename = DumbPtrTraits<T>> class RefPtr;
5758
template<typename> class StringBuffer;
5859
template<typename, typename = void> class StringTypeAdapter;
5960

Source/WTF/wtf/Poisoned.h

+12
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,18 @@ using Poisoned = PoisonedImpl<uintptr_t&, key, T>;
228228
template<uint32_t key, typename T>
229229
using ConstExprPoisoned = PoisonedImpl<uintptr_t, makePoison(key), T>;
230230

231+
template<uint32_t key, typename T>
232+
struct ConstExprPoisonedPtrTraits {
233+
using StorageType = ConstExprPoisoned<key, T*>;
234+
235+
template<class U> static ALWAYS_INLINE T* exchange(StorageType& ptr, U&& newValue) { return ptr.exchange(newValue); }
236+
237+
template<typename K1, K1 k1, typename T1, typename K2, K2 k2, typename T2>
238+
static ALWAYS_INLINE void swap(PoisonedImpl<K1, k1, T1>& a, PoisonedImpl<K2, k2, T2>& b) { a.swap(b); }
239+
240+
static ALWAYS_INLINE T* unwrap(const StorageType& ptr) { return ptr.unpoisoned(); }
241+
};
242+
231243
} // namespace WTF
232244

233245
using WTF::ConstExprPoisoned;

0 commit comments

Comments
 (0)