blob: a6eca3743ea92f5da1f1faab237d4e65e9122648 [file] [log] [blame]
Eric Fiselier257fd692016-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 Fiselierab46ace2016-05-07 05:37:31 +000014#elif !defined(_LIBCPP_HAS_NO_THREADS)
Eric Fiselier257fd692016-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 Fiselierc3589a82017-01-04 23:56:00 +000026class _LIBCPP_TYPE_VIS __new_delete_memory_resource_imp
Eric Fiselier257fd692016-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 Fiseliera8312872018-03-22 04:42:56 +000034 { return _VSTD::__libcpp_allocate(__size, __align); /* FIXME */}
Eric Fiselier257fd692016-05-07 01:04:55 +000035
Eric Fiseliera8312872018-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 Fiselier257fd692016-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 Fiselierc3589a82017-01-04 23:56:00 +000045class _LIBCPP_TYPE_VIS __null_memory_resource_imp
Eric Fiselier257fd692016-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 Clow14c09a22016-08-25 15:09:01 +000053 __throw_bad_alloc();
Eric Fiselier257fd692016-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 Fiselier4a98c1e2016-07-11 19:22:09 +000060namespace {
61
Eric Fiselier257fd692016-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};
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.
Eric Fiselier766a31a2016-09-03 00:11:33 +000073#if _LIBCPP_STD_VER > 11
74_LIBCPP_SAFE_STATIC
Eric Fiselier766a31a2016-09-03 00:11:33 +000075#endif
Eric Fiselier5e5eb632016-09-03 07:05:40 +000076ResourceInitHelper res_init __attribute__((init_priority (101)));
Eric Fiselier257fd692016-05-07 01:04:55 +000077
Eric Fiselier4a98c1e2016-07-11 19:22:09 +000078} // end namespace
79
80
Eric Fiselier257fd692016-05-07 01:04:55 +000081memory_resource * new_delete_resource() _NOEXCEPT {
82 return &res_init.resources.new_delete_res;
83}
84
85memory_resource * null_memory_resource() _NOEXCEPT {
86 return &res_init.resources.null_res;
87}
88
89// default_memory_resource()
90
91static memory_resource *
92__default_memory_resource(bool set = false, memory_resource * new_res = nullptr) _NOEXCEPT
93{
94#ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
Eric Fiselier766a31a2016-09-03 00:11:33 +000095 _LIBCPP_SAFE_STATIC static atomic<memory_resource*> __res =
Eric Fiselier257fd692016-05-07 01:04:55 +000096 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 }
Eric Fiselierab46ace2016-05-07 05:37:31 +0000107#elif !defined(_LIBCPP_HAS_NO_THREADS)
Eric Fiselier766a31a2016-09-03 00:11:33 +0000108 _LIBCPP_SAFE_STATIC static memory_resource * res = &res_init.resources.new_delete_res;
Eric Fiselier257fd692016-05-07 01:04:55 +0000109 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 }
Eric Fiselierab46ace2016-05-07 05:37:31 +0000120#else
Eric Fiselier766a31a2016-09-03 00:11:33 +0000121 _LIBCPP_SAFE_STATIC static memory_resource* res = &res_init.resources.new_delete_res;
Eric Fiselierab46ace2016-05-07 05:37:31 +0000122 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 }
Eric Fiselier257fd692016-05-07 01:04:55 +0000130#endif
131}
132
133memory_resource * get_default_resource() _NOEXCEPT
134{
135 return __default_memory_resource();
136}
137
138memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT
139{
140 return __default_memory_resource(true, __new_res);
141}
142
Eric Fiselierc3589a82017-01-04 23:56:00 +0000143_LIBCPP_END_NAMESPACE_LFTS_PMR