Title: [150450] trunk/Source/WTF
Revision
150450
Author
[email protected]
Date
2013-05-21 09:56:14 -0700 (Tue, 21 May 2013)

Log Message

Add WTF::NeverDestroyed and start using it in WTF
https://bugs.webkit.org/show_bug.cgi?id=116472

Reviewed by Benjamin Poulain.

NeverDestroyed is a class template that can be used for singletons and other objects that we never
want to destroy. It's intended as a replacement for WTF_STATIC_LOCAL with the advantage that it doesn't
fragment the heap.

* GNUmakefile.list.am:
* WTF.vcproj/WTF.vcproj:
* WTF.vcxproj/WTF.vcxproj:
* WTF.vcxproj/WTF.vcxproj.filters:
* WTF.xcodeproj/project.pbxproj:
Add NeverDestroyed.h

* wtf/Compiler.h:
Add a helper macro, WTF_DELETED_FUNCTION.

* wtf/CryptographicallyRandomNumber.cpp:
Use NeverDestroyed.

* wtf/NeverDestroyed.h: Added.

* wtf/Noncopyable.h:
Use WTF_DELETED_FUNCTION.

* wtf/text/WTFString.cpp:
(WTF::emptyString):
Use

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (150449 => 150450)


--- trunk/Source/WTF/ChangeLog	2013-05-21 16:44:01 UTC (rev 150449)
+++ trunk/Source/WTF/ChangeLog	2013-05-21 16:56:14 UTC (rev 150450)
@@ -1,3 +1,36 @@
+2013-05-20  Anders Carlsson  <[email protected]>
+
+        Add WTF::NeverDestroyed and start using it in WTF
+        https://bugs.webkit.org/show_bug.cgi?id=116472
+
+        Reviewed by Benjamin Poulain.
+
+        NeverDestroyed is a class template that can be used for singletons and other objects that we never
+        want to destroy. It's intended as a replacement for WTF_STATIC_LOCAL with the advantage that it doesn't
+        fragment the heap.
+
+        * GNUmakefile.list.am:
+        * WTF.vcproj/WTF.vcproj:
+        * WTF.vcxproj/WTF.vcxproj:
+        * WTF.vcxproj/WTF.vcxproj.filters:
+        * WTF.xcodeproj/project.pbxproj:
+        Add NeverDestroyed.h
+
+        * wtf/Compiler.h:
+        Add a helper macro, WTF_DELETED_FUNCTION.
+
+        * wtf/CryptographicallyRandomNumber.cpp:
+        Use NeverDestroyed.
+
+        * wtf/NeverDestroyed.h: Added.
+
+        * wtf/Noncopyable.h:
+        Use WTF_DELETED_FUNCTION.
+
+        * wtf/text/WTFString.cpp:
+        (WTF::emptyString):
+        Use 
+
 2013-05-20  Zan Dobersek  <[email protected]>
 
         Use '__typeof__' keyword to avoid problems when compiling in C++11 standard compliance

Modified: trunk/Source/WTF/GNUmakefile.list.am (150449 => 150450)


--- trunk/Source/WTF/GNUmakefile.list.am	2013-05-21 16:44:01 UTC (rev 150449)
+++ trunk/Source/WTF/GNUmakefile.list.am	2013-05-21 16:56:14 UTC (rev 150450)
@@ -89,6 +89,7 @@
     Source/WTF/wtf/MetaAllocator.cpp \
     Source/WTF/wtf/MetaAllocator.h \
     Source/WTF/wtf/MetaAllocatorHandle.h \
+    Source/WTF/wtf/NeverDestroyed.h \
     Source/WTF/wtf/NonCopyingSort.h \
     Source/WTF/wtf/Noncopyable.h \
     Source/WTF/wtf/NotFound.h \

Modified: trunk/Source/WTF/WTF.vcproj/WTF.vcproj (150449 => 150450)


--- trunk/Source/WTF/WTF.vcproj/WTF.vcproj	2013-05-21 16:44:01 UTC (rev 150449)
+++ trunk/Source/WTF/WTF.vcproj/WTF.vcproj	2013-05-21 16:56:14 UTC (rev 150450)
@@ -935,6 +935,10 @@
 			>
 		</File>
 		<File
+			RelativePath="..\wtf\NeverDestroyed.h"
+			>
+		</File>
+		<File
 			RelativePath="..\wtf\Noncopyable.h"
 			>
 		</File>

Modified: trunk/Source/WTF/WTF.vcxproj/WTF.vcxproj (150449 => 150450)


--- trunk/Source/WTF/WTF.vcxproj/WTF.vcxproj	2013-05-21 16:44:01 UTC (rev 150449)
+++ trunk/Source/WTF/WTF.vcxproj/WTF.vcxproj	2013-05-21 16:56:14 UTC (rev 150450)
@@ -166,7 +166,7 @@
     <ClInclude Include="..\wtf\MessageQueue.h" />
     <ClInclude Include="..\wtf\MetaAllocator.h" />
     <ClInclude Include="..\wtf\MetaAllocatorHandle.h" />
