mfbt/WeakPtr.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 /* Weak pointer functionality, implemented as a mixin for use with any class. */
michael@0 8
michael@0 9 /**
michael@0 10 * SupportsWeakPtr lets you have a pointer to an object 'Foo' without affecting
michael@0 11 * its lifetime. It works by creating a single shared reference counted object
michael@0 12 * (WeakReference) that each WeakPtr will access 'Foo' through. This lets 'Foo'
michael@0 13 * clear the pointer in the WeakReference without having to know about all of
michael@0 14 * the WeakPtrs to it and allows the WeakReference to live beyond the lifetime
michael@0 15 * of 'Foo'.
michael@0 16 *
michael@0 17 * PLEASE NOTE: This weak pointer implementation is not thread-safe.
michael@0 18 *
michael@0 19 * Note that when deriving from SupportsWeakPtr you should add
michael@0 20 * MOZ_DECLARE_REFCOUNTED_TYPENAME(ClassName) to the public section of your
michael@0 21 * class, where ClassName is the name of your class.
michael@0 22 *
michael@0 23 * The overhead of WeakPtr is that accesses to 'Foo' becomes an additional
michael@0 24 * dereference, and an additional heap allocated pointer sized object shared
michael@0 25 * between all of the WeakPtrs.
michael@0 26 *
michael@0 27 * Example of usage:
michael@0 28 *
michael@0 29 * // To have a class C support weak pointers, inherit from SupportsWeakPtr<C>.
michael@0 30 * class C : public SupportsWeakPtr<C>
michael@0 31 * {
michael@0 32 * public:
michael@0 33 * MOZ_DECLARE_REFCOUNTED_TYPENAME(C)
michael@0 34 * int num;
michael@0 35 * void act();
michael@0 36 * };
michael@0 37 *
michael@0 38 * C* ptr = new C();
michael@0 39 *
michael@0 40 * // Get weak pointers to ptr. The first time asWeakPtr is called
michael@0 41 * // a reference counted WeakReference object is created that
michael@0 42 * // can live beyond the lifetime of 'ptr'. The WeakReference
michael@0 43 * // object will be notified of 'ptr's destruction.
michael@0 44 * WeakPtr<C> weak = ptr->asWeakPtr();
michael@0 45 * WeakPtr<C> other = ptr->asWeakPtr();
michael@0 46 *
michael@0 47 * // Test a weak pointer for validity before using it.
michael@0 48 * if (weak) {
michael@0 49 * weak->num = 17;
michael@0 50 * weak->act();
michael@0 51 * }
michael@0 52 *
michael@0 53 * // Destroying the underlying object clears weak pointers to it.
michael@0 54 * delete ptr;
michael@0 55 *
michael@0 56 * MOZ_ASSERT(!weak, "Deleting |ptr| clears weak pointers to it.");
michael@0 57 * MOZ_ASSERT(!other, "Deleting |ptr| clears all weak pointers to it.");
michael@0 58 *
michael@0 59 * WeakPtr is typesafe and may be used with any class. It is not required that
michael@0 60 * the class be reference-counted or allocated in any particular way.
michael@0 61 *
michael@0 62 * The API was loosely inspired by Chromium's weak_ptr.h:
michael@0 63 * http://src.chromium.org/svn/trunk/src/base/memory/weak_ptr.h
michael@0 64 */
michael@0 65
michael@0 66 #ifndef mozilla_WeakPtr_h
michael@0 67 #define mozilla_WeakPtr_h
michael@0 68
michael@0 69 #include "mozilla/ArrayUtils.h"
michael@0 70 #include "mozilla/Assertions.h"
michael@0 71 #include "mozilla/NullPtr.h"
michael@0 72 #include "mozilla/RefPtr.h"
michael@0 73 #include "mozilla/TypeTraits.h"
michael@0 74
michael@0 75 #include <string.h>
michael@0 76
michael@0 77 namespace mozilla {
michael@0 78
michael@0 79 template <typename T, class WeakReference> class WeakPtrBase;
michael@0 80 template <typename T, class WeakReference> class SupportsWeakPtrBase;
michael@0 81
michael@0 82 namespace detail {
michael@0 83
michael@0 84 // This can live beyond the lifetime of the class derived from SupportsWeakPtrBase.
michael@0 85 template<class T>
michael@0 86 class WeakReference : public ::mozilla::RefCounted<WeakReference<T> >
michael@0 87 {
michael@0 88 public:
michael@0 89 explicit WeakReference(T* p) : ptr(p) {}
michael@0 90 T* get() const {
michael@0 91 return ptr;
michael@0 92 }
michael@0 93
michael@0 94 #ifdef MOZ_REFCOUNTED_LEAK_CHECKING
michael@0 95 #ifdef XP_WIN
michael@0 96 #define snprintf _snprintf
michael@0 97 #endif
michael@0 98 const char* typeName() const {
michael@0 99 static char nameBuffer[1024];
michael@0 100 const char* innerType = ptr->typeName();
michael@0 101 // We could do fancier length checks at runtime, but innerType is
michael@0 102 // controlled by us so we can ensure that this never causes a buffer
michael@0 103 // overflow by this assertion.
michael@0 104 MOZ_ASSERT(strlen(innerType) + sizeof("WeakReference<>") < ArrayLength(nameBuffer),
michael@0 105 "Exceedingly large type name");
michael@0 106 snprintf(nameBuffer, ArrayLength(nameBuffer), "WeakReference<%s>", innerType);
michael@0 107 // This is usually not OK, but here we are returning a pointer to a static
michael@0 108 // buffer which will immediately be used by the caller.
michael@0 109 return nameBuffer;
michael@0 110 }
michael@0 111 size_t typeSize() const {
michael@0 112 return sizeof(*this);
michael@0 113 }
michael@0 114 #undef snprintf
michael@0 115 #endif
michael@0 116
michael@0 117 private:
michael@0 118 friend class WeakPtrBase<T, WeakReference<T> >;
michael@0 119 friend class SupportsWeakPtrBase<T, WeakReference<T> >;
michael@0 120 void detach() {
michael@0 121 ptr = nullptr;
michael@0 122 }
michael@0 123 T* ptr;
michael@0 124 };
michael@0 125
michael@0 126 } // namespace detail
michael@0 127
michael@0 128 template <typename T, class WeakReference>
michael@0 129 class SupportsWeakPtrBase
michael@0 130 {
michael@0 131 public:
michael@0 132 WeakPtrBase<T, WeakReference> asWeakPtr() {
michael@0 133 if (!weakRef)
michael@0 134 weakRef = new WeakReference(static_cast<T*>(this));
michael@0 135 return WeakPtrBase<T, WeakReference>(weakRef);
michael@0 136 }
michael@0 137
michael@0 138 protected:
michael@0 139 ~SupportsWeakPtrBase() {
michael@0 140 static_assert(IsBaseOf<SupportsWeakPtrBase<T, WeakReference>, T>::value,
michael@0 141 "T must derive from SupportsWeakPtrBase<T, WeakReference>");
michael@0 142 if (weakRef)
michael@0 143 weakRef->detach();
michael@0 144 }
michael@0 145
michael@0 146 private:
michael@0 147 friend class WeakPtrBase<T, WeakReference>;
michael@0 148
michael@0 149 RefPtr<WeakReference> weakRef;
michael@0 150 };
michael@0 151
michael@0 152 template <typename T>
michael@0 153 class SupportsWeakPtr : public SupportsWeakPtrBase<T, detail::WeakReference<T> >
michael@0 154 {
michael@0 155 };
michael@0 156
michael@0 157 template <typename T, class WeakReference>
michael@0 158 class WeakPtrBase
michael@0 159 {
michael@0 160 public:
michael@0 161 WeakPtrBase(const WeakPtrBase<T, WeakReference>& o) : ref(o.ref) {}
michael@0 162 // Ensure that ref is dereferenceable in the uninitialized state
michael@0 163 WeakPtrBase() : ref(new WeakReference(nullptr)) {}
michael@0 164
michael@0 165 operator T*() const {
michael@0 166 return ref->get();
michael@0 167 }
michael@0 168 T& operator*() const {
michael@0 169 return *ref->get();
michael@0 170 }
michael@0 171
michael@0 172 T* operator->() const {
michael@0 173 return ref->get();
michael@0 174 }
michael@0 175
michael@0 176 T* get() const {
michael@0 177 return ref->get();
michael@0 178 }
michael@0 179
michael@0 180 private:
michael@0 181 friend class SupportsWeakPtrBase<T, WeakReference>;
michael@0 182
michael@0 183 explicit WeakPtrBase(const RefPtr<WeakReference> &o) : ref(o) {}
michael@0 184
michael@0 185 RefPtr<WeakReference> ref;
michael@0 186 };
michael@0 187
michael@0 188 template <typename T>
michael@0 189 class WeakPtr : public WeakPtrBase<T, detail::WeakReference<T> >
michael@0 190 {
michael@0 191 typedef WeakPtrBase<T, detail::WeakReference<T> > Base;
michael@0 192 public:
michael@0 193 WeakPtr(const WeakPtr<T>& o) : Base(o) {}
michael@0 194 WeakPtr(const Base& o) : Base(o) {}
michael@0 195 WeakPtr() {}
michael@0 196 };
michael@0 197
michael@0 198 } // namespace mozilla
michael@0 199
michael@0 200 #endif /* mozilla_WeakPtr_h */

mercurial