2 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include <wtf/DumbPtrTraits.h>
29 #include <wtf/Gigacage.h>
30 #include <wtf/PtrTag.h>
36 constexpr bool tagCagedPtr = true;
38 template<Gigacage::Kind passedKind, typename T, bool shouldTag = false, typename PtrTraits = DumbPtrTraits<T>>
41 static constexpr Gigacage::Kind kind = passedKind;
43 CagedPtr() : CagedPtr(nullptr) { }
44 CagedPtr(std::nullptr_t)
45 : m_ptr(shouldTag ? tagArrayPtr<T>(nullptr, 0) : nullptr)
48 CagedPtr(T* ptr, unsigned size)
49 : m_ptr(shouldTag ? tagArrayPtr(ptr, size) : ptr)
52 T* get(unsigned size) const
55 T* ptr = PtrTraits::unwrap(m_ptr);
56 T* cagedPtr = Gigacage::caged(kind, ptr);
57 T* untaggedPtr = shouldTag ? untagArrayPtr(mergePointers(ptr, cagedPtr), size) : cagedPtr;
61 T* getMayBeNull(unsigned size) const
63 T* ptr = PtrTraits::unwrap(m_ptr);
64 T* cagedPtr = Gigacage::cagedMayBeNull(kind, ptr);
65 T* untaggedPtr = shouldTag ? untagArrayPtr(mergePointers(ptr, cagedPtr), size) : cagedPtr;
71 T* ptr = PtrTraits::unwrap(m_ptr);
72 ptr = shouldTag ? removeArrayPtrTag(ptr) : ptr;
73 return Gigacage::cagedMayBeNull(kind, ptr);
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]; }
81 void recage(unsigned oldSize, unsigned newSize)
83 auto ptr = get(oldSize);
84 ASSERT(ptr == getUnsafe());
85 *this = CagedPtr(ptr, newSize);
88 CagedPtr(CagedPtr& other)
93 CagedPtr& operator=(const CagedPtr& ptr)
99 CagedPtr(CagedPtr&& other)
100 : m_ptr(PtrTraits::exchange(other.m_ptr, nullptr))
104 CagedPtr& operator=(CagedPtr&& ptr)
106 m_ptr = PtrTraits::exchange(ptr.m_ptr, nullptr);
110 bool operator==(const CagedPtr& other) const
112 bool result = m_ptr == other.m_ptr;
113 ASSERT(result == (getUnsafe() == other.getUnsafe()));
117 bool operator!=(const CagedPtr& other) const
119 return !(*this == other);
122 explicit operator bool() const
124 return getUnsafe() != nullptr;
128 static inline T* mergePointers(T* sourcePtr, T* cagedPtr)
131 constexpr unsigned numberOfPACBits = 25;
132 constexpr uintptr_t mask = (1ull << ((sizeof(T*) * CHAR_BIT) - numberOfPACBits)) - 1;
133 return reinterpret_cast<T*>((reinterpret_cast<uintptr_t>(sourcePtr) & ~mask) | (reinterpret_cast<uintptr_t>(cagedPtr) & mask));
135 UNUSED_PARAM(sourcePtr);
140 typename PtrTraits::StorageType m_ptr;
146 using WTF::tagCagedPtr;