xref: /freebsd/contrib/llvm-project/libcxx/src/memory.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
9*81ad6265SDimitry Andric #include <__config>
10*81ad6265SDimitry Andric #ifdef _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS
11*81ad6265SDimitry Andric #   define _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS
12*81ad6265SDimitry Andric #endif
13*81ad6265SDimitry Andric 
14*81ad6265SDimitry Andric #include <memory>
15*81ad6265SDimitry Andric 
160b57cec5SDimitry Andric #ifndef _LIBCPP_HAS_NO_THREADS
17*81ad6265SDimitry Andric #  include <mutex>
18*81ad6265SDimitry Andric #  include <thread>
19480093f4SDimitry Andric #  if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
200b57cec5SDimitry Andric #    pragma comment(lib, "pthread")
210b57cec5SDimitry Andric #  endif
220b57cec5SDimitry Andric #endif
23*81ad6265SDimitry Andric 
240b57cec5SDimitry Andric #include "include/atomic_support.h"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric const allocator_arg_t allocator_arg = allocator_arg_t();
290b57cec5SDimitry Andric 
30fe6060f1SDimitry Andric bad_weak_ptr::~bad_weak_ptr() noexcept {}
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric const char*
33fe6060f1SDimitry Andric bad_weak_ptr::what() const noexcept
340b57cec5SDimitry Andric {
350b57cec5SDimitry Andric     return "bad_weak_ptr";
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric __shared_count::~__shared_count()
390b57cec5SDimitry Andric {
400b57cec5SDimitry Andric }
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric __shared_weak_count::~__shared_weak_count()
430b57cec5SDimitry Andric {
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric 
46*81ad6265SDimitry Andric #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
470b57cec5SDimitry Andric void
48fe6060f1SDimitry Andric __shared_count::__add_shared() noexcept
490b57cec5SDimitry Andric {
500b57cec5SDimitry Andric     __libcpp_atomic_refcount_increment(__shared_owners_);
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric bool
54fe6060f1SDimitry Andric __shared_count::__release_shared() noexcept
550b57cec5SDimitry Andric {
560b57cec5SDimitry Andric     if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1)
570b57cec5SDimitry Andric     {
580b57cec5SDimitry Andric         __on_zero_shared();
590b57cec5SDimitry Andric         return true;
600b57cec5SDimitry Andric     }
610b57cec5SDimitry Andric     return false;
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric void
65fe6060f1SDimitry Andric __shared_weak_count::__add_shared() noexcept
660b57cec5SDimitry Andric {
670b57cec5SDimitry Andric     __shared_count::__add_shared();
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric void
71fe6060f1SDimitry Andric __shared_weak_count::__add_weak() noexcept
720b57cec5SDimitry Andric {
730b57cec5SDimitry Andric     __libcpp_atomic_refcount_increment(__shared_weak_owners_);
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric void
77fe6060f1SDimitry Andric __shared_weak_count::__release_shared() noexcept
780b57cec5SDimitry Andric {
790b57cec5SDimitry Andric     if (__shared_count::__release_shared())
800b57cec5SDimitry Andric         __release_weak();
810b57cec5SDimitry Andric }
82*81ad6265SDimitry Andric #endif // _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric void
85fe6060f1SDimitry Andric __shared_weak_count::__release_weak() noexcept
860b57cec5SDimitry Andric {
870b57cec5SDimitry Andric     // NOTE: The acquire load here is an optimization of the very
880b57cec5SDimitry Andric     // common case where a shared pointer is being destructed while
890b57cec5SDimitry Andric     // having no other contended references.
900b57cec5SDimitry Andric     //
910b57cec5SDimitry Andric     // BENEFIT: We avoid expensive atomic stores like XADD and STREX
920b57cec5SDimitry Andric     // in a common case.  Those instructions are slow and do nasty
930b57cec5SDimitry Andric     // things to caches.
940b57cec5SDimitry Andric     //
950b57cec5SDimitry Andric     // IS THIS SAFE?  Yes.  During weak destruction, if we see that we
960b57cec5SDimitry Andric     // are the last reference, we know that no-one else is accessing
970b57cec5SDimitry Andric     // us. If someone were accessing us, then they would be doing so
980b57cec5SDimitry Andric     // while the last shared / weak_ptr was being destructed, and
990b57cec5SDimitry Andric     // that's undefined anyway.
1000b57cec5SDimitry Andric     //
1010b57cec5SDimitry Andric     // If we see anything other than a 0, then we have possible
1020b57cec5SDimitry Andric     // contention, and need to use an atomicrmw primitive.
1030b57cec5SDimitry Andric     // The same arguments don't apply for increment, where it is legal
1040b57cec5SDimitry Andric     // (though inadvisable) to share shared_ptr references between
1050b57cec5SDimitry Andric     // threads, and have them all get copied at once.  The argument
1060b57cec5SDimitry Andric     // also doesn't apply for __release_shared, because an outstanding
1070b57cec5SDimitry Andric     // weak_ptr::lock() could read / modify the shared count.
1080b57cec5SDimitry Andric     if (__libcpp_atomic_load(&__shared_weak_owners_, _AO_Acquire) == 0)
1090b57cec5SDimitry Andric     {
1100b57cec5SDimitry Andric         // no need to do this store, because we are about
1110b57cec5SDimitry Andric         // to destroy everything.
1120b57cec5SDimitry Andric         //__libcpp_atomic_store(&__shared_weak_owners_, -1, _AO_Release);
1130b57cec5SDimitry Andric         __on_zero_shared_weak();
1140b57cec5SDimitry Andric     }
1150b57cec5SDimitry Andric     else if (__libcpp_atomic_refcount_decrement(__shared_weak_owners_) == -1)
1160b57cec5SDimitry Andric         __on_zero_shared_weak();
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric __shared_weak_count*
120fe6060f1SDimitry Andric __shared_weak_count::lock() noexcept
1210b57cec5SDimitry Andric {
1220b57cec5SDimitry Andric     long object_owners = __libcpp_atomic_load(&__shared_owners_);
1230b57cec5SDimitry Andric     while (object_owners != -1)
1240b57cec5SDimitry Andric     {
1250b57cec5SDimitry Andric         if (__libcpp_atomic_compare_exchange(&__shared_owners_,
1260b57cec5SDimitry Andric                                              &object_owners,
1270b57cec5SDimitry Andric                                              object_owners+1))
1280b57cec5SDimitry Andric             return this;
1290b57cec5SDimitry Andric     }
1300b57cec5SDimitry Andric     return nullptr;
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric const void*
134fe6060f1SDimitry Andric __shared_weak_count::__get_deleter(const type_info&) const noexcept
1350b57cec5SDimitry Andric {
1360b57cec5SDimitry Andric     return nullptr;
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric 
139349cc55cSDimitry Andric #if !defined(_LIBCPP_HAS_NO_THREADS)
1400b57cec5SDimitry Andric 
141*81ad6265SDimitry Andric static constexpr std::size_t __sp_mut_count = 32;
142*81ad6265SDimitry Andric static constinit __libcpp_mutex_t mut_back[__sp_mut_count] =
1430b57cec5SDimitry Andric {
1440b57cec5SDimitry Andric     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
1450b57cec5SDimitry Andric     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
1460b57cec5SDimitry Andric     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
147*81ad6265SDimitry Andric     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
148*81ad6265SDimitry Andric     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
149*81ad6265SDimitry Andric     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
150*81ad6265SDimitry Andric     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
1510b57cec5SDimitry Andric     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER
1520b57cec5SDimitry Andric };
1530b57cec5SDimitry Andric 
154fe6060f1SDimitry Andric _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) noexcept
1550b57cec5SDimitry Andric    : __lx(p)
1560b57cec5SDimitry Andric {
1570b57cec5SDimitry Andric }
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric void
160fe6060f1SDimitry Andric __sp_mut::lock() noexcept
1610b57cec5SDimitry Andric {
1620b57cec5SDimitry Andric     auto m = static_cast<__libcpp_mutex_t*>(__lx);
1630b57cec5SDimitry Andric     __libcpp_mutex_lock(m);
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric void
167fe6060f1SDimitry Andric __sp_mut::unlock() noexcept
1680b57cec5SDimitry Andric {
1690b57cec5SDimitry Andric     __libcpp_mutex_unlock(static_cast<__libcpp_mutex_t*>(__lx));
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric __sp_mut&
1730b57cec5SDimitry Andric __get_sp_mut(const void* p)
1740b57cec5SDimitry Andric {
175*81ad6265SDimitry Andric     static constinit __sp_mut muts[__sp_mut_count] = {
1760b57cec5SDimitry Andric         &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3],
1770b57cec5SDimitry Andric         &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7],
1780b57cec5SDimitry Andric         &mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11],
179*81ad6265SDimitry Andric         &mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15],
180*81ad6265SDimitry Andric         &mut_back[16], &mut_back[17], &mut_back[18], &mut_back[19],
181*81ad6265SDimitry Andric         &mut_back[20], &mut_back[21], &mut_back[22], &mut_back[23],
182*81ad6265SDimitry Andric         &mut_back[24], &mut_back[25], &mut_back[26], &mut_back[27],
183*81ad6265SDimitry Andric         &mut_back[28], &mut_back[29], &mut_back[30], &mut_back[31]
1840b57cec5SDimitry Andric     };
1850b57cec5SDimitry Andric     return muts[hash<const void*>()(p) & (__sp_mut_count-1)];
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric 
188349cc55cSDimitry Andric #endif // !defined(_LIBCPP_HAS_NO_THREADS)
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric void*
1910b57cec5SDimitry Andric align(size_t alignment, size_t size, void*& ptr, size_t& space)
1920b57cec5SDimitry Andric {
1930b57cec5SDimitry Andric     void* r = nullptr;
1940b57cec5SDimitry Andric     if (size <= space)
1950b57cec5SDimitry Andric     {
1960b57cec5SDimitry Andric         char* p1 = static_cast<char*>(ptr);
1970b57cec5SDimitry Andric         char* p2 = reinterpret_cast<char*>(reinterpret_cast<size_t>(p1 + (alignment - 1)) & -alignment);
1980b57cec5SDimitry Andric         size_t d = static_cast<size_t>(p2 - p1);
1990b57cec5SDimitry Andric         if (d <= space - size)
2000b57cec5SDimitry Andric         {
2010b57cec5SDimitry Andric             r = p2;
2020b57cec5SDimitry Andric             ptr = r;
2030b57cec5SDimitry Andric             space -= d;
2040b57cec5SDimitry Andric         }
2050b57cec5SDimitry Andric     }
2060b57cec5SDimitry Andric     return r;
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
210