Bug 1320905 - Add js::RefCounted that uses js_delete (r=waldo)
authorLuke Wagner <luke@mozilla.com>
Mon, 05 Dec 2016 15:59:18 -0800
changeset 325297 3a52f03ddcb07c790965426a156b3cd0d0b1685e
parent 325296 f3e7734500e0e001f6af7521a3c9784f6bb42996
child 325298 69526e44e3b9cd96a95ac85a44561de59787fdb6
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerswaldo
bugs1320905
milestone53.0a1
Bug 1320905 - Add js::RefCounted that uses js_delete (r=waldo)
js/public/RefCounted.h
js/public/UniquePtr.h
js/src/jsapi-tests/testIntTypesABI.cpp
js/src/jsapi.h
js/src/moz.build
mfbt/RefCounted.h
mozglue/linker/ElfLoader.h
new file mode 100644
--- /dev/null
+++ b/js/public/RefCounted.h
@@ -0,0 +1,27 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: set ts=8 sts=4 et sw=4 tw=99:
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef js_RefCounted_h
+#define js_RefCounted_h
+
+#include "mozilla/RefCounted.h"
+
+#include "js/Utility.h"
+
+namespace js {
+
+// Replacement for mozilla::RefCounted and mozilla::external::AtomicRefCounted
+// that default to JS::DeletePolicy.
+
+template <typename T, typename D = JS::DeletePolicy<T>>
+using RefCounted = mozilla::RefCounted<T, D>;
+
+template <typename T, typename D = JS::DeletePolicy<T>>
+using AtomicRefCounted = mozilla::external::AtomicRefCounted<T, D>;
+
+} // namespace js
+
+#endif /* js_RefCounted_h */
--- a/js/public/UniquePtr.h
+++ b/js/public/UniquePtr.h
@@ -8,17 +8,17 @@
 #define js_UniquePtr_h
 
 #include "mozilla/UniquePtr.h"
 
 #include "js/Utility.h"
 
 namespace js {
 
-// Replacement for mozilla::UniquePtr that defaults to js::DefaultDelete.
+// Replacement for mozilla::UniquePtr that defaults to JS::DeletePolicy.
 template <typename T, typename D = JS::DeletePolicy<T>>
 using UniquePtr = mozilla::UniquePtr<T, D>;
 
 namespace detail {
 
 template<typename T>
 struct UniqueSelector
 {
--- a/js/src/jsapi-tests/testIntTypesABI.cpp
+++ b/js/src/jsapi-tests/testIntTypesABI.cpp
@@ -21,16 +21,17 @@
 #include "js/Debug.h"
 #include "js/GCAPI.h"
 #include "js/HashTable.h"
 #include "js/HeapAPI.h"
 #include "js/Id.h"
 /* LegacyIntTypes.h is deliberately exempted from this requirement */
 #include "js/MemoryMetrics.h"
 #include "js/ProfilingStack.h"
+#include "js/RefCounted.h"
 #include "js/RequiredDefines.h"
 #include "js/RootingAPI.h"
 #include "js/SliceBudget.h"
 #include "js/StructuredClone.h"
 #include "js/TracingAPI.h"
 #include "js/TrackedOptimizationInfo.h"
 #include "js/TypeDecls.h"
 #include "js/UbiNode.h"
--- a/js/src/jsapi.h
+++ b/js/src/jsapi.h
@@ -9,17 +9,16 @@
 #ifndef jsapi_h
 #define jsapi_h
 
 #include "mozilla/AlreadyAddRefed.h"
 #include "mozilla/FloatingPoint.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/Range.h"
 #include "mozilla/RangedPtr.h"
-#include "mozilla/RefCounted.h"
 #include "mozilla/RefPtr.h"
 #include "mozilla/Variant.h"
 
 #include <stdarg.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
 
@@ -29,16 +28,17 @@
 #include "js/CallArgs.h"
 #include "js/CharacterEncoding.h"
 #include "js/Class.h"
 #include "js/GCVector.h"
 #include "js/HashTable.h"
 #include "js/Id.h"
 #include "js/Principals.h"
 #include "js/Realm.h"
+#include "js/RefCounted.h"
 #include "js/RootingAPI.h"
 #include "js/TracingAPI.h"
 #include "js/Utility.h"
 #include "js/Value.h"
 #include "js/Vector.h"
 
 /************************************************************************/
 
@@ -6082,17 +6082,17 @@ SetBuildIdOp(JSContext* cx, BuildIdOp bu
  * still valid (as described above). DeserializeWasmModule is then called to
  * construct a JS::WasmModule (also on the background thread), passing the
  * bytecode file descriptor and, if valid, the compiled-code file descriptor.
  * The JS::WasmObject is then transported to the JSRuntime thread (which
  * originated the request) and the wrapping WebAssembly.Module object is created
  * by calling createObject().
  */
 
-struct WasmModule : mozilla::external::AtomicRefCounted<WasmModule>
+struct WasmModule : js::AtomicRefCounted<WasmModule>
 {
     MOZ_DECLARE_REFCOUNTED_TYPENAME(WasmModule)
     virtual ~WasmModule() {}
 
     virtual void serializedSize(size_t* bytecodeSize, size_t* compiledSize) const = 0;
     virtual void serialize(uint8_t* bytecodeBegin, size_t bytecodeSize,
                            uint8_t* compiledBegin, size_t compiledSize) const = 0;
 
--- a/js/src/moz.build
+++ b/js/src/moz.build
@@ -128,16 +128,17 @@ EXPORTS.js += [
     '../public/Initialization.h',
     '../public/LegacyIntTypes.h',
     '../public/MemoryMetrics.h',
     '../public/Principals.h',
     '../public/ProfilingFrameIterator.h',
     '../public/ProfilingStack.h',
     '../public/Proxy.h',
     '../public/Realm.h',
+    '../public/RefCounted.h',
     '../public/RequiredDefines.h',
     '../public/Result.h',
     '../public/RootingAPI.h',
     '../public/SliceBudget.h',
     '../public/StructuredClone.h',
     '../public/SweepingAPI.h',
     '../public/TraceKind.h',
     '../public/TracingAPI.h',
--- a/mfbt/RefCounted.h
+++ b/mfbt/RefCounted.h
@@ -11,16 +11,17 @@
 
 #include "mozilla/AlreadyAddRefed.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/Atomics.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/Move.h"
 #include "mozilla/RefCountType.h"
 #include "mozilla/TypeTraits.h"
+#include "mozilla/UniquePtr.h"
 
 #if defined(MOZILLA_INTERNAL_API)
 #include "nsXPCOM.h"
 #endif
 
 #if defined(MOZILLA_INTERNAL_API) && \
     (defined(DEBUG) || defined(FORCE_BUILD_REFCNT_LOGGING))
 #define MOZ_REFCOUNTED_LEAK_CHECKING
@@ -81,17 +82,17 @@ public:
 
 // This is used WeakPtr.h as well as this file.
 enum RefCountAtomicity
 {
   AtomicRefCount,
   NonAtomicRefCount
 };
 
-template<typename T, RefCountAtomicity Atomicity>
+template<typename T, RefCountAtomicity Atomicity, typename D>
 class RefCounted
 {
 protected:
   RefCounted() : mRefCnt(0) {}
   ~RefCounted() { MOZ_ASSERT(mRefCnt == detail::DEAD); }
 
 public:
   // Compatibility with nsRefPtr.
@@ -127,17 +128,17 @@ public:
     if (0 == cnt) {
       // Because we have atomically decremented the refcount above, only
       // one thread can get a 0 count here, so as long as we can assume that
       // everything else in the system is accessing this object through
       // RefPtrs, it's safe to access |this| here.
 #ifdef DEBUG
       mRefCnt = detail::DEAD;
 #endif
-      delete static_cast<const T*>(this);
+      D()(const_cast<T*>(static_cast<const T*>(this)));
     }
   }
 
   // Compatibility with wtf::RefPtr.
   void ref() { AddRef(); }
   void deref() { Release(); }
   MozRefCountType refCount() const { return mRefCnt; }
   bool hasOneRef() const
@@ -166,18 +167,18 @@ private:
 // two small inline functions which will hopefully get eliminated by the linker
 // in non-leak-checking builds.
 #define MOZ_DECLARE_REFCOUNTED_TYPENAME(T) \
   const char* typeName() const { return #T; } \
   size_t typeSize() const { return sizeof(*this); }
 
 } // namespace detail
 
-template<typename T>
-class RefCounted : public detail::RefCounted<T, detail::NonAtomicRefCount>
+template<typename T, class D = DefaultDelete<T>>
+class RefCounted : public detail::RefCounted<T, detail::NonAtomicRefCount, D>
 {
 public:
   ~RefCounted()
   {
     static_assert(IsBaseOf<RefCounted, T>::value,
                   "T must derive from RefCounted<T>");
   }
 };
@@ -186,19 +187,19 @@ namespace external {
 
 /**
  * AtomicRefCounted<T> is like RefCounted<T>, with an atomically updated
  * reference counter.
  *
  * NOTE: Please do not use this class, use NS_INLINE_DECL_THREADSAFE_REFCOUNTING
  * instead.
  */
-template<typename T>
+template<typename T, typename D = DefaultDelete<T>>
 class AtomicRefCounted :
-  public mozilla::detail::RefCounted<T, mozilla::detail::AtomicRefCount>
+  public mozilla::detail::RefCounted<T, mozilla::detail::AtomicRefCount, D>
 {
 public:
   ~AtomicRefCounted()
   {
     static_assert(IsBaseOf<AtomicRefCounted, T>::value,
                   "T must derive from AtomicRefCounted<T>");
   }
 };
--- a/mozglue/linker/ElfLoader.h
+++ b/mozglue/linker/ElfLoader.h
@@ -76,19 +76,19 @@ class SystemElf;
  * RefCounted<LibHandle>::Release to support some reentrancy. See further
  * below.
  */
 class LibHandle;
 
 namespace mozilla {
 namespace detail {
 
-template <> inline void RefCounted<LibHandle, AtomicRefCount>::Release() const;
+template <> inline void RefCounted<LibHandle, AtomicRefCount, DefaultDelete<LibHandle>>::Release() const;
 
-template <> inline RefCounted<LibHandle, AtomicRefCount>::~RefCounted()
+template <> inline RefCounted<LibHandle, AtomicRefCount, DefaultDelete<LibHandle>>::~RefCounted()
 {
   MOZ_ASSERT(mRefCnt == 0x7fffdead);
 }
 
 } /* namespace detail */
 } /* namespace mozilla */
 
 /**
@@ -244,17 +244,17 @@ private:
  * increased to 1 on normal builds, and 0x7fffdead on debug builds so that the
  * LibHandle can still be referenced while the destructor is executing. The
  * mRefCnt is allowed to grow > 0x7fffdead, but not to decrease under that
  * value, which would mean too many Releases from within the destructor.
  */
 namespace mozilla {
 namespace detail {
 
-template <> inline void RefCounted<LibHandle, AtomicRefCount>::Release() const {
+template <> inline void RefCounted<LibHandle, AtomicRefCount, DefaultDelete<LibHandle>>::Release() const {
 #ifdef DEBUG
   if (mRefCnt > 0x7fff0000)
     MOZ_ASSERT(mRefCnt > 0x7fffdead);
 #endif
   MOZ_ASSERT(mRefCnt > 0);
   if (mRefCnt > 0) {
     if (0 == --mRefCnt) {
 #ifdef DEBUG