Skip to content

Commit

Permalink
Bug 1239601 - improve the UniquePtr situation (r=jorendorff)
Browse files Browse the repository at this point in the history
--HG--
extra : commitid : FwqWNSZ3SKY
extra : rebase_source : 75ed67bfadcbdaeb5bf89a57ad6ca9ef75b7f1f0
  • Loading branch information
Luke Wagner committed Jan 16, 2016
1 parent c93c572 commit 6ebd199
Show file tree
Hide file tree
Showing 63 changed files with 174 additions and 178 deletions.
7 changes: 3 additions & 4 deletions devtools/shared/heapsnapshot/DeserializedNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,15 @@ Concrete<DeserializedNode>::allocationStack() const
}


UniquePtr<EdgeRange>
js::UniquePtr<EdgeRange>
Concrete<DeserializedNode>::edges(JSRuntime* rt, bool) const
{
UniquePtr<DeserializedEdgeRange, JS::DeletePolicy<DeserializedEdgeRange>> range(
js_new<DeserializedEdgeRange>(get()));
js::UniquePtr<DeserializedEdgeRange> range(js_new<DeserializedEdgeRange>(get()));

if (!range)
return nullptr;

return UniquePtr<EdgeRange>(range.release());
return js::UniquePtr<EdgeRange>(range.release());
}

StackFrame
Expand Down
5 changes: 2 additions & 3 deletions devtools/shared/heapsnapshot/DeserializedNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#define mozilla_devtools_DeserializedNode__

#include "js/UbiNode.h"
#include "js/UniquePtr.h"
#include "mozilla/devtools/CoreDump.pb.h"
#include "mozilla/Maybe.h"
#include "mozilla/Move.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Vector.h"

// `Deserialized{Node,Edge}` translate protobuf messages from our core dump
Expand Down Expand Up @@ -242,7 +242,6 @@ namespace ubi {

using mozilla::devtools::DeserializedNode;
using mozilla::devtools::DeserializedStackFrame;
using mozilla::UniquePtr;

template<>
struct Concrete<DeserializedNode> : public Base
Expand Down Expand Up @@ -273,7 +272,7 @@ struct Concrete<DeserializedNode> : public Base

// We ignore the `bool wantNames` parameter because we can't control whether
// the core dump was serialized with edge names or not.
UniquePtr<EdgeRange> edges(JSRuntime* rt, bool) const override;
js::UniquePtr<EdgeRange> edges(JSRuntime* rt, bool) const override;
};

template<>
Expand Down
8 changes: 3 additions & 5 deletions devtools/shared/heapsnapshot/tests/gtest/DevTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#include "mozilla/dom/ChromeUtils.h"
#include "mozilla/CycleCollectedJSRuntime.h"
#include "mozilla/Move.h"
#include "mozilla/UniquePtr.h"
#include "js/Principals.h"
#include "js/UbiNode.h"
#include "js/UniquePtr.h"

