Bug 1276573 - Add a new constructor for Range<T>. r=Waldo
authorBoris Chiou <boris.chiou@gmail.com>
Mon, 30 May 2016 13:58:13 +0800
changeset 304590 3695acc329e68a33ad30063f97b9ea5b148faa88
parent 304589 9b1032ae8fac9bec0f8232ad6594dfe1467ef1f6
child 304591 a4113131625db23e9bc749822e009e17f4d40384
push id30433
push usercbook@mozilla.com
push dateTue, 12 Jul 2016 14:25:39 +0000
treeherdermozilla-central@fcfa26398425 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs1276573
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1276573 - Add a new constructor for Range<T>. r=Waldo These new constructor accepts two RangedPtr<T> arguments. MozReview-Commit-ID: 8a3bYserLMr
mfbt/Range.h
mfbt/RangedPtr.h
--- a/mfbt/Range.h
+++ b/mfbt/Range.h
@@ -21,16 +21,24 @@ class Range
   const RangedPtr<T> mEnd;
 
 public:
   Range() : mStart(nullptr, 0), mEnd(nullptr, 0) {}
   Range(T* aPtr, size_t aLength)
     : mStart(aPtr, aPtr, aPtr + aLength),
       mEnd(aPtr + aLength, aPtr, aPtr + aLength)
   {}
+  Range(const RangedPtr<T>& aStart, const RangedPtr<T>& aEnd)
+    : mStart(aStart.get(), aStart.get(), aEnd.get()),
+      mEnd(aEnd.get(), aStart.get(), aEnd.get())
+  {
+    // Only accept two RangedPtrs within the same range.
+    aStart.checkIdenticalRange(aEnd);
+    MOZ_ASSERT(aStart <= aEnd);
+  }
 
   RangedPtr<T> start() const { return mStart; }
   RangedPtr<T> end() const { return mEnd; }
   size_t length() const { return mEnd - mStart; }
 
   T& operator[](size_t aOffset) const { return mStart[aOffset]; }
 
   explicit operator bool() const { return mStart != nullptr; }
--- a/mfbt/RangedPtr.h
+++ b/mfbt/RangedPtr.h
@@ -112,30 +112,35 @@ public:
   {
     checkSanity();
   }
 
   T* get() const { return mPtr; }
 
   explicit operator bool() const { return mPtr != nullptr; }
 
+  void checkIdenticalRange(const RangedPtr<T>& aOther) const
+  {
+    MOZ_ASSERT(mRangeStart == aOther.mRangeStart);
+    MOZ_ASSERT(mRangeEnd == aOther.mRangeEnd);
+  }
+
   /*
    * 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)
   {
-    MOZ_ASSERT(mRangeStart == aOther.mRangeStart);
-    MOZ_ASSERT(mRangeEnd == aOther.mRangeEnd);
+    checkIdenticalRange(aOther);
     mPtr = aOther.mPtr;
     checkSanity();
     return *this;
   }
 
   RangedPtr<T> operator+(size_t aInc) const
   {
     MOZ_ASSERT(aInc <= size_t(-1) / sizeof(T));