Skip to content

Commit

Permalink
Bug 936964 (part 7, attempt 2) - Make lots of classes report their ow…
Browse files Browse the repository at this point in the history
…n memory consumption, instead of using a separate reporter class. r=mccr8,sr=bz.

* * *
Bug 936964 (part 7b) - Fix a use-after-free found by ASan.

--HG--
extra : rebase_source : d38ab6b63e9d576773baf8e55fa4f7182c33915e
  • Loading branch information
nnethercote committed Nov 7, 2013
1 parent 8fd06cf commit 39004db
Show file tree
Hide file tree
Showing 68 changed files with 904 additions and 1,057 deletions.
2 changes: 1 addition & 1 deletion content/base/src/nsContentUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ nsContentUtils::Init()
return NS_ERROR_OUT_OF_MEMORY;
}

NS_RegisterMemoryReporter(new DOMEventListenerManagersHashReporter);
RegisterStrongMemoryReporter(new DOMEventListenerManagersHashReporter());
}

sBlockedScriptRunners = new nsTArray< nsCOMPtr<nsIRunnable> >;
Expand Down
4 changes: 1 addition & 3 deletions content/base/src/nsDOMFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,9 +733,7 @@ nsDOMMemoryFile::DataOwner::EnsureMemoryReporterRegistered()
return;
}

nsRefPtr<nsDOMMemoryFileDataOwnerMemoryReporter> reporter = new
nsDOMMemoryFileDataOwnerMemoryReporter();
NS_RegisterMemoryReporter(reporter);
RegisterStrongMemoryReporter(new nsDOMMemoryFileDataOwnerMemoryReporter());

sMemoryReporterRegistered = true;
}
Expand Down
4 changes: 2 additions & 2 deletions content/base/src/nsFrameMessageManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ NS_NewGlobalMessageManager(nsIMessageBroadcaster** aResult)
nsFrameMessageManager* mm = new nsFrameMessageManager(nullptr,
nullptr,
MM_CHROME | MM_GLOBAL | MM_BROADCASTER);
NS_RegisterMemoryReporter(new MessageManagerReporter());
RegisterStrongMemoryReporter(new MessageManagerReporter());
return CallQueryInterface(mm, aResult);
}

Expand Down Expand Up @@ -1835,7 +1835,7 @@ NS_NewChildProcessMessageManager(nsISyncMessageSender** aResult)
cb = new SameChildProcessMessageManagerCallback();
} else {
cb = new ChildProcessMessageManagerCallback();
NS_RegisterMemoryReporter(new MessageManagerReporter());
RegisterStrongMemoryReporter(new MessageManagerReporter());
}
nsFrameMessageManager* mm = new nsFrameMessageManager(cb,
nullptr,
Expand Down
2 changes: 1 addition & 1 deletion content/base/src/nsHostObjectProtocolHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ nsHostObjectProtocolHandler::nsHostObjectProtocolHandler()

if (!initialized) {
initialized = true;
NS_RegisterMemoryReporter(new mozilla::HostObjectURLsReporter());
RegisterStrongMemoryReporter(new mozilla::HostObjectURLsReporter());
}
}

Expand Down
2 changes: 1 addition & 1 deletion content/canvas/src/CanvasRenderingContext2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ CanvasRenderingContext2D::EnsureTarget()
static bool registered = false;
if (!registered) {
registered = true;
NS_RegisterMemoryReporter(new Canvas2dPixelsReporter());
RegisterStrongMemoryReporter(new Canvas2dPixelsReporter());
}

gCanvasAzureMemoryUsed += mWidth * mHeight * 4;
Expand Down
66 changes: 25 additions & 41 deletions content/canvas/src/WebGLContextReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,129 +5,113 @@

#include "WebGLContext.h"
#include "WebGLMemoryTracker.h"
#include "nsIMemoryReporter.h"

using namespace mozilla;

NS_IMPL_ISUPPORTS1(WebGLMemoryPressureObserver, nsIObserver)

