1 //===------------------------ memory.cpp ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define _LIBCPP_BUILDING_MEMORY
12 #ifndef _LIBCPP_HAS_NO_THREADS
16 #include "include/atomic_support.h"
18 _LIBCPP_BEGIN_NAMESPACE_STD
23 // 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
28 increment(T
& t
) _NOEXCEPT
30 return __libcpp_atomic_add(&t
, 1, _AO_Relaxed
);
35 decrement(T
& t
) _NOEXCEPT
37 return __libcpp_atomic_add(&t
, -1, _AO_Acq_Rel
);
42 const allocator_arg_t allocator_arg
= allocator_arg_t();
44 bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT
{}
47 bad_weak_ptr::what() const _NOEXCEPT
49 return "bad_weak_ptr";
52 __shared_count::~__shared_count()
57 __shared_count::__add_shared() _NOEXCEPT
59 increment(__shared_owners_
);
63 __shared_count::__release_shared() _NOEXCEPT
65 if (decrement(__shared_owners_
) == -1)
73 __shared_weak_count::~__shared_weak_count()
78 __shared_weak_count::__add_shared() _NOEXCEPT
80 __shared_count::__add_shared();
84 __shared_weak_count::__add_weak() _NOEXCEPT
86 increment(__shared_weak_owners_
);
90 __shared_weak_count::__release_shared() _NOEXCEPT
92 if (__shared_count::__release_shared())
97 __shared_weak_count::__release_weak() _NOEXCEPT
99 if (decrement(__shared_weak_owners_
) == -1)
100 __on_zero_shared_weak();
104 __shared_weak_count::lock() _NOEXCEPT
106 long object_owners
= __libcpp_atomic_load(&__shared_owners_
);
107 while (object_owners
!= -1)
109 if (__libcpp_atomic_compare_exchange(&__shared_owners_
,
117 #if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
120 __shared_weak_count::__get_deleter(const type_info
&) const _NOEXCEPT
125 #endif // _LIBCPP_NO_RTTI
127 #if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
129 static const std::size_t __sp_mut_count
= 16;
130 static pthread_mutex_t mut_back_imp
[__sp_mut_count
] =
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
138 static mutex
* mut_back
= reinterpret_cast<std::mutex
*>(mut_back_imp
);
140 _LIBCPP_CONSTEXPR
__sp_mut::__sp_mut(void* p
) _NOEXCEPT
146 __sp_mut::lock() _NOEXCEPT
148 mutex
& m
= *static_cast<mutex
*>(__lx
);
150 while (!m
.try_lock())
157 this_thread::yield();
162 __sp_mut::unlock() _NOEXCEPT
164 static_cast<mutex
*>(__lx
)->unlock();
168 __get_sp_mut(const void* p
)
170 static __sp_mut muts
[__sp_mut_count
]
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]
177 return muts
[hash
<const void*>()(p
) & (__sp_mut_count
-1)];
180 #endif // defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
183 declare_reachable(void*)
188 declare_no_pointers(char*, size_t)
193 undeclare_no_pointers(char*, size_t)
198 get_pointer_safety() _NOEXCEPT
200 return pointer_safety::relaxed
;
204 __undeclare_reachable(void* p
)
210 align(size_t alignment
, size_t size
, void*& ptr
, size_t& space
)
215 char* p1
= static_cast<char*>(ptr
);
216 char* p2
= reinterpret_cast<char*>(reinterpret_cast<size_t>(p1
+ (alignment
- 1)) & -alignment
);
217 size_t d
= static_cast<size_t>(p2
- p1
);
218 if (d
<= space
- size
)
228 _LIBCPP_END_NAMESPACE_STD