1 //===------------------------ memory_resource.cpp -------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "experimental/memory_resource"
11 #ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
13 #elif !defined(_LIBCPP_HAS_NO_THREADS)
15 #if defined(__unix__) && defined(__ELF__) && defined(_LIBCPP_HAS_COMMENT_LIB_PRAGMA)
16 #pragma comment(lib, "pthread")
20 _LIBCPP_BEGIN_NAMESPACE_LFTS_PMR
24 //memory_resource::~memory_resource() {}
26 // new_delete_resource()
28 class _LIBCPP_TYPE_VIS __new_delete_memory_resource_imp
29 : public memory_resource
31 void *do_allocate(size_t size, size_t align) override {
32 #ifdef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
33 if (__is_overaligned_for_new(align))
36 return _VSTD::__libcpp_allocate(size, align);
39 void do_deallocate(void *p, size_t n, size_t align) override {
40 _VSTD::__libcpp_deallocate(p, n, align);
43 bool do_is_equal(memory_resource const & other) const _NOEXCEPT override
44 { return &other == this; }
47 ~__new_delete_memory_resource_imp() override = default;
50 // null_memory_resource()
52 class _LIBCPP_TYPE_VIS __null_memory_resource_imp
53 : public memory_resource
56 ~__null_memory_resource_imp() = default;
59 virtual void* do_allocate(size_t, size_t) {
62 virtual void do_deallocate(void *, size_t, size_t) {}
63 virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT
64 { return &__other == this; }
69 union ResourceInitHelper {
71 __new_delete_memory_resource_imp new_delete_res;
72 __null_memory_resource_imp null_res;
75 _LIBCPP_CONSTEXPR_AFTER_CXX11 ResourceInitHelper() : resources() {}
76 ~ResourceInitHelper() {}
79 // Detect if the init_priority attribute is supported.
80 #if (defined(_LIBCPP_COMPILER_GCC) && defined(__APPLE__)) \
81 || defined(_LIBCPP_COMPILER_MSVC)
82 // GCC on Apple doesn't support the init priority attribute,
83 // and MSVC doesn't support any GCC attributes.
84 # define _LIBCPP_INIT_PRIORITY_MAX
86 # define _LIBCPP_INIT_PRIORITY_MAX __attribute__((init_priority(101)))
89 // When compiled in C++14 this initialization should be a constant expression.
90 // Only in C++11 is "init_priority" needed to ensure initialization order.
91 #if _LIBCPP_STD_VER > 11
94 ResourceInitHelper res_init _LIBCPP_INIT_PRIORITY_MAX;
99 memory_resource * new_delete_resource() _NOEXCEPT {
100 return &res_init.resources.new_delete_res;
103 memory_resource * null_memory_resource() _NOEXCEPT {
104 return &res_init.resources.null_res;
107 // default_memory_resource()
109 static memory_resource *
110 __default_memory_resource(bool set = false, memory_resource * new_res = nullptr) _NOEXCEPT
112 #ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
113 _LIBCPP_SAFE_STATIC static atomic<memory_resource*> __res =
114 ATOMIC_VAR_INIT(&res_init.resources.new_delete_res);
116 new_res = new_res ? new_res : new_delete_resource();
117 // TODO: Can a weaker ordering be used?
118 return _VSTD::atomic_exchange_explicit(
119 &__res, new_res, memory_order::memory_order_acq_rel);
122 return _VSTD::atomic_load_explicit(
123 &__res, memory_order::memory_order_acquire);
125 #elif !defined(_LIBCPP_HAS_NO_THREADS)
126 _LIBCPP_SAFE_STATIC static memory_resource * res = &res_init.resources.new_delete_res;
127 static mutex res_lock;
129 new_res = new_res ? new_res : new_delete_resource();
130 lock_guard<mutex> guard(res_lock);
131 memory_resource * old_res = res;
135 lock_guard<mutex> guard(res_lock);
139 _LIBCPP_SAFE_STATIC static memory_resource* res = &res_init.resources.new_delete_res;
141 new_res = new_res ? new_res : new_delete_resource();
142 memory_resource * old_res = res;
151 memory_resource * get_default_resource() _NOEXCEPT
153 return __default_memory_resource();
156 memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT
158 return __default_memory_resource(true, __new_res);
161 _LIBCPP_END_NAMESPACE_LFTS_PMR