Reduce the size of ref<T> to a single pointer.
[zcpointer.git] / zcpointer.h
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <limits>
16 #include <memory>
17 #include <forward_list>
18
19 namespace zc {
20
21 #if defined(ZCPOINTER_TRACK_REFS) && ZCPOINTER_TRACK_REFS
22
23 template <typename T> class ref;
24
25 namespace internal {
26
27 template <typename T>
28 class OwnedPtrDeleter {
29 public:
30 OwnedPtrDeleter() {}
31 ~OwnedPtrDeleter() {}
32
33 OwnedPtrDeleter(OwnedPtrDeleter&& other) : refs_(std::move(other.refs_)) {
34 }
35
36 void operator()(T* t) const {
37 for (auto& ref : refs_) {
38 ref->MarkDeleted();
39 }
40 delete t;
41 }
42
43 protected:
44 friend class ref<T>;
45
46 void AddRef(ref<T>* ref) {
47 refs_.push_front(ref);
48 }
49
50 void RemoveRef(ref<T>* ref) {
51 refs_.remove(ref);
52 }
53
54 private:
55 std::forward_list<ref<T>*> refs_;
56 };
57
58 void RaiseUseAfterFree(const char* error) __attribute__((noreturn));
59
60 } // namespace internal
61
62 template <typename T>
63 class owned : public std::unique_ptr<T, internal::OwnedPtrDeleter<T>> {
64 private:
65 using Deleter = internal::OwnedPtrDeleter<T>;
66
67 public:
68 using std::unique_ptr<T, Deleter>::unique_ptr;
69
70 ref<T> get() {
71 return ref<T>(*this);
72 }
73
74 protected:
75 friend class ref<T>;
76
77 T* GetRawPointer() const {
78 return get();
79 }
80
81 private:
82 T* get() const {
83 return this->std::unique_ptr<T, Deleter>::get();
84 }
85 };
86
87 template <typename T>
88 class ref {
89 public:
90 ref() : ptr_(DeletedSentinel()) {}
91
92 explicit ref(owned<T>& o) : ptr_(&o) {
93 ptr_->get_deleter().AddRef(this);
94 }
95
96 ref(const ref<T>& o) {
97 *this = o;
98 }
99
100 ref<T>& operator=(const ref<T>& o) {
101 ptr_ = o.ptr_;
102 if (!IsDeleted()) {
103 ptr_->get_deleter().AddRef(this);
104 }
105 return *this;
106 }
107
108 ~ref() {
109 ptr_->get_deleter().RemoveRef(this);
110 MarkDeleted();
111 }
112
113 T* operator->() const {
114 CheckDeleted();
115 return ptr_->GetRawPointer();
116 }
117
118 protected:
119 friend class internal::OwnedPtrDeleter<T>;
120
121 void MarkDeleted() {
122 ptr_ = DeletedSentinel();
123 }
124
125 private:
126 void CheckDeleted() const {
127 if (IsDeleted()) {
128 internal::RaiseUseAfterFree("attempt to access deleted pointer");
129 }
130 }
131
132 bool IsDeleted() const {
133 return ptr_ == DeletedSentinel();
134 }
135
136 inline static owned<T>* DeletedSentinel() {
137 return reinterpret_cast<owned<T>*>(std::numeric_limits<uintptr_t>::max());
138 }
139
140 owned<T>* ptr_;
141 };
142
143 #else
144
145 template <typename T>
146 using owned = std::unique_ptr<T>;
147
148 template <typename T>
149 using ref = T*;
150
151 #endif
152
153 } // namespace zc