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.
18 struct counted_shared_tag {};
19 template <template <typename> class Atom = std::atomic>
20 struct intrusive_shared_count {
21 intrusive_shared_count() {
24 void add_ref(uint64_t count = 1) {
25 counts.fetch_add(count);
28 uint64_t release_ref(uint64_t count = 1) {
29 return counts.fetch_sub(count);
31 Atom<uint64_t> counts;
34 template <template <typename> class Atom = std::atomic>
35 struct counted_ptr_base {
37 static intrusive_shared_count<Atom>* getRef(void* pt) {
39 p -= sizeof(intrusive_shared_count<Atom>);
40 return (intrusive_shared_count<Atom>*)p;
44 // basically shared_ptr, but only supports make_counted, and provides
45 // access to add_ref / release_ref with a count. Alias not supported.
46 template <typename T, template <typename> class Atom = std::atomic>
47 class counted_ptr : public counted_ptr_base<Atom> {
50 counted_ptr() : p_(nullptr) {}
51 counted_ptr(counted_shared_tag, T* p) : p_(p) {
53 counted_ptr_base<Atom>::getRef(p_)->add_ref();
57 counted_ptr(const counted_ptr& o) : p_(o.p_) {
59 counted_ptr_base<Atom>::getRef(p_)->add_ref();
62 counted_ptr& operator=(const counted_ptr& o) {
63 if (p_ && counted_ptr_base<Atom>::getRef(p_)->release_ref() == 1) {
65 free(counted_ptr_base<Atom>::getRef(p_));
69 counted_ptr_base<Atom>::getRef(p_)->add_ref();
73 explicit counted_ptr(T* p) : p_(p) {
77 if (p_ && counted_ptr_base<Atom>::getRef(p_)->release_ref() == 1) {
79 free(counted_ptr_base<Atom>::getRef(p_));
82 typename std::add_lvalue_reference<T>::type operator*() const {
89 T* operator->() const {
92 explicit operator bool() const {
93 return p_ == nullptr ? false : true;
95 bool operator==(const counted_ptr<T, Atom>& p) const {
96 return get() == p.get();
101 template <typename> class Atom = std::atomic,
104 counted_ptr<T, Atom> make_counted(Args&&... args) {
105 char* mem = (char*)malloc(sizeof(T) + sizeof(intrusive_shared_count<Atom>));
107 throw std::bad_alloc();
109 new (mem) intrusive_shared_count<Atom>();
110 T* ptr = (T*)(mem + sizeof(intrusive_shared_count<Atom>));
111 new (ptr) T(std::forward<Args>(args)...);
112 return counted_ptr<T, Atom>(counted_shared_tag(), ptr);
115 template <template <typename> class Atom = std::atomic>
116 class counted_ptr_internals : public counted_ptr_base<Atom> {
118 template <typename T, typename... Args>
119 static counted_ptr<T, Atom> make_ptr(Args&&... args) {
120 return make_counted<Atom, T>(std::forward<Args...>(args...));
122 template <typename T>
123 using CountedPtr = counted_ptr<T, Atom>;
124 typedef void counted_base;
126 template <typename T>
127 static counted_base* get_counted_base(const counted_ptr<T, Atom>& bar) {
131 template <typename T>
132 static T* get_shared_ptr(counted_base* base) {
136 template <typename T>
137 static T* release_ptr(counted_ptr<T, Atom>& p) {
143 template <typename T>
144 static counted_ptr<T, Atom> get_shared_ptr_from_counted_base(
147 auto res = counted_ptr<T, Atom>(counted_shared_tag(), (T*)(base));
149 release_shared<T>(base, 1);
154 static void inc_shared_count(counted_base* base, int64_t count) {
155 counted_ptr_base<Atom>::getRef(base)->add_ref(count);
158 template <typename T>
159 static void release_shared(counted_base* base, uint64_t count) {
160 if (count == counted_ptr_base<Atom>::getRef(base)->release_ref(count)) {
162 free(counted_ptr_base<Atom>::getRef(base));