-    <ClInclude Include="..\wtf\Noncopyable.h" />
+    <ClInclude Include="..\wtf\NeverDestroyed.h" />    <ClInclude Include="..\wtf\Noncopyable.h" />
     <ClInclude Include="..\wtf\NonCopyingSort.h" />
     <ClInclude Include="..\wtf\NotFound.h" />
     <ClInclude Include="..\wtf\NullPtr.h" />

Modified: trunk/Source/WTF/WTF.vcxproj/WTF.vcxproj.filters (150449 => 150450)


--- trunk/Source/WTF/WTF.vcxproj/WTF.vcxproj.filters	2013-05-21 16:44:01 UTC (rev 150449)
+++ trunk/Source/WTF/WTF.vcxproj/WTF.vcxproj.filters	2013-05-21 16:56:14 UTC (rev 150450)
@@ -501,6 +501,9 @@
     <ClInclude Include="..\wtf\MetaAllocatorHandle.h">
       <Filter>wtf</Filter>
     </ClInclude>
+    <ClInclude Include="..\wtf\NeverDestroyed.h">
+      <Filter>wtf</Filter>
+    </ClInclude>
     <ClInclude Include="..\wtf\Noncopyable.h">
       <Filter>wtf</Filter>
     </ClInclude>

Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (150449 => 150450)


--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2013-05-21 16:44:01 UTC (rev 150449)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2013-05-21 16:56:14 UTC (rev 150450)
@@ -306,6 +306,7 @@
 		14F3B0F615E45E4600210069 /* SaturatedArithmetic.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SaturatedArithmetic.h; sourceTree = "<group>"; };
 		1A1D8B9B173186CE00141DA4 /* FunctionDispatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FunctionDispatcher.h; sourceTree = "<group>"; };
 		1A1D8B9D1731879800141DA4 /* FunctionDispatcher.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FunctionDispatcher.cpp; sourceTree = "<group>"; };
+		1A3F6BE6174ADA2100B2EEA7 /* NeverDestroyed.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NeverDestroyed.h; sourceTree = "<group>"; };
 		1A6BB768162F300500DD16DB /* StreamBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StreamBuffer.h; sourceTree = "<group>"; };
 		26147B0815DDCCDC00DDB907 /* IntegerToStringConversion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntegerToStringConversion.h; sourceTree = "<group>"; };
 		2C05385315BC819000F21B96 /* GregorianDateTime.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GregorianDateTime.h; sourceTree = "<group>"; };
@@ -690,6 +691,7 @@
 				A8A472CD151A825B004123FF /* MetaAllocator.cpp */,
 				A8A472CE151A825B004123FF /* MetaAllocator.h */,
 				A8A472CF151A825B004123FF /* MetaAllocatorHandle.h */,
+				1A3F6BE6174ADA2100B2EEA7 /* NeverDestroyed.h */,
 				A8A472D0151A825B004123FF /* Noncopyable.h */,
 				A8A472D1151A825B004123FF /* NonCopyingSort.h */,
 				A8A472D2151A825B004123FF /* NotFound.h */,

Modified: trunk/Source/WTF/wtf/Compiler.h (150449 => 150450)


--- trunk/Source/WTF/wtf/Compiler.h	2013-05-21 16:44:01 UTC (rev 150449)
+++ trunk/Source/WTF/wtf/Compiler.h	2013-05-21 16:56:14 UTC (rev 150450)
@@ -65,7 +65,6 @@
 #define WTF_COMPILER_SUPPORTS_HAS_TRIVIAL_DESTRUCTOR __has_extension(has_trivial_destructor)
 #define WTF_COMPILER_SUPPORTS_CXX_STRONG_ENUMS __has_extension(cxx_strong_enums)
 #define WTF_COMPILER_SUPPORTS_CXX_REFERENCE_QUALIFIED_FUNCTIONS __has_extension(cxx_reference_qualified_functions)
-
 #endif
 
 #ifndef CLANG_PRAGMA
@@ -276,6 +275,12 @@
 #define FINAL
 #endif
 
+#if COMPILER_SUPPORTS(CXX_DELETED_FUNCTIONS)
+#define WTF_DELETED_FUNCTION = delete
+#else
+#define WTF_DELETED_FUNCTION
+#endif
+
 /* REFERENCED_FROM_ASM */
 
 #ifndef REFERENCED_FROM_ASM
@@ -320,4 +325,6 @@
 #define UNUSED_LABEL(label) UNUSED_PARAM(&& label)
 #endif
 
+
+
 #endif /* WTF_Compiler_h */

Modified: trunk/Source/WTF/wtf/CryptographicallyRandomNumber.cpp (150449 => 150450)


--- trunk/Source/WTF/wtf/CryptographicallyRandomNumber.cpp	2013-05-21 16:44:01 UTC (rev 150449)
+++ trunk/Source/WTF/wtf/CryptographicallyRandomNumber.cpp	2013-05-21 16:56:14 UTC (rev 150450)
@@ -30,8 +30,8 @@
 #include "config.h"
 #include "CryptographicallyRandomNumber.h"
 
+#include "NeverDestroyed.h"
 #include "OSRandomSource.h"
-#include "StdLibExtras.h"
 #include "ThreadingPrimitives.h"
 
 namespace WTF {
@@ -158,7 +158,8 @@
 
 ARC4RandomNumberGenerator& sharedRandomNumberGenerator()
 {
-    DEFINE_STATIC_LOCAL(ARC4RandomNumberGenerator, randomNumberGenerator, ());
+    static NeverDestroyed<ARC4RandomNumberGenerator> randomNumberGenerator;
+
     return randomNumberGenerator;
 }
 

Added: trunk/Source/WTF/wtf/NeverDestroyed.h (0 => 150450)


--- trunk/Source/WTF/wtf/NeverDestroyed.h	                        (rev 0)
+++ trunk/Source/WTF/wtf/NeverDestroyed.h	2013-05-21 16:56:14 UTC (rev 150450)
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2013 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef NeverDestroyed_h
+#define NeverDestroyed_h
+
+#include <wtf/Alignment.h>
+#include <wtf/Noncopyable.h>
+#include <wtf/StdLibExtras.h>
+#include <wtf/TypeTraits.h>
+
+// NeverDestroyed is a smart pointer like class who ensures that the destructor
+// for the given object is never called, but doesn't use the heap to allocate it.
+// It's useful for static local variables, and can be used like so:
+//
+// MySharedGlobal& mySharedGlobal()
+// {
+//   static NeverDestroyed<MySharedGlobal> myGlobal("Hello", 42);
+//   return myGlobal;
+// }
+//
+
+namespace WTF {
+
+template<typename T> class NeverDestroyed {
+    WTF_MAKE_NONCOPYABLE(NeverDestroyed);
+
+public:
+#if COMPILER_SUPPORTS(CXX_VARIADIC_TEMPLATES)
+    template<typename... Args>
+    NeverDestroyed(Args&&... args)
+    {
+        new (asPtr()) T(std::forward<Args>(args)...);
+    }
+#else
+    NeverDestroyed()
+    {
+        new (NotNull, asPtr()) T;
+    }
+
+    template<typename P1>
+    NeverDestroyed(const P1& p1)
+    {
+        new (NotNull, asPtr()) T(p1);
+    }
+#endif
+
+    operator T&() { return *asPtr(); }
+
+private:
+#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
+    NeverDestroyed(NeverDestroyed&&) WTF_DELETED_FUNCTION;
+    NeverDestroyed& operator=(NeverDestroyed&&) = WTF_DELETED_FUNCTION;
+#endif
+
+    typedef typename WTF::RemoveConst<T>::Type *PointerType;
+
+    PointerType asPtr() { return reinterpret_cast<PointerType>(&m_storage); }
+
+    // FIXME: Investigate whether we should allocate a hunk of virtual memory
+    // and hand out chunks of it to NeverDestroyed instead, to reduce fragmentation.
+    AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage;
+};
+
+} // namespace WTF;
+
+using WTF::NeverDestroyed;
+
+#endif // NeverDestroyed_h

Modified: trunk/Source/WTF/wtf/Noncopyable.h (150449 => 150450)


--- trunk/Source/WTF/wtf/Noncopyable.h	2013-05-21 16:44:01 UTC (rev 150449)
+++ trunk/Source/WTF/wtf/Noncopyable.h	2013-05-21 16:56:14 UTC (rev 150450)
@@ -23,16 +23,9 @@
 
 #include <wtf/Compiler.h>
 
-#if COMPILER_SUPPORTS(CXX_DELETED_FUNCTIONS)
-    #define WTF_MAKE_NONCOPYABLE(ClassName) \
-        private: \
-            ClassName(const ClassName&) = delete; \
-            ClassName& operator=(const ClassName&) = delete;
-#else
-    #define WTF_MAKE_NONCOPYABLE(ClassName) \
-        private: \
-            ClassName(const ClassName&); \
-            ClassName& operator=(const ClassName&)
-#endif
+#define WTF_MAKE_NONCOPYABLE(ClassName) \
+    private: \
+        ClassName(const ClassName&) WTF_DELETED_FUNCTION; \
+        ClassName& operator=(const ClassName&) WTF_DELETED_FUNCTION; \
 
 #endif // WTF_Noncopyable_h

Modified: trunk/Source/WTF/wtf/text/WTFString.cpp (150449 => 150450)


--- trunk/Source/WTF/wtf/text/WTFString.cpp	2013-05-21 16:44:01 UTC (rev 150449)
+++ trunk/Source/WTF/wtf/text/WTFString.cpp	2013-05-21 16:56:14 UTC (rev 150450)
@@ -28,6 +28,7 @@
 #include <wtf/DataLog.h>
 #include <wtf/HexNumber.h>
 #include <wtf/MathExtras.h>
+#include <wtf/NeverDestroyed.h>
 #include <wtf/text/CString.h>
 #include <wtf/StringExtras.h>
 #include <wtf/Vector.h>
@@ -1242,7 +1243,8 @@
 
 const String& emptyString()
 {
-    DEFINE_STATIC_LOCAL(String, emptyString, (StringImpl::empty()));
+    static NeverDestroyed<String> emptyString(StringImpl::empty());
+
     return emptyString;
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to