class WebGLMemoryReporter MOZ_FINAL : public MemoryMultiReporter
{
public:
WebGLMemoryReporter()
: MemoryMultiReporter("webgl")
{}

NS_IMETHOD CollectReports(nsIMemoryReporterCallback* aCb,
nsISupports* aClosure);
};

NS_IMETHODIMP
WebGLMemoryReporter::CollectReports(nsIMemoryReporterCallback* aCb,
nsISupports* aClosure)
WebGLMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData)
{
#define REPORT(_path, _kind, _units, _amount, _desc) \
do { \
nsresult rv; \
rv = aCb->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), _kind, \
_units, _amount, NS_LITERAL_CSTRING(_desc), \
aClosure); \
rv = aHandleReport->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), \
_kind, _units, _amount, \
NS_LITERAL_CSTRING(_desc), aData); \
NS_ENSURE_SUCCESS(rv, rv); \
} while (0)

REPORT("webgl-texture-memory",
nsIMemoryReporter::KIND_OTHER, nsIMemoryReporter::UNITS_BYTES,
WebGLMemoryTracker::GetTextureMemoryUsed(),
KIND_OTHER, UNITS_BYTES, GetTextureMemoryUsed(),
"Memory used by WebGL textures.The OpenGL"
" implementation is free to store these textures in either video"
" memory or main memory. This measurement is only a lower bound,"
" actual memory usage may be higher for example if the storage"
" is strided.");

REPORT("webgl-texture-count",
nsIMemoryReporter::KIND_OTHER, nsIMemoryReporter::UNITS_COUNT,
WebGLMemoryTracker::GetTextureCount(),
KIND_OTHER, UNITS_COUNT, GetTextureCount(),
"Number of WebGL textures.");

REPORT("webgl-buffer-memory",
nsIMemoryReporter::KIND_OTHER, nsIMemoryReporter::UNITS_BYTES,
WebGLMemoryTracker::GetBufferMemoryUsed(),
KIND_OTHER, UNITS_BYTES, GetBufferMemoryUsed(),
"Memory used by WebGL buffers. The OpenGL"
" implementation is free to store these buffers in either video"
" memory or main memory. This measurement is only a lower bound,"
" actual memory usage may be higher for example if the storage"
" is strided.");

REPORT("explicit/webgl/buffer-cache-memory",
nsIMemoryReporter::KIND_HEAP, nsIMemoryReporter::UNITS_BYTES,
WebGLMemoryTracker::GetBufferCacheMemoryUsed(),
KIND_HEAP, UNITS_BYTES, GetBufferCacheMemoryUsed(),
"Memory used by WebGL buffer caches. The WebGL"
" implementation caches the contents of element array buffers"
" only.This adds up with the webgl-buffer-memory value, but"
" contrary to it, this one represents bytes on the heap,"
" not managed by OpenGL.");

REPORT("webgl-buffer-count",
nsIMemoryReporter::KIND_OTHER, nsIMemoryReporter::UNITS_COUNT,
WebGLMemoryTracker::GetBufferCount(),
KIND_OTHER, UNITS_COUNT, GetBufferCount(),
"Number of WebGL buffers.");

REPORT("webgl-renderbuffer-memory",
nsIMemoryReporter::KIND_OTHER, nsIMemoryReporter::UNITS_BYTES,
WebGLMemoryTracker::GetRenderbufferMemoryUsed(),
KIND_OTHER, UNITS_BYTES, GetRenderbufferMemoryUsed(),
"Memory used by WebGL renderbuffers. The OpenGL"
" implementation is free to store these renderbuffers in either"
" video memory or main memory. This measurement is only a lower"
" bound, actual memory usage may be higher for example if the"
" storage is strided.");

REPORT("webgl-renderbuffer-count",
nsIMemoryReporter::KIND_OTHER, nsIMemoryReporter::UNITS_COUNT,
WebGLMemoryTracker::GetRenderbufferCount(),
KIND_OTHER, UNITS_COUNT, GetRenderbufferCount(),
"Number of WebGL renderbuffers.");

REPORT("explicit/webgl/shader",
nsIMemoryReporter::KIND_HEAP, nsIMemoryReporter::UNITS_BYTES,
WebGLMemoryTracker::GetShaderSize(),
KIND_HEAP, UNITS_BYTES, GetShaderSize(),
"Combined size of WebGL shader ASCII sources and translation"
" logs cached on the heap.");

