Bug 1382645: Part 1 - Add memory reporter for StructuredCloneHolder binding implementation. r=billm

MozReview-Commit-ID: aK3ljfCJVi

--HG--
extra : rebase_source : d788c6c686cc72b423ca100795a379d343c398db
This commit is contained in:
Kris Maglione 2017-07-25 14:53:41 -07:00
Родитель d7051087d1
Коммит 8683b183cd
5 изменённых файлов: 77 добавлений и 13 удалений

Просмотреть файл

@ -22,7 +22,12 @@ namespace dom {
StructuredCloneBlob::StructuredCloneBlob()
: StructuredCloneHolder(CloningSupported, TransferringNotSupported,
StructuredCloneScope::DifferentProcess)
{};
{}
StructuredCloneBlob::~StructuredCloneBlob()
{
UnregisterWeakMemoryReporter(this);
}
/* static */ already_AddRefed<StructuredCloneBlob>
@ -32,7 +37,7 @@ StructuredCloneBlob::Constructor(GlobalObject& aGlobal, JS::HandleValue aValue,
{
JSContext* cx = aGlobal.Context();
RefPtr<StructuredCloneBlob> holder = new StructuredCloneBlob();
RefPtr<StructuredCloneBlob> holder = StructuredCloneBlob::Create();
Maybe<JSAutoCompartment> ac;
JS::RootedValue value(cx, aValue);
@ -104,7 +109,7 @@ StructuredCloneBlob::ReadStructuredClone(JSContext* aCx, JSStructuredCloneReader
{
JS::RootedObject obj(aCx);
{
RefPtr<StructuredCloneBlob> holder = new StructuredCloneBlob();
RefPtr<StructuredCloneBlob> holder = StructuredCloneBlob::Create();
if (!holder->ReadStructuredCloneInternal(aCx, aReader, aHolder) ||
!holder->WrapObject(aCx, nullptr, &obj)) {
@ -181,5 +186,20 @@ StructuredCloneBlob::WrapObject(JSContext* aCx, JS::HandleObject aGivenProto, JS
return StructuredCloneHolderBinding::Wrap(aCx, this, aGivenProto, aResult);
}
NS_IMETHODIMP
StructuredCloneBlob::CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData, bool aAnonymize)
{
MOZ_COLLECT_REPORT(
"explicit/dom/structured-clone-holder", KIND_HEAP, UNITS_BYTES,
MallocSizeOf(this) + SizeOfExcludingThis(MallocSizeOf),
"Memory used by StructuredCloneHolder DOM objects.");
return NS_OK;
}
NS_IMPL_ISUPPORTS(StructuredCloneBlob, nsIMemoryReporter)
} // namespace dom
} // namespace mozilla

Просмотреть файл

@ -9,22 +9,23 @@
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/StructuredCloneHolder.h"
#include "mozilla/dom/StructuredCloneHolderBinding.h"
#include "mozilla/RefCounted.h"
#include "jsapi.h"
#include "nsIMemoryReporter.h"
#include "nsISupports.h"
namespace mozilla {
namespace dom {
class StructuredCloneBlob : public StructuredCloneHolder
, public RefCounted<StructuredCloneBlob>
class StructuredCloneBlob final : public nsIMemoryReporter
, public StructuredCloneHolder
{
public:
MOZ_DECLARE_REFCOUNTED_TYPENAME(StructuredCloneBlob)
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
explicit StructuredCloneBlob();
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIMEMORYREPORTER
static JSObject* ReadStructuredClone(JSContext* aCx, JSStructuredCloneReader* aReader,
StructuredCloneHolder* aHolder);
@ -43,12 +44,18 @@ public:
bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto, JS::MutableHandleObject aResult);
protected:
template <typename T, detail::RefCountAtomicity>
friend class detail::RefCounted;
~StructuredCloneBlob() = default;
virtual ~StructuredCloneBlob();
private:
explicit StructuredCloneBlob();
static already_AddRefed<StructuredCloneBlob> Create()
{
RefPtr<StructuredCloneBlob> holder = new StructuredCloneBlob();
RegisterWeakMemoryReporter(holder);
return holder.forget();
}
bool ReadStructuredCloneInternal(JSContext* aCx, JSStructuredCloneReader* aReader,
StructuredCloneHolder* aHolder);
};

Просмотреть файл

@ -8,6 +8,7 @@
#include "jsapi.h"
#include "js/StructuredClone.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Move.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/BindingDeclarations.h"
@ -116,6 +117,15 @@ public:
return mBuffer->data();
}
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf)
{
size_t size = 0;
if (HasData()) {
size += mBuffer->sizeOfIncludingThis(aMallocSizeOf);
}
return size;
}
protected:
UniquePtr<JSAutoStructuredCloneBuffer> mBuffer;
@ -306,6 +316,12 @@ protected:
bool mSupportsCloning;
bool mSupportsTransferring;
// SizeOfExcludingThis is inherited from StructuredCloneHolderBase. It doesn't
// account for objects in the following arrays because a) they're not expected
// to be stored in long-lived StructuredCloneHolder objects, and b) in the
// case of BlobImpl objects, MemoryBlobImpls have their own memory reporters,
// and the other types do not hold significant amounts of memory alive.
// Used for cloning blobs in the structured cloning algorithm.
nsTArray<RefPtr<BlobImpl>> mBlobImplArray;

Просмотреть файл

@ -9,6 +9,7 @@
#include "mozilla/Attributes.h"
#include "mozilla/BufferList.h"
#include "mozilla/MemoryReporting.h"
#include <stdint.h>
@ -339,6 +340,16 @@ class JS_PUBLIC_API(JSAutoStructuredCloneBuffer) {
JS::CloneDataPolicy cloneDataPolicy,
const JSStructuredCloneCallbacks* optionalCallbacks=nullptr, void* closure=nullptr);
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf)
{
return data_.SizeOfExcludingThis(mallocSizeOf);
}
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf)
{
return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf);
}
private:
// Copy and assignment are not supported.
JSAutoStructuredCloneBuffer(const JSAutoStructuredCloneBuffer& other) = delete;

Просмотреть файл

@ -9,6 +9,7 @@
#include <algorithm>
#include "mozilla/AllocPolicy.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Move.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Types.h"
@ -136,6 +137,15 @@ class BufferList : private AllocPolicy
// Returns the sum of the sizes of all the buffers.
size_t Size() const { return mSize; }
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf)
{
size_t size = mSegments.sizeOfExcludingThis(aMallocSizeOf);
for (Segment& segment : mSegments) {
size += aMallocSizeOf(segment.Start());
}
return size;
}
void Clear()
{
if (mOwning) {