1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
* Implements a smart pointer asserted to remain within a range specified at
11
#ifndef mozilla_RangedPtr_h_
12
#define mozilla_RangedPtr_h_
14
#include "mozilla/Assertions.h"
15
#include "mozilla/Attributes.h"
16
#include "mozilla/Util.h"
21
* RangedPtr is a smart pointer restricted to an address range specified at
22
* creation. The pointer (and any smart pointers derived from it) must remain
23
* within the range [start, end] (inclusive of end to facilitate use as
24
* sentinels). Dereferencing or indexing into the pointer (or pointers derived
25
* from it) must remain within the range [start, end). All the standard pointer
26
* operators are defined on it; in debug builds these operations assert that the
27
* range specified at construction is respected.
29
* In theory passing a smart pointer instance as an argument can be slightly
30
* slower than passing a T* (due to ABI requirements for passing structs versus
31
* passing pointers), if the method being called isn't inlined. If you are in
32
* extremely performance-critical code, you may want to be careful using this
33
* smart pointer as an argument type.
35
* RangedPtr<T> intentionally does not implicitly convert to T*. Use get() to
36
* explicitly convert to T*. Keep in mind that the raw pointer of course won't
37
* implement bounds checking in debug builds.
49
typedef void (RangedPtr::* ConvertibleToBool)();
53
MOZ_ASSERT(rangeStart <= ptr);
54
MOZ_ASSERT(ptr <= rangeEnd);
57
/* Creates a new pointer for |p|, restricted to this pointer's range. */
58
RangedPtr<T> create(T *p) const {
60
return RangedPtr<T>(p, rangeStart, rangeEnd);
62
return RangedPtr<T>(p, NULL, size_t(0));
66
uintptr_t asUintptr() const { return uintptr_t(ptr); }
69
RangedPtr(T* p, T* start, T* end)
72
, rangeStart(start), rangeEnd(end)
75
MOZ_ASSERT(rangeStart <= rangeEnd);
78
RangedPtr(T* p, T* start, size_t length)
81
, rangeStart(start), rangeEnd(start + length)
84
MOZ_ASSERT(length <= size_t(-1) / sizeof(T));
85
MOZ_ASSERT(uintptr_t(rangeStart) + length * sizeof(T) >= uintptr_t(rangeStart));
89
/* Equivalent to RangedPtr(p, p, length). */
90
RangedPtr(T* p, size_t length)
93
, rangeStart(p), rangeEnd(p + length)
96
MOZ_ASSERT(length <= size_t(-1) / sizeof(T));
97
MOZ_ASSERT(uintptr_t(rangeStart) + length * sizeof(T) >= uintptr_t(rangeStart));
101
/* Equivalent to RangedPtr(arr, arr, N). */
103
RangedPtr(T (&arr)[N])
106
, rangeStart(arr), rangeEnd(arr + N)
116
operator ConvertibleToBool() const { return ptr ? &RangedPtr::nonNull : 0; }
119
* You can only assign one RangedPtr into another if the two pointers have
120
* the same valid range:
122
* char arr1[] = "hi";
123
* char arr2[] = "bye";
124
* RangedPtr<char> p1(arr1, 2);
125
* p1 = RangedPtr<char>(arr1 + 1, arr1, arr1 + 2); // works
126
* p1 = RangedPtr<char>(arr2, 3); // asserts
128
RangedPtr<T>& operator=(const RangedPtr<T>& other) {
129
MOZ_ASSERT(rangeStart == other.rangeStart);
130
MOZ_ASSERT(rangeEnd == other.rangeEnd);
136
RangedPtr<T> operator+(size_t inc) {
137
MOZ_ASSERT(inc <= size_t(-1) / sizeof(T));
138
MOZ_ASSERT(asUintptr() + inc * sizeof(T) >= asUintptr());
139
return create(ptr + inc);
142
RangedPtr<T> operator-(size_t dec) {
143
MOZ_ASSERT(dec <= size_t(-1) / sizeof(T));
144
MOZ_ASSERT(asUintptr() - dec * sizeof(T) <= asUintptr());
145
return create(ptr - dec);
149
* You can assign a raw pointer into a RangedPtr if the raw pointer is
150
* within the range specified at creation.
152
template <typename U>
153
RangedPtr<T>& operator=(U* p) {
158
template <typename U>
159
RangedPtr<T>& operator=(const RangedPtr<U>& p) {
160
MOZ_ASSERT(rangeStart <= p.ptr);
161
MOZ_ASSERT(p.ptr <= rangeEnd);
167
RangedPtr<T>& operator++() {
171
RangedPtr<T> operator++(int) {
172
RangedPtr<T> rcp = *this;
177
RangedPtr<T>& operator--() {
181
RangedPtr<T> operator--(int) {
182
RangedPtr<T> rcp = *this;
187
RangedPtr<T>& operator+=(size_t inc) {
192
RangedPtr<T>& operator-=(size_t dec) {
197
T& operator[](int index) const {
198
MOZ_ASSERT(size_t(index > 0 ? index : -index) <= size_t(-1) / sizeof(T));
199
return *create(ptr + index);
202
T& operator*() const {
206
template <typename U>
207
bool operator==(const RangedPtr<U>& other) const {
208
return ptr == other.ptr;
210
template <typename U>
211
bool operator!=(const RangedPtr<U>& other) const {
212
return !(*this == other);
216
bool operator==(const U* u) const {
220
bool operator!=(const U* u) const {
221
return !(*this == u);
224
template <typename U>
225
bool operator<(const RangedPtr<U>& other) const {
226
return ptr < other.ptr;
228
template <typename U>
229
bool operator<=(const RangedPtr<U>& other) const {
230
return ptr <= other.ptr;
233
template <typename U>
234
bool operator>(const RangedPtr<U>& other) const {
235
return ptr > other.ptr;
237
template <typename U>
238
bool operator>=(const RangedPtr<U>& other) const {
239
return ptr >= other.ptr;
242
size_t operator-(const RangedPtr<T>& other) const {
243
MOZ_ASSERT(ptr >= other.ptr);
244
return PointerRangeSize(other.ptr, ptr);
248
RangedPtr() MOZ_DELETE;
249
T* operator&() MOZ_DELETE;
252
} /* namespace mozilla */
254
#endif /* mozilla_RangedPtr_h_ */