]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libc++/src/memory.cpp
Merge libc++ trunk r338150 (just before the 7.0.0 branch point), and
[FreeBSD/FreeBSD.git] / contrib / libc++ / src / memory.cpp
1 //===------------------------ memory.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 "memory"
11 #ifndef _LIBCPP_HAS_NO_THREADS
12 #include "mutex"
13 #include "thread"
14 #endif
15 #include "include/atomic_support.h"
16
17 _LIBCPP_BEGIN_NAMESPACE_STD
18
19 const allocator_arg_t allocator_arg = allocator_arg_t();
20
21 bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {}
22
23 const char*
24 bad_weak_ptr::what() const _NOEXCEPT
25 {
26     return "bad_weak_ptr";
27 }
28
29 __shared_count::~__shared_count()
30 {
31 }
32
33 __shared_weak_count::~__shared_weak_count()
34 {
35 }
36
37 #if defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
38 void
39 __shared_count::__add_shared() _NOEXCEPT
40 {
41     __libcpp_atomic_refcount_increment(__shared_owners_);
42 }
43
44 bool
45 __shared_count::__release_shared() _NOEXCEPT
46 {
47     if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1)
48     {
49         __on_zero_shared();
50         return true;
51     }
52     return false;
53 }
54
55 void
56 __shared_weak_count::__add_shared() _NOEXCEPT
57 {
58     __shared_count::__add_shared();
59 }
60
61 void
62 __shared_weak_count::__add_weak() _NOEXCEPT
63 {
64     __libcpp_atomic_refcount_increment(__shared_weak_owners_);
65 }
66
67 void
68 __shared_weak_count::__release_shared() _NOEXCEPT
69 {
70     if (__shared_count::__release_shared())
71         __release_weak();
72 }
73
74 #endif // _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS
75
76 void
77 __shared_weak_count::__release_weak() _NOEXCEPT
78 {
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.
100     if (__libcpp_atomic_load(&__shared_weak_owners_, _AO_Acquire) == 0)
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     }
107     else if (__libcpp_atomic_refcount_decrement(__shared_weak_owners_) == -1)
108         __on_zero_shared_weak();
109 }
110
111 __shared_weak_count*
112 __shared_weak_count::lock() _NOEXCEPT
113 {
114     long object_owners = __libcpp_atomic_load(&__shared_owners_);
115     while (object_owners != -1)
116     {
117         if (__libcpp_atomic_compare_exchange(&__shared_owners_,
118                                              &object_owners,
119                                              object_owners+1))
120             return this;
121     }
122     return nullptr;
123 }
124
125 #if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
126
127 const void*
128 __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT
129 {
130     return nullptr;
131 }
132
133 #endif  // _LIBCPP_NO_RTTI
134
135 #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
136
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] =
139 {
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
144 };
145
146 _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT
147    : __lx(p)
148 {
149 }
150
151 void
152 __sp_mut::lock() _NOEXCEPT
153 {
154     auto m = static_cast<__libcpp_mutex_t*>(__lx);
155     unsigned count = 0;
156     while (!__libcpp_mutex_trylock(m))
157     {
158         if (++count > 16)
159         {
160             __libcpp_mutex_lock(m);
161             break;
162         }
163         this_thread::yield();
164     }
165 }
166
167 void
168 __sp_mut::unlock() _NOEXCEPT
169 {
170     __libcpp_mutex_unlock(static_cast<__libcpp_mutex_t*>(__lx));
171 }
172
173 __sp_mut&
174 __get_sp_mut(const void* p)
175 {
176     static __sp_mut muts[__sp_mut_count]
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
186 #endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
187
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
203 #if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
204 pointer_safety get_pointer_safety() _NOEXCEPT
205 {
206     return pointer_safety::relaxed;
207 }
208 #endif
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);
223         char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment);
224         size_t d = static_cast<size_t>(p2 - p1);
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