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#include <wtf/PtrTag.h>
31
32namespace WTF {
33
34constexpr bool tagCagedPtr = true;
35
36template<Gigacage::Kind passedKind, typename T, bool shouldTag = false, typename PtrTraits = DumbPtrTraits<T>>
37class CagedPtr {
38public:
39 static constexpr Gigacage::Kind kind = passedKind;
40
41 CagedPtr() : CagedPtr(nullptr) { }
42 CagedPtr(std::nullptr_t)
43 : m_ptr(shouldTag ? tagArrayPtr<T>(nullptr, 0) : nullptr)
44 { }
45
46 CagedPtr(T* ptr, unsigned size)
47 : m_ptr(shouldTag ? tagArrayPtr(ptr, size) : ptr)
48 { }
49
50
51 T* get(unsigned size) const
52 {
53 ASSERT(m_ptr);
54 T* ptr = PtrTraits::unwrap(m_ptr);
55 if (shouldTag)
56 ptr = untagArrayPtr(ptr, size);
57 return Gigacage::caged(kind, ptr);
58 }
59
60 T* getMayBeNull(unsigned size) const
61 {
62 T* ptr = PtrTraits::unwrap(m_ptr);
63 if (shouldTag)
64 ptr = untagArrayPtr(ptr, size);
65 return Gigacage::cagedMayBeNull(kind, ptr);
66 }
67
68 T* getUnsafe() const
69 {
70 T* ptr = PtrTraits::unwrap(m_ptr);
71 if (shouldTag)
72 ptr = removeArrayPtrTag(ptr);
73 return Gigacage::cagedMayBeNull(kind, ptr);
74 }
75
76 // We need the template here so that the type of U is deduced at usage time rather than class time. U should always be T.
77 template<typename U = T>
78 typename std::enable_if<!std::is_same<void, U>::value, T>::type&
79 /* T& */ at(unsigned index, unsigned size) const { return get(size)[index]; }
80
81 void recage(unsigned oldSize, unsigned newSize)
82 {
83 auto ptr = get(oldSize);
84 ASSERT(ptr == getUnsafe());
85 *this = CagedPtr(ptr, newSize);
86 }
87
88 CagedPtr(CagedPtr& other)
89 : m_ptr(other.m_ptr)
90 {
91 }
92
93 CagedPtr& operator=(const CagedPtr& ptr)
94 {
95 m_ptr = ptr.m_ptr;
96 return *this;
97 }
98
99 CagedPtr(CagedPtr&& other)
100 : m_ptr(PtrTraits::exchange(other.m_ptr, nullptr))
101 {
102 }
103
104 CagedPtr& operator=(CagedPtr&& ptr)
105 {
106 m_ptr = PtrTraits::exchange(ptr.m_ptr, nullptr);
107 return *this;
108 }
109
110 bool operator==(const CagedPtr& other) const
111 {
112 bool result = m_ptr == other.m_ptr;
113 ASSERT(result == (getUnsafe() == other.getUnsafe()));
114 return result;
115 }
116
117 bool operator!=(const CagedPtr& other) const
118 {
119 return !(*this == other);
120 }
121
122 explicit operator bool() const
123 {
124 return getUnsafe() != nullptr;
125 }
126
127protected:
128 typename PtrTraits::StorageType m_ptr;
129};
130
131} // namespace WTF
132
133using WTF::CagedPtr;
134using WTF::tagCagedPtr;
135
136