From 4efaa30934e8143cab9c47563ef49678aa9f635a Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Sat, 3 Sep 2016 00:11:33 +0000 Subject: [PATCH] Define _LIBCPP_SAFE_STATIC __attribute__((require_constant_initialization)), and apply it to memory_resource llvm-svn: 280561 --- libcxx/include/__config | 6 ++++++ libcxx/src/experimental/memory_resource.cpp | 13 +++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libcxx/include/__config b/libcxx/include/__config index bd3f59a27c5a..b462191e436f 100644 --- a/libcxx/include/__config +++ b/libcxx/include/__config @@ -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 diff --git a/libcxx/src/experimental/memory_resource.cpp b/libcxx/src/experimental/memory_resource.cpp index c5de1aa00b86..c0880597537b 100644 --- a/libcxx/src/experimental/memory_resource.cpp +++ b/libcxx/src/experimental/memory_resource.cpp @@ -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 __res = + _LIBCPP_SAFE_STATIC static atomic __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;