2013-07-24 11:41:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-06-04 07:36:43 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-06-06 22:02:34 +04:00
|
|
|
|
2011-12-28 20:48:54 +04:00
|
|
|
/*
|
|
|
|
* Implements a smart pointer asserted to remain within a range specified at
|
|
|
|
* construction.
|
|
|
|
*/
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#ifndef mozilla_RangedPtr_h
|
|
|
|
#define mozilla_RangedPtr_h
|
2011-06-06 22:02:34 +04:00
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-12-19 23:28:35 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2011-11-21 00:22:51 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-12-09 06:52:54 +04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2019-03-26 22:10:24 +03:00
|
|
|
#include <cstddef>
|
2011-06-06 22:02:34 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RangedPtr is a smart pointer restricted to an address range specified at
|
|
|
|
* creation. The pointer (and any smart pointers derived from it) must remain
|
|
|
|
* within the range [start, end] (inclusive of end to facilitate use as
|
|
|
|
* sentinels). Dereferencing or indexing into the pointer (or pointers derived
|
|
|
|
* from it) must remain within the range [start, end). All the standard pointer
|
|
|
|
* operators are defined on it; in debug builds these operations assert that the
|
|
|
|
* range specified at construction is respected.
|
|
|
|
*
|
|
|
|
* In theory passing a smart pointer instance as an argument can be slightly
|
|
|
|
* slower than passing a T* (due to ABI requirements for passing structs versus
|
|
|
|
* passing pointers), if the method being called isn't inlined. If you are in
|
|
|
|
* extremely performance-critical code, you may want to be careful using this
|
|
|
|
* smart pointer as an argument type.
|
|
|
|
*
|
|
|
|
* RangedPtr<T> intentionally does not implicitly convert to T*. Use get() to
|
|
|
|
* explicitly convert to T*. Keep in mind that the raw pointer of course won't
|
|
|
|
* implement bounds checking in debug builds.
|
|
|
|
*/
|
2012-06-04 07:36:43 +04:00
|
|
|
template <typename T>
|
2011-06-06 22:02:34 +04:00
|
|
|
class RangedPtr {
|
2014-07-11 06:10:17 +04:00
|
|
|
T* mPtr;
|
2011-06-06 22:02:34 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
T* const mRangeStart;
|
|
|
|
T* const mRangeEnd;
|
2011-06-06 22:02:34 +04:00
|
|
|
#endif
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
void checkSanity() {
|
|
|
|
MOZ_ASSERT(mRangeStart <= mPtr);
|
|
|
|
MOZ_ASSERT(mPtr <= mRangeEnd);
|
|
|
|
}
|
2011-06-06 22:02:34 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Creates a new pointer for |aPtr|, restricted to this pointer's range. */
|
|
|
|
RangedPtr<T> create(T* aPtr) const {
|
2011-06-06 22:02:34 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
return RangedPtr<T>(aPtr, mRangeStart, mRangeEnd);
|
2011-06-06 22:02:34 +04:00
|
|
|
#else
|
2014-07-11 06:10:17 +04:00
|
|
|
return RangedPtr<T>(aPtr, nullptr, size_t(0));
|
2011-06-06 22:02:34 +04:00
|
|
|
#endif
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2011-06-06 22:02:34 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
uintptr_t asUintptr() const { return reinterpret_cast<uintptr_t>(mPtr); }
|
2012-10-17 03:30:02 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
public:
|
|
|
|
RangedPtr(T* aPtr, T* aStart, T* aEnd)
|
|
|
|
: mPtr(aPtr)
|
2011-06-06 22:02:34 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
,
|
|
|
|
mRangeStart(aStart),
|
|
|
|
mRangeEnd(aEnd)
|
2011-06-06 22:02:34 +04:00
|
|
|
#endif
|
2014-07-11 06:10:17 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mRangeStart <= mRangeEnd);
|
|
|
|
checkSanity();
|
|
|
|
}
|
|
|
|
RangedPtr(T* aPtr, T* aStart, size_t aLength)
|
|
|
|
: mPtr(aPtr)
|
2011-06-06 22:02:34 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
,
|
|
|
|
mRangeStart(aStart),
|
|
|
|
mRangeEnd(aStart + aLength)
|
2011-06-06 22:02:34 +04:00
|
|
|
#endif
|
2014-07-11 06:10:17 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aLength <= size_t(-1) / sizeof(T));
|
|
|
|
MOZ_ASSERT(reinterpret_cast<uintptr_t>(mRangeStart) + aLength * sizeof(T) >=
|
|
|
|
reinterpret_cast<uintptr_t>(mRangeStart));
|
|
|
|
checkSanity();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Equivalent to RangedPtr(aPtr, aPtr, aLength). */
|
|
|
|
RangedPtr(T* aPtr, size_t aLength)
|
|
|
|
: mPtr(aPtr)
|
2011-06-06 22:02:34 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
,
|
|
|
|
mRangeStart(aPtr),
|
|
|
|
mRangeEnd(aPtr + aLength)
|
2011-06-06 22:02:34 +04:00
|
|
|
#endif
|
2014-07-11 06:10:17 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aLength <= size_t(-1) / sizeof(T));
|
|
|
|
MOZ_ASSERT(reinterpret_cast<uintptr_t>(mRangeStart) + aLength * sizeof(T) >=
|
|
|
|
reinterpret_cast<uintptr_t>(mRangeStart));
|
|
|
|
checkSanity();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Equivalent to RangedPtr(aArr, aArr, N). */
|
|
|
|
template <size_t N>
|
2015-07-28 04:12:06 +03:00
|
|
|
explicit RangedPtr(T (&aArr)[N])
|
2014-07-11 06:10:17 +04:00
|
|
|
: mPtr(aArr)
|
2011-06-08 00:25:36 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
,
|
|
|
|
mRangeStart(aArr),
|
|
|
|
mRangeEnd(aArr + N)
|
2011-06-08 00:25:36 +04:00
|
|
|
#endif
|
2014-07-11 06:10:17 +04:00
|
|
|
{
|
|
|
|
checkSanity();
|
|
|
|
}
|
|
|
|
|
2019-12-12 12:50:24 +03:00
|
|
|
MOZ_IMPLICIT RangedPtr(const RangedPtr<T>& aOther)
|
|
|
|
: mPtr(aOther.mPtr)
|
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mRangeStart(aOther.mRangeStart),
|
|
|
|
mRangeEnd(aOther.mRangeEnd)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
checkSanity();
|
|
|
|
}
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T* get() const { return mPtr; }
|
|
|
|
|
2015-02-03 19:52:36 +03:00
|
|
|
explicit operator bool() const { return mPtr != nullptr; }
|
2014-07-11 06:10:17 +04:00
|
|
|
|
2016-05-30 08:58:13 +03:00
|
|
|
void checkIdenticalRange(const RangedPtr<T>& aOther) const {
|
|
|
|
MOZ_ASSERT(mRangeStart == aOther.mRangeStart);
|
|
|
|
MOZ_ASSERT(mRangeEnd == aOther.mRangeEnd);
|
|
|
|
}
|
|
|
|
|
2018-06-30 08:50:41 +03:00
|
|
|
template <typename U>
|
|
|
|
RangedPtr<U> ReinterpretCast() const {
|
|
|
|
#ifdef DEBUG
|
|
|
|
return {reinterpret_cast<U*>(mPtr), reinterpret_cast<U*>(mRangeStart),
|
|
|
|
reinterpret_cast<U*>(mRangeEnd)};
|
|
|
|
#else
|
|
|
|
return {reinterpret_cast<U*>(mPtr), nullptr, nullptr};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* You can only assign one RangedPtr into another if the two pointers have
|
|
|
|
* the same valid range:
|
|
|
|
*
|
|
|
|
* char arr1[] = "hi";
|
|
|
|
* char arr2[] = "bye";
|
|
|
|
* RangedPtr<char> p1(arr1, 2);
|
|
|
|
* p1 = RangedPtr<char>(arr1 + 1, arr1, arr1 + 2); // works
|
|
|
|
* p1 = RangedPtr<char>(arr2, 3); // asserts
|
|
|
|
*/
|
|
|
|
RangedPtr<T>& operator=(const RangedPtr<T>& aOther) {
|
2016-05-30 08:58:13 +03:00
|
|
|
checkIdenticalRange(aOther);
|
2014-07-11 06:10:17 +04:00
|
|
|
mPtr = aOther.mPtr;
|
|
|
|
checkSanity();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-06-03 07:16:33 +03:00
|
|
|
RangedPtr<T> operator+(size_t aInc) const {
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(aInc <= size_t(-1) / sizeof(T));
|
|
|
|
MOZ_ASSERT(asUintptr() + aInc * sizeof(T) >= asUintptr());
|
|
|
|
return create(mPtr + aInc);
|
|
|
|
}
|
|
|
|
|
2016-06-03 07:16:33 +03:00
|
|
|
RangedPtr<T> operator-(size_t aDec) const {
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(aDec <= size_t(-1) / sizeof(T));
|
|
|
|
MOZ_ASSERT(asUintptr() - aDec * sizeof(T) <= asUintptr());
|
|
|
|
return create(mPtr - aDec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* You can assign a raw pointer into a RangedPtr if the raw pointer is
|
|
|
|
* within the range specified at creation.
|
|
|
|
*/
|
|
|
|
template <typename U>
|
|
|
|
RangedPtr<T>& operator=(U* aPtr) {
|
|
|
|
*this = create(aPtr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
RangedPtr<T>& operator=(const RangedPtr<U>& aPtr) {
|
|
|
|
MOZ_ASSERT(mRangeStart <= aPtr.mPtr);
|
|
|
|
MOZ_ASSERT(aPtr.mPtr <= mRangeEnd);
|
|
|
|
mPtr = aPtr.mPtr;
|
|
|
|
checkSanity();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
RangedPtr<T>& operator++() { return (*this += 1); }
|
|
|
|
|
|
|
|
RangedPtr<T> operator++(int) {
|
|
|
|
RangedPtr<T> rcp = *this;
|
|
|
|
++*this;
|
|
|
|
return rcp;
|
|
|
|
}
|
|
|
|
|
|
|
|
RangedPtr<T>& operator--() { return (*this -= 1); }
|
|
|
|
|
|
|
|
RangedPtr<T> operator--(int) {
|
|
|
|
RangedPtr<T> rcp = *this;
|
|
|
|
--*this;
|
|
|
|
return rcp;
|
|
|
|
}
|
|
|
|
|
|
|
|
RangedPtr<T>& operator+=(size_t aInc) {
|
|
|
|
*this = *this + aInc;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
RangedPtr<T>& operator-=(size_t aDec) {
|
|
|
|
*this = *this - aDec;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-02-08 06:13:23 +03:00
|
|
|
T& operator[](ptrdiff_t aIndex) const {
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(size_t(aIndex > 0 ? aIndex : -aIndex) <= size_t(-1) / sizeof(T));
|
|
|
|
return *create(mPtr + aIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator*() const {
|
|
|
|
MOZ_ASSERT(mPtr >= mRangeStart);
|
|
|
|
MOZ_ASSERT(mPtr < mRangeEnd);
|
|
|
|
return *mPtr;
|
|
|
|
}
|
|
|
|
|
2016-05-30 06:55:57 +03:00
|
|
|
T* operator->() const {
|
|
|
|
MOZ_ASSERT(mPtr >= mRangeStart);
|
|
|
|
MOZ_ASSERT(mPtr < mRangeEnd);
|
|
|
|
return mPtr;
|
|
|
|
}
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
template <typename U>
|
|
|
|
bool operator==(const RangedPtr<U>& aOther) const {
|
|
|
|
return mPtr == aOther.mPtr;
|
|
|
|
}
|
|
|
|
template <typename U>
|
|
|
|
bool operator!=(const RangedPtr<U>& aOther) const {
|
|
|
|
return !(*this == aOther);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
bool operator==(const U* u) const {
|
|
|
|
return mPtr == u;
|
|
|
|
}
|
|
|
|
template <typename U>
|
|
|
|
bool operator!=(const U* u) const {
|
|
|
|
return !(*this == u);
|
|
|
|
}
|
|
|
|
|
2019-03-26 22:10:24 +03:00
|
|
|
bool operator==(std::nullptr_t) const { return mPtr == nullptr; }
|
|
|
|
bool operator!=(std::nullptr_t) const { return mPtr != nullptr; }
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
template <typename U>
|
|
|
|
bool operator<(const RangedPtr<U>& aOther) const {
|
|
|
|
return mPtr < aOther.mPtr;
|
|
|
|
}
|
|
|
|
template <typename U>
|
|
|
|
bool operator<=(const RangedPtr<U>& aOther) const {
|
|
|
|
return mPtr <= aOther.mPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
bool operator>(const RangedPtr<U>& aOther) const {
|
|
|
|
return mPtr > aOther.mPtr;
|
|
|
|
}
|
|
|
|
template <typename U>
|
|
|
|
bool operator>=(const RangedPtr<U>& aOther) const {
|
|
|
|
return mPtr >= aOther.mPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t operator-(const RangedPtr<T>& aOther) const {
|
|
|
|
MOZ_ASSERT(mPtr >= aOther.mPtr);
|
|
|
|
return PointerRangeSize(aOther.mPtr, mPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-01-07 02:35:02 +03:00
|
|
|
RangedPtr() = delete;
|
2011-06-06 22:02:34 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_RangedPtr_h */
|