mfbt/Scoped.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 number of structures to simplify scope-based RAII management. */
michael@0 8
michael@0 9 #ifndef mozilla_Scoped_h
michael@0 10 #define mozilla_Scoped_h
michael@0 11
michael@0 12 /*
michael@0 13 * Resource Acquisition Is Initialization is a programming idiom used
michael@0 14 * to write robust code that is able to deallocate resources properly,
michael@0 15 * even in presence of execution errors or exceptions that need to be
michael@0 16 * propagated. The Scoped* classes defined in this header perform the
michael@0 17 * deallocation of the resource they hold once program execution
michael@0 18 * reaches the end of the scope for which they have been defined.
michael@0 19 *
michael@0 20 * This header provides the following RAII classes:
michael@0 21 *
michael@0 22 * - |ScopedFreePtr| - a container for a pointer, that automatically calls
michael@0 23 * |free()| at the end of the scope;
michael@0 24 * - |ScopedDeletePtr| - a container for a pointer, that automatically calls
michael@0 25 * |delete| at the end of the scope;
michael@0 26 * - |ScopedDeleteArray| - a container for a pointer to an array, that
michael@0 27 * automatically calls |delete[]| at the end of the scope.
michael@0 28 *
michael@0 29 * The general scenario for each of the RAII classes is the following:
michael@0 30 *
michael@0 31 * ScopedClass foo(create_value());
michael@0 32 * // ... In this scope, |foo| is defined. Use |foo.get()| or |foo.rwget()|
michael@0 33 * to access the value.
michael@0 34 * // ... In case of |return| or |throw|, |foo| is deallocated automatically.
michael@0 35 * // ... If |foo| needs to be returned or stored, use |foo.forget()|
michael@0 36 *
michael@0 37 * Note that the RAII classes defined in this header do _not_ perform any form
michael@0 38 * of reference-counting or garbage-collection. These classes have exactly two
michael@0 39 * behaviors:
michael@0 40 *
michael@0 41 * - if |forget()| has not been called, the resource is always deallocated at
michael@0 42 * the end of the scope;
michael@0 43 * - if |forget()| has been called, any control on the resource is unbound
michael@0 44 * and the resource is not deallocated by the class.
michael@0 45 *
michael@0 46 * Extension:
michael@0 47 *
michael@0 48 * In addition, this header provides class |Scoped| and macros |SCOPED_TEMPLATE|
michael@0 49 * and |MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE| to simplify the definition
michael@0 50 * of RAII classes for other scenarios. These macros have been used to
michael@0 51 * automatically close file descriptors/file handles when reaching the end of
michael@0 52 * the scope, graphics contexts, etc.
michael@0 53 */
michael@0 54
michael@0 55 #include "mozilla/Assertions.h"
michael@0 56 #include "mozilla/Attributes.h"
michael@0 57 #include "mozilla/GuardObjects.h"
michael@0 58 #include "mozilla/Move.h"
michael@0 59 #include "mozilla/NullPtr.h"
michael@0 60
michael@0 61 namespace mozilla {
michael@0 62
michael@0 63 /*
michael@0 64 * Scoped is a helper to create RAII wrappers
michael@0 65 * Type argument |Traits| is expected to have the following structure:
michael@0 66 *
michael@0 67 * struct Traits {
michael@0 68 * // Define the type of the value stored in the wrapper
michael@0 69 * typedef value_type type;
michael@0 70 * // Returns the value corresponding to the uninitialized or freed state
michael@0 71 * const static type empty();
michael@0 72 * // Release resources corresponding to the wrapped value
michael@0 73 * // This function is responsible for not releasing an |empty| value
michael@0 74 * const static void release(type);
michael@0 75 * }
michael@0 76 */
michael@0 77 template<typename Traits>
michael@0 78 class Scoped
michael@0 79 {
michael@0 80 public:
michael@0 81 typedef typename Traits::type Resource;
michael@0 82
michael@0 83 explicit Scoped(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM)
michael@0 84 : value(Traits::empty())
michael@0 85 {
michael@0 86 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 87 }
michael@0 88
michael@0 89 explicit Scoped(const Resource& v
michael@0 90 MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
michael@0 91 : value(v)
michael@0 92 {
michael@0 93 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 94 }
michael@0 95
michael@0 96 /* Move constructor. */
michael@0 97 explicit Scoped(Scoped&& v
michael@0 98 MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
michael@0 99 : value(Move(v.value))
michael@0 100 {
michael@0 101 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
michael@0 102 v.value = Traits::empty();
michael@0 103 }
michael@0 104
michael@0 105 ~Scoped() {
michael@0 106 Traits::release(value);
michael@0 107 }
michael@0 108
michael@0 109 // Constant getter
michael@0 110 operator const Resource&() const { return value; }
michael@0 111 const Resource& operator->() const { return value; }
michael@0 112 const Resource& get() const { return value; }
michael@0 113 // Non-constant getter.
michael@0 114 Resource& rwget() { return value; }
michael@0 115
michael@0 116 /*
michael@0 117 * Forget the resource.
michael@0 118 *
michael@0 119 * Once |forget| has been called, the |Scoped| is neutralized, i.e. it will
michael@0 120 * have no effect at destruction (unless it is reset to another resource by
michael@0 121 * |operator=|).
michael@0 122 *
michael@0 123 * @return The original resource.
michael@0 124 */
michael@0 125 Resource forget() {
michael@0 126 Resource tmp = value;
michael@0 127 value = Traits::empty();
michael@0 128 return tmp;
michael@0 129 }
michael@0 130
michael@0 131 /*
michael@0 132 * Perform immediate clean-up of this |Scoped|.
michael@0 133 *
michael@0 134 * If this |Scoped| is currently empty, this method has no effect.
michael@0 135 */
michael@0 136 void dispose() {
michael@0 137 Traits::release(value);
michael@0 138 value = Traits::empty();
michael@0 139 }
michael@0 140
michael@0 141 bool operator==(const Resource& other) const {
michael@0 142 return value == other;
michael@0 143 }
michael@0 144
michael@0 145 /*
michael@0 146 * Replace the resource with another resource.
michael@0 147 *
michael@0 148 * Calling |operator=| has the side-effect of triggering clean-up. If you do
michael@0 149 * not want to trigger clean-up, you should first invoke |forget|.
michael@0 150 *
michael@0 151 * @return this
michael@0 152 */
michael@0 153 Scoped& operator=(const Resource& other) {
michael@0 154 return reset(other);
michael@0 155 }
michael@0 156 Scoped& reset(const Resource& other) {
michael@0 157 Traits::release(value);
michael@0 158 value = other;
michael@0 159 return *this;
michael@0 160 }
michael@0 161
michael@0 162 /* Move assignment operator. */
michael@0 163 Scoped& operator=(Scoped&& rhs) {
michael@0 164 MOZ_ASSERT(&rhs != this, "self-move-assignment not allowed");
michael@0 165 this->~Scoped();
michael@0 166 new(this) Scoped(Move(rhs));
michael@0 167 return *this;
michael@0 168 }
michael@0 169
michael@0 170 private:
michael@0 171 explicit Scoped(const Scoped& value) MOZ_DELETE;
michael@0 172 Scoped& operator=(const Scoped& value) MOZ_DELETE;
michael@0 173
michael@0 174 private:
michael@0 175 Resource value;
michael@0 176 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
michael@0 177 };
michael@0 178
michael@0 179 /*
michael@0 180 * SCOPED_TEMPLATE defines a templated class derived from Scoped
michael@0 181 * This allows to implement templates such as ScopedFreePtr.
michael@0 182 *
michael@0 183 * @param name The name of the class to define.
michael@0 184 * @param Traits A struct implementing clean-up. See the implementations
michael@0 185 * for more details.
michael@0 186 */
michael@0 187 #define SCOPED_TEMPLATE(name, Traits) \
michael@0 188 template<typename Type> \
michael@0 189 struct name : public mozilla::Scoped<Traits<Type> > \
michael@0 190 { \
michael@0 191 typedef mozilla::Scoped<Traits<Type> > Super; \
michael@0 192 typedef typename Super::Resource Resource; \
michael@0 193 name& operator=(Resource rhs) { \
michael@0 194 Super::operator=(rhs); \
michael@0 195 return *this; \
michael@0 196 } \
michael@0 197 name& operator=(name&& rhs) { \
michael@0 198 Super::operator=(Move(rhs)); \
michael@0 199 return *this; \
michael@0 200 } \
michael@0 201 explicit name(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM) \
michael@0 202 : Super(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT) \
michael@0 203 {} \
michael@0 204 explicit name(Resource rhs \
michael@0 205 MOZ_GUARD_OBJECT_NOTIFIER_PARAM) \
michael@0 206 : Super(rhs \
michael@0 207 MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) \
michael@0 208 {} \
michael@0 209 explicit name(name&& rhs \
michael@0 210 MOZ_GUARD_OBJECT_NOTIFIER_PARAM) \
michael@0 211 : Super(Move(rhs) \
michael@0 212 MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) \
michael@0 213 {} \
michael@0 214 private: \
michael@0 215 explicit name(name&) MOZ_DELETE; \
michael@0 216 name& operator=(name&) MOZ_DELETE; \
michael@0 217 };
michael@0 218
michael@0 219 /*
michael@0 220 * ScopedFreePtr is a RAII wrapper for pointers that need to be free()d.
michael@0 221 *
michael@0 222 * struct S { ... };
michael@0 223 * ScopedFreePtr<S> foo = malloc(sizeof(S));
michael@0 224 * ScopedFreePtr<char> bar = strdup(str);
michael@0 225 */
michael@0 226 template<typename T>
michael@0 227 struct ScopedFreePtrTraits
michael@0 228 {
michael@0 229 typedef T* type;
michael@0 230 static T* empty() { return nullptr; }
michael@0 231 static void release(T* ptr) { free(ptr); }
michael@0 232 };
michael@0 233 SCOPED_TEMPLATE(ScopedFreePtr, ScopedFreePtrTraits)
michael@0 234
michael@0 235 /*
michael@0 236 * ScopedDeletePtr is a RAII wrapper for pointers that need to be deleted.
michael@0 237 *
michael@0 238 * struct S { ... };
michael@0 239 * ScopedDeletePtr<S> foo = new S();
michael@0 240 */
michael@0 241 template<typename T>
michael@0 242 struct ScopedDeletePtrTraits : public ScopedFreePtrTraits<T>
michael@0 243 {
michael@0 244 static void release(T* ptr) { delete ptr; }
michael@0 245 };
michael@0 246 SCOPED_TEMPLATE(ScopedDeletePtr, ScopedDeletePtrTraits)
michael@0 247
michael@0 248 /*
michael@0 249 * ScopedDeleteArray is a RAII wrapper for pointers that need to be delete[]ed.
michael@0 250 *
michael@0 251 * struct S { ... };
michael@0 252 * ScopedDeleteArray<S> foo = new S[42];
michael@0 253 */
michael@0 254 template<typename T>
michael@0 255 struct ScopedDeleteArrayTraits : public ScopedFreePtrTraits<T>
michael@0 256 {
michael@0 257 static void release(T* ptr) { delete [] ptr; }
michael@0 258 };
michael@0 259 SCOPED_TEMPLATE(ScopedDeleteArray, ScopedDeleteArrayTraits)
michael@0 260
michael@0 261 /*
michael@0 262 * MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE makes it easy to create scoped
michael@0 263 * pointers for types with custom deleters; just overload
michael@0 264 * TypeSpecificDelete(T*) in the same namespace as T to call the deleter for
michael@0 265 * type T.
michael@0 266 *
michael@0 267 * @param name The name of the class to define.
michael@0 268 * @param Type A struct implementing clean-up. See the implementations
michael@0 269 * for more details.
michael@0 270 * *param Deleter The function that is used to delete/destroy/free a
michael@0 271 * non-null value of Type*.
michael@0 272 *
michael@0 273 * Example:
michael@0 274 *
michael@0 275 * MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(ScopedPRFileDesc, PRFileDesc, \
michael@0 276 * PR_Close)
michael@0 277 * ...
michael@0 278 * {
michael@0 279 * ScopedPRFileDesc file(PR_OpenFile(...));
michael@0 280 * ...
michael@0 281 * } // file is closed with PR_Close here
michael@0 282 */
michael@0 283 #define MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(name, Type, Deleter) \
michael@0 284 template <> inline void TypeSpecificDelete(Type * value) { Deleter(value); } \
michael@0 285 typedef ::mozilla::TypeSpecificScopedPointer<Type> name;
michael@0 286
michael@0 287 template <typename T> void TypeSpecificDelete(T * value);
michael@0 288
michael@0 289 template <typename T>
michael@0 290 struct TypeSpecificScopedPointerTraits
michael@0 291 {
michael@0 292 typedef T* type;
michael@0 293 const static type empty() { return nullptr; }
michael@0 294 const static void release(type value)
michael@0 295 {
michael@0 296 if (value)
michael@0 297 TypeSpecificDelete(value);
michael@0 298 }
michael@0 299 };
michael@0 300
michael@0 301 SCOPED_TEMPLATE(TypeSpecificScopedPointer, TypeSpecificScopedPointerTraits)
michael@0 302
michael@0 303 } /* namespace mozilla */
michael@0 304
michael@0 305 #endif /* mozilla_Scoped_h */

mercurial