Bug 1831122 [wpt PR 39823] - Update wpt metadata, a=testonly
[gecko.git] / mfbt / RangedPtr.h
blob9aec59f9360c24fd46033a73e15aacfcb25a9803
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 * Implements a smart pointer asserted to remain within a range specified at
9 * construction.
12 #ifndef mozilla_RangedPtr_h
13 #define mozilla_RangedPtr_h
15 #include "mozilla/ArrayUtils.h"
16 #include "mozilla/Assertions.h"
17 #include "mozilla/Attributes.h"
19 #include <stdint.h>
20 #include <cstddef>
22 namespace mozilla {
25 * RangedPtr is a smart pointer restricted to an address range specified at
26 * creation. The pointer (and any smart pointers derived from it) must remain
27 * within the range [start, end] (inclusive of end to facilitate use as
28 * sentinels). Dereferencing or indexing into the pointer (or pointers derived
29 * from it) must remain within the range [start, end). All the standard pointer
30 * operators are defined on it; in debug builds these operations assert that the
31 * range specified at construction is respected.
33 * In theory passing a smart pointer instance as an argument can be slightly
34 * slower than passing a T* (due to ABI requirements for passing structs versus
35 * passing pointers), if the method being called isn't inlined. If you are in
36 * extremely performance-critical code, you may want to be careful using this
37 * smart pointer as an argument type.
39 * RangedPtr<T> intentionally does not implicitly convert to T*. Use get() to
40 * explicitly convert to T*. Keep in mind that the raw pointer of course won't
41 * implement bounds checking in debug builds.
43 template <typename T>
44 class RangedPtr {
45 template <typename U>
46 friend class RangedPtr;
48 T* mPtr;
50 #ifdef DEBUG
51 T* const mRangeStart;
52 T* const mRangeEnd;
53 #endif
55 void checkSanity() {
56 MOZ_ASSERT(mRangeStart <= mPtr);
57 MOZ_ASSERT(mPtr <= mRangeEnd);
60 /* Creates a new pointer for |aPtr|, restricted to this pointer's range. */
61 RangedPtr<T> create(T* aPtr) const {
62 #ifdef DEBUG
63 return RangedPtr<T>(aPtr, mRangeStart, mRangeEnd);
64 #else
65 return RangedPtr<T>(aPtr, nullptr, size_t(0));
66 #endif
69 uintptr_t asUintptr() const { return reinterpret_cast<uintptr_t>(mPtr); }
71 public:
72 RangedPtr(T* aPtr, T* aStart, T* aEnd)
73 : mPtr(aPtr)
74 #ifdef DEBUG
76 mRangeStart(aStart),
77 mRangeEnd(aEnd)
78 #endif
80 MOZ_ASSERT(mRangeStart <= mRangeEnd);
81 checkSanity();
83 RangedPtr(T* aPtr, T* aStart, size_t aLength)
84 : mPtr(aPtr)
85 #ifdef DEBUG
87 mRangeStart(aStart),
88 mRangeEnd(aStart + aLength)
89 #endif
91 MOZ_ASSERT(aLength <= size_t(-1) / sizeof(T));
92 MOZ_ASSERT(reinterpret_cast<uintptr_t>(mRangeStart) + aLength * sizeof(T) >=
93 reinterpret_cast<uintptr_t>(mRangeStart));
94 checkSanity();
97 /* Equivalent to RangedPtr(aPtr, aPtr, aLength). */
98 RangedPtr(T* aPtr, size_t aLength)
99 : mPtr(aPtr)
100 #ifdef DEBUG
102 mRangeStart(aPtr),
103 mRangeEnd(aPtr + aLength)
104 #endif
106 MOZ_ASSERT(aLength <= size_t(-1) / sizeof(T));
107 MOZ_ASSERT(reinterpret_cast<uintptr_t>(mRangeStart) + aLength * sizeof(T) >=
108 reinterpret_cast<uintptr_t>(mRangeStart));
109 checkSanity();
112 /* Equivalent to RangedPtr(aArr, aArr, N). */
113 template <size_t N>
114 explicit RangedPtr(T (&aArr)[N])
115 : mPtr(aArr)
116 #ifdef DEBUG
118 mRangeStart(aArr),
119 mRangeEnd(aArr + N)
120 #endif
122 checkSanity();
125 RangedPtr(const RangedPtr& aOther)
126 : mPtr(aOther.mPtr)
127 #ifdef DEBUG
129 mRangeStart(aOther.mRangeStart),
130 mRangeEnd(aOther.mRangeEnd)
131 #endif
133 checkSanity();
136 template <typename U>
137 MOZ_IMPLICIT RangedPtr(const RangedPtr<U>& aOther)
138 : mPtr(aOther.mPtr)
139 #ifdef DEBUG
141 mRangeStart(aOther.mRangeStart),
142 mRangeEnd(aOther.mRangeEnd)
143 #endif
145 checkSanity();
148 T* get() const { return mPtr; }
150 explicit operator bool() const { return mPtr != nullptr; }
152 void checkIdenticalRange(const RangedPtr<T>& aOther) const {
153 MOZ_ASSERT(mRangeStart == aOther.mRangeStart);
154 MOZ_ASSERT(mRangeEnd == aOther.mRangeEnd);
157 template <typename U>
158 RangedPtr<U> ReinterpretCast() const {
159 #ifdef DEBUG
160 return {reinterpret_cast<U*>(mPtr), reinterpret_cast<U*>(mRangeStart),
161 reinterpret_cast<U*>(mRangeEnd)};
162 #else
163 return {reinterpret_cast<U*>(mPtr), nullptr, nullptr};
164 #endif
168 * You can only assign one RangedPtr into another if the two pointers have
169 * the same valid range:
171 * char arr1[] = "hi";
172 * char arr2[] = "bye";
173 * RangedPtr<char> p1(arr1, 2);
174 * p1 = RangedPtr<char>(arr1 + 1, arr1, arr1 + 2); // works
175 * p1 = RangedPtr<char>(arr2, 3); // asserts
177 RangedPtr<T>& operator=(const RangedPtr<T>& aOther) {
178 checkIdenticalRange(aOther);
179 mPtr = aOther.mPtr;
180 checkSanity();
181 return *this;
184 RangedPtr<T> operator+(size_t aInc) const {
185 MOZ_ASSERT(aInc <= size_t(-1) / sizeof(T));
186 MOZ_ASSERT(asUintptr() + aInc * sizeof(T) >= asUintptr());
187 return create(mPtr + aInc);
190 RangedPtr<T> operator-(size_t aDec) const {
191 MOZ_ASSERT(aDec <= size_t(-1) / sizeof(T));
192 MOZ_ASSERT(asUintptr() - aDec * sizeof(T) <= asUintptr());
193 return create(mPtr - aDec);
197 * You can assign a raw pointer into a RangedPtr if the raw pointer is
198 * within the range specified at creation.
200 template <typename U>
201 RangedPtr<T>& operator=(U* aPtr) {
202 *this = create(aPtr);
203 return *this;
206 template <typename U>
207 RangedPtr<T>& operator=(const RangedPtr<U>& aPtr) {
208 MOZ_ASSERT(mRangeStart <= aPtr.mPtr);
209 MOZ_ASSERT(aPtr.mPtr <= mRangeEnd);
210 mPtr = aPtr.mPtr;
211 checkSanity();
212 return *this;
215 RangedPtr<T>& operator++() { return (*this += 1); }
217 RangedPtr<T> operator++(int) {
218 RangedPtr<T> rcp = *this;
219 ++*this;
220 return rcp;
223 RangedPtr<T>& operator--() { return (*this -= 1); }
225 RangedPtr<T> operator--(int) {
226 RangedPtr<T> rcp = *this;
227 --*this;
228 return rcp;
231 RangedPtr<T>& operator+=(size_t aInc) {
232 *this = *this + aInc;
233 return *this;
236 RangedPtr<T>& operator-=(size_t aDec) {
237 *this = *this - aDec;
238 return *this;
241 T& operator[](ptrdiff_t aIndex) const {
242 MOZ_ASSERT(size_t(aIndex > 0 ? aIndex : -aIndex) <= size_t(-1) / sizeof(T));
243 return *create(mPtr + aIndex);
246 T& operator*() const {
247 MOZ_ASSERT(mPtr >= mRangeStart);
248 MOZ_ASSERT(mPtr < mRangeEnd);
249 return *mPtr;
252 T* operator->() const {
253 MOZ_ASSERT(mPtr >= mRangeStart);
254 MOZ_ASSERT(mPtr < mRangeEnd);
255 return mPtr;
258 template <typename U>
259 bool operator==(const RangedPtr<U>& aOther) const {
260 return mPtr == aOther.mPtr;
262 template <typename U>
263 bool operator!=(const RangedPtr<U>& aOther) const {
264 return !(*this == aOther);
267 template <typename U>
268 bool operator==(const U* u) const {
269 return mPtr == u;
271 template <typename U>
272 bool operator!=(const U* u) const {
273 return !(*this == u);
276 bool operator==(std::nullptr_t) const { return mPtr == nullptr; }
277 bool operator!=(std::nullptr_t) const { return mPtr != nullptr; }
279 template <typename U>
280 bool operator<(const RangedPtr<U>& aOther) const {
281 return mPtr < aOther.mPtr;
283 template <typename U>
284 bool operator<=(const RangedPtr<U>& aOther) const {
285 return mPtr <= aOther.mPtr;
288 template <typename U>
289 bool operator>(const RangedPtr<U>& aOther) const {
290 return mPtr > aOther.mPtr;
292 template <typename U>
293 bool operator>=(const RangedPtr<U>& aOther) const {
294 return mPtr >= aOther.mPtr;
297 size_t operator-(const RangedPtr<T>& aOther) const {
298 MOZ_ASSERT(mPtr >= aOther.mPtr);
299 return PointerRangeSize(aOther.mPtr, mPtr);
302 private:
303 RangedPtr() = delete;
306 } /* namespace mozilla */
308 #endif /* mozilla_RangedPtr_h */