2 * Copyright 2017-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 class shared_ptr_internals {
25 template <typename T, typename... Args>
26 static std::shared_ptr<T> make_ptr(Args&&... args) {
27 return std::make_shared<T>(std::forward<Args...>(args...));
29 typedef std::__shared_count<std::_S_atomic> shared_count;
30 typedef std::_Sp_counted_base<std::_S_atomic> counted_base;
32 using CountedPtr = std::shared_ptr<T>;
34 static counted_base* get_counted_base(const std::shared_ptr<T>& bar) {
35 // reinterpret_pointer_cast<const void>
36 // Not quite C++ legal, but explicit template instantiation access to
37 // private members requires full type name (i.e. shared_ptr<const void>, not
39 const std::shared_ptr<const void>& ptr(
40 reinterpret_cast<const std::shared_ptr<const void>&>(bar));
41 return (ptr.*fieldPtr(access_shared_ptr{})).*fieldPtr(access_base{});
44 static void inc_shared_count(counted_base* base, long count) {
45 __gnu_cxx::__atomic_add_dispatch(
46 &(base->*fieldPtr(access_use_count{})), count);
50 static void release_shared(counted_base* base, long count) {
51 // If count == 1, this is equivalent to base->_M_release()
52 if (__gnu_cxx::__exchange_and_add_dispatch(
53 &(base->*fieldPtr(access_use_count{})), -count) == count) {
56 if (__gnu_cxx::__exchange_and_add_dispatch(
57 &(base->*fieldPtr(access_weak_count{})), -1) == 1) {
64 static T* get_shared_ptr(counted_base* base) {
65 // See if this was a make_shared allocation
66 auto inplace = base->_M_get_deleter(typeid(std::_Sp_make_shared_tag));
70 // Could also be a _Sp_counted_deleter, but the layout is the same
71 using derived_type = std::_Sp_counted_ptr<const void*, std::_S_atomic>;
72 auto ptr = reinterpret_cast<derived_type*>(base);
73 return (T*)(ptr->*fieldPtr(access_counted_ptr_ptr{}));
77 static T* release_ptr(std::shared_ptr<T>& p) {
79 std::shared_ptr<const void>& ptr(
80 reinterpret_cast<std::shared_ptr<const void>&>(p));
81 ptr.*fieldPtr(access_shared_ptr_ptr{}) = nullptr;
82 (ptr.*fieldPtr(access_refcount{})).*fieldPtr(access_base{}) = nullptr;
87 static std::shared_ptr<T> get_shared_ptr_from_counted_base(
93 std::shared_ptr<const void> newp;
95 inc_shared_count(base, 1);
97 newp.*fieldPtr(access_shared_ptr_ptr{}) =
98 get_shared_ptr<const void>(base); // _M_ptr
99 (newp.*fieldPtr(access_refcount{})).*fieldPtr(access_base{}) = base;
100 // reinterpret_pointer_cast<T>
101 auto res = reinterpret_cast<std::shared_ptr<T>*>(&newp);
102 return std::move(*res);
106 /* Accessors for private members using explicit template instantiation */
107 struct access_shared_ptr {
108 typedef shared_count std::__shared_ptr<const void, std::_S_atomic>::*type;
109 friend type fieldPtr(access_shared_ptr);
113 typedef counted_base* shared_count::*type;
114 friend type fieldPtr(access_base);
117 struct access_use_count {
118 typedef _Atomic_word counted_base::*type;
119 friend type fieldPtr(access_use_count);
122 struct access_weak_count {
123 typedef _Atomic_word counted_base::*type;
124 friend type fieldPtr(access_weak_count);
127 struct access_counted_ptr_ptr {
128 typedef const void* std::_Sp_counted_ptr<const void*, std::_S_atomic>::*
130 friend type fieldPtr(access_counted_ptr_ptr);
133 struct access_shared_ptr_ptr {
134 typedef const void* std::__shared_ptr<const void, std::_S_atomic>::*type;
135 friend type fieldPtr(access_shared_ptr_ptr);
138 struct access_refcount {
139 typedef shared_count std::__shared_ptr<const void, std::_S_atomic>::*type;
140 friend type fieldPtr(access_refcount);
143 template <typename Tag, typename Tag::type M>
145 friend typename Tag::type fieldPtr(Tag) {
151 template struct shared_ptr_internals::Rob<
152 shared_ptr_internals::access_shared_ptr,
153 &std::__shared_ptr<const void, std::_S_atomic>::_M_refcount>;
154 template struct shared_ptr_internals::Rob<
155 shared_ptr_internals::access_base,
156 &shared_ptr_internals::shared_count::_M_pi>;
157 template struct shared_ptr_internals::Rob<
158 shared_ptr_internals::access_use_count,
159 &shared_ptr_internals::counted_base::_M_use_count>;
160 template struct shared_ptr_internals::Rob<
161 shared_ptr_internals::access_weak_count,
162 &shared_ptr_internals::counted_base::_M_weak_count>;
163 template struct shared_ptr_internals::Rob<
164 shared_ptr_internals::access_counted_ptr_ptr,
165 &std::_Sp_counted_ptr<const void*, std::_S_atomic>::_M_ptr>;
166 template struct shared_ptr_internals::Rob<
167 shared_ptr_internals::access_shared_ptr_ptr,
168 &std::__shared_ptr<const void, std::_S_atomic>::_M_ptr>;
169 template struct shared_ptr_internals::Rob<
170 shared_ptr_internals::access_refcount,
171 &std::__shared_ptr<const void, std::_S_atomic>::_M_refcount>;
173 } // namespace detail