blob: a3b64cc8b493ffe4fc0301bdfa507e11e30784dd [file] [log] [blame]
Eric Fiselier15551ef2016-05-07 01:04:55 +00001//===------------------------ 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"
Eric Fiselier2266a8d2016-05-07 05:37:31 +000014#elif !defined(_LIBCPP_HAS_NO_THREADS)
Eric Fiselier15551ef2016-05-07 01:04:55 +000015#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
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +000026class _LIBCPP_TYPE_VIS __new_delete_memory_resource_imp
Eric Fiselier15551ef2016-05-07 01:04:55 +000027 : public memory_resource
28{
29public:
30 ~__new_delete_memory_resource_imp() = default;
31
32protected:
33 virtual void* do_allocate(size_t __size, size_t __align)
Eric Fiselierf2918d12018-03-22 04:42:56 +000034 { return _VSTD::__libcpp_allocate(__size, __align); /* FIXME */}
Eric Fiselier15551ef2016-05-07 01:04:55 +000035
Eric Fiselierf2918d12018-03-22 04:42:56 +000036 virtual void do_deallocate(void * __p, size_t, size_t __align)
37 { _VSTD::__libcpp_deallocate(__p, __align); /* FIXME */ }
Eric Fiselier15551ef2016-05-07 01:04:55 +000038
39 virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT
40 { return &__other == this; }
41};
42
43// null_memory_resource()
44
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +000045class _LIBCPP_TYPE_VIS __null_memory_resource_imp
Eric Fiselier15551ef2016-05-07 01:04:55 +000046 : public memory_resource
47{
48public:
49 ~__null_memory_resource_imp() = default;
50
51protected:
52 virtual void* do_allocate(size_t, size_t) {
Marshall Clowd437fa52016-08-25 15:09:01 +000053 __throw_bad_alloc();
Eric Fiselier15551ef2016-05-07 01:04:55 +000054 }
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
Eric Fiselierfaaeaaf2016-07-11 19:22:09 +000060namespace {
61
Eric Fiselier15551ef2016-05-07 01:04:55 +000062union 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};
Eric Fiselier241d4ad2018-07-16 20:01:59 +000071
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
Eric Fiselier15551ef2016-05-07 01:04:55 +000082// 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.
Eric Fiselier4efaa302016-09-03 00:11:33 +000084#if _LIBCPP_STD_VER > 11
85_LIBCPP_SAFE_STATIC
Eric Fiselier4efaa302016-09-03 00:11:33 +000086#endif
Eric Fiselier241d4ad2018-07-16 20:01:59 +000087ResourceInitHelper res_init _LIBCPP_INIT_PRIORITY_MAX;
Eric Fiselier15551ef2016-05-07 01:04:55 +000088
Eric Fiselierfaaeaaf2016-07-11 19:22:09 +000089} // end namespace
90
91
Eric Fiselier15551ef2016-05-07 01:04:55 +000092memory_resource * new_delete_resource() _NOEXCEPT {
93 return &res_init.resources.new_delete_res;
94}
95
96memory_resource * null_memory_resource() _NOEXCEPT {
97 return &res_init.resources.null_res;
98}
99
100// default_memory_resource()
101
102static memory_resource *
103__default_memory_resource(bool set = false, memory_resource * new_res = nullptr) _NOEXCEPT
104{
105#ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
Eric Fiselier4efaa302016-09-03 00:11:33 +0000106 _LIBCPP_SAFE_STATIC static atomic<memory_resource*> __res =
Eric Fiselier15551ef2016-05-07 01:04:55 +0000107 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 }
Eric Fiselier2266a8d2016-05-07 05:37:31 +0000118#elif !defined(_LIBCPP_HAS_NO_THREADS)
Eric Fiselier4efaa302016-09-03 00:11:33 +0000119 _LIBCPP_SAFE_STATIC static memory_resource * res = &res_init.resources.new_delete_res;
Eric Fiselier15551ef2016-05-07 01:04:55 +0000120 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 }
Eric Fiselier2266a8d2016-05-07 05:37:31 +0000131#else
Eric Fiselier4efaa302016-09-03 00:11:33 +0000132 _LIBCPP_SAFE_STATIC static memory_resource* res = &res_init.resources.new_delete_res;
Eric Fiselier2266a8d2016-05-07 05:37:31 +0000133 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 }
Eric Fiselier15551ef2016-05-07 01:04:55 +0000141#endif
142}
143
144memory_resource * get_default_resource() _NOEXCEPT
145{
146 return __default_memory_resource();
147}
148
149memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT
150{
151 return __default_memory_resource(true, __new_res);
152}
153
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000154_LIBCPP_END_NAMESPACE_LFTS_PMR