Tue, 06 Jan 2015 21:39:09 +0100
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.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* |
michael@0 | 8 | * Provides DebugOnly, a type for variables used only in debug builds (i.e. by |
michael@0 | 9 | * assertions). |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | #ifndef mozilla_DebugOnly_h |
michael@0 | 13 | #define mozilla_DebugOnly_h |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * DebugOnly contains a value of type T, but only in debug builds. In release |
michael@0 | 19 | * builds, it does not contain a value. This helper is intended to be used with |
michael@0 | 20 | * MOZ_ASSERT()-style macros, allowing one to write: |
michael@0 | 21 | * |
michael@0 | 22 | * DebugOnly<bool> check = func(); |
michael@0 | 23 | * MOZ_ASSERT(check); |
michael@0 | 24 | * |
michael@0 | 25 | * more concisely than declaring |check| conditional on #ifdef DEBUG, but also |
michael@0 | 26 | * without allocating storage space for |check| in release builds. |
michael@0 | 27 | * |
michael@0 | 28 | * DebugOnly instances can only be coerced to T in debug builds. In release |
michael@0 | 29 | * builds they don't have a value, so type coercion is not well defined. |
michael@0 | 30 | * |
michael@0 | 31 | * Note that DebugOnly instances still take up one byte of space, plus padding, |
michael@0 | 32 | * when used as members of structs. |
michael@0 | 33 | */ |
michael@0 | 34 | template<typename T> |
michael@0 | 35 | class DebugOnly |
michael@0 | 36 | { |
michael@0 | 37 | public: |
michael@0 | 38 | #ifdef DEBUG |
michael@0 | 39 | T value; |
michael@0 | 40 | |
michael@0 | 41 | DebugOnly() { } |
michael@0 | 42 | DebugOnly(const T& other) : value(other) { } |
michael@0 | 43 | DebugOnly(const DebugOnly& other) : value(other.value) { } |
michael@0 | 44 | DebugOnly& operator=(const T& rhs) { |
michael@0 | 45 | value = rhs; |
michael@0 | 46 | return *this; |
michael@0 | 47 | } |
michael@0 | 48 | void operator++(int) { |
michael@0 | 49 | value++; |
michael@0 | 50 | } |
michael@0 | 51 | void operator--(int) { |
michael@0 | 52 | value--; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | T* operator&() { return &value; } |
michael@0 | 56 | |
michael@0 | 57 | operator T&() { return value; } |
michael@0 | 58 | operator const T&() const { return value; } |
michael@0 | 59 | |
michael@0 | 60 | T& operator->() { return value; } |
michael@0 | 61 | const T& operator->() const { return value; } |
michael@0 | 62 | |
michael@0 | 63 | #else |
michael@0 | 64 | DebugOnly() { } |
michael@0 | 65 | DebugOnly(const T&) { } |
michael@0 | 66 | DebugOnly(const DebugOnly&) { } |
michael@0 | 67 | DebugOnly& operator=(const T&) { return *this; } |
michael@0 | 68 | void operator++(int) { } |
michael@0 | 69 | void operator--(int) { } |
michael@0 | 70 | #endif |
michael@0 | 71 | |
michael@0 | 72 | /* |
michael@0 | 73 | * DebugOnly must always have a destructor or else it will |
michael@0 | 74 | * generate "unused variable" warnings, exactly what it's intended |
michael@0 | 75 | * to avoid! |
michael@0 | 76 | */ |
michael@0 | 77 | ~DebugOnly() {} |
michael@0 | 78 | }; |
michael@0 | 79 | |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | #endif /* mozilla_DebugOnly_h */ |