File indexing completed on 2026-05-03 08:13:56
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___MEMORY_SHARED_COUNT_H
0010 #define _LIBCPP___MEMORY_SHARED_COUNT_H
0011
0012 #include <__config>
0013 #include <typeinfo>
0014
0015 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0016 # pragma GCC system_header
0017 #endif
0018
0019 _LIBCPP_BEGIN_NAMESPACE_STD
0020
0021
0022
0023
0024 #if (defined(__clang__) && __has_builtin(__atomic_add_fetch) && defined(__ATOMIC_RELAXED) && \
0025 defined(__ATOMIC_ACQ_REL)) || \
0026 defined(_LIBCPP_COMPILER_GCC)
0027 # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 1
0028 #else
0029 # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 0
0030 #endif
0031
0032 template <class _ValueType>
0033 inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_relaxed_load(_ValueType const* __value) {
0034 #if _LIBCPP_HAS_THREADS && defined(__ATOMIC_RELAXED) && \
0035 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
0036 return __atomic_load_n(__value, __ATOMIC_RELAXED);
0037 #else
0038 return *__value;
0039 #endif
0040 }
0041
0042 template <class _ValueType>
0043 inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_acquire_load(_ValueType const* __value) {
0044 #if _LIBCPP_HAS_THREADS && defined(__ATOMIC_ACQUIRE) && \
0045 (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
0046 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
0047 #else
0048 return *__value;
0049 #endif
0050 }
0051
0052 template <class _Tp>
0053 inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT {
0054 #if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
0055 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
0056 #else
0057 return __t += 1;
0058 #endif
0059 }
0060
0061 template <class _Tp>
0062 inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT {
0063 #if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
0064 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
0065 #else
0066 return __t -= 1;
0067 #endif
0068 }
0069
0070 class _LIBCPP_EXPORTED_FROM_ABI __shared_count {
0071 __shared_count(const __shared_count&);
0072 __shared_count& operator=(const __shared_count&);
0073
0074 protected:
0075 long __shared_owners_;
0076 virtual ~__shared_count();
0077
0078 private:
0079 virtual void __on_zero_shared() _NOEXCEPT = 0;
0080
0081 public:
0082 _LIBCPP_HIDE_FROM_ABI explicit __shared_count(long __refs = 0) _NOEXCEPT : __shared_owners_(__refs) {}
0083
0084 #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
0085 void __add_shared() noexcept;
0086 bool __release_shared() noexcept;
0087 #else
0088 _LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __libcpp_atomic_refcount_increment(__shared_owners_); }
0089 _LIBCPP_HIDE_FROM_ABI bool __release_shared() _NOEXCEPT {
0090 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
0091 __on_zero_shared();
0092 return true;
0093 }
0094 return false;
0095 }
0096 #endif
0097 _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __libcpp_relaxed_load(&__shared_owners_) + 1; }
0098 };
0099
0100 class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count : private __shared_count {
0101 long __shared_weak_owners_;
0102
0103 public:
0104 _LIBCPP_HIDE_FROM_ABI explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
0105 : __shared_count(__refs),
0106 __shared_weak_owners_(__refs) {}
0107
0108 protected:
0109 ~__shared_weak_count() override;
0110
0111 public:
0112 #if defined(_LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS)
0113 void __add_shared() noexcept;
0114 void __add_weak() noexcept;
0115 void __release_shared() noexcept;
0116 #else
0117 _LIBCPP_HIDE_FROM_ABI void __add_shared() _NOEXCEPT { __shared_count::__add_shared(); }
0118 _LIBCPP_HIDE_FROM_ABI void __add_weak() _NOEXCEPT { __libcpp_atomic_refcount_increment(__shared_weak_owners_); }
0119 _LIBCPP_HIDE_FROM_ABI void __release_shared() _NOEXCEPT {
0120 if (__shared_count::__release_shared())
0121 __release_weak();
0122 }
0123 #endif
0124 void __release_weak() _NOEXCEPT;
0125 _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __shared_count::use_count(); }
0126 __shared_weak_count* lock() _NOEXCEPT;
0127
0128 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
0129
0130 private:
0131 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
0132 };
0133
0134 _LIBCPP_END_NAMESPACE_STD
0135
0136 #endif