pjs/xpcom/ds/nsSizeOfHandler.cpp

276 строки
7.2 KiB
C++
Исходник Обычный вид История

1998-06-06 00:46:23 +04:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
1998-06-06 00:46:23 +04:00
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
1998-06-06 00:46:23 +04:00
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
1998-06-06 00:46:23 +04:00
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
1998-06-06 00:46:23 +04:00
*/
#include "nsISizeOfHandler.h"
#include "nsIAtom.h"
1998-06-06 00:46:23 +04:00
#include "plhash.h"
class nsSizeOfHandler : public nsISizeOfHandler {
public:
nsSizeOfHandler();
virtual ~nsSizeOfHandler();
1998-06-06 00:46:23 +04:00
// nsISupports
NS_DECL_ISUPPORTS
// nsISizeOfHandler
NS_IMETHOD Init();
NS_IMETHOD RecordObject(void* aObject, PRBool* aResult);
NS_IMETHOD AddSize(nsIAtom* aKey, PRUint32 aSize);
NS_IMETHOD Report(nsISizeofReportFunc aFunc, void* aArg);
NS_IMETHOD GetTotals(PRUint32* aCountResult, PRUint32* aTotalSizeResult);
1998-06-06 00:46:23 +04:00
protected:
PRUint32 mTotalSize;
PRUint32 mTotalCount;
PLHashTable* mSizeTable;
PLHashTable* mObjectTable;
static PRIntn RemoveObjectEntry(PLHashEntry* he, PRIntn i, void* arg);
static PRIntn RemoveSizeEntry(PLHashEntry* he, PRIntn i, void* arg);
static PRIntn ReportEntry(PLHashEntry* he, PRIntn i, void* arg);
};
class SizeOfDataStats {
public:
SizeOfDataStats(nsIAtom* aType, PRUint32 aSize);
~SizeOfDataStats();
void Update(PRUint32 aSize);
nsIAtom* mType; // type
PRUint32 mCount; // # of objects of this type
PRUint32 mTotalSize; // total size of all objects of this type
PRUint32 mMinSize; // smallest size for this type
PRUint32 mMaxSize; // largest size for this type
1998-06-06 00:46:23 +04:00
};
//----------------------------------------------------------------------
MOZ_DECL_CTOR_COUNTER(SizeOfDataStats);
SizeOfDataStats::SizeOfDataStats(nsIAtom* aType, PRUint32 aSize)
: mType(aType),
mCount(1),
mTotalSize(aSize),
mMinSize(aSize),
mMaxSize(aSize)
{
MOZ_COUNT_CTOR(SizeOfDataStats);
NS_IF_ADDREF(mType);
}
SizeOfDataStats::~SizeOfDataStats()
{
MOZ_COUNT_DTOR(SizeOfDataStats);
NS_IF_RELEASE(mType);
}
void
SizeOfDataStats::Update(PRUint32 aSize)
{
mCount++;
if (aSize < mMinSize) mMinSize = aSize;
if (aSize > mMaxSize) mMaxSize = aSize;
mTotalSize += aSize;
}
//----------------------------------------------------------------------
#define POINTER_HASH_KEY(_atom) ((PLHashNumber) _atom)
1998-06-06 00:46:23 +04:00
static PLHashNumber
PointerHashKey(nsIAtom* key)
1998-06-06 00:46:23 +04:00
{
return POINTER_HASH_KEY(key);
1998-06-06 00:46:23 +04:00
}
static PRIntn
PointerCompareKeys(void* key1, void* key2)
1998-06-06 00:46:23 +04:00
{
return key1 == key2;
}
nsSizeOfHandler::nsSizeOfHandler()
: mTotalSize(0),
mTotalCount(0)
1998-06-06 00:46:23 +04:00
{
NS_INIT_REFCNT();
mTotalSize = 0;
mSizeTable = PL_NewHashTable(32, (PLHashFunction) PointerHashKey,
(PLHashComparator) PointerCompareKeys,
(PLHashComparator) nsnull,
nsnull, nsnull);
mObjectTable = PL_NewHashTable(32, (PLHashFunction) PointerHashKey,
(PLHashComparator) PointerCompareKeys,
(PLHashComparator) nsnull,
nsnull, nsnull);
}
PRIntn
nsSizeOfHandler::RemoveObjectEntry(PLHashEntry* he, PRIntn i, void* arg)
{
// Remove and free this entry and continue enumerating
return HT_ENUMERATE_REMOVE | HT_ENUMERATE_NEXT;
}
PRIntn
nsSizeOfHandler::RemoveSizeEntry(PLHashEntry* he, PRIntn i, void* arg)
{
if (he->value) {
SizeOfDataStats* stats = (SizeOfDataStats*) he->value;
he->value = nsnull;
delete stats;
}
// Remove and free this entry and continue enumerating
return HT_ENUMERATE_REMOVE | HT_ENUMERATE_NEXT;
1998-06-06 00:46:23 +04:00
}
nsSizeOfHandler::~nsSizeOfHandler()
{
if (nsnull != mObjectTable) {
PL_HashTableEnumerateEntries(mObjectTable, RemoveObjectEntry, 0);
PL_HashTableDestroy(mObjectTable);
}
if (nsnull != mSizeTable) {
PL_HashTableEnumerateEntries(mSizeTable, RemoveSizeEntry, 0);
PL_HashTableDestroy(mSizeTable);
1998-06-06 00:46:23 +04:00
}
}
NS_IMPL_ISUPPORTS1(nsSizeOfHandler, nsISizeOfHandler)
1998-06-06 00:46:23 +04:00
NS_IMETHODIMP
nsSizeOfHandler::Init()
1998-06-06 00:46:23 +04:00
{
if (mObjectTable) {
PL_HashTableEnumerateEntries(mObjectTable, RemoveObjectEntry, 0);
}
if (mSizeTable) {
PL_HashTableEnumerateEntries(mSizeTable, RemoveSizeEntry, 0);
}
mTotalCount = 0;
mTotalSize = 0;
1998-06-06 00:46:23 +04:00
return NS_OK;
}
NS_IMETHODIMP
nsSizeOfHandler::RecordObject(void* aAddr, PRBool* aResult)
1998-06-06 00:46:23 +04:00
{
if (!aResult) {
return NS_ERROR_NULL_POINTER;
1998-06-06 00:46:23 +04:00
}
PRBool result = PR_TRUE;
if (mObjectTable && aAddr) {
PLHashNumber hashCode = POINTER_HASH_KEY(aAddr);
PLHashEntry** hep = PL_HashTableRawLookup(mObjectTable, hashCode, aAddr);
1998-06-06 00:46:23 +04:00
PLHashEntry* he = *hep;
if (!he) {
// We've never seen it before. Add it to the table
(void) PL_HashTableRawAdd(mObjectTable, hep, hashCode, aAddr, aAddr);
result = PR_FALSE;
1998-06-06 00:46:23 +04:00
}
}
*aResult = result;
return NS_OK;
}
NS_IMETHODIMP
nsSizeOfHandler::AddSize(nsIAtom* aType, PRUint32 aSize)
{
PLHashNumber hashCode = POINTER_HASH_KEY(aType);
PLHashEntry** hep = PL_HashTableRawLookup(mSizeTable, hashCode, aType);
PLHashEntry* he = *hep;
if (he) {
// Stats already exist
SizeOfDataStats* stats = (SizeOfDataStats*) he->value;
stats->Update(aSize);
}
else {
// Make new stats for the new frame type
SizeOfDataStats* newStats = new SizeOfDataStats(aType, aSize);
PL_HashTableRawAdd(mSizeTable, hep, hashCode, aType, newStats);
}
mTotalCount++;
mTotalSize += aSize;
return NS_OK;
}
struct ReportArgs {
nsISizeOfHandler* mHandler;
nsISizeofReportFunc mFunc;
void* mArg;
};
PRIntn
nsSizeOfHandler::ReportEntry(PLHashEntry* he, PRIntn i, void* arg)
{
ReportArgs* ra = (ReportArgs*) arg;
if (he && ra && ra->mFunc) {
SizeOfDataStats* stats = (SizeOfDataStats*) he->value;
if (stats) {
(*ra->mFunc)(ra->mHandler, stats->mType, stats->mCount,
stats->mTotalSize, stats->mMinSize, stats->mMaxSize,
ra->mArg);
1998-06-06 00:46:23 +04:00
}
}
return HT_ENUMERATE_NEXT;
1998-06-06 00:46:23 +04:00
}
NS_IMETHODIMP
nsSizeOfHandler::Report(nsISizeofReportFunc aFunc, void* aArg)
1998-06-06 00:46:23 +04:00
{
ReportArgs ra;
ra.mHandler = this;
ra.mFunc = aFunc;
ra.mArg = aArg;
PL_HashTableEnumerateEntries(mSizeTable, ReportEntry, (void*) &ra);
return NS_OK;
}
NS_IMETHODIMP
nsSizeOfHandler::GetTotals(PRUint32* aTotalCountResult,
PRUint32* aTotalSizeResult)
{
if (!aTotalCountResult || !aTotalSizeResult) {
return NS_ERROR_NULL_POINTER;
}
*aTotalCountResult = mTotalCount;
*aTotalSizeResult = mTotalSize;
1998-06-06 00:46:23 +04:00
return NS_OK;
}
1999-05-26 05:38:36 +04:00
NS_COM nsresult
1998-06-06 00:46:23 +04:00
NS_NewSizeOfHandler(nsISizeOfHandler** aInstancePtrResult)
{
if (!aInstancePtrResult) {
return NS_ERROR_NULL_POINTER;
}
1998-06-06 00:46:23 +04:00
nsISizeOfHandler *it = new nsSizeOfHandler();
if (it == nsnull) {
return NS_ERROR_OUT_OF_MEMORY;
}
return it->QueryInterface(NS_GET_IID(nsISizeOfHandler), (void **) aInstancePtrResult);
1998-06-06 00:46:23 +04:00
}