using namespace mozilla;
using namespace mozilla::devtools;
Expand Down Expand Up @@ -173,17 +173,15 @@ class MOZ_STACK_CLASS FakeNode
namespace JS {
namespace ubi {

using mozilla::UniquePtr;

template<>
class Concrete<FakeNode> : public Base
{
const char16_t* typeName() const override {
return concreteTypeName;
}

UniquePtr<EdgeRange> edges(JSRuntime*, bool) const override {
return UniquePtr<EdgeRange>(js_new<PreComputedEdgeRange>(get().edges));
js::UniquePtr<EdgeRange> edges(JSRuntime*, bool) const override {
return js::UniquePtr<EdgeRange>(js_new<PreComputedEdgeRange>(get().edges));
}

Size size(mozilla::MallocSizeOf) const override {
Expand Down
2 changes: 1 addition & 1 deletion docshell/base/nsDocShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14073,7 +14073,7 @@ nsDocShell::NotifyJSRunToCompletionStart(const char* aReason,
RefPtr<TimelineConsumers> timelines = TimelineConsumers::Get();
if (timelines && timelines->HasConsumer(this)) {
timelines->AddMarkerForDocShell(this, Move(
MakeUnique<JavascriptTimelineMarker>(
mozilla::MakeUnique<JavascriptTimelineMarker>(
aReason, aFunctionName, aFilename, aLineNumber, MarkerTracingType::START,
aAsyncStack, aAsyncCause)));
}
Expand Down
4 changes: 0 additions & 4 deletions js/public/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/UniquePtr.h"

#include "jsapi.h"
#include "jspubtd.h"
Expand All @@ -26,9 +25,6 @@ class Debugger;
} // namespace js

namespace JS {

using mozilla::UniquePtr;

namespace dbg {

// Helping embedding code build objects for Debugger
Expand Down
6 changes: 2 additions & 4 deletions js/public/GCAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#ifndef js_GCAPI_h
#define js_GCAPI_h

#include "mozilla/UniquePtr.h"
#include "mozilla/Vector.h"

#include "js/HeapAPI.h"
#include "js/UniquePtr.h"

namespace js {
namespace gc {
Expand Down Expand Up @@ -48,8 +48,6 @@ typedef enum JSGCInvocationKind {

namespace JS {

using mozilla::UniquePtr;

#define GCREASONS(D) \
/* Reasons internal to the JS engine */ \
D(API) \
Expand Down Expand Up @@ -300,7 +298,7 @@ class GarbageCollectionEvent
, collections()
{ }

using Ptr = UniquePtr<GarbageCollectionEvent, DeletePolicy<GarbageCollectionEvent>>;
using Ptr = js::UniquePtr<GarbageCollectionEvent>;
static Ptr Create(JSRuntime* rt, ::js::gcstats::Statistics& stats, uint64_t majorGCNumber);

JSObject* toJSObject(JSContext* cx) const;
Expand Down
12 changes: 6 additions & 6 deletions js/public/HashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -606,21 +606,21 @@ template <class T>
struct DefaultHasher<T*> : PointerHasher<T*, mozilla::tl::FloorLog2<sizeof(void*)>::value>
{};

// Specialize hashing policy for mozilla::UniquePtr<T> to proxy the UniquePtr's
// Specialize hashing policy for mozilla::UniquePtr to proxy the UniquePtr's
// raw pointer to PointerHasher.
template <class T>
struct DefaultHasher<mozilla::UniquePtr<T>>
template <class T, class D>
struct DefaultHasher<mozilla::UniquePtr<T, D>>
{
using Lookup = mozilla::UniquePtr<T>;
using Lookup = mozilla::UniquePtr<T, D>;
using PtrHasher = PointerHasher<T*, mozilla::tl::FloorLog2<sizeof(void*)>::value>;

static HashNumber hash(const Lookup& l) {
return PtrHasher::hash(l.get());
}
static bool match(const mozilla::UniquePtr<T>& k, const Lookup& l) {
static bool match(const mozilla::UniquePtr<T, D>& k, const Lookup& l) {
return PtrHasher::match(k.get(), l.get());
}
static void rekey(mozilla::UniquePtr<T>& k, mozilla::UniquePtr<T>&& newKey) {
static void rekey(mozilla::UniquePtr<T, D>& k, mozilla::UniquePtr<T, D>&& newKey) {
k = mozilla::Move(newKey);
}
};
Expand Down
31 changes: 10 additions & 21 deletions js/public/UbiNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "mozilla/Move.h"
#include "mozilla/RangedPtr.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Variant.h"

#include "jspubtd.h"
Expand All @@ -25,6 +24,7 @@
#include "js/RootingAPI.h"
#include "js/TracingAPI.h"
#include "js/TypeDecls.h"
#include "js/UniquePtr.h"
#include "js/Value.h"
#include "js/Vector.h"

Expand Down Expand Up @@ -174,24 +174,13 @@ class StackFrame;
} // namespace ubi
} // namespace JS

namespace mozilla {

template<>
class DefaultDelete<JS::ubi::EdgeRange> : public JS::DeletePolicy<JS::ubi::EdgeRange> { };

template<>
class DefaultDelete<JS::ubi::StackFrame> : public JS::DeletePolicy<JS::ubi::StackFrame> { };

} // namespace mozilla

namespace JS {
namespace ubi {

using mozilla::Forward;
using mozilla::Maybe;
using mozilla::Move;
using mozilla::RangedPtr;
using mozilla::UniquePtr;
using mozilla::Variant;

/*** ubi::StackFrame ******************************************************************************/
Expand Down Expand Up @@ -597,7 +586,7 @@ class Base {
//
// If wantNames is true, compute names for edges. Doing so can be expensive
// in time and memory.
virtual UniquePtr<EdgeRange> edges(JSRuntime* rt, bool wantNames) const = 0;
virtual js::UniquePtr<EdgeRange> edges(JSRuntime* rt, bool wantNames) const = 0;

// Return the Zone to which this node's referent belongs, or nullptr if the
// referent is not of a type allocated in SpiderMonkey Zones.
Expand Down Expand Up @@ -634,7 +623,7 @@ class Base {
// of the out parameter. True is returned on success, false is returned on
// OOM.
virtual bool jsObjectConstructorName(JSContext* cx,
UniquePtr<char16_t[], JS::FreePolicy>& outName) const {
js::UniquePtr<char16_t[], JS::FreePolicy>& outName) const {
outName.reset(nullptr);
return true;
}
Expand Down Expand Up @@ -781,7 +770,7 @@ class Node {
JSCompartment* compartment() const { return base()->compartment(); }
const char* jsObjectClassName() const { return base()->jsObjectClassName(); }
bool jsObjectConstructorName(JSContext* cx,
UniquePtr<char16_t[], JS::FreePolicy>& outName) const {
js::UniquePtr<char16_t[], JS::FreePolicy>& outName) const {
return base()->jsObjectConstructorName(cx, outName);
}

Expand All @@ -796,7 +785,7 @@ class Node {
return size;
}

UniquePtr<EdgeRange> edges(JSRuntime* rt, bool wantNames = true) const {
js::UniquePtr<EdgeRange> edges(JSRuntime* rt, bool wantNames = true) const {
return base()->edges(rt, wantNames);
}

Expand Down Expand Up @@ -830,7 +819,7 @@ class Node {

/*** Edge and EdgeRange ***************************************************************************/

using EdgeName = UniquePtr<const char16_t[], JS::FreePolicy>;
using EdgeName = js::UniquePtr<const char16_t[], JS::FreePolicy>;

// An outgoing edge to a referent node.
class Edge {
Expand Down Expand Up @@ -1002,7 +991,7 @@ class MOZ_STACK_CLASS RootList {

template<>
struct Concrete<RootList> : public Base {
UniquePtr<EdgeRange> edges(JSRuntime* rt, bool wantNames) const override;
js::UniquePtr<EdgeRange> edges(JSRuntime* rt, bool wantNames) const override;
const char16_t* typeName() const override { return concreteTypeName; }

protected:
Expand All @@ -1019,7 +1008,7 @@ struct Concrete<RootList> : public Base {
template<typename Referent>
class TracerConcrete : public Base {
const char16_t* typeName() const override { return concreteTypeName; }
UniquePtr<EdgeRange> edges(JSRuntime* rt, bool wantNames) const override;
js::UniquePtr<EdgeRange> edges(JSRuntime* rt, bool wantNames) const override;
JS::Zone* zone() const override;

protected:
Expand Down Expand Up @@ -1078,7 +1067,7 @@ template<>
class Concrete<JSObject> : public TracerConcreteWithCompartment<JSObject> {
const char* jsObjectClassName() const override;
bool jsObjectConstructorName(JSContext* cx,
UniquePtr<char16_t[], JS::FreePolicy>& outName) const override;
js::UniquePtr<char16_t[], JS::FreePolicy>& outName) const override;
Size size(mozilla::MallocSizeOf mallocSizeOf) const override;

bool hasAllocationStack() const override;
Expand Down Expand Up @@ -1113,7 +1102,7 @@ template<>
class Concrete<void> : public Base {
const char16_t* typeName() const override;
Size size(mozilla::MallocSizeOf mallocSizeOf) const override;
UniquePtr<EdgeRange> edges(JSRuntime* rt, bool wantNames) const override;
js::UniquePtr<EdgeRange> edges(JSRuntime* rt, bool wantNames) const override;
JS::Zone* zone() const override;
JSCompartment* compartment() const override;
CoarseType coarseType() const final;
Expand Down
4 changes: 2 additions & 2 deletions js/public/UbiNodeCensus.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct CountDeleter {
void operator()(CountBase*);
};

using CountBasePtr = UniquePtr<CountBase, CountDeleter>;
using CountBasePtr = js::UniquePtr<CountBase, CountDeleter>;

// Abstract base class for CountType nodes.
struct CountType {
Expand Down Expand Up @@ -111,7 +111,7 @@ struct CountType {
virtual bool report(JSContext* cx, CountBase& count, MutableHandleValue report) = 0;
};

using CountTypePtr = UniquePtr<CountType, JS::DeletePolicy<CountType>>;
using CountTypePtr = js::UniquePtr<CountType>;

// An abstract base class for count tree nodes.
class CountBase {
Expand Down
2 changes: 1 addition & 1 deletion js/public/UbiNodePostOrder.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct PostOrder {
mozilla::DebugOnly<bool> traversed;

private:
bool fillEdgesFromRange(EdgeVector& edges, UniquePtr<EdgeRange>& range) {
bool fillEdgesFromRange(EdgeVector& edges, js::UniquePtr<EdgeRange>& range) {
MOZ_ASSERT(range);
for ( ; !range->empty(); range->popFront()) {
if (!edges.append(mozilla::Move(range->front())))
Expand Down
61 changes: 61 additions & 0 deletions js/public/UniquePtr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* -*- 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_UniquePtr_h
#define js_UniquePtr_h

#include "mozilla/UniquePtr.h"

#include "js/Utility.h"

namespace js {

// Replacement for mozilla::UniquePtr that defaults to js::DefaultDelete.
template <typename T, typename D = JS::DeletePolicy<T>>
using UniquePtr = mozilla::UniquePtr<T, D>;

namespace detail {

template<typename T>
struct UniqueSelector
{
typedef UniquePtr<T> SingleObject;
};

template<typename T>
struct UniqueSelector<T[]>
{
typedef UniquePtr<T[]> UnknownBound;
};

template<typename T, decltype(sizeof(int)) N>
struct UniqueSelector<T[N]>
{
typedef UniquePtr<T[N]> KnownBound;
};

} // namespace detail

// Replacement for mozilla::MakeUnique that correctly calls js_new and produces
// a js::UniquePtr.
template<typename T, typename... Args>
typename detail::UniqueSelector<T>::SingleObject
MakeUnique(Args&&... aArgs)
{
return UniquePtr<T>(js_new<T>(mozilla::Forward<Args>(aArgs)...));
}

template<typename T>
typename detail::UniqueSelector<T>::UnknownBound
MakeUnique(decltype(sizeof(int)) aN) = delete;

template<typename T, typename... Args>
typename detail::UniqueSelector<T>::KnownBound
MakeUnique(Args&&... aArgs) = delete;

} // namespace js

#endif /* js_UniquePtr_h */
8 changes: 3 additions & 5 deletions js/src/asmjs/AsmJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,14 @@ struct AsmJSModuleData : AsmJSModuleCacheablePod
WASM_DECLARE_SERIALIZABLE(AsmJSModuleData)
};

typedef UniquePtr<AsmJSModuleData, JS::DeletePolicy<AsmJSModuleData>> UniqueAsmJSModuleData;
typedef UniquePtr<AsmJSModuleData> UniqueAsmJSModuleData;

// An AsmJSModule is-a Module with the extra persistent state necessary to
// represent a compiled asm.js module.
class js::AsmJSModule final : public Module
{
typedef UniquePtr<const AsmJSModuleData,
JS::DeletePolicy<const AsmJSModuleData>> UniqueConstAsmJSModuleData;
typedef UniquePtr<const StaticLinkData,
JS::DeletePolicy<const StaticLinkData>> UniqueConstStaticLinkData;
typedef UniquePtr<const AsmJSModuleData> UniqueConstAsmJSModuleData;
typedef UniquePtr<const StaticLinkData> UniqueConstStaticLinkData;

const UniqueConstStaticLinkData link_;
const UniqueConstAsmJSModuleData module_;
Expand Down
Loading

0 comments on commit 6ebd199

Please sign in to comment.