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"
18 #include "mozilla/NullPtr.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.
53 typedef void (RangedPtr::* ConvertibleToBool
)();
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
66 return RangedPtr
<T
>(aPtr
, mRangeStart
, mRangeEnd
);
68 return RangedPtr
<T
>(aPtr
, nullptr, size_t(0));
72 uintptr_t asUintptr() const { return reinterpret_cast<uintptr_t>(mPtr
); }
75 RangedPtr(T
* aPtr
, T
* aStart
, T
* aEnd
)
78 , mRangeStart(aStart
), mRangeEnd(aEnd
)
81 MOZ_ASSERT(mRangeStart
<= mRangeEnd
);
84 RangedPtr(T
* aPtr
, T
* aStart
, size_t aLength
)
87 , mRangeStart(aStart
), mRangeEnd(aStart
+ aLength
)
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
));
96 /* Equivalent to RangedPtr(aPtr, aPtr, aLength). */
97 RangedPtr(T
* aPtr
, size_t aLength
)
100 , mRangeStart(aPtr
), mRangeEnd(aPtr
+ aLength
)
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
));
109 /* Equivalent to RangedPtr(aArr, aArr, N). */
111 RangedPtr(T (&aArr
)[N
])
114 , mRangeStart(aArr
), mRangeEnd(aArr
+ N
)
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
);
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
);
168 template <typename U
>
169 RangedPtr
<T
>& operator=(const RangedPtr
<U
>& aPtr
)
171 MOZ_ASSERT(mRangeStart
<= aPtr
.mPtr
);
172 MOZ_ASSERT(aPtr
.mPtr
<= mRangeEnd
);
178 RangedPtr
<T
>& operator++()
183 RangedPtr
<T
> operator++(int)
185 RangedPtr
<T
> rcp
= *this;
190 RangedPtr
<T
>& operator--()
195 RangedPtr
<T
> operator--(int)
197 RangedPtr
<T
> rcp
= *this;
202 RangedPtr
<T
>& operator+=(size_t aInc
)
204 *this = *this + aInc
;
208 RangedPtr
<T
>& operator-=(size_t aDec
)
210 *this = *this - aDec
;
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
);
222 MOZ_ASSERT(mPtr
>= mRangeStart
);
223 MOZ_ASSERT(mPtr
< mRangeEnd
);
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
);
239 bool operator==(const U
* u
) const
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
);
278 RangedPtr() MOZ_DELETE
;
279 T
* operator&() MOZ_DELETE
;
282 } /* namespace mozilla */
284 #endif /* mozilla_RangedPtr_h */