REPORT("webgl-shader-count",
nsIMemoryReporter::KIND_OTHER, nsIMemoryReporter::UNITS_COUNT,
WebGLMemoryTracker::GetShaderCount(),
KIND_OTHER, UNITS_COUNT, GetShaderCount(),
"Number of WebGL shaders.");

REPORT("webgl-context-count",
nsIMemoryReporter::KIND_OTHER, nsIMemoryReporter::UNITS_COUNT,
WebGLMemoryTracker::GetContextCount(),
KIND_OTHER, UNITS_COUNT, GetContextCount(),
"Number of WebGL contexts.");

#undef REPORT

return NS_OK;
}

NS_IMPL_ISUPPORTS1(WebGLMemoryTracker, nsISupports)
NS_IMPL_ISUPPORTS_INHERITED0(WebGLMemoryTracker, MemoryMultiReporter)

StaticRefPtr<WebGLMemoryTracker> WebGLMemoryTracker::sUniqueInstance;

WebGLMemoryTracker* WebGLMemoryTracker::UniqueInstance()
{
if (!sUniqueInstance) {
sUniqueInstance = new WebGLMemoryTracker;
sUniqueInstance->InitMemoryReporter();
}
return sUniqueInstance;
}

WebGLMemoryTracker::WebGLMemoryTracker()
: MemoryMultiReporter("webgl")
{
}

void
WebGLMemoryTracker::InitMemoryReporter()
{
mReporter = new WebGLMemoryReporter;
NS_RegisterMemoryReporter(mReporter);
RegisterWeakMemoryReporter(this);
}

WebGLMemoryTracker::~WebGLMemoryTracker()
{
NS_UnregisterMemoryReporter(mReporter);
UnregisterWeakMemoryReporter(this);
}

NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(WebGLBufferMallocSizeOf)
Expand Down
11 changes: 8 additions & 3 deletions content/canvas/src/WebGLMemoryTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@
#include "WebGLTexture.h"
#include "WebGLRenderbuffer.h"
#include "mozilla/StaticPtr.h"
#include "nsIMemoryReporter.h"

namespace mozilla {

class WebGLMemoryTracker : public nsISupports
class WebGLMemoryTracker : public MemoryMultiReporter
{
NS_DECL_ISUPPORTS

WebGLMemoryTracker();
virtual ~WebGLMemoryTracker();
static StaticRefPtr<WebGLMemoryTracker> sUniqueInstance;

// here we store plain pointers, not RefPtrs: we don't want the
// Here we store plain pointers, not RefPtrs: we don't want the
// WebGLMemoryTracker unique instance to keep alive all
// WebGLContexts ever created.
typedef nsTArray<const WebGLContext*> ContextsArrayType;
ContextsArrayType mContexts;

nsCOMPtr<nsIMemoryReporter> mReporter;
void InitMemoryReporter();

static WebGLMemoryTracker* UniqueInstance();

Expand All @@ -54,6 +55,10 @@ class WebGLMemoryTracker : public nsISupports
}
}

NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData);

private:
static int64_t GetTextureMemoryUsed() {
const ContextsArrayType & contexts = Contexts();
int64_t result = 0;
Expand Down
82 changes: 38 additions & 44 deletions content/media/MediaDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,20 @@ PRLogModuleInfo* gMediaDecoderLog;
#define DECODER_LOG(type, msg)
#endif

class MediaMemoryTracker : public nsISupports
class MediaMemoryTracker : public MemoryMultiReporter
{
NS_DECL_ISUPPORTS

MediaMemoryTracker();
virtual ~MediaMemoryTracker();
void InitMemoryReporter();

static StaticRefPtr<MediaMemoryTracker> sUniqueInstance;

static MediaMemoryTracker* UniqueInstance() {
if (!sUniqueInstance) {
sUniqueInstance = new MediaMemoryTracker();
sUniqueInstance->InitMemoryReporter();
}
return sUniqueInstance;
}
Expand All @@ -76,8 +79,6 @@ class MediaMemoryTracker : public nsISupports

DecodersArray mDecoders;

nsCOMPtr<nsIMemoryReporter> mReporter;

public:
static void AddMediaDecoder(MediaDecoder* aDecoder)
{
Expand All @@ -93,21 +94,13 @@ class MediaMemoryTracker : public nsISupports
}
}

static void GetAmounts(int64_t* aVideo, int64_t* aAudio)
{
*aVideo = 0;
*aAudio = 0;
DecodersArray& decoders = Decoders();
for (size_t i = 0; i < decoders.Length(); ++i) {
*aVideo += decoders[i]->VideoQueueMemoryInUse();
*aAudio += decoders[i]->AudioQueueMemoryInUse();
}
}
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData);
};

