31550514b7ce0c1be38f47db0aae7c2ac154ec31
[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 private:
75 T* get() const {
76 return this->std::unique_ptr<T, Deleter>::get();
77 }
78 };
79
80 template <typename T>
81 class ref {
82 public:
83 ref() : ptr_(DeletedSentinel()) {}
84
85 explicit ref(owned<T>& o) : ptr_(&o) {
86 ptr_->get_deleter().AddRef(this);
87 }
88
89 ref(const ref<T>& o) {
90 *this = o;
91 }
92
93 ref<T>& operator=(const ref<T>& o) {
94 ptr_ = o.ptr_;
95 if (!IsDeleted()) {
96 ptr_->get_deleter().AddRef(this);
97 }
98 return *this;
99 }
100
101 ~ref() {
102 if (!IsDeleted()) {
103 ptr_->get_deleter().RemoveRef(this);
104 MarkDeleted();
105 }
106 }
107
108 T* operator->() const {
109 CheckDeleted();
110 return ptr_->operator->();
111 }
112
113 protected:
114 friend class internal::OwnedPtrDeleter<T>;
115
116 void MarkDeleted() {
117 ptr_ = DeletedSentinel();
118 }
119
120 private:
121 void CheckDeleted() const {
122 if (IsDeleted()) {
123 internal::RaiseUseAfterFree("attempt to access deleted pointer");
124 }
125 }
126
127 bool IsDeleted() const {
128 return ptr_ == DeletedSentinel();
129 }
130
131 inline static owned<T>* DeletedSentinel() {
132 return reinterpret_cast<owned<T>*>(std::numeric_limits<uintptr_t>::max());
133 }
134
135 owned<T>* ptr_;
136 };
137
138 #else
139
140 template <typename T>
141 using owned = std::unique_ptr<T>;
142
143 template <typename T>
144 using ref = T*;
145
146 #endif
147
148 } // namespace zc