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 | /* C++11-style, but C++98-usable, "move references" implementation. */ |
michael@0 | 8 | |
michael@0 | 9 | #ifndef mozilla_Move_h |
michael@0 | 10 | #define mozilla_Move_h |
michael@0 | 11 | |
michael@0 | 12 | #include "mozilla/TypeTraits.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | |
michael@0 | 16 | /* |
michael@0 | 17 | * "Move" References |
michael@0 | 18 | * |
michael@0 | 19 | * Some types can be copied much more efficiently if we know the original's |
michael@0 | 20 | * value need not be preserved --- that is, if we are doing a "move", not a |
michael@0 | 21 | * "copy". For example, if we have: |
michael@0 | 22 | * |
michael@0 | 23 | * Vector<T> u; |
michael@0 | 24 | * Vector<T> v(u); |
michael@0 | 25 | * |
michael@0 | 26 | * the constructor for v must apply a copy constructor to each element of u --- |
michael@0 | 27 | * taking time linear in the length of u. However, if we know we will not need u |
michael@0 | 28 | * any more once v has been initialized, then we could initialize v very |
michael@0 | 29 | * efficiently simply by stealing u's dynamically allocated buffer and giving it |
michael@0 | 30 | * to v --- a constant-time operation, regardless of the size of u. |
michael@0 | 31 | * |
michael@0 | 32 | * Moves often appear in container implementations. For example, when we append |
michael@0 | 33 | * to a vector, we may need to resize its buffer. This entails moving each of |
michael@0 | 34 | * its extant elements from the old, smaller buffer to the new, larger buffer. |
michael@0 | 35 | * But once the elements have been migrated, we're just going to throw away the |
michael@0 | 36 | * old buffer; we don't care if they still have their values. So if the vector's |
michael@0 | 37 | * element type can implement "move" more efficiently than "copy", the vector |
michael@0 | 38 | * resizing should by all means use a "move" operation. Hash tables should also |
michael@0 | 39 | * use moves when resizing their internal array as entries are added and |
michael@0 | 40 | * removed. |
michael@0 | 41 | * |
michael@0 | 42 | * The details of the optimization, and whether it's worth applying, vary |
michael@0 | 43 | * from one type to the next: copying an 'int' is as cheap as moving it, so |
michael@0 | 44 | * there's no benefit in distinguishing 'int' moves from copies. And while |
michael@0 | 45 | * some constructor calls for complex types are moves, many really have to |
michael@0 | 46 | * be copies, and can't be optimized this way. So we need: |
michael@0 | 47 | * |
michael@0 | 48 | * 1) a way for a type (like Vector) to announce that it can be moved more |
michael@0 | 49 | * efficiently than it can be copied, and provide an implementation of that |
michael@0 | 50 | * move operation; and |
michael@0 | 51 | * |
michael@0 | 52 | * 2) a way for a particular invocation of a copy constructor to say that it's |
michael@0 | 53 | * really a move, not a copy, and that the value of the original isn't |
michael@0 | 54 | * important afterwards (although it must still be safe to destroy). |
michael@0 | 55 | * |
michael@0 | 56 | * If a constructor has a single argument of type 'T&&' (an 'rvalue reference |
michael@0 | 57 | * to T'), that indicates that it is a 'move constructor'. That's 1). It should |
michael@0 | 58 | * move, not copy, its argument into the object being constructed. It may leave |
michael@0 | 59 | * the original in any safely-destructible state. |
michael@0 | 60 | * |
michael@0 | 61 | * If a constructor's argument is an rvalue, as in 'C(f(x))' or 'C(x + y)', as |
michael@0 | 62 | * opposed to an lvalue, as in 'C(x)', then overload resolution will prefer the |
michael@0 | 63 | * move constructor, if there is one. The 'mozilla::Move' function, defined in |
michael@0 | 64 | * this file, is an identity function you can use in a constructor invocation to |
michael@0 | 65 | * make any argument into an rvalue, like this: C(Move(x)). That's 2). (You |
michael@0 | 66 | * could use any function that works, but 'Move' indicates your intention |
michael@0 | 67 | * clearly.) |
michael@0 | 68 | * |
michael@0 | 69 | * Where we might define a copy constructor for a class C like this: |
michael@0 | 70 | * |
michael@0 | 71 | * C(const C& rhs) { ... copy rhs to this ... } |
michael@0 | 72 | * |
michael@0 | 73 | * we would declare a move constructor like this: |
michael@0 | 74 | * |
michael@0 | 75 | * C(C&& rhs) { .. move rhs to this ... } |
michael@0 | 76 | * |
michael@0 | 77 | * And where we might perform a copy like this: |
michael@0 | 78 | * |
michael@0 | 79 | * C c2(c1); |
michael@0 | 80 | * |
michael@0 | 81 | * we would perform a move like this: |
michael@0 | 82 | * |
michael@0 | 83 | * C c2(Move(c1)); |
michael@0 | 84 | * |
michael@0 | 85 | * Note that 'T&&' implicitly converts to 'T&'. So you can pass a 'T&&' to an |
michael@0 | 86 | * ordinary copy constructor for a type that doesn't support a special move |
michael@0 | 87 | * constructor, and you'll just get a copy. This means that templates can use |
michael@0 | 88 | * Move whenever they know they won't use the original value any more, even if |
michael@0 | 89 | * they're not sure whether the type at hand has a specialized move constructor. |
michael@0 | 90 | * If it doesn't, the 'T&&' will just convert to a 'T&', and the ordinary copy |
michael@0 | 91 | * constructor will apply. |
michael@0 | 92 | * |
michael@0 | 93 | * A class with a move constructor can also provide a move assignment operator. |
michael@0 | 94 | * A generic definition would run this's destructor, and then apply the move |
michael@0 | 95 | * constructor to *this's memory. A typical definition: |
michael@0 | 96 | * |
michael@0 | 97 | * C& operator=(C&& rhs) { |
michael@0 | 98 | * MOZ_ASSERT(&rhs != this, "self-moves are prohibited"); |
michael@0 | 99 | * this->~C(); |
michael@0 | 100 | * new(this) C(Move(rhs)); |
michael@0 | 101 | * return *this; |
michael@0 | 102 | * } |
michael@0 | 103 | * |
michael@0 | 104 | * With that in place, one can write move assignments like this: |
michael@0 | 105 | * |
michael@0 | 106 | * c2 = Move(c1); |
michael@0 | 107 | * |
michael@0 | 108 | * This destroys c2, moves c1's value to c2, and leaves c1 in an undefined but |
michael@0 | 109 | * destructible state. |
michael@0 | 110 | * |
michael@0 | 111 | * As we say, a move must leave the original in a "destructible" state. The |
michael@0 | 112 | * original's destructor will still be called, so if a move doesn't |
michael@0 | 113 | * actually steal all its resources, that's fine. We require only that the |
michael@0 | 114 | * move destination must take on the original's value; and that destructing |
michael@0 | 115 | * the original must not break the move destination. |
michael@0 | 116 | * |
michael@0 | 117 | * (Opinions differ on whether move assignment operators should deal with move |
michael@0 | 118 | * assignment of an object onto itself. It seems wise to either handle that |
michael@0 | 119 | * case, or assert that it does not occur.) |
michael@0 | 120 | * |
michael@0 | 121 | * Forwarding: |
michael@0 | 122 | * |
michael@0 | 123 | * Sometimes we want copy construction or assignment if we're passed an ordinary |
michael@0 | 124 | * value, but move construction if passed an rvalue reference. For example, if |
michael@0 | 125 | * our constructor takes two arguments and either could usefully be a move, it |
michael@0 | 126 | * seems silly to write out all four combinations: |
michael@0 | 127 | * |
michael@0 | 128 | * C::C(X& x, Y& y) : x(x), y(y) { } |
michael@0 | 129 | * C::C(X& x, Y&& y) : x(x), y(Move(y)) { } |
michael@0 | 130 | * C::C(X&& x, Y& y) : x(Move(x)), y(y) { } |
michael@0 | 131 | * C::C(X&& x, Y&& y) : x(Move(x)), y(Move(y)) { } |
michael@0 | 132 | * |
michael@0 | 133 | * To avoid this, C++11 has tweaks to make it possible to write what you mean. |
michael@0 | 134 | * The four constructor overloads above can be written as one constructor |
michael@0 | 135 | * template like so[0]: |
michael@0 | 136 | * |
michael@0 | 137 | * template <typename XArg, typename YArg> |
michael@0 | 138 | * C::C(XArg&& x, YArg&& y) : x(Forward<XArg>(x)), y(Forward<YArg>(y)) { } |
michael@0 | 139 | * |
michael@0 | 140 | * ("'Don't Repeat Yourself'? What's that?") |
michael@0 | 141 | * |
michael@0 | 142 | * This takes advantage of two new rules in C++11: |
michael@0 | 143 | * |
michael@0 | 144 | * - First, when a function template takes an argument that is an rvalue |
michael@0 | 145 | * reference to a template argument (like 'XArg&& x' and 'YArg&& y' above), |
michael@0 | 146 | * then when the argument is applied to an lvalue, the template argument |
michael@0 | 147 | * resolves to 'T &'; and when it is applied to an rvalue, the template |
michael@0 | 148 | * argument resolves to 'T &&'. Thus, in a call to C::C like: |
michael@0 | 149 | * |
michael@0 | 150 | * X foo(int); |
michael@0 | 151 | * Y yy; |
michael@0 | 152 | * |
michael@0 | 153 | * C(foo(5), yy) |
michael@0 | 154 | * |
michael@0 | 155 | * XArg would resolve to 'X&&', and YArg would resolve to 'Y&'. |
michael@0 | 156 | * |
michael@0 | 157 | * - Second, Whereas C++ used to forbid references to references, C++11 defines |
michael@0 | 158 | * 'collapsing rules': 'T& &', 'T&& &', and 'T& &&' (that is, any combination |
michael@0 | 159 | * involving an lvalue reference) now collapse to simply 'T&'; and 'T&& &&' |
michael@0 | 160 | * collapses to 'T&&'. |
michael@0 | 161 | * |
michael@0 | 162 | * Thus, in the call above, 'XArg&&' is 'X&& &&', collapsing to 'X&&'; and |
michael@0 | 163 | * 'YArg&&' is 'Y& &&', which collapses to 'Y &'. Because the arguments are |
michael@0 | 164 | * declared as rvalue references to template arguments, the rvalue-ness |
michael@0 | 165 | * "shines through" where present. |
michael@0 | 166 | * |
michael@0 | 167 | * Then, the 'Forward<T>' function --- you must invoke 'Forward' with its type |
michael@0 | 168 | * argument --- returns an lvalue reference or an rvalue reference to its |
michael@0 | 169 | * argument, depending on what T is. In our unified constructor definition, that |
michael@0 | 170 | * means that we'll invoke either the copy or move constructors for x and y, |
michael@0 | 171 | * depending on what we gave C's constructor. In our call, we'll move 'foo()' |
michael@0 | 172 | * into 'x', but copy 'yy' into 'y'. |
michael@0 | 173 | * |
michael@0 | 174 | * This header file defines Move and Forward in the mozilla namespace. It's up |
michael@0 | 175 | * to individual containers to annotate moves as such, by calling Move; and it's |
michael@0 | 176 | * up to individual types to define move constructors and assignment operators |
michael@0 | 177 | * when valuable. |
michael@0 | 178 | * |
michael@0 | 179 | * (C++11 says that the <utility> header file should define 'std::move' and |
michael@0 | 180 | * 'std::forward', which are just like our 'Move' and 'Forward'; but those |
michael@0 | 181 | * definitions aren't available in that header on all our platforms, so we |
michael@0 | 182 | * define them ourselves here.) |
michael@0 | 183 | * |
michael@0 | 184 | * 0. This pattern is known as "perfect forwarding". Interestingly, it is not |
michael@0 | 185 | * actually perfect, and it can't forward all possible argument expressions! |
michael@0 | 186 | * There are two issues: one that's a C++11 issue, and one that's a legacy |
michael@0 | 187 | * compiler issue. |
michael@0 | 188 | * |
michael@0 | 189 | * The C++11 issue is that you can't form a reference to a bit-field. As a |
michael@0 | 190 | * workaround, assign the bit-field to a local variable and use that: |
michael@0 | 191 | * |
michael@0 | 192 | * // C is as above |
michael@0 | 193 | * struct S { int x : 1; } s; |
michael@0 | 194 | * C(s.x, 0); // BAD: s.x is a reference to a bit-field, can't form those |
michael@0 | 195 | * int tmp = s.x; |
michael@0 | 196 | * C(tmp, 0); // OK: tmp not a bit-field |
michael@0 | 197 | * |
michael@0 | 198 | * The legacy issue is that when we don't have true nullptr and must emulate |
michael@0 | 199 | * it (gcc 4.4/4.5), forwarding |nullptr| results in an |int| or |long| |
michael@0 | 200 | * forwarded reference. But such a reference, even if its value is a null |
michael@0 | 201 | * pointer constant expression, is not itself a null pointer constant |
michael@0 | 202 | * expression. This causes -Werror=conversion-null errors and pointer-to- |
michael@0 | 203 | * integer comparison errors. Until we always have true nullptr, users of |
michael@0 | 204 | * forwarding methods must not pass |nullptr| to them. |
michael@0 | 205 | */ |
michael@0 | 206 | |
michael@0 | 207 | /** |
michael@0 | 208 | * Identical to std::Move(); this is necessary until our stlport supports |
michael@0 | 209 | * std::move(). |
michael@0 | 210 | */ |
michael@0 | 211 | template<typename T> |
michael@0 | 212 | inline typename RemoveReference<T>::Type&& |
michael@0 | 213 | Move(T&& a) |
michael@0 | 214 | { |
michael@0 | 215 | return static_cast<typename RemoveReference<T>::Type&&>(a); |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | /** |
michael@0 | 219 | * These two overloads are identical to std::forward(); they are necessary until |
michael@0 | 220 | * our stlport supports std::forward(). |
michael@0 | 221 | */ |
michael@0 | 222 | template<typename T> |
michael@0 | 223 | inline T&& |
michael@0 | 224 | Forward(typename RemoveReference<T>::Type& a) |
michael@0 | 225 | { |
michael@0 | 226 | return static_cast<T&&>(a); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | template<typename T> |
michael@0 | 230 | inline T&& |
michael@0 | 231 | Forward(typename RemoveReference<T>::Type&& t) |
michael@0 | 232 | { |
michael@0 | 233 | static_assert(!IsLvalueReference<T>::value, |
michael@0 | 234 | "misuse of Forward detected! try the other overload"); |
michael@0 | 235 | return static_cast<T&&>(t); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | /** Swap |t| and |u| using move-construction if possible. */ |
michael@0 | 239 | template<typename T> |
michael@0 | 240 | inline void |
michael@0 | 241 | Swap(T& t, T& u) |
michael@0 | 242 | { |
michael@0 | 243 | T tmp(Move(t)); |
michael@0 | 244 | t = Move(u); |
michael@0 | 245 | u = Move(tmp); |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | } // namespace mozilla |
michael@0 | 249 | |
michael@0 | 250 | #endif /* mozilla_Move_h */ |