1 //===------------------------ memory_resource.cpp -------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "experimental/memory_resource"
11
12 #ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
13 #include "atomic"
14 #elif !defined(_LIBCPP_HAS_NO_THREADS)
15 #include "mutex"
16 #endif
17
18 _LIBCPP_BEGIN_NAMESPACE_LFTS_PMR
19
20 // memory_resource
21
22 //memory_resource::~memory_resource() {}
23
24 // new_delete_resource()
25
26 class _LIBCPP_TYPE_VIS __new_delete_memory_resource_imp
27 : public memory_resource
28 {
29 public:
30 ~__new_delete_memory_resource_imp() = default;
31
32 protected:
do_allocate(size_t __size,size_t __align)33 virtual void* do_allocate(size_t __size, size_t __align)
34 { return __allocate(__size); }
35
do_deallocate(void * __p,size_t,size_t)36 virtual void do_deallocate(void * __p, size_t, size_t)
37 { _VSTD::__libcpp_deallocate(__p); }
38
do_is_equal(memory_resource const & __other) const39 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:
do_allocate(size_t,size_t)52 virtual void* do_allocate(size_t, size_t) {
53 __throw_bad_alloc();
54 }
do_deallocate(void *,size_t,size_t)55 virtual void do_deallocate(void *, size_t, size_t) {}
do_is_equal(memory_resource const & __other) const56 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;
ResourceInitHelper()68 _LIBCPP_CONSTEXPR_AFTER_CXX11 ResourceInitHelper() : resources() {}
~ResourceInitHelper()69 ~ResourceInitHelper() {}
70 };
71 // When compiled in C++14 this initialization should be a constant expression.
72 // Only in C++11 is "init_priority" needed to ensure initialization order.
73 #if _LIBCPP_STD_VER > 11
74 _LIBCPP_SAFE_STATIC
75 #endif
76 ResourceInitHelper res_init __attribute__((init_priority (101)));
77
78 } // end namespace
79
80
new_delete_resource()81 memory_resource * new_delete_resource() _NOEXCEPT {
82 return &res_init.resources.new_delete_res;
83 }
84
null_memory_resource()85 memory_resource * null_memory_resource() _NOEXCEPT {
86 return &res_init.resources.null_res;
87 }
88
89 // default_memory_resource()
90
91 static memory_resource *
__default_memory_resource(bool set=false,memory_resource * new_res=nullptr)92 __default_memory_resource(bool set = false, memory_resource * new_res = nullptr) _NOEXCEPT
93 {
94 #ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
95 _LIBCPP_SAFE_STATIC static atomic<memory_resource*> __res =
96 ATOMIC_VAR_INIT(&res_init.resources.new_delete_res);
97 if (set) {
98 new_res = new_res ? new_res : new_delete_resource();
99 // TODO: Can a weaker ordering be used?
100 return _VSTD::atomic_exchange_explicit(
101 &__res, new_res, memory_order::memory_order_acq_rel);
102 }
103 else {
104 return _VSTD::atomic_load_explicit(
105 &__res, memory_order::memory_order_acquire);
106 }
107 #elif !defined(_LIBCPP_HAS_NO_THREADS)
108 _LIBCPP_SAFE_STATIC static memory_resource * res = &res_init.resources.new_delete_res;
109 static mutex res_lock;
110 if (set) {
111 new_res = new_res ? new_res : new_delete_resource();
112 lock_guard<mutex> guard(res_lock);
113 memory_resource * old_res = res;
114 res = new_res;
115 return old_res;
116 } else {
117 lock_guard<mutex> guard(res_lock);
118 return res;
119 }
120 #else
121 _LIBCPP_SAFE_STATIC static memory_resource* res = &res_init.resources.new_delete_res;
122 if (set) {
123 new_res = new_res ? new_res : new_delete_resource();
124 memory_resource * old_res = res;
125 res = new_res;
126 return old_res;
127 } else {
128 return res;
129 }
130 #endif
131 }
132
get_default_resource()133 memory_resource * get_default_resource() _NOEXCEPT
134 {
135 return __default_memory_resource();
136 }
137
set_default_resource(memory_resource * __new_res)138 memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT
139 {
140 return __default_memory_resource(true, __new_res);
141 }
142
143 _LIBCPP_END_NAMESPACE_LFTS_PMR
144