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/. */
8 * Implements a smart pointer asserted to remain within a range specified at
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"
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.
46 friend class RangedPtr
;
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 {
63 return RangedPtr
<T
>(aPtr
, mRangeStart
, mRangeEnd
);
65 return RangedPtr
<T
>(aPtr
, nullptr, size_t(0));
69 uintptr_t asUintptr() const { return reinterpret_cast<uintptr_t>(mPtr
); }
72 RangedPtr(T
* aPtr
, T
* aStart
, T
* aEnd
)
80 MOZ_ASSERT(mRangeStart
<= mRangeEnd
);
83 RangedPtr(T
* aPtr
, T
* aStart
, size_t aLength
)
88 mRangeEnd(aStart
+ aLength
)
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
));
97 /* Equivalent to RangedPtr(aPtr, aPtr, aLength). */
98 RangedPtr(T
* aPtr
, size_t aLength
)
103 mRangeEnd(aPtr
+ aLength
)
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
));
112 /* Equivalent to RangedPtr(aArr, aArr, N). */
114 explicit RangedPtr(T (&aArr
)[N
])
125 RangedPtr(const RangedPtr
& aOther
)
129 mRangeStart(aOther
.mRangeStart
),
130 mRangeEnd(aOther
.mRangeEnd
)
136 template <typename U
>
137 MOZ_IMPLICIT
RangedPtr(const RangedPtr
<U
>& aOther
)
141 mRangeStart(aOther
.mRangeStart
),
142 mRangeEnd(aOther
.mRangeEnd
)
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 {
160 return {reinterpret_cast<U
*>(mPtr
), reinterpret_cast<U
*>(mRangeStart
),
161 reinterpret_cast<U
*>(mRangeEnd
)};
163 return {reinterpret_cast<U
*>(mPtr
), nullptr, nullptr};
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
);
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
);
206 template <typename U
>
207 RangedPtr
<T
>& operator=(const RangedPtr
<U
>& aPtr
) {
208 MOZ_ASSERT(mRangeStart
<= aPtr
.mPtr
);
209 MOZ_ASSERT(aPtr
.mPtr
<= mRangeEnd
);
215 RangedPtr
<T
>& operator++() { return (*this += 1); }
217 RangedPtr
<T
> operator++(int) {
218 RangedPtr
<T
> rcp
= *this;
223 RangedPtr
<T
>& operator--() { return (*this -= 1); }
225 RangedPtr
<T
> operator--(int) {
226 RangedPtr
<T
> rcp
= *this;
231 RangedPtr
<T
>& operator+=(size_t aInc
) {
232 *this = *this + aInc
;
236 RangedPtr
<T
>& operator-=(size_t aDec
) {
237 *this = *this - aDec
;
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
);
252 T
* operator->() const {
253 MOZ_ASSERT(mPtr
>= mRangeStart
);
254 MOZ_ASSERT(mPtr
< mRangeEnd
);
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 {
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
);
303 RangedPtr() = delete;
306 } /* namespace mozilla */
308 #endif /* mozilla_RangedPtr_h */