blob: 369f97ffbb583f63e230df78461c6d5f5c5ed64d [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
20namespace
21{
22
Eric Fiselierc6e46692015-07-07 00:27:16 +000023// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
24// should be sufficient for thread safety.
25// See https://llvm.org/bugs/show_bug.cgi?id=22803
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026template <class T>
Howard Hinnantadff4892010-05-24 17:49:41 +000027inline T
Howard Hinnant1694d232011-05-28 14:41:13 +000028increment(T& t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000029{
Eric Fiselierc6e46692015-07-07 00:27:16 +000030 return __libcpp_atomic_add(&t, 1, _AO_Relaxed);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031}
32
33template <class T>
Howard Hinnantadff4892010-05-24 17:49:41 +000034inline T
Howard Hinnant1694d232011-05-28 14:41:13 +000035decrement(T& t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000036{
Eric Fiselierc6e46692015-07-07 00:27:16 +000037 return __libcpp_atomic_add(&t, -1, _AO_Acq_Rel);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000038}
39
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040} // namespace
41
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042const allocator_arg_t allocator_arg = allocator_arg_t();
43
Howard Hinnant1694d232011-05-28 14:41:13 +000044bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045
46const char*
Howard Hinnant1694d232011-05-28 14:41:13 +000047bad_weak_ptr::what() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048{
49 return "bad_weak_ptr";
50}
51
52__shared_count::~__shared_count()
53{
54}
55
56void
Howard Hinnant1694d232011-05-28 14:41:13 +000057__shared_count::__add_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000058{
59 increment(__shared_owners_);
60}
61
Howard Hinnant28dbbe02010-11-16 21:33:17 +000062bool
Howard Hinnant1694d232011-05-28 14:41:13 +000063__shared_count::__release_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000064{
65 if (decrement(__shared_owners_) == -1)
Howard Hinnant28dbbe02010-11-16 21:33:17 +000066 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000067 __on_zero_shared();
Howard Hinnant28dbbe02010-11-16 21:33:17 +000068 return true;
69 }
70 return false;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071}
72
73__shared_weak_count::~__shared_weak_count()
74{
75}
76
77void
Howard Hinnant1694d232011-05-28 14:41:13 +000078__shared_weak_count::__add_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079{
80 __shared_count::__add_shared();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000081}
82
83void
Howard Hinnant1694d232011-05-28 14:41:13 +000084__shared_weak_count::__add_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000085{
86 increment(__shared_weak_owners_);
87}
88
89void
Howard Hinnant1694d232011-05-28 14:41:13 +000090__shared_weak_count::__release_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000091{
Howard Hinnant28dbbe02010-11-16 21:33:17 +000092 if (__shared_count::__release_shared())
93 __release_weak();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000094}
95
96void
Howard Hinnant1694d232011-05-28 14:41:13 +000097__shared_weak_count::__release_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098{
99 if (decrement(__shared_weak_owners_) == -1)
100 __on_zero_shared_weak();
101}
102
103__shared_weak_count*
Howard Hinnant1694d232011-05-28 14:41:13 +0000104__shared_weak_count::lock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000105{
Eric Fiselierc6e46692015-07-07 00:27:16 +0000106 long object_owners = __libcpp_atomic_load(&__shared_owners_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000107 while (object_owners != -1)
108 {
Eric Fiselierc6e46692015-07-07 00:27:16 +0000109 if (__libcpp_atomic_compare_exchange(&__shared_owners_,
110 &object_owners,
111 object_owners+1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000112 return this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000113 }
114 return 0;
115}
116
Eric Fiselier01f6a142014-12-12 02:36:23 +0000117#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnantd4444702010-08-11 17:04:31 +0000118
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119const void*
Howard Hinnant1694d232011-05-28 14:41:13 +0000120__shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000121{
122 return 0;
123}
124
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000125#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000126
Jonathan Roelofsbaed05d2014-09-05 20:28:44 +0000127#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant96c60b42012-08-19 15:13:16 +0000128
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000129static const std::size_t __sp_mut_count = 16;
Howard Hinnante33c2d12013-03-16 00:17:53 +0000130static pthread_mutex_t mut_back_imp[__sp_mut_count] =
131{
132 PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
133 PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
134 PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
135 PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER
136};
137
138static mutex* mut_back = reinterpret_cast<std::mutex*>(mut_back_imp);
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000139
140_LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT
Howard Hinnant9c0df142012-10-30 19:06:59 +0000141 : __lx(p)
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000142{
143}
144
145void
146__sp_mut::lock() _NOEXCEPT
147{
Howard Hinnant9c0df142012-10-30 19:06:59 +0000148 mutex& m = *static_cast<mutex*>(__lx);
Howard Hinnant65f059b2012-07-30 17:13:21 +0000149 unsigned count = 0;
150 while (!m.try_lock())
151 {
152 if (++count > 16)
153 {
154 m.lock();
155 break;
156 }
157 this_thread::yield();
158 }
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000159}
160
161void
162__sp_mut::unlock() _NOEXCEPT
163{
Howard Hinnant9c0df142012-10-30 19:06:59 +0000164 static_cast<mutex*>(__lx)->unlock();
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000165}
166
167__sp_mut&
168__get_sp_mut(const void* p)
169{
170 static __sp_mut muts[__sp_mut_count]
171 {
172 &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3],
173 &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7],
174 &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11],
175 &mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15]
176 };
177 return muts[hash<const void*>()(p) & (__sp_mut_count-1)];
178}
179
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000180#endif // __has_feature(cxx_atomic) && !_LIBCPP_HAS_NO_THREADS
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000181
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182void
183declare_reachable(void*)
184{
185}
186
187void
188declare_no_pointers(char*, size_t)
189{
190}
191
192void
193undeclare_no_pointers(char*, size_t)
194{
195}
196
197pointer_safety
Howard Hinnant1694d232011-05-28 14:41:13 +0000198get_pointer_safety() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199{
200 return pointer_safety::relaxed;
201}
202
203void*
204__undeclare_reachable(void* p)
205{
206 return p;
207}
208
209void*
210align(size_t alignment, size_t size, void*& ptr, size_t& space)
211{
212 void* r = nullptr;
213 if (size <= space)
214 {
215 char* p1 = static_cast<char*>(ptr);
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000216 char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment);
Howard Hinnantec3773c2011-12-01 20:21:04 +0000217 size_t d = static_cast<size_t>(p2 - p1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000218 if (d <= space - size)
219 {
220 r = p2;
221 ptr = r;
222 space -= d;
223 }
224 }
225 return r;
226}
227
228_LIBCPP_END_NAMESPACE_STD