tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RootedOwningNonNull.h (2066B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /**
      8 * An implementation of Rooted for OwningNonNull<T>.  This works by assuming
      9 * that T has a Trace() method defined on it which will trace whatever things
     10 * inside the T instance need tracing.
     11 *
     12 * This implementation has one serious drawback: operator= doesn't work right
     13 * because it's declared on Rooted directly and expects the type Rooted is
     14 * templated over.
     15 */
     16 
     17 #ifndef mozilla_RootedOwningNonNull_h__
     18 #define mozilla_RootedOwningNonNull_h__
     19 
     20 #include "js/GCPolicyAPI.h"
     21 #include "js/TypeDecls.h"
     22 #include "mozilla/OwningNonNull.h"
     23 
     24 namespace JS {
     25 template <typename T>
     26 struct GCPolicy<mozilla::OwningNonNull<T>> {
     27  typedef mozilla::OwningNonNull<T> SmartPtrType;
     28 
     29  static SmartPtrType initial() { return SmartPtrType(); }
     30 
     31  static void trace(JSTracer* trc, SmartPtrType* tp, const char* name) {
     32    // We have to be very careful here.  Normally, OwningNonNull can't be null.
     33    // But binding code can end up in a situation where it sets up a
     34    // Rooted<OwningNonNull> and then before it gets a chance to assign to it
     35    // (e.g. from the constructor of the thing being assigned) a GC happens.  So
     36    // we can land here when *tp stores a null pointer because it's not
     37    // initialized.
     38    //
     39    // So we need to check for that before jumping.
     40    if ((*tp).isInitialized()) {
     41      (*tp)->Trace(trc);
     42    }
     43  }
     44 
     45  static bool isValid(const SmartPtrType& v) {
     46    return !v.isInitialized() || GCPolicy<T>::isValid(v);
     47  }
     48 };
     49 }  // namespace JS
     50 
     51 namespace js {
     52 template <typename T, typename Wrapper>
     53 struct WrappedPtrOperations<mozilla::OwningNonNull<T>, Wrapper> {
     54  operator T&() const { return static_cast<const Wrapper*>(this)->get(); }
     55 };
     56 }  // namespace js
     57 
     58 #endif /* mozilla_RootedOwningNonNull_h__ */