[libcxx] r280561 - Define _LIBCPP_SAFE_STATIC __attribute__((require_constant_initialization)), and apply it to memory_resource

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 2 17:11:34 PDT 2016


Author: ericwf
Date: Fri Sep  2 19:11:33 2016
New Revision: 280561

URL: http://llvm.org/viewvc/llvm-project?rev=280561&view=rev
Log:
Define _LIBCPP_SAFE_STATIC  __attribute__((require_constant_initialization)), and apply it to memory_resource

Modified:
    libcxx/trunk/include/__config
    libcxx/trunk/src/experimental/memory_resource.cpp

Modified: libcxx/trunk/include/__config
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__config?rev=280561&r1=280560&r2=280561&view=diff
==============================================================================
--- libcxx/trunk/include/__config (original)
+++ libcxx/trunk/include/__config Fri Sep  2 19:11:33 2016
@@ -907,6 +907,12 @@ extern "C" void __sanitizer_annotate_con
 #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

Modified: libcxx/trunk/src/experimental/memory_resource.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/experimental/memory_resource.cpp?rev=280561&r1=280560&r2=280561&view=diff
==============================================================================
--- libcxx/trunk/src/experimental/memory_resource.cpp (original)
+++ libcxx/trunk/src/experimental/memory_resource.cpp Fri Sep  2 19:11:33 2016
@@ -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 = fal
             &__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 = fal
         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;




More information about the cfe-commits mailing list