Blame mfbt/RefCounted.h

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
/* CRTP refcounting templates.  Do not use unless you are an Expert. */
Packit f0b94e
Packit f0b94e
#ifndef mozilla_RefCounted_h
Packit f0b94e
#define mozilla_RefCounted_h
Packit f0b94e
Packit f0b94e
#include "mozilla/AlreadyAddRefed.h"
Packit f0b94e
#include "mozilla/Assertions.h"
Packit f0b94e
#include "mozilla/Atomics.h"
Packit f0b94e
#include "mozilla/Attributes.h"
Packit f0b94e
#include "mozilla/Move.h"
Packit f0b94e
#include "mozilla/RefCountType.h"
Packit f0b94e
#include "mozilla/TypeTraits.h"
Packit f0b94e
Packit f0b94e
#include <atomic>
Packit f0b94e
Packit f0b94e
#if defined(MOZILLA_INTERNAL_API)
Packit f0b94e
#include "nsXPCOM.h"
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
#if defined(MOZILLA_INTERNAL_API) && \
Packit f0b94e
    (defined(DEBUG) || defined(FORCE_BUILD_REFCNT_LOGGING))
Packit f0b94e
#define MOZ_REFCOUNTED_LEAK_CHECKING
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
namespace mozilla {
Packit f0b94e
Packit f0b94e
/**
Packit f0b94e
 * RefCounted<T> is a sort of a "mixin" for a class T.  RefCounted
Packit f0b94e
 * manages, well, refcounting for T, and because RefCounted is
Packit f0b94e
 * parameterized on T, RefCounted<T> can call T's destructor directly.
Packit f0b94e
 * This means T doesn't need to have a virtual dtor and so doesn't
Packit f0b94e
 * need a vtable.
Packit f0b94e
 *
Packit f0b94e
 * RefCounted<T> is created with refcount == 0.  Newly-allocated
Packit f0b94e
 * RefCounted<T> must immediately be assigned to a RefPtr to make the
Packit f0b94e
 * refcount > 0.  It's an error to allocate and free a bare
Packit f0b94e
 * RefCounted<T>, i.e. outside of the RefPtr machinery.  Attempts to
Packit f0b94e
 * do so will abort DEBUG builds.
Packit f0b94e
 *
Packit f0b94e
 * Live RefCounted<T> have refcount > 0.  The lifetime (refcounts) of
Packit f0b94e
 * live RefCounted<T> are controlled by RefPtr<T> and
Packit f0b94e
 * RefPtr<super/subclass of T>.  Upon a transition from refcounted==1
Packit f0b94e
 * to 0, the RefCounted<T> "dies" and is destroyed.  The "destroyed"
Packit f0b94e
 * state is represented in DEBUG builds by refcount==0xffffdead.  This
Packit f0b94e
 * state distinguishes use-before-ref (refcount==0) from
Packit f0b94e
 * use-after-destroy (refcount==0xffffdead).
Packit f0b94e
 *
Packit f0b94e
 * Note that when deriving from RefCounted or AtomicRefCounted, you
Packit f0b94e
 * should add MOZ_DECLARE_REFCOUNTED_TYPENAME(ClassName) to the public
Packit f0b94e
 * section of your class, where ClassName is the name of your class.
Packit f0b94e
 *
Packit f0b94e
 * Note: SpiderMonkey should use js::RefCounted instead since that type
Packit f0b94e
 * will use appropriate js_delete and also not break ref-count logging.
Packit f0b94e
 */
Packit f0b94e
namespace detail {
Packit f0b94e
const MozRefCountType DEAD = 0xffffdead;
Packit f0b94e
Packit f0b94e
// When building code that gets compiled into Gecko, try to use the
Packit f0b94e
// trace-refcount leak logging facilities.
Packit f0b94e
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
Packit f0b94e
class RefCountLogger {
Packit f0b94e
 public:
Packit f0b94e
  static void logAddRef(const void* aPointer, MozRefCountType aRefCount,
Packit f0b94e
                        const char* aTypeName, uint32_t aInstanceSize) {
Packit f0b94e
    MOZ_ASSERT(aRefCount != DEAD);
Packit f0b94e
    NS_LogAddRef(const_cast<void*>(aPointer), aRefCount, aTypeName,
Packit f0b94e
                 aInstanceSize);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  static void logRelease(const void* aPointer, MozRefCountType aRefCount,
Packit f0b94e
                         const char* aTypeName) {
Packit f0b94e
    MOZ_ASSERT(aRefCount != DEAD);
Packit f0b94e
    NS_LogRelease(const_cast<void*>(aPointer), aRefCount, aTypeName);
Packit f0b94e
  }
Packit f0b94e
};
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
// This is used WeakPtr.h as well as this file.
Packit f0b94e
enum RefCountAtomicity { AtomicRefCount, NonAtomicRefCount };
Packit f0b94e
Packit f0b94e
template <typename T, RefCountAtomicity Atomicity>
Packit f0b94e
class RC {
Packit f0b94e
 public:
Packit f0b94e
  explicit RC(T aCount) : mValue(aCount) {}
Packit f0b94e
Packit f0b94e
  T operator++() { return ++mValue; }
Packit f0b94e
  T operator--() { return --mValue; }
Packit f0b94e
Packit f0b94e
  void operator=(const T& aValue) { mValue = aValue; }
Packit f0b94e
Packit f0b94e
  operator T() const { return mValue; }
Packit f0b94e
Packit f0b94e
 private:
Packit f0b94e
  T mValue;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
template <typename T>
Packit f0b94e
class RC<T, AtomicRefCount> {
Packit f0b94e
 public:
Packit f0b94e
  explicit RC(T aCount) : mValue(aCount) {}
Packit f0b94e
Packit f0b94e
  T operator++() {
Packit f0b94e
    // Memory synchronization is not required when incrementing a
Packit f0b94e
    // reference count.  The first increment of a reference count on a
Packit f0b94e
    // thread is not important, since the first use of the object on a
Packit f0b94e
    // thread can happen before it.  What is important is the transfer
Packit f0b94e
    // of the pointer to that thread, which may happen prior to the
Packit f0b94e
    // first increment on that thread.  The necessary memory
Packit f0b94e
    // synchronization is done by the mechanism that transfers the
Packit f0b94e
    // pointer between threads.
Packit f0b94e
    return mValue.fetch_add(1, std::memory_order_relaxed) + 1;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  T operator--() {
Packit f0b94e
    // Since this may be the last release on this thread, we need
Packit f0b94e
    // release semantics so that prior writes on this thread are visible
Packit f0b94e
    // to the thread that destroys the object when it reads mValue with
Packit f0b94e
    // acquire semantics.
Packit f0b94e
    T result = mValue.fetch_sub(1, std::memory_order_release) - 1;
Packit f0b94e
    if (result == 0) {
Packit f0b94e
      // We're going to destroy the object on this thread, so we need
Packit f0b94e
      // acquire semantics to synchronize with the memory released by
Packit f0b94e
      // the last release on other threads, that is, to ensure that
Packit f0b94e
      // writes prior to that release are now visible on this thread.
Packit f0b94e
      std::atomic_thread_fence(std::memory_order_acquire);
Packit f0b94e
    }
Packit f0b94e
    return result;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // This method is only called in debug builds, so we're not too concerned
Packit f0b94e
  // about its performance.
Packit f0b94e
  void operator=(const T& aValue) {
Packit f0b94e
    mValue.store(aValue, std::memory_order_seq_cst);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  operator T() const {
Packit f0b94e
    // Use acquire semantics since we're not sure what the caller is
Packit f0b94e
    // doing.
Packit f0b94e
    return mValue.load(std::memory_order_acquire);
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
 private:
Packit f0b94e
  std::atomic<T> mValue;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
template <typename T, RefCountAtomicity Atomicity>
Packit f0b94e
class RefCounted {
Packit f0b94e
 protected:
Packit f0b94e
  RefCounted() : mRefCnt(0) {}
Packit f0b94e
  ~RefCounted() { MOZ_ASSERT(mRefCnt == detail::DEAD); }
Packit f0b94e
Packit f0b94e
 public:
Packit f0b94e
  // Compatibility with nsRefPtr.
Packit f0b94e
  void AddRef() const {
Packit f0b94e
    // Note: this method must be thread safe for AtomicRefCounted.
Packit f0b94e
    MOZ_ASSERT(int32_t(mRefCnt) >= 0);
Packit f0b94e
#ifndef MOZ_REFCOUNTED_LEAK_CHECKING
Packit f0b94e
    ++mRefCnt;
Packit f0b94e
#else
Packit f0b94e
    const char* type = static_cast<const T*>(this)->typeName();
Packit f0b94e
    uint32_t size = static_cast<const T*>(this)->typeSize();
Packit f0b94e
    const void* ptr = static_cast<const T*>(this);
Packit f0b94e
    MozRefCountType cnt = ++mRefCnt;
Packit f0b94e
    detail::RefCountLogger::logAddRef(ptr, cnt, type, size);
Packit f0b94e
#endif
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  void Release() const {
Packit f0b94e
    // Note: this method must be thread safe for AtomicRefCounted.
Packit f0b94e
    MOZ_ASSERT(int32_t(mRefCnt) > 0);
Packit f0b94e
#ifndef MOZ_REFCOUNTED_LEAK_CHECKING
Packit f0b94e
    MozRefCountType cnt = --mRefCnt;
Packit f0b94e
#else
Packit f0b94e
    const char* type = static_cast<const T*>(this)->typeName();
Packit f0b94e
    const void* ptr = static_cast<const T*>(this);
Packit f0b94e
    MozRefCountType cnt = --mRefCnt;
Packit f0b94e
    // Note: it's not safe to touch |this| after decrementing the refcount,
Packit f0b94e
    // except for below.
Packit f0b94e
    detail::RefCountLogger::logRelease(ptr, cnt, type);
Packit f0b94e
#endif
Packit f0b94e
    if (0 == cnt) {
Packit f0b94e
    // Because we have atomically decremented the refcount above, only
Packit f0b94e
    // one thread can get a 0 count here, so as long as we can assume that
Packit f0b94e
    // everything else in the system is accessing this object through
Packit f0b94e
    // RefPtrs, it's safe to access |this| here.
Packit f0b94e
#ifdef DEBUG
Packit f0b94e
      mRefCnt = detail::DEAD;
Packit f0b94e
#endif
Packit f0b94e
      delete static_cast<const T*>(this);
Packit f0b94e
    }
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  // Compatibility with wtf::RefPtr.
Packit f0b94e
  void ref() { AddRef(); }
Packit f0b94e
  void deref() { Release(); }
Packit f0b94e
  MozRefCountType refCount() const { return mRefCnt; }
Packit f0b94e
  bool hasOneRef() const {
Packit f0b94e
    MOZ_ASSERT(mRefCnt > 0);
Packit f0b94e
    return mRefCnt == 1;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
 private:
Packit f0b94e
  mutable RC<MozRefCountType, Atomicity> mRefCnt;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
Packit f0b94e
// Passing override for the optional argument marks the typeName and
Packit f0b94e
// typeSize functions defined by this macro as overrides.
Packit f0b94e
#define MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(T, ...)           \
Packit f0b94e
  virtual const char* typeName() const __VA_ARGS__ { return #T; } \
Packit f0b94e
  virtual size_t typeSize() const __VA_ARGS__ { return sizeof(*this); }
Packit f0b94e
#else
Packit f0b94e
#define MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(T, ...)
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
// Note that this macro is expanded unconditionally because it declares only
Packit f0b94e
// two small inline functions which will hopefully get eliminated by the linker
Packit f0b94e
// in non-leak-checking builds.
Packit f0b94e
#define MOZ_DECLARE_REFCOUNTED_TYPENAME(T)    \
Packit f0b94e
  const char* typeName() const { return #T; } \
Packit f0b94e
  size_t typeSize() const { return sizeof(*this); }
Packit f0b94e
Packit f0b94e
}  // namespace detail
Packit f0b94e
Packit f0b94e
template <typename T>
Packit f0b94e
class RefCounted : public detail::RefCounted<T, detail::NonAtomicRefCount> {
Packit f0b94e
 public:
Packit f0b94e
  ~RefCounted() {
Packit f0b94e
    static_assert(IsBaseOf<RefCounted, T>::value,
Packit f0b94e
                  "T must derive from RefCounted<T>");
Packit f0b94e
  }
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
namespace external {
Packit f0b94e
Packit f0b94e
/**
Packit f0b94e
 * AtomicRefCounted<T> is like RefCounted<T>, with an atomically updated
Packit f0b94e
 * reference counter.
Packit f0b94e
 *
Packit f0b94e
 * NOTE: Please do not use this class, use NS_INLINE_DECL_THREADSAFE_REFCOUNTING
Packit f0b94e
 * instead.
Packit f0b94e
 */
Packit f0b94e
template <typename T>
Packit f0b94e
class AtomicRefCounted
Packit f0b94e
    : public mozilla::detail::RefCounted<T, mozilla::detail::AtomicRefCount> {
Packit f0b94e
 public:
Packit f0b94e
  ~AtomicRefCounted() {
Packit f0b94e
    static_assert(IsBaseOf<AtomicRefCounted, T>::value,
Packit f0b94e
                  "T must derive from AtomicRefCounted<T>");
Packit f0b94e
  }
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
}  // namespace external
Packit f0b94e
Packit f0b94e
}  // namespace mozilla
Packit f0b94e
Packit f0b94e
#endif  // mozilla_RefCounted_h