Eric Fiselier | 257fd69 | 2016-05-07 01:04:55 +0000 | [diff] [blame] | 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" |
Eric Fiselier | ab46ace | 2016-05-07 05:37:31 +0000 | [diff] [blame^] | 14 | #elif !defined(_LIBCPP_HAS_NO_THREADS) |
Eric Fiselier | 257fd69 | 2016-05-07 01:04:55 +0000 | [diff] [blame] | 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_ONLY __new_delete_memory_resource_imp |
| 27 | : public memory_resource |
| 28 | { |
| 29 | public: |
| 30 | ~__new_delete_memory_resource_imp() = default; |
| 31 | |
| 32 | protected: |
| 33 | virtual void* do_allocate(size_t __size, size_t __align) |
| 34 | { return __allocate(__size); } |
| 35 | |
| 36 | virtual void do_deallocate(void * __p, size_t, size_t) |
| 37 | { __deallocate(__p); } |
| 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_ONLY __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) { |
Eric Fiselier | 33861da | 2016-05-07 02:33:25 +0000 | [diff] [blame] | 53 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Eric Fiselier | 257fd69 | 2016-05-07 01:04:55 +0000 | [diff] [blame] | 54 | throw std::bad_alloc(); |
| 55 | #else |
| 56 | abort(); |
| 57 | #endif |
| 58 | } |
| 59 | virtual void do_deallocate(void *, size_t, size_t) {} |
| 60 | virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT |
| 61 | { return &__other == this; } |
| 62 | }; |
| 63 | |
| 64 | union ResourceInitHelper { |
| 65 | struct { |
| 66 | __new_delete_memory_resource_imp new_delete_res; |
| 67 | __null_memory_resource_imp null_res; |
| 68 | } resources; |
| 69 | char dummy; |
| 70 | _LIBCPP_CONSTEXPR_AFTER_CXX11 ResourceInitHelper() : resources() {} |
| 71 | ~ResourceInitHelper() {} |
| 72 | }; |
| 73 | // When compiled in C++14 this initialization should be a constant expression. |
| 74 | // Only in C++11 is "init_priority" needed to ensure initialization order. |
| 75 | ResourceInitHelper res_init __attribute__((init_priority (101))); |
| 76 | |
| 77 | memory_resource * new_delete_resource() _NOEXCEPT { |
| 78 | return &res_init.resources.new_delete_res; |
| 79 | } |
| 80 | |
| 81 | memory_resource * null_memory_resource() _NOEXCEPT { |
| 82 | return &res_init.resources.null_res; |
| 83 | } |
| 84 | |
| 85 | // default_memory_resource() |
| 86 | |
| 87 | static memory_resource * |
| 88 | __default_memory_resource(bool set = false, memory_resource * new_res = nullptr) _NOEXCEPT |
| 89 | { |
| 90 | #ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER |
| 91 | static atomic<memory_resource*> __res = |
| 92 | ATOMIC_VAR_INIT(&res_init.resources.new_delete_res); |
| 93 | if (set) { |
| 94 | new_res = new_res ? new_res : new_delete_resource(); |
| 95 | // TODO: Can a weaker ordering be used? |
| 96 | return _VSTD::atomic_exchange_explicit( |
| 97 | &__res, new_res, memory_order::memory_order_acq_rel); |
| 98 | } |
| 99 | else { |
| 100 | return _VSTD::atomic_load_explicit( |
| 101 | &__res, memory_order::memory_order_acquire); |
| 102 | } |
Eric Fiselier | ab46ace | 2016-05-07 05:37:31 +0000 | [diff] [blame^] | 103 | #elif !defined(_LIBCPP_HAS_NO_THREADS) |
Eric Fiselier | 257fd69 | 2016-05-07 01:04:55 +0000 | [diff] [blame] | 104 | static memory_resource * res = &res_init.resources.new_delete_res; |
| 105 | static mutex res_lock; |
| 106 | if (set) { |
| 107 | new_res = new_res ? new_res : new_delete_resource(); |
| 108 | lock_guard<mutex> guard(res_lock); |
| 109 | memory_resource * old_res = res; |
| 110 | res = new_res; |
| 111 | return old_res; |
| 112 | } else { |
| 113 | lock_guard<mutex> guard(res_lock); |
| 114 | return res; |
| 115 | } |
Eric Fiselier | ab46ace | 2016-05-07 05:37:31 +0000 | [diff] [blame^] | 116 | #else |
| 117 | static memory_resource* res = &res_init.resources.new_delete_res; |
| 118 | if (set) { |
| 119 | new_res = new_res ? new_res : new_delete_resource(); |
| 120 | memory_resource * old_res = res; |
| 121 | res = new_res; |
| 122 | return old_res; |
| 123 | } else { |
| 124 | return res; |
| 125 | } |
Eric Fiselier | 257fd69 | 2016-05-07 01:04:55 +0000 | [diff] [blame] | 126 | #endif |
| 127 | } |
| 128 | |
| 129 | memory_resource * get_default_resource() _NOEXCEPT |
| 130 | { |
| 131 | return __default_memory_resource(); |
| 132 | } |
| 133 | |
| 134 | memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT |
| 135 | { |
| 136 | return __default_memory_resource(true, __new_res); |
| 137 | } |
| 138 | |
| 139 | _LIBCPP_END_NAMESPACE_LFTS_PMR |