Bumping manifests a=b2g-bump
[gecko.git] / mfbt / RangedPtr.h
blob4d94035b904ba68d16b3726612aeaa7218ec41e6
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"
18 #include "mozilla/NullPtr.h"
20 #include <stdint.h>
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
46 T* mPtr;
48 #ifdef DEBUG
49 T* const mRangeStart;
50 T* const mRangeEnd;
51 #endif
53 typedef void (RangedPtr::* ConvertibleToBool)();
54 void nonNull() {}
56 void checkSanity()
58 MOZ_ASSERT(mRangeStart <= mPtr);
59 MOZ_ASSERT(mPtr <= mRangeEnd);
62 /* Creates a new pointer for |aPtr|, restricted to this pointer's range. */
63 RangedPtr<T> create(T* aPtr) const
65 #ifdef DEBUG
66 return RangedPtr<T>(aPtr, mRangeStart, mRangeEnd);
67 #else
68 return RangedPtr<T>(aPtr, nullptr, size_t(0));
69 #endif
72 uintptr_t asUintptr() const { return reinterpret_cast<uintptr_t>(mPtr); }
74 public:
75 RangedPtr(T* aPtr, T* aStart, T* aEnd)
76 : mPtr(aPtr)
77 #ifdef DEBUG
78 , mRangeStart(aStart), mRangeEnd(aEnd)
79 #endif
81 MOZ_ASSERT(mRangeStart <= mRangeEnd);
82 checkSanity();
84 RangedPtr(T* aPtr, T* aStart, size_t aLength)
85 : mPtr(aPtr)
86 #ifdef DEBUG
87 , mRangeStart(aStart), mRangeEnd(aStart + aLength)
88 #endif
90 MOZ_ASSERT(aLength <= size_t(-1) / sizeof(T));
91 MOZ_ASSERT(reinterpret_cast<uintptr_t>(mRangeStart) + aLength * sizeof(T) >=
92 reinterpret_cast<uintptr_t>(mRangeStart));
93 checkSanity();
96 /* Equivalent to RangedPtr(aPtr, aPtr, aLength). */
97 RangedPtr(T* aPtr, size_t aLength)
98 : mPtr(aPtr)
99 #ifdef DEBUG
100 , mRangeStart(aPtr), mRangeEnd(aPtr + aLength)
101 #endif
103 MOZ_ASSERT(aLength <= size_t(-1) / sizeof(T));
104 MOZ_ASSERT(reinterpret_cast<uintptr_t>(mRangeStart) + aLength * sizeof(T) >=
105 reinterpret_cast<uintptr_t>(mRangeStart));
106 checkSanity();
109 /* Equivalent to RangedPtr(aArr, aArr, N). */
110 template<size_t N>
111 RangedPtr(T (&aArr)[N])
112 : mPtr(aArr)
113 #ifdef DEBUG
114 , mRangeStart(aArr), mRangeEnd(aArr + N)
115 #endif
117 checkSanity();
120 T* get() const { return mPtr; }
122 operator ConvertibleToBool() const { return mPtr ? &RangedPtr::nonNull : 0; }
125 * You can only assign one RangedPtr into another if the two pointers have
126 * the same valid range:
128 * char arr1[] = "hi";
129 * char arr2[] = "bye";
130 * RangedPtr<char> p1(arr1, 2);
131 * p1 = RangedPtr<char>(arr1 + 1, arr1, arr1 + 2); // works
132 * p1 = RangedPtr<char>(arr2, 3); // asserts
134 RangedPtr<T>& operator=(const RangedPtr<T>& aOther)
136 MOZ_ASSERT(mRangeStart == aOther.mRangeStart);
137 MOZ_ASSERT(mRangeEnd == aOther.mRangeEnd);
138 mPtr = aOther.mPtr;
139 checkSanity();
140 return *this;
143 RangedPtr<T> operator+(size_t aInc)
145 MOZ_ASSERT(aInc <= size_t(-1) / sizeof(T));
146 MOZ_ASSERT(asUintptr() + aInc * sizeof(T) >= asUintptr());
147 return create(mPtr + aInc);
150 RangedPtr<T> operator-(size_t aDec)
152 MOZ_ASSERT(aDec <= size_t(-1) / sizeof(T));
153 MOZ_ASSERT(asUintptr() - aDec * sizeof(T) <= asUintptr());
154 return create(mPtr - aDec);
158 * You can assign a raw pointer into a RangedPtr if the raw pointer is
159 * within the range specified at creation.
161 template <typename U>
162 RangedPtr<T>& operator=(U* aPtr)
164 *this = create(aPtr);
165 return *this;
168 template <typename U>
169 RangedPtr<T>& operator=(const RangedPtr<U>& aPtr)
171 MOZ_ASSERT(mRangeStart <= aPtr.mPtr);
172 MOZ_ASSERT(aPtr.mPtr <= mRangeEnd);
173 mPtr = aPtr.mPtr;
174 checkSanity();
175 return *this;
178 RangedPtr<T>& operator++()
180 return (*this += 1);
183 RangedPtr<T> operator++(int)
185 RangedPtr<T> rcp = *this;
186 ++*this;
187 return rcp;
190 RangedPtr<T>& operator--()
192 return (*this -= 1);
195 RangedPtr<T> operator--(int)
197 RangedPtr<T> rcp = *this;
198 --*this;
199 return rcp;
202 RangedPtr<T>& operator+=(size_t aInc)
204 *this = *this + aInc;
205 return *this;
208 RangedPtr<T>& operator-=(size_t aDec)
210 *this = *this - aDec;
211 return *this;
214 T& operator[](int aIndex) const
216 MOZ_ASSERT(size_t(aIndex > 0 ? aIndex : -aIndex) <= size_t(-1) / sizeof(T));
217 return *create(mPtr + aIndex);
220 T& operator*() const
222 MOZ_ASSERT(mPtr >= mRangeStart);
223 MOZ_ASSERT(mPtr < mRangeEnd);
224 return *mPtr;
227 template <typename U>
228 bool operator==(const RangedPtr<U>& aOther) const
230 return mPtr == aOther.mPtr;
232 template <typename U>
233 bool operator!=(const RangedPtr<U>& aOther) const
235 return !(*this == aOther);
238 template<typename U>
239 bool operator==(const U* u) const
241 return mPtr == u;
243 template<typename U>
244 bool operator!=(const U* u) const
246 return !(*this == u);
249 template <typename U>
250 bool operator<(const RangedPtr<U>& aOther) const
252 return mPtr < aOther.mPtr;
254 template <typename U>
255 bool operator<=(const RangedPtr<U>& aOther) const
257 return mPtr <= aOther.mPtr;
260 template <typename U>
261 bool operator>(const RangedPtr<U>& aOther) const
263 return mPtr > aOther.mPtr;
265 template <typename U>
266 bool operator>=(const RangedPtr<U>& aOther) const
268 return mPtr >= aOther.mPtr;
271 size_t operator-(const RangedPtr<T>& aOther) const
273 MOZ_ASSERT(mPtr >= aOther.mPtr);
274 return PointerRangeSize(aOther.mPtr, mPtr);
277 private:
278 RangedPtr() MOZ_DELETE;
279 T* operator&() MOZ_DELETE;
282 } /* namespace mozilla */
284 #endif /* mozilla_RangedPtr_h */