blob: 099f2d61c978df37475f1023b66bdffef46774f8 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===------------------------ memory.cpp ----------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
Howard Hinnant46e94932012-07-07 20:56:04 +000010#define _LIBCPP_BUILDING_MEMORY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000011#include "memory"
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +000012#ifndef _LIBCPP_HAS_NO_THREADS
Howard Hinnant5fec82d2012-07-30 01:40:57 +000013#include "mutex"
Howard Hinnant65f059b2012-07-30 17:13:21 +000014#include "thread"
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +000015#endif
Eric Fiselier3b1fb532015-08-18 21:08:54 +000016#include "include/atomic_support.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000017
18_LIBCPP_BEGIN_NAMESPACE_STD
19
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000020const allocator_arg_t allocator_arg = allocator_arg_t();
21
Howard Hinnant1694d232011-05-28 14:41:13 +000022bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023
24const char*
Howard Hinnant1694d232011-05-28 14:41:13 +000025bad_weak_ptr::what() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026{
27 return "bad_weak_ptr";
28}
29
30__shared_count::~__shared_count()
31{
32}
33
Kevin Hu89937592017-01-17 02:46:33 +000034__shared_weak_count::~__shared_weak_count()
35{
36}
37
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000038void
Howard Hinnant1694d232011-05-28 14:41:13 +000039__shared_count::__add_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040{
Kevin Hu89937592017-01-17 02:46:33 +000041 __libcpp_atomic_refcount_increment(__shared_owners_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042}
43
Howard Hinnant28dbbe02010-11-16 21:33:17 +000044bool
Howard Hinnant1694d232011-05-28 14:41:13 +000045__shared_count::__release_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000046{
Kevin Hu89937592017-01-17 02:46:33 +000047 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1)
Howard Hinnant28dbbe02010-11-16 21:33:17 +000048 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049 __on_zero_shared();
Howard Hinnant28dbbe02010-11-16 21:33:17 +000050 return true;
51 }
52 return false;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000053}
54
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000055void
Howard Hinnant1694d232011-05-28 14:41:13 +000056__shared_weak_count::__add_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000057{
58 __shared_count::__add_shared();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000059}
60
61void
Howard Hinnant1694d232011-05-28 14:41:13 +000062__shared_weak_count::__add_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063{
Kevin Hu89937592017-01-17 02:46:33 +000064 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000065}
66
67void
Howard Hinnant1694d232011-05-28 14:41:13 +000068__shared_weak_count::__release_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069{
Howard Hinnant28dbbe02010-11-16 21:33:17 +000070 if (__shared_count::__release_shared())
71 __release_weak();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072}
73
74void
Howard Hinnant1694d232011-05-28 14:41:13 +000075__shared_weak_count::__release_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000076{
Ben Craig16d768b2016-08-01 17:51:26 +000077 // NOTE: The acquire load here is an optimization of the very
78 // common case where a shared pointer is being destructed while
79 // having no other contended references.
80 //
81 // BENEFIT: We avoid expensive atomic stores like XADD and STREX
82 // in a common case. Those instructions are slow and do nasty
83 // things to caches.
84 //
85 // IS THIS SAFE? Yes. During weak destruction, if we see that we
86 // are the last reference, we know that no-one else is accessing
87 // us. If someone were accessing us, then they would be doing so
88 // while the last shared / weak_ptr was being destructed, and
89 // that's undefined anyway.
90 //
91 // If we see anything other than a 0, then we have possible
92 // contention, and need to use an atomicrmw primitive.
93 // The same arguments don't apply for increment, where it is legal
94 // (though inadvisable) to share shared_ptr references between
95 // threads, and have them all get copied at once. The argument
96 // also doesn't apply for __release_shared, because an outstanding
97 // weak_ptr::lock() could read / modify the shared count.
Ben Craigdb870e62016-08-02 13:43:48 +000098 if (__libcpp_atomic_load(&__shared_weak_owners_, _AO_Acquire) == 0)
Ben Craig16d768b2016-08-01 17:51:26 +000099 {
100 // no need to do this store, because we are about
101 // to destroy everything.
102 //__libcpp_atomic_store(&__shared_weak_owners_, -1, _AO_Release);
103 __on_zero_shared_weak();
104 }
Kevin Hu89937592017-01-17 02:46:33 +0000105 else if (__libcpp_atomic_refcount_decrement(__shared_weak_owners_) == -1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106 __on_zero_shared_weak();
107}
108
109__shared_weak_count*
Howard Hinnant1694d232011-05-28 14:41:13 +0000110__shared_weak_count::lock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000111{
Eric Fiselierc6e46692015-07-07 00:27:16 +0000112 long object_owners = __libcpp_atomic_load(&__shared_owners_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000113 while (object_owners != -1)
114 {
Eric Fiselierc6e46692015-07-07 00:27:16 +0000115 if (__libcpp_atomic_compare_exchange(&__shared_owners_,
116 &object_owners,
117 object_owners+1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118 return this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119 }
120 return 0;
121}
122
Eric Fiselier01f6a142014-12-12 02:36:23 +0000123#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnantd4444702010-08-11 17:04:31 +0000124
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000125const void*
Howard Hinnant1694d232011-05-28 14:41:13 +0000126__shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000127{
128 return 0;
129}
130
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000131#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000132
Eric Fiselierba9dccd2016-06-18 02:12:53 +0000133#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant96c60b42012-08-19 15:13:16 +0000134
Eric Fiselierff4f2982016-09-28 22:08:13 +0000135_LIBCPP_SAFE_STATIC static const std::size_t __sp_mut_count = 16;
136_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut_back[__sp_mut_count] =
Howard Hinnante33c2d12013-03-16 00:17:53 +0000137{
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000138 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
139 _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
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
Howard Hinnante33c2d12013-03-16 00:17:53 +0000142};
143
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000144_LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT
Howard Hinnant9c0df142012-10-30 19:06:59 +0000145 : __lx(p)
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000146{
147}
148
149void
150__sp_mut::lock() _NOEXCEPT
151{
Eric Fiselierff4f2982016-09-28 22:08:13 +0000152 auto m = static_cast<__libcpp_mutex_t*>(__lx);
Howard Hinnant65f059b2012-07-30 17:13:21 +0000153 unsigned count = 0;
Eric Fiselierff4f2982016-09-28 22:08:13 +0000154 while (__libcpp_mutex_trylock(m) != 0)
Howard Hinnant65f059b2012-07-30 17:13:21 +0000155 {
156 if (++count > 16)
157 {
Eric Fiselierff4f2982016-09-28 22:08:13 +0000158 __libcpp_mutex_lock(m);
Howard Hinnant65f059b2012-07-30 17:13:21 +0000159 break;
160 }
161 this_thread::yield();
162 }
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000163}
164
165void
166__sp_mut::unlock() _NOEXCEPT
167{
Eric Fiselierff4f2982016-09-28 22:08:13 +0000168 __libcpp_mutex_unlock(static_cast<__libcpp_mutex_t*>(__lx));
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000169}
170
171__sp_mut&
172__get_sp_mut(const void* p)
173{
Eric Fiselierff4f2982016-09-28 22:08:13 +0000174 static __sp_mut muts[__sp_mut_count]
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000175 {
176 &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3],
177 &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7],
178 &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11],
179 &mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15]
180 };
181 return muts[hash<const void*>()(p) & (__sp_mut_count-1)];
182}
183
Eric Fiselierba9dccd2016-06-18 02:12:53 +0000184#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000185
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000186void
187declare_reachable(void*)
188{
189}
190
191void
192declare_no_pointers(char*, size_t)
193{
194}
195
196void
197undeclare_no_pointers(char*, size_t)
198{
199}
200
Eric Fiselier46a0c2e2017-01-05 01:15:42 +0000201#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
202pointer_safety get_pointer_safety() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000203{
204 return pointer_safety::relaxed;
205}
Eric Fiselier46a0c2e2017-01-05 01:15:42 +0000206#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207
208void*
209__undeclare_reachable(void* p)
210{
211 return p;
212}
213
214void*
215align(size_t alignment, size_t size, void*& ptr, size_t& space)
216{
217 void* r = nullptr;
218 if (size <= space)
219 {
220 char* p1 = static_cast<char*>(ptr);
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000221 char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment);
Howard Hinnantec3773c2011-12-01 20:21:04 +0000222 size_t d = static_cast<size_t>(p2 - p1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223 if (d <= space - size)
224 {
225 r = p2;
226 ptr = r;
227 space -= d;
228 }
229 }
230 return r;
231}
232
233_LIBCPP_END_NAMESPACE_STD