Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------ memory.cpp ----------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "memory" |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 11 | #ifndef _LIBCPP_HAS_NO_THREADS |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 12 | #include "mutex" |
Howard Hinnant | 65f059b | 2012-07-30 17:13:21 +0000 | [diff] [blame] | 13 | #include "thread" |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 14 | #endif |
Eric Fiselier | 3b1fb53 | 2015-08-18 21:08:54 +0000 | [diff] [blame] | 15 | #include "include/atomic_support.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 16 | |
| 17 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 18 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | const allocator_arg_t allocator_arg = allocator_arg_t(); |
| 20 | |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 21 | bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | |
| 23 | const char* |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 24 | bad_weak_ptr::what() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 25 | { |
| 26 | return "bad_weak_ptr"; |
| 27 | } |
| 28 | |
| 29 | __shared_count::~__shared_count() |
| 30 | { |
| 31 | } |
| 32 | |
Kevin Hu | 8993759 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 33 | __shared_weak_count::~__shared_weak_count() |
| 34 | { |
| 35 | } |
| 36 | |
Eric Fiselier | a7ae303 | 2017-01-17 03:16:26 +0000 | [diff] [blame] | 37 | #if defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 39 | __shared_count::__add_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | { |
Kevin Hu | 8993759 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 41 | __libcpp_atomic_refcount_increment(__shared_owners_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 44 | bool |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 45 | __shared_count::__release_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 46 | { |
Kevin Hu | 8993759 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 47 | if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 48 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 49 | __on_zero_shared(); |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | return false; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 55 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 56 | __shared_weak_count::__add_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 57 | { |
| 58 | __shared_count::__add_shared(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 62 | __shared_weak_count::__add_weak() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 63 | { |
Kevin Hu | 8993759 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 64 | __libcpp_atomic_refcount_increment(__shared_weak_owners_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 68 | __shared_weak_count::__release_shared() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 69 | { |
Howard Hinnant | 28dbbe0 | 2010-11-16 21:33:17 +0000 | [diff] [blame] | 70 | if (__shared_count::__release_shared()) |
| 71 | __release_weak(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Eric Fiselier | a7ae303 | 2017-01-17 03:16:26 +0000 | [diff] [blame] | 74 | #endif // _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS |
| 75 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 76 | void |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 77 | __shared_weak_count::__release_weak() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 78 | { |
Ben Craig | 16d768b | 2016-08-01 17:51:26 +0000 | [diff] [blame] | 79 | // NOTE: The acquire load here is an optimization of the very |
| 80 | // common case where a shared pointer is being destructed while |
| 81 | // having no other contended references. |
| 82 | // |
| 83 | // BENEFIT: We avoid expensive atomic stores like XADD and STREX |
| 84 | // in a common case. Those instructions are slow and do nasty |
| 85 | // things to caches. |
| 86 | // |
| 87 | // IS THIS SAFE? Yes. During weak destruction, if we see that we |
| 88 | // are the last reference, we know that no-one else is accessing |
| 89 | // us. If someone were accessing us, then they would be doing so |
| 90 | // while the last shared / weak_ptr was being destructed, and |
| 91 | // that's undefined anyway. |
| 92 | // |
| 93 | // If we see anything other than a 0, then we have possible |
| 94 | // contention, and need to use an atomicrmw primitive. |
| 95 | // The same arguments don't apply for increment, where it is legal |
| 96 | // (though inadvisable) to share shared_ptr references between |
| 97 | // threads, and have them all get copied at once. The argument |
| 98 | // also doesn't apply for __release_shared, because an outstanding |
| 99 | // weak_ptr::lock() could read / modify the shared count. |
Ben Craig | db870e6 | 2016-08-02 13:43:48 +0000 | [diff] [blame] | 100 | if (__libcpp_atomic_load(&__shared_weak_owners_, _AO_Acquire) == 0) |
Ben Craig | 16d768b | 2016-08-01 17:51:26 +0000 | [diff] [blame] | 101 | { |
| 102 | // no need to do this store, because we are about |
| 103 | // to destroy everything. |
| 104 | //__libcpp_atomic_store(&__shared_weak_owners_, -1, _AO_Release); |
| 105 | __on_zero_shared_weak(); |
| 106 | } |
Kevin Hu | 8993759 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 107 | else if (__libcpp_atomic_refcount_decrement(__shared_weak_owners_) == -1) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 108 | __on_zero_shared_weak(); |
| 109 | } |
| 110 | |
| 111 | __shared_weak_count* |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 112 | __shared_weak_count::lock() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 113 | { |
Eric Fiselier | c6e4669 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 114 | long object_owners = __libcpp_atomic_load(&__shared_owners_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 115 | while (object_owners != -1) |
| 116 | { |
Eric Fiselier | c6e4669 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 117 | if (__libcpp_atomic_compare_exchange(&__shared_owners_, |
| 118 | &object_owners, |
| 119 | object_owners+1)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 120 | return this; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 121 | } |
Eric Fiselier | 83e040f | 2017-05-04 01:06:56 +0000 | [diff] [blame] | 122 | return nullptr; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Eric Fiselier | 01f6a14 | 2014-12-12 02:36:23 +0000 | [diff] [blame] | 125 | #if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC) |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 126 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 127 | const void* |
Howard Hinnant | 1694d23 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 128 | __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 129 | { |
Eric Fiselier | 83e040f | 2017-05-04 01:06:56 +0000 | [diff] [blame] | 130 | return nullptr; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 133 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 134 | |
Eric Fiselier | ba9dccd | 2016-06-18 02:12:53 +0000 | [diff] [blame] | 135 | #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) |
Howard Hinnant | 96c60b4 | 2012-08-19 15:13:16 +0000 | [diff] [blame] | 136 | |
Eric Fiselier | ff4f298 | 2016-09-28 22:08:13 +0000 | [diff] [blame] | 137 | _LIBCPP_SAFE_STATIC static const std::size_t __sp_mut_count = 16; |
| 138 | _LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut_back[__sp_mut_count] = |
Howard Hinnant | e33c2d1 | 2013-03-16 00:17:53 +0000 | [diff] [blame] | 139 | { |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 140 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, |
| 141 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, |
| 142 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, |
| 143 | _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER |
Howard Hinnant | e33c2d1 | 2013-03-16 00:17:53 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 146 | _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 147 | : __lx(p) |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 148 | { |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | __sp_mut::lock() _NOEXCEPT |
| 153 | { |
Eric Fiselier | ff4f298 | 2016-09-28 22:08:13 +0000 | [diff] [blame] | 154 | auto m = static_cast<__libcpp_mutex_t*>(__lx); |
Howard Hinnant | 65f059b | 2012-07-30 17:13:21 +0000 | [diff] [blame] | 155 | unsigned count = 0; |
Eric Fiselier | 474dfc3 | 2017-05-04 07:45:09 +0000 | [diff] [blame] | 156 | while (!__libcpp_mutex_trylock(m)) |
Howard Hinnant | 65f059b | 2012-07-30 17:13:21 +0000 | [diff] [blame] | 157 | { |
| 158 | if (++count > 16) |
| 159 | { |
Eric Fiselier | ff4f298 | 2016-09-28 22:08:13 +0000 | [diff] [blame] | 160 | __libcpp_mutex_lock(m); |
Howard Hinnant | 65f059b | 2012-07-30 17:13:21 +0000 | [diff] [blame] | 161 | break; |
| 162 | } |
| 163 | this_thread::yield(); |
| 164 | } |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void |
| 168 | __sp_mut::unlock() _NOEXCEPT |
| 169 | { |
Eric Fiselier | ff4f298 | 2016-09-28 22:08:13 +0000 | [diff] [blame] | 170 | __libcpp_mutex_unlock(static_cast<__libcpp_mutex_t*>(__lx)); |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | __sp_mut& |
| 174 | __get_sp_mut(const void* p) |
| 175 | { |
Eric Fiselier | ff4f298 | 2016-09-28 22:08:13 +0000 | [diff] [blame] | 176 | static __sp_mut muts[__sp_mut_count] |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 177 | { |
| 178 | &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3], |
| 179 | &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7], |
| 180 | &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11], |
| 181 | &mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15] |
| 182 | }; |
| 183 | return muts[hash<const void*>()(p) & (__sp_mut_count-1)]; |
| 184 | } |
| 185 | |
Eric Fiselier | ba9dccd | 2016-06-18 02:12:53 +0000 | [diff] [blame] | 186 | #endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) |
Howard Hinnant | 5fec82d | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 187 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 188 | void |
| 189 | declare_reachable(void*) |
| 190 | { |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | declare_no_pointers(char*, size_t) |
| 195 | { |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | undeclare_no_pointers(char*, size_t) |
| 200 | { |
| 201 | } |
| 202 | |
Eric Fiselier | 46a0c2e | 2017-01-05 01:15:42 +0000 | [diff] [blame] | 203 | #if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) |
| 204 | pointer_safety get_pointer_safety() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 205 | { |
| 206 | return pointer_safety::relaxed; |
| 207 | } |
Eric Fiselier | 46a0c2e | 2017-01-05 01:15:42 +0000 | [diff] [blame] | 208 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 209 | |
| 210 | void* |
| 211 | __undeclare_reachable(void* p) |
| 212 | { |
| 213 | return p; |
| 214 | } |
| 215 | |
| 216 | void* |
| 217 | align(size_t alignment, size_t size, void*& ptr, size_t& space) |
| 218 | { |
| 219 | void* r = nullptr; |
| 220 | if (size <= space) |
| 221 | { |
| 222 | char* p1 = static_cast<char*>(ptr); |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 223 | char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment); |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 224 | size_t d = static_cast<size_t>(p2 - p1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 225 | if (d <= space - size) |
| 226 | { |
| 227 | r = p2; |
| 228 | ptr = r; |
| 229 | space -= d; |
| 230 | } |
| 231 | } |
| 232 | return r; |
| 233 | } |
| 234 | |
| 235 | _LIBCPP_END_NAMESPACE_STD |