Define _LIBCPP_SAFE_STATIC __attribute__((require_constant_initialization)), and apply it to memory_resource

llvm-svn: 280561
This commit is contained in:
Eric Fiselier 2016-09-03 00:11:33 +00:00
parent 28842b96f3
commit 4efaa30934
2 changed files with 15 additions and 4 deletions

View File

@ -907,6 +907,12 @@ extern "C" void __sanitizer_annotate_contiguous_container(
#define _LIBCPP_HAS_THREAD_SAFETY_ANNOTATIONS
#endif
#if __has_attribute(require_constant_initialization)
#define _LIBCPP_SAFE_STATIC __attribute__((__require_constant_initialization__))
#else
#define _LIBCPP_SAFE_STATIC
#endif
#endif // __cplusplus
#endif // _LIBCPP_CONFIG

View File

@ -70,7 +70,12 @@ union ResourceInitHelper {
};
// When compiled in C++14 this initialization should be a constant expression.
// Only in C++11 is "init_priority" needed to ensure initialization order.
ResourceInitHelper res_init __attribute__((init_priority (101)));
#if _LIBCPP_STD_VER > 11
_LIBCPP_SAFE_STATIC
#else
__attribute__((init_priority (101)))
#endif
ResourceInitHelper res_init;
} // end namespace
@ -89,7 +94,7 @@ static memory_resource *
__default_memory_resource(bool set = false, memory_resource * new_res = nullptr) _NOEXCEPT
{
#ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
static atomic<memory_resource*> __res =
_LIBCPP_SAFE_STATIC static atomic<memory_resource*> __res =
ATOMIC_VAR_INIT(&res_init.resources.new_delete_res);
if (set) {
new_res = new_res ? new_res : new_delete_resource();
@ -102,7 +107,7 @@ __default_memory_resource(bool set = false, memory_resource * new_res = nullptr)
&__res, memory_order::memory_order_acquire);
}
#elif !defined(_LIBCPP_HAS_NO_THREADS)
static memory_resource * res = &res_init.resources.new_delete_res;
_LIBCPP_SAFE_STATIC static memory_resource * res = &res_init.resources.new_delete_res;
static mutex res_lock;
if (set) {
new_res = new_res ? new_res : new_delete_resource();
@ -115,7 +120,7 @@ __default_memory_resource(bool set = false, memory_resource * new_res = nullptr)
return res;
}
#else
static memory_resource* res = &res_init.resources.new_delete_res;
_LIBCPP_SAFE_STATIC static memory_resource* res = &res_init.resources.new_delete_res;
if (set) {
new_res = new_res ? new_res : new_delete_resource();
memory_resource * old_res = res;