Commit 7867ba07 authored by andersca@apple.com's avatar andersca@apple.com
Browse files

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

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@150450 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 3089c6b9
2013-05-20 Anders Carlsson <andersca@apple.com>
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 <zdobersek@igalia.com>
 
Use '__typeof__' keyword to avoid problems when compiling in C++11 standard compliance
......
......@@ -89,6 +89,7 @@ wtf_sources += \
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 \
......
......@@ -934,6 +934,10 @@
RelativePath="..\wtf\MetaAllocatorHandle.h"
>
</File>
<File
RelativePath="..\wtf\NeverDestroyed.h"
>
</File>
<File
RelativePath="..\wtf\Noncopyable.h"
>
......
......@@ -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" />
......
......@@ -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>
......
......@@ -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 */,
......
......@@ -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 @@ inline void unusedParam(T& x) { (void)x; }
#define UNUSED_LABEL(label) UNUSED_PARAM(&& label)
#endif
#endif /* WTF_Compiler_h */
......@@ -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 @@ void ARC4RandomNumberGenerator::randomValues(void* buffer, size_t length)
ARC4RandomNumberGenerator& sharedRandomNumberGenerator()
{
DEFINE_STATIC_LOCAL(ARC4RandomNumberGenerator, randomNumberGenerator, ());
static NeverDestroyed<ARC4RandomNumberGenerator> randomNumberGenerator;
return randomNumberGenerator;
}
......
/*
* 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
......@@ -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
......@@ -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 @@ float charactersToFloat(const UChar* data, size_t length, size_t& parsedLength)
const String& emptyString()
{
DEFINE_STATIC_LOCAL(String, emptyString, (StringImpl::empty()));
static NeverDestroyed<String> emptyString(StringImpl::empty());
return emptyString;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment