Bug 800187 (part 1) - Add memory reporter for the nsStyleSheetService. r=bz.

--HG--
extra : rebase_source : fc5c04daa0fd919f11470b090e38e9e8a5f19db6
This commit is contained in:
Nicholas Nethercote 2012-10-11 21:05:38 -07:00
parent e37c1e657a
commit 8653ecbff2
4 changed files with 76 additions and 12 deletions

View File

@ -19,6 +19,24 @@
#include "nsNetUtil.h"
#include "nsIObserverService.h"
#include "nsLayoutStatics.h"
#include "nsIMemoryReporter.h"
NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(LayoutStyleSheetServiceMallocSizeOf,
"layout/style-sheet-service")
static int64_t
GetStyleSheetServiceSize()
{
return nsStyleSheetService::SizeOfIncludingThis(
LayoutStyleSheetServiceMallocSizeOf);
}
NS_MEMORY_REPORTER_IMPLEMENT(StyleSheetService,
"explicit/layout/style-sheet-service",
KIND_HEAP,
nsIMemoryReporter::UNITS_BYTES,
GetStyleSheetServiceSize,
"Memory used for style sheets held by the style sheet service.")
nsStyleSheetService *nsStyleSheetService::gInstance = nullptr;
@ -28,12 +46,18 @@ nsStyleSheetService::nsStyleSheetService()
NS_ASSERTION(!gInstance, "Someone is using CreateInstance instead of GetService");
gInstance = this;
nsLayoutStatics::AddRef();
mReporter = new NS_MEMORY_REPORTER_NAME(StyleSheetService);
(void)::NS_RegisterMemoryReporter(mReporter);
}
nsStyleSheetService::~nsStyleSheetService()
{
gInstance = nullptr;
nsLayoutStatics::Release();
(void)::NS_UnregisterMemoryReporter(mReporter);
mReporter = nullptr;
}
NS_IMPL_ISUPPORTS1(nsStyleSheetService, nsIStyleSheetService)
@ -173,7 +197,7 @@ nsStyleSheetService::UnregisterSheet(nsIURI *sheetURI, uint32_t aSheetType)
NS_ENSURE_TRUE(foundIndex >= 0, NS_ERROR_INVALID_ARG);
nsCOMPtr<nsIStyleSheet> sheet = mSheets[aSheetType][foundIndex];
mSheets[aSheetType].RemoveObjectAt(foundIndex);
const char* message = (aSheetType == AGENT_SHEET) ?
"agent-sheet-removed" : "user-sheet-removed";
nsCOMPtr<nsIObserverService> serv =
@ -183,3 +207,34 @@ nsStyleSheetService::UnregisterSheet(nsIURI *sheetURI, uint32_t aSheetType)
return NS_OK;
}
size_t
nsStyleSheetService::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf)
{
if (!nsStyleSheetService::gInstance) {
return 0;
}
return nsStyleSheetService::gInstance->
SizeOfIncludingThisHelper(aMallocSizeOf);
}
static size_t
SizeOfElementIncludingThis(nsIStyleSheet* aElement,
nsMallocSizeOfFun aMallocSizeOf, void *aData)
{
return aElement->SizeOfIncludingThis(aMallocSizeOf);
}
size_t
nsStyleSheetService::SizeOfIncludingThisHelper(nsMallocSizeOfFun aMallocSizeOf) const
{
size_t n = aMallocSizeOf(this);
n += mSheets[AGENT_SHEET].SizeOfExcludingThis(SizeOfElementIncludingThis,
aMallocSizeOf);
n += mSheets[USER_SHEET].SizeOfExcludingThis(SizeOfElementIncludingThis,
aMallocSizeOf);
return n;
}

View File

@ -22,6 +22,8 @@ class nsICategoryManager;
#define NS_STYLESHEETSERVICE_CONTRACTID \
"@mozilla.org/content/style-sheet-service;1"
class nsIMemoryReporter;
class nsStyleSheetService MOZ_FINAL : public nsIStyleSheetService
{
public:
@ -36,6 +38,8 @@ class nsStyleSheetService MOZ_FINAL : public nsIStyleSheetService
nsCOMArray<nsIStyleSheet>* AgentStyleSheets() { return &mSheets[AGENT_SHEET]; }
nsCOMArray<nsIStyleSheet>* UserStyleSheets() { return &mSheets[USER_SHEET]; }
static size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf);
static nsStyleSheetService *gInstance;
private:
@ -52,8 +56,12 @@ class nsStyleSheetService MOZ_FINAL : public nsIStyleSheetService
// new sheet will be the last sheet in mSheets[aSheetType].
NS_HIDDEN_(nsresult) LoadAndRegisterSheetInternal(nsIURI *aSheetURI,
uint32_t aSheetType);
size_t SizeOfIncludingThisHelper(nsMallocSizeOfFun aMallocSizeOf) const;
nsCOMArray<nsIStyleSheet> mSheets[2];
nsIMemoryReporter* mReporter;
};
#endif

View File

@ -26,7 +26,7 @@ GetStylesheetCacheSize()
LayoutStyleSheetCacheMallocSizeOf);
}
NS_MEMORY_REPORTER_IMPLEMENT(Sheets,
NS_MEMORY_REPORTER_IMPLEMENT(StyleSheetCache,
"explicit/layout/style-sheet-cache",
KIND_HEAP,
nsIMemoryReporter::UNITS_BYTES,
@ -161,11 +161,12 @@ nsLayoutStylesheetCache::Shutdown()
size_t
nsLayoutStylesheetCache::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf)
{
if (nsLayoutStylesheetCache::gStyleCache) {
return nsLayoutStylesheetCache::gStyleCache->
SizeOfIncludingThisHelper(aMallocSizeOf);
if (!nsLayoutStylesheetCache::gStyleCache) {
return 0;
}
return 0;
return nsLayoutStylesheetCache::gStyleCache->
SizeOfIncludingThisHelper(aMallocSizeOf);
}
size_t
@ -226,14 +227,14 @@ nsLayoutStylesheetCache::nsLayoutStylesheetCache()
}
NS_ASSERTION(mFullScreenOverrideSheet, "Could not load full-screen-override.css");
mSheetsReporter = new NS_MEMORY_REPORTER_NAME(Sheets);
(void)::NS_RegisterMemoryReporter(mSheetsReporter);
mReporter = new NS_MEMORY_REPORTER_NAME(StyleSheetCache);
(void)::NS_RegisterMemoryReporter(mReporter);
}
nsLayoutStylesheetCache::~nsLayoutStylesheetCache()
{
(void)::NS_UnregisterMemoryReporter(mSheetsReporter);
mSheetsReporter = nullptr;
(void)::NS_UnregisterMemoryReporter(mReporter);
mReporter = nullptr;
}
void

View File

@ -63,7 +63,7 @@ private:
nsRefPtr<nsCSSStyleSheet> mQuirkSheet;
nsRefPtr<nsCSSStyleSheet> mFullScreenOverrideSheet;
nsIMemoryReporter* mSheetsReporter;
nsIMemoryReporter* mReporter;
};
#endif