1 // Copyright 2016 Google Inc. All rights reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef ZCPOINTER_ZCPOINTER_H_
16 #define ZCPOINTER_ZCPOINTER_H_
20 #include <forward_list>
26 class UseAfterFreeError
: public std
::logic_error
{
28 using std
::logic_error
::logic_error
;
31 #if defined(ZCPOINTER_TRACK_REFS) && ZCPOINTER_TRACK_REFS
33 template <typename T
> class ref
;
37 enum class OwnershipBehavior
{
43 class OwnedPtrDeleter
{
45 OwnedPtrDeleter() : refs_(), behavior_(OwnershipBehavior
::DELETE_POINTER
) {}
48 explicit OwnedPtrDeleter(OwnershipBehavior behavior
)
53 OwnedPtrDeleter(OwnedPtrDeleter
&& other
)
54 : refs_(std
::move(other
.refs_
)),
55 behavior_(other
.behavior_
) {
58 void operator=(const OwnedPtrDeleter
& o
) {
60 behavior_
= o
.behavior_
;
63 void operator()(T
* t
) const {
64 for (auto& ref
: refs_
) {
67 if (behavior_
== OwnershipBehavior
::DELETE_POINTER
) {
75 void AddRef(ref
<T
>* ref
) {
76 refs_
.push_front(ref
);
79 void RemoveRef(ref
<T
>* ref
) {
84 std
::forward_list
<ref
<T
>*> refs_
;
85 OwnershipBehavior behavior_
;
88 void RaiseUseAfterFree(const char* error
) __attribute__((noreturn
));
90 } // namespace internal
93 class owned
: public std
::unique_ptr
<T
, internal
::OwnedPtrDeleter
<T
>> {
95 using Deleter
= internal
::OwnedPtrDeleter
<T
>;
98 using std
::unique_ptr
<T
, Deleter
>::unique_ptr
;
101 return ref
<T
>(*this);
106 return this->std
::unique_ptr
<T
, Deleter
>::get();
110 template <typename T
>
113 ref() : ptr_(nullptr) {}
115 ref(std
::nullptr_t
) : ref() {}
117 explicit ref(owned
<T
>& o
) : ptr_(nullptr) {
120 ptr_
->get_deleter().AddRef(this);
124 ref(const ref
<T
>& r
) {
128 ref
<T
>& operator=(const ref
<T
>& o
) {
130 if (ptr_
!= nullptr && !IsDeleted()) {
131 ptr_
->get_deleter().AddRef(this);
137 if (ptr_
!= nullptr && !IsDeleted()) {
138 ptr_
->get_deleter().RemoveRef(this);
143 T
* operator->() const {
145 return ptr_
->operator->();
148 bool operator==(const ref
<T
>& r
) const {
149 if (ptr_
== nullptr) {
150 return r
.ptr_
== nullptr;
152 return ptr_
== r
.ptr_
&& *ptr_
== *r
.ptr_
;
156 bool operator==(std
::nullptr_t
) const {
157 return ptr_
== nullptr;
160 bool operator!=(const ref
<T
>& r
) const {
161 return !(*this == r
);
165 friend class internal
::OwnedPtrDeleter
<T
>;
168 ptr_
= DeletedSentinel();
172 void CheckDeleted() const {
174 internal
::RaiseUseAfterFree("attempt to access deleted pointer");
178 bool IsDeleted() const {
179 return ptr_
== DeletedSentinel();
182 inline static owned
<T
>* DeletedSentinel() {
183 return reinterpret_cast<owned
<T
>*>(std
::numeric_limits
<uintptr_t>::max());
189 template <typename T
>
190 class member
: public T
{
199 owned
<T
> ptr_
= owned
<T
>(this,
200 internal
::OwnedPtrDeleter
<T
>(
201 internal
::OwnershipBehavior
::BORROW_POINTER
));
206 template <typename T
>
207 using owned
= std
::unique_ptr
<T
>;
209 template <typename T
>
212 template <typename T
>
219 #endif // ZCPOINTER_ZCPOINTER_H_