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 | * Implements various helper functions related to arrays. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #ifndef mozilla_ArrayUtils_h |
michael@0 | 12 | #define mozilla_ArrayUtils_h |
michael@0 | 13 | |
michael@0 | 14 | #include "mozilla/Assertions.h" |
michael@0 | 15 | #include "mozilla/Attributes.h" |
michael@0 | 16 | |
michael@0 | 17 | #include <stddef.h> |
michael@0 | 18 | |
michael@0 | 19 | #ifdef __cplusplus |
michael@0 | 20 | |
michael@0 | 21 | #include "mozilla/Array.h" |
michael@0 | 22 | |
michael@0 | 23 | namespace mozilla { |
michael@0 | 24 | |
michael@0 | 25 | /* |
michael@0 | 26 | * Safely subtract two pointers when it is known that end >= begin. This avoids |
michael@0 | 27 | * the common compiler bug that if (size_t(end) - size_t(begin)) has the MSB |
michael@0 | 28 | * set, the unsigned subtraction followed by right shift will produce -1, or |
michael@0 | 29 | * size_t(-1), instead of the real difference. |
michael@0 | 30 | */ |
michael@0 | 31 | template<class T> |
michael@0 | 32 | MOZ_ALWAYS_INLINE size_t |
michael@0 | 33 | PointerRangeSize(T* begin, T* end) |
michael@0 | 34 | { |
michael@0 | 35 | MOZ_ASSERT(end >= begin); |
michael@0 | 36 | return (size_t(end) - size_t(begin)) / sizeof(T); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | /* |
michael@0 | 40 | * Compute the length of an array with constant length. (Use of this method |
michael@0 | 41 | * with a non-array pointer will not compile.) |
michael@0 | 42 | * |
michael@0 | 43 | * Beware of the implicit trailing '\0' when using this with string constants. |
michael@0 | 44 | */ |
michael@0 | 45 | template<typename T, size_t N> |
michael@0 | 46 | MOZ_CONSTEXPR size_t |
michael@0 | 47 | ArrayLength(T (&arr)[N]) |
michael@0 | 48 | { |
michael@0 | 49 | return N; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | template<typename T, size_t N> |
michael@0 | 53 | MOZ_CONSTEXPR size_t |
michael@0 | 54 | ArrayLength(const Array<T, N>& arr) |
michael@0 | 55 | { |
michael@0 | 56 | return N; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | /* |
michael@0 | 60 | * Compute the address one past the last element of a constant-length array. |
michael@0 | 61 | * |
michael@0 | 62 | * Beware of the implicit trailing '\0' when using this with string constants. |
michael@0 | 63 | */ |
michael@0 | 64 | template<typename T, size_t N> |
michael@0 | 65 | MOZ_CONSTEXPR T* |
michael@0 | 66 | ArrayEnd(T (&arr)[N]) |
michael@0 | 67 | { |
michael@0 | 68 | return arr + ArrayLength(arr); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | template<typename T, size_t N> |
michael@0 | 72 | MOZ_CONSTEXPR T* |
michael@0 | 73 | ArrayEnd(Array<T, N>& arr) |
michael@0 | 74 | { |
michael@0 | 75 | return &arr[0] + ArrayLength(arr); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | template<typename T, size_t N> |
michael@0 | 79 | MOZ_CONSTEXPR const T* |
michael@0 | 80 | ArrayEnd(const Array<T, N>& arr) |
michael@0 | 81 | { |
michael@0 | 82 | return &arr[0] + ArrayLength(arr); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | namespace detail { |
michael@0 | 86 | |
michael@0 | 87 | /* |
michael@0 | 88 | * Helper for the MOZ_ARRAY_LENGTH() macro to make the length a typesafe |
michael@0 | 89 | * compile-time constant even on compilers lacking constexpr support. |
michael@0 | 90 | */ |
michael@0 | 91 | template <typename T, size_t N> |
michael@0 | 92 | char (&ArrayLengthHelper(T (&array)[N]))[N]; |
michael@0 | 93 | |
michael@0 | 94 | } /* namespace detail */ |
michael@0 | 95 | |
michael@0 | 96 | } /* namespace mozilla */ |
michael@0 | 97 | |
michael@0 | 98 | #endif /* __cplusplus */ |
michael@0 | 99 | |
michael@0 | 100 | /* |
michael@0 | 101 | * MOZ_ARRAY_LENGTH() is an alternative to mozilla::ArrayLength() for C files |
michael@0 | 102 | * that can't use C++ template functions and for static_assert() calls that |
michael@0 | 103 | * can't call ArrayLength() when it is not a C++11 constexpr function. |
michael@0 | 104 | */ |
michael@0 | 105 | #ifdef __cplusplus |
michael@0 | 106 | # define MOZ_ARRAY_LENGTH(array) sizeof(mozilla::detail::ArrayLengthHelper(array)) |
michael@0 | 107 | #else |
michael@0 | 108 | # define MOZ_ARRAY_LENGTH(array) (sizeof(array)/sizeof((array)[0])) |
michael@0 | 109 | #endif |
michael@0 | 110 | |
michael@0 | 111 | #endif /* mozilla_ArrayUtils_h */ |