StaticRefPtr<MediaMemoryTracker> MediaMemoryTracker::sUniqueInstance;

NS_IMPL_ISUPPORTS1(MediaMemoryTracker, nsISupports)
NS_IMPL_ISUPPORTS_INHERITED0(MediaMemoryTracker, MemoryMultiReporter)

NS_IMPL_ISUPPORTS1(MediaDecoder, nsIObserver)

Expand Down Expand Up @@ -1776,38 +1769,34 @@ MediaDecoder::IsAppleMP3Enabled()
}
#endif

class MediaReporter MOZ_FINAL : public MemoryMultiReporter
NS_IMETHODIMP
MediaMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData)
{
public:
MediaReporter()
: MemoryMultiReporter("media")
{}

NS_IMETHOD CollectReports(nsIMemoryReporterCallback* aCb,
nsISupports* aClosure)
{
int64_t video, audio;
MediaMemoryTracker::GetAmounts(&video, &audio);
int64_t video = 0, audio = 0;
DecodersArray& decoders = Decoders();
for (size_t i = 0; i < decoders.Length(); ++i) {
video += decoders[i]->VideoQueueMemoryInUse();
audio += decoders[i]->AudioQueueMemoryInUse();
}

#define REPORT(_path, _amount, _desc) \
do { \
nsresult rv; \
rv = aCb->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), \
nsIMemoryReporter::KIND_HEAP, \
nsIMemoryReporter::UNITS_BYTES, _amount, \
NS_LITERAL_CSTRING(_desc), aClosure); \
NS_ENSURE_SUCCESS(rv, rv); \
} while (0)
#define REPORT(_path, _amount, _desc) \
do { \
nsresult rv; \
rv = aHandleReport->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), \
KIND_HEAP, UNITS_BYTES, _amount, \
NS_LITERAL_CSTRING(_desc), aData); \
NS_ENSURE_SUCCESS(rv, rv); \
} while (0)

REPORT("explicit/media/decoded-video", video,
"Memory used by decoded video frames.");
REPORT("explicit/media/decoded-video", video,
"Memory used by decoded video frames.");

REPORT("explicit/media/decoded-audio", audio,
"Memory used by decoded audio chunks.");
REPORT("explicit/media/decoded-audio", audio,
"Memory used by decoded audio chunks.");

return NS_OK;
}
};
return NS_OK;
}

MediaDecoderOwner*
MediaDecoder::GetOwner()
Expand All @@ -1817,14 +1806,19 @@ MediaDecoder::GetOwner()
}

MediaMemoryTracker::MediaMemoryTracker()
: mReporter(new MediaReporter())
: MemoryMultiReporter("media")
{
}

void
MediaMemoryTracker::InitMemoryReporter()
{
NS_RegisterMemoryReporter(mReporter);
RegisterWeakMemoryReporter(this);
}

MediaMemoryTracker::~MediaMemoryTracker()
{
NS_UnregisterMemoryReporter(mReporter);
UnregisterWeakMemoryReporter(this);
}

} // namespace mozilla
Expand Down
1 change: 0 additions & 1 deletion content/media/MediaDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ destroying the MediaDecoder object.
#include "necko-config.h"

class nsIStreamListener;
class nsIMemoryReporter;
class nsIPrincipal;
class nsITimer;

Expand Down
Loading

0 comments on commit 39004db

Please sign in to comment.