Skip to content

Commit

Permalink
Define _LIBCPP_SAFE_STATIC __attribute__((require_constant_initializa…
Browse files Browse the repository at this point in the history
…tion)), and apply it to memory_resource

llvm-svn: 280561
  • Loading branch information
EricWF committed Sep 3, 2016
1 parent 28842b9 commit 4efaa30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions libcxx/include/__config
Expand Up @@ -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
13 changes: 9 additions & 4 deletions libcxx/src/experimental/memory_resource.cpp
Expand Up @@ -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

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
Expand Down

0 comments on commit 4efaa30

Please sign in to comment.