mfbt/Maybe.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.

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 /* A class for lazily constructing an object without sticking it on the heap. */
michael@0 8
michael@0 9 #ifndef mozilla_Maybe_h
michael@0 10 #define mozilla_Maybe_h
michael@0 11
michael@0 12 #include "mozilla/Alignment.h"
michael@0 13 #include "mozilla/Assertions.h"
michael@0 14
michael@0 15 // For placement new
michael@0 16 #include <new>
michael@0 17
michael@0 18 namespace mozilla {
michael@0 19
michael@0 20 /*
michael@0 21 * Small utility for lazily constructing objects without using dynamic storage.
michael@0 22 * When a Maybe<T> is constructed, it is |empty()|, i.e., no value of T has
michael@0 23 * been constructed and no T destructor will be called when the Maybe<T> is
michael@0 24 * destroyed. Upon calling |construct|, a T object will be constructed with the
michael@0 25 * given arguments and that object will be destroyed when the owning Maybe<T>
michael@0 26 * is destroyed.
michael@0 27 *
michael@0 28 * N.B. GCC seems to miss some optimizations with Maybe and may generate extra
michael@0 29 * branches/loads/stores. Use with caution on hot paths.
michael@0 30 */
michael@0 31 template<class T>
michael@0 32 class Maybe
michael@0 33 {
michael@0 34 AlignedStorage2<T> storage;
michael@0 35 bool constructed;
michael@0 36
michael@0 37 T& asT() { return *storage.addr(); }
michael@0 38
michael@0 39 public:
michael@0 40 Maybe() { constructed = false; }
michael@0 41 ~Maybe() { if (constructed) asT().~T(); }
michael@0 42
michael@0 43 bool empty() const { return !constructed; }
michael@0 44
michael@0 45 void construct() {
michael@0 46 MOZ_ASSERT(!constructed);
michael@0 47 ::new (storage.addr()) T();
michael@0 48 constructed = true;
michael@0 49 }
michael@0 50
michael@0 51 template<class T1>
michael@0 52 void construct(const T1& t1) {
michael@0 53 MOZ_ASSERT(!constructed);
michael@0 54 ::new (storage.addr()) T(t1);
michael@0 55 constructed = true;
michael@0 56 }
michael@0 57
michael@0 58 template<class T1, class T2>
michael@0 59 void construct(const T1& t1, const T2& t2) {
michael@0 60 MOZ_ASSERT(!constructed);
michael@0 61 ::new (storage.addr()) T(t1, t2);
michael@0 62 constructed = true;
michael@0 63 }
michael@0 64
michael@0 65 template<class T1, class T2, class T3>
michael@0 66 void construct(const T1& t1, const T2& t2, const T3& t3) {
michael@0 67 MOZ_ASSERT(!constructed);
michael@0 68 ::new (storage.addr()) T(t1, t2, t3);
michael@0 69 constructed = true;
michael@0 70 }
michael@0 71
michael@0 72 template<class T1, class T2, class T3, class T4>
michael@0 73 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4) {
michael@0 74 MOZ_ASSERT(!constructed);
michael@0 75 ::new (storage.addr()) T(t1, t2, t3, t4);
michael@0 76 constructed = true;
michael@0 77 }
michael@0 78
michael@0 79 template<class T1, class T2, class T3, class T4, class T5>
michael@0 80 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) {
michael@0 81 MOZ_ASSERT(!constructed);
michael@0 82 ::new (storage.addr()) T(t1, t2, t3, t4, t5);
michael@0 83 constructed = true;
michael@0 84 }
michael@0 85
michael@0 86 template<class T1, class T2, class T3, class T4, class T5,
michael@0 87 class T6>
michael@0 88 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
michael@0 89 const T6& t6) {
michael@0 90 MOZ_ASSERT(!constructed);
michael@0 91 ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6);
michael@0 92 constructed = true;
michael@0 93 }
michael@0 94
michael@0 95 template<class T1, class T2, class T3, class T4, class T5,
michael@0 96 class T6, class T7>
michael@0 97 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
michael@0 98 const T6& t6, const T7& t7) {
michael@0 99 MOZ_ASSERT(!constructed);
michael@0 100 ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7);
michael@0 101 constructed = true;
michael@0 102 }
michael@0 103
michael@0 104 template<class T1, class T2, class T3, class T4, class T5,
michael@0 105 class T6, class T7, class T8>
michael@0 106 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
michael@0 107 const T6& t6, const T7& t7, const T8& t8) {
michael@0 108 MOZ_ASSERT(!constructed);
michael@0 109 ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8);
michael@0 110 constructed = true;
michael@0 111 }
michael@0 112
michael@0 113 template<class T1, class T2, class T3, class T4, class T5,
michael@0 114 class T6, class T7, class T8, class T9>
michael@0 115 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
michael@0 116 const T6& t6, const T7& t7, const T8& t8, const T9& t9) {
michael@0 117 MOZ_ASSERT(!constructed);
michael@0 118 ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8, t9);
michael@0 119 constructed = true;
michael@0 120 }
michael@0 121
michael@0 122 template<class T1, class T2, class T3, class T4, class T5,
michael@0 123 class T6, class T7, class T8, class T9, class T10>
michael@0 124 void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5,
michael@0 125 const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10) {
michael@0 126 MOZ_ASSERT(!constructed);
michael@0 127 ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
michael@0 128 constructed = true;
michael@0 129 }
michael@0 130
michael@0 131 T* addr() {
michael@0 132 MOZ_ASSERT(constructed);
michael@0 133 return &asT();
michael@0 134 }
michael@0 135
michael@0 136 T& ref() {
michael@0 137 MOZ_ASSERT(constructed);
michael@0 138 return asT();
michael@0 139 }
michael@0 140
michael@0 141 const T& ref() const {
michael@0 142 MOZ_ASSERT(constructed);
michael@0 143 return const_cast<Maybe*>(this)->asT();
michael@0 144 }
michael@0 145
michael@0 146 void destroy() {
michael@0 147 ref().~T();
michael@0 148 constructed = false;
michael@0 149 }
michael@0 150
michael@0 151 void destroyIfConstructed() {
michael@0 152 if (!empty())
michael@0 153 destroy();
michael@0 154 }
michael@0 155
michael@0 156 private:
michael@0 157 Maybe(const Maybe& other) MOZ_DELETE;
michael@0 158 const Maybe& operator=(const Maybe& other) MOZ_DELETE;
michael@0 159 };
michael@0 160
michael@0 161 } // namespace mozilla
michael@0 162
michael@0 163 #endif /* mozilla_Maybe_h */

mercurial