1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/DumbPtrTraits.h>
29#include <wtf/Gigacage.h>
30
31namespace WTF {
32
33template<Gigacage::Kind passedKind, typename T, typename PtrTraits = DumbPtrTraits<T>>
34class CagedPtr {
35public:
36 static constexpr Gigacage::Kind kind = passedKind;
37
38 CagedPtr() : CagedPtr(nullptr) { }
39 CagedPtr(std::nullptr_t) : m_ptr(nullptr) { }
40
41 explicit CagedPtr(T* ptr)
42 : m_ptr(ptr)
43 {
44 }
45
46 T* get() const
47 {
48 ASSERT(m_ptr);
49 return Gigacage::caged(kind, PtrTraits::unwrap(m_ptr));
50 }
51
52 T* getMayBeNull() const
53 {
54 if (!m_ptr)
55 return nullptr;
56 return get();
57 }
58
59 CagedPtr& operator=(T* ptr)
60 {
61 m_ptr = ptr;
62 return *this;
63 }
64
65 CagedPtr& operator=(T*&& ptr)
66 {
67 m_ptr = WTFMove(ptr);
68 return *this;
69 }
70
71 bool operator==(const CagedPtr& other) const
72 {
73 return getMayBeNull() == other.getMayBeNull();
74 }
75
76 bool operator!=(const CagedPtr& other) const
77 {
78 return !(*this == other);
79 }
80
81 explicit operator bool() const
82 {
83 return *this != CagedPtr();
84 }
85
86 T& operator*() const { return *get(); }
87 T* operator->() const { return get(); }
88
89 template<typename IndexType>
90 T& operator[](IndexType index) const { return get()[index]; }
91
92protected:
93 typename PtrTraits::StorageType m_ptr;
94};
95
96template<Gigacage::Kind passedKind, typename PtrTraits>
97class CagedPtr<passedKind, void, PtrTraits> {
98public:
99 static constexpr Gigacage::Kind kind = passedKind;
100
101 CagedPtr() : CagedPtr(nullptr) { }
102 CagedPtr(std::nullptr_t) : m_ptr(nullptr) { }
103
104 explicit CagedPtr(void* ptr)
105 : m_ptr(ptr)
106 {
107 }
108
109 void* get() const
110 {
111 ASSERT(m_ptr);
112 return Gigacage::caged(kind, PtrTraits::unwrap(m_ptr));
113 }
114
115 void* getMayBeNull() const
116 {
117 if (!m_ptr)
118 return nullptr;
119 return get();
120 }
121
122 CagedPtr& operator=(void* ptr)
123 {
124 m_ptr = ptr;
125 return *this;
126 }
127
128 bool operator==(const CagedPtr& other) const
129 {
130 return getMayBeNull() == other.getMayBeNull();
131 }
132
133 bool operator!=(const CagedPtr& other) const
134 {
135 return !(*this == other);
136 }
137
138 explicit operator bool() const
139 {
140 return *this != CagedPtr();
141 }
142
143protected:
144 typename PtrTraits::StorageType m_ptr;
145};
146
147} // namespace WTF
148
149using WTF::CagedPtr;
150
151