1 //===------------------------ memory_resource.cpp -------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "experimental/memory_resource"
10 
11 #ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
12 #include "atomic"
13 #elif !defined(_LIBCPP_HAS_NO_THREADS)
14 #include "mutex"
15 #endif
16 
17 _LIBCPP_BEGIN_NAMESPACE_LFTS_PMR
18 
19 // memory_resource
20 
21 //memory_resource::~memory_resource() {}
22 
23 // new_delete_resource()
24 
25 class _LIBCPP_TYPE_VIS __new_delete_memory_resource_imp
26     : public memory_resource
27 {
28 public:
29     ~__new_delete_memory_resource_imp() = default;
30 
31 protected:
32     virtual void* do_allocate(size_t __size, size_t __align)
33         { return _VSTD::__libcpp_allocate(__size, __align); /* FIXME */}
34 
35     virtual void do_deallocate(void* __p, size_t __n, size_t __align) {
36       _VSTD::__libcpp_deallocate(__p, __n, __align); /* FIXME */
37     }
38 
39     virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT
40         { return &__other == this; }
41 };
42 
43 // null_memory_resource()
44 
45 class _LIBCPP_TYPE_VIS __null_memory_resource_imp
46     : public memory_resource
47 {
48 public:
49     ~__null_memory_resource_imp() = default;
50 
51 protected:
52     virtual void* do_allocate(size_t, size_t) {
53         __throw_bad_alloc();
54     }
55     virtual void do_deallocate(void *, size_t, size_t) {}
56     virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT
57     { return &__other == this; }
58 };
59 
60 namespace {
61 
62 union ResourceInitHelper {
63   struct {
64     __new_delete_memory_resource_imp new_delete_res;
65     __null_memory_resource_imp       null_res;
66   } resources;
67   char dummy;
68   _LIBCPP_CONSTEXPR_AFTER_CXX11 ResourceInitHelper() : resources() {}
69   ~ResourceInitHelper() {}
70 };
71 
72 // Detect if the init_priority attribute is supported.
73 #if (defined(_LIBCPP_COMPILER_GCC) && defined(__APPLE__)) \
74   || defined(_LIBCPP_COMPILER_MSVC)
75 // GCC on Apple doesn't support the init priority attribute,
76 // and MSVC doesn't support any GCC attributes.
77 # define _LIBCPP_INIT_PRIORITY_MAX
78 #else
79 # define _LIBCPP_INIT_PRIORITY_MAX __attribute__((init_priority(101)))
80 #endif
81 
82 // When compiled in C++14 this initialization should be a constant expression.
83 // Only in C++11 is "init_priority" needed to ensure initialization order.
84 #if _LIBCPP_STD_VER > 11
85 _LIBCPP_SAFE_STATIC
86 #endif
87 ResourceInitHelper res_init _LIBCPP_INIT_PRIORITY_MAX;
88 
89 } // end namespace
90 
91 
92 memory_resource * new_delete_resource() _NOEXCEPT {
93     return &res_init.resources.new_delete_res;
94 }
95 
96 memory_resource * null_memory_resource() _NOEXCEPT {
97     return &res_init.resources.null_res;
98 }
99 
100 // default_memory_resource()
101 
102 static memory_resource *
103 __default_memory_resource(bool set = false, memory_resource * new_res = nullptr) _NOEXCEPT
104 {
105 #ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
106     _LIBCPP_SAFE_STATIC static atomic<memory_resource*> __res =
107         ATOMIC_VAR_INIT(&res_init.resources.new_delete_res);
108     if (set) {
109         new_res = new_res ? new_res : new_delete_resource();
110         // TODO: Can a weaker ordering be used?
111         return _VSTD::atomic_exchange_explicit(
112             &__res, new_res, memory_order::memory_order_acq_rel);
113     }
114     else {
115         return _VSTD::atomic_load_explicit(
116             &__res, memory_order::memory_order_acquire);
117     }
118 #elif !defined(_LIBCPP_HAS_NO_THREADS)
119     _LIBCPP_SAFE_STATIC static memory_resource * res = &res_init.resources.new_delete_res;
120     static mutex res_lock;
121     if (set) {
122         new_res = new_res ? new_res : new_delete_resource();
123         lock_guard<mutex> guard(res_lock);
124         memory_resource * old_res = res;
125         res = new_res;
126         return old_res;
127     } else {
128         lock_guard<mutex> guard(res_lock);
129         return res;
130     }
131 #else
132     _LIBCPP_SAFE_STATIC static memory_resource* res = &res_init.resources.new_delete_res;
133     if (set) {
134         new_res = new_res ? new_res : new_delete_resource();
135         memory_resource * old_res = res;
136         res = new_res;
137         return old_res;
138     } else {
139         return res;
140     }
141 #endif
142 }
143 
144 memory_resource * get_default_resource() _NOEXCEPT
145 {
146     return __default_memory_resource();
147 }
148 
149 memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT
150 {
151     return __default_memory_resource(true, __new_res);
152 }
153 
154 _LIBCPP_END_NAMESPACE_LFTS_PMR
155