mfbt/Range.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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/. */
     7 #ifndef mozilla_Range_h
     8 #define mozilla_Range_h
    10 #include "mozilla/NullPtr.h"
    11 #include "mozilla/RangedPtr.h"
    13 #include <stddef.h>
    15 namespace mozilla {
    17 // Range<T> is a tuple containing a pointer and a length.
    18 template <typename T>
    19 class Range
    20 {
    21     RangedPtr<T> mStart;
    22     RangedPtr<T> mEnd;
    24     typedef void (Range::* ConvertibleToBool)();
    25     void nonNull() {}
    27   public:
    28     Range() : mStart(nullptr, 0), mEnd(nullptr, 0) {}
    29     Range(T* p, size_t len)
    30       : mStart(p, p, p + len),
    31         mEnd(p + len, p, p + len)
    32     {}
    34     RangedPtr<T> start() const { return mStart; }
    35     RangedPtr<T> end() const { return mEnd; }
    36     size_t length() const { return mEnd - mStart; }
    38     T& operator[](size_t offset) {
    39       return mStart[offset];
    40     }
    42     const T& operator[](size_t offset) const {
    43       return mStart[offset];
    44     }
    46     operator ConvertibleToBool() const { return mStart ? &Range::nonNull : 0; }
    47 };
    49 } // namespace mozilla
    51 #endif /* mozilla_Range_h */

mercurial