From bc4023da9ba1db253b55a5eb2641cbe234f54873 Mon Sep 17 00:00:00 2001 From: kipp Date: Fri, 5 Jun 1998 21:38:36 +0000 Subject: [PATCH] Added SizeOf method git-svn-id: svn://10.0.0.236/trunk@3380 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/base/src/nsAtomTable.cpp | 10 ++++++++ mozilla/base/src/nsIAtom.h | 6 +++++ mozilla/base/src/nsString.cpp | 18 ++++++++++++++- mozilla/base/src/nsString.h | 6 ++++- mozilla/base/src/nsVoidArray.cpp | 27 ++++++++++++++-------- mozilla/base/src/nsVoidArray.h | 3 +++ mozilla/string/obsolete/nsString.cpp | 18 ++++++++++++++- mozilla/string/obsolete/nsString.h | 6 ++++- mozilla/xpcom/ds/nsAtomTable.cpp | 10 ++++++++ mozilla/xpcom/ds/nsIAtom.h | 6 +++++ mozilla/xpcom/ds/nsString.cpp | 18 ++++++++++++++- mozilla/xpcom/ds/nsString.h | 6 ++++- mozilla/xpcom/ds/nsVoidArray.cpp | 27 ++++++++++++++-------- mozilla/xpcom/ds/nsVoidArray.h | 3 +++ mozilla/xpcom/glue/nsVoidArray.cpp | 27 ++++++++++++++-------- mozilla/xpcom/glue/nsVoidArray.h | 3 +++ mozilla/xpcom/string/obsolete/nsString.cpp | 18 ++++++++++++++- mozilla/xpcom/string/obsolete/nsString.h | 6 ++++- 18 files changed, 183 insertions(+), 35 deletions(-) diff --git a/mozilla/base/src/nsAtomTable.cpp b/mozilla/base/src/nsAtomTable.cpp index 75998008cb5..41c5de0282b 100644 --- a/mozilla/base/src/nsAtomTable.cpp +++ b/mozilla/base/src/nsAtomTable.cpp @@ -19,6 +19,7 @@ #include "nsString.h" #include "nsCRT.h" #include "plhash.h" +#include "nsISizeOfHandler.h" /** * The shared hash table for atom lookups. @@ -39,6 +40,8 @@ public: virtual const PRUnichar* GetUnicode() const; + NS_IMETHOD SizeOf(nsISizeOfHandler* aHandler) const; + // Actually more; 0 terminated. This slot is reserved for the // terminating zero. PRUnichar mString[1]; @@ -89,6 +92,13 @@ const PRUnichar* AtomImpl::GetUnicode() const return mString; } +NS_IMETHODIMP +AtomImpl::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this) + nsCRT::strlen(mString) * sizeof(PRUnichar)); + return NS_OK; +} + //---------------------------------------------------------------------- static PLHashNumber HashKey(const PRUnichar* k) diff --git a/mozilla/base/src/nsIAtom.h b/mozilla/base/src/nsIAtom.h index 59e13f2035e..4492666f624 100644 --- a/mozilla/base/src/nsIAtom.h +++ b/mozilla/base/src/nsIAtom.h @@ -21,6 +21,7 @@ #include "nscore.h" #include "nsISupports.h" class nsString; +class nsISizeOfHandler; #define NS_IATOM_IID \ { 0x3d1b15b0, 0x93b4, 0x11d1, \ @@ -43,6 +44,11 @@ public: * Return a pointer to a zero terminated unicode string. */ virtual const PRUnichar* GetUnicode() const = 0; + + /** + * Add the size, in bytes, of the atom to the handler. + */ + NS_IMETHOD SizeOf(nsISizeOfHandler* aHandler) const = 0; }; /** diff --git a/mozilla/base/src/nsString.cpp b/mozilla/base/src/nsString.cpp index 0a3f4c9d43b..b2690e01391 100644 --- a/mozilla/base/src/nsString.cpp +++ b/mozilla/base/src/nsString.cpp @@ -24,7 +24,7 @@ #include "nsDebug.h" #include "prprf.h" #include "prdtoa.h" - +#include "nsISizeOfHandler.h" const PRInt32 kGrowthDelta = 8; const PRInt32 kNotFound = -1; @@ -116,6 +116,13 @@ nsString::~nsString() mCapacity=mLength=0; } +void +nsString::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this)); + aHandler->Add(mCapacity * sizeof(chartype)); +} + /*------------------------------------------------------- * This method gets called when the internal buffer needs * to grow to a given size. @@ -2029,6 +2036,15 @@ nsAutoString::~nsAutoString() } } +void +nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this)); + if (mStr != mBuf) { + aHandler->Add(mCapacity * sizeof(chartype)); + } +} + /**------------------------------------------------------- * diff --git a/mozilla/base/src/nsString.h b/mozilla/base/src/nsString.h index 075b014e1a8..d8d3018d6a8 100644 --- a/mozilla/base/src/nsString.h +++ b/mozilla/base/src/nsString.h @@ -35,7 +35,7 @@ #include "nsIAtom.h" #include #include - +class nsISizeOfHandler; class NS_BASE nsString { public: @@ -56,6 +56,8 @@ class NS_BASE nsString { void Truncate(PRInt32 anIndex=0); virtual void EnsureCapacityFor(PRInt32 aNewLength); + virtual void SizeOf(nsISizeOfHandler* aHandler) const; + ///accessor methods //@{ PRUnichar* GetUnicode(void) const; @@ -218,6 +220,8 @@ public: nsAutoString(const PRUnichar* us, PRInt32 uslen = -1); virtual ~nsAutoString(); + virtual void SizeOf(nsISizeOfHandler* aHandler) const; + static void SelfTest(); protected: diff --git a/mozilla/base/src/nsVoidArray.cpp b/mozilla/base/src/nsVoidArray.cpp index 804eb684f67..ee875757a65 100644 --- a/mozilla/base/src/nsVoidArray.cpp +++ b/mozilla/base/src/nsVoidArray.cpp @@ -17,6 +17,7 @@ */ #include "nsVoidArray.h" #include "nsCRT.h" +#include "nsISizeOfHandler.h" static PRInt32 kGrowArrayBy = 8; @@ -47,9 +48,15 @@ nsVoidArray& nsVoidArray::operator=(const nsVoidArray& other) nsVoidArray::~nsVoidArray() { if (nsnull != mArray) { - delete mArray; + delete [] mArray; } } +void +nsVoidArray::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this)); + aHandler->Add(sizeof(void*) * mArraySize); +} void* nsVoidArray::ElementAt(PRInt32 aIndex) const { @@ -84,16 +91,16 @@ PRBool nsVoidArray::InsertElementAt(void* aElement, PRInt32 aIndex) // We have to grow the array PRInt32 newCount = oldCount + kGrowArrayBy; void** newArray = new void*[newCount]; - if (mArray != nsnull && aIndex != 0) - nsCRT::memcpy(newArray, mArray, aIndex * sizeof(void*)); + if (mArray != nsnull && aIndex != 0) + nsCRT::memcpy(newArray, mArray, aIndex * sizeof(void*)); PRInt32 slide = oldCount - aIndex; if (0 != slide) { // Slide data over to make room for the insertion nsCRT::memcpy(newArray + aIndex + 1, mArray + aIndex, slide * sizeof(void*)); } - if (mArray != nsnull) - delete mArray; + if (mArray != nsnull) + delete [] mArray; mArray = newArray; mArraySize = newCount; } else { @@ -164,10 +171,12 @@ void nsVoidArray::Compact() PRInt32 count = mCount; if (mArraySize != count) { void** newArray = new void*[count]; - nsCRT::memcpy(newArray, mArray, count * sizeof(void*)); - delete mArray; - mArray = newArray; - mArraySize = count; + if (nsnull != newArray) { + nsCRT::memcpy(newArray, mArray, count * sizeof(void*)); + delete [] mArray; + mArray = newArray; + mArraySize = count; + } } } diff --git a/mozilla/base/src/nsVoidArray.h b/mozilla/base/src/nsVoidArray.h index 5f0345658ad..86ff1aeb518 100644 --- a/mozilla/base/src/nsVoidArray.h +++ b/mozilla/base/src/nsVoidArray.h @@ -19,6 +19,7 @@ #define nsVoidArray_h___ #include "nscore.h" +class nsISizeOfHandler; // Enumerator callback function. Return PR_FALSE to stop typedef PRBool (*nsVoidArrayEnumFunc)(void* aElement, void *aData); @@ -31,6 +32,8 @@ public: nsVoidArray& operator=(const nsVoidArray& other); + void SizeOf(nsISizeOfHandler* aHandler) const; + PRInt32 Count() const { return mCount; } diff --git a/mozilla/string/obsolete/nsString.cpp b/mozilla/string/obsolete/nsString.cpp index 0a3f4c9d43b..b2690e01391 100644 --- a/mozilla/string/obsolete/nsString.cpp +++ b/mozilla/string/obsolete/nsString.cpp @@ -24,7 +24,7 @@ #include "nsDebug.h" #include "prprf.h" #include "prdtoa.h" - +#include "nsISizeOfHandler.h" const PRInt32 kGrowthDelta = 8; const PRInt32 kNotFound = -1; @@ -116,6 +116,13 @@ nsString::~nsString() mCapacity=mLength=0; } +void +nsString::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this)); + aHandler->Add(mCapacity * sizeof(chartype)); +} + /*------------------------------------------------------- * This method gets called when the internal buffer needs * to grow to a given size. @@ -2029,6 +2036,15 @@ nsAutoString::~nsAutoString() } } +void +nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this)); + if (mStr != mBuf) { + aHandler->Add(mCapacity * sizeof(chartype)); + } +} + /**------------------------------------------------------- * diff --git a/mozilla/string/obsolete/nsString.h b/mozilla/string/obsolete/nsString.h index 075b014e1a8..d8d3018d6a8 100644 --- a/mozilla/string/obsolete/nsString.h +++ b/mozilla/string/obsolete/nsString.h @@ -35,7 +35,7 @@ #include "nsIAtom.h" #include #include - +class nsISizeOfHandler; class NS_BASE nsString { public: @@ -56,6 +56,8 @@ class NS_BASE nsString { void Truncate(PRInt32 anIndex=0); virtual void EnsureCapacityFor(PRInt32 aNewLength); + virtual void SizeOf(nsISizeOfHandler* aHandler) const; + ///accessor methods //@{ PRUnichar* GetUnicode(void) const; @@ -218,6 +220,8 @@ public: nsAutoString(const PRUnichar* us, PRInt32 uslen = -1); virtual ~nsAutoString(); + virtual void SizeOf(nsISizeOfHandler* aHandler) const; + static void SelfTest(); protected: diff --git a/mozilla/xpcom/ds/nsAtomTable.cpp b/mozilla/xpcom/ds/nsAtomTable.cpp index 75998008cb5..41c5de0282b 100644 --- a/mozilla/xpcom/ds/nsAtomTable.cpp +++ b/mozilla/xpcom/ds/nsAtomTable.cpp @@ -19,6 +19,7 @@ #include "nsString.h" #include "nsCRT.h" #include "plhash.h" +#include "nsISizeOfHandler.h" /** * The shared hash table for atom lookups. @@ -39,6 +40,8 @@ public: virtual const PRUnichar* GetUnicode() const; + NS_IMETHOD SizeOf(nsISizeOfHandler* aHandler) const; + // Actually more; 0 terminated. This slot is reserved for the // terminating zero. PRUnichar mString[1]; @@ -89,6 +92,13 @@ const PRUnichar* AtomImpl::GetUnicode() const return mString; } +NS_IMETHODIMP +AtomImpl::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this) + nsCRT::strlen(mString) * sizeof(PRUnichar)); + return NS_OK; +} + //---------------------------------------------------------------------- static PLHashNumber HashKey(const PRUnichar* k) diff --git a/mozilla/xpcom/ds/nsIAtom.h b/mozilla/xpcom/ds/nsIAtom.h index 59e13f2035e..4492666f624 100644 --- a/mozilla/xpcom/ds/nsIAtom.h +++ b/mozilla/xpcom/ds/nsIAtom.h @@ -21,6 +21,7 @@ #include "nscore.h" #include "nsISupports.h" class nsString; +class nsISizeOfHandler; #define NS_IATOM_IID \ { 0x3d1b15b0, 0x93b4, 0x11d1, \ @@ -43,6 +44,11 @@ public: * Return a pointer to a zero terminated unicode string. */ virtual const PRUnichar* GetUnicode() const = 0; + + /** + * Add the size, in bytes, of the atom to the handler. + */ + NS_IMETHOD SizeOf(nsISizeOfHandler* aHandler) const = 0; }; /** diff --git a/mozilla/xpcom/ds/nsString.cpp b/mozilla/xpcom/ds/nsString.cpp index 0a3f4c9d43b..b2690e01391 100644 --- a/mozilla/xpcom/ds/nsString.cpp +++ b/mozilla/xpcom/ds/nsString.cpp @@ -24,7 +24,7 @@ #include "nsDebug.h" #include "prprf.h" #include "prdtoa.h" - +#include "nsISizeOfHandler.h" const PRInt32 kGrowthDelta = 8; const PRInt32 kNotFound = -1; @@ -116,6 +116,13 @@ nsString::~nsString() mCapacity=mLength=0; } +void +nsString::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this)); + aHandler->Add(mCapacity * sizeof(chartype)); +} + /*------------------------------------------------------- * This method gets called when the internal buffer needs * to grow to a given size. @@ -2029,6 +2036,15 @@ nsAutoString::~nsAutoString() } } +void +nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this)); + if (mStr != mBuf) { + aHandler->Add(mCapacity * sizeof(chartype)); + } +} + /**------------------------------------------------------- * diff --git a/mozilla/xpcom/ds/nsString.h b/mozilla/xpcom/ds/nsString.h index 075b014e1a8..d8d3018d6a8 100644 --- a/mozilla/xpcom/ds/nsString.h +++ b/mozilla/xpcom/ds/nsString.h @@ -35,7 +35,7 @@ #include "nsIAtom.h" #include #include - +class nsISizeOfHandler; class NS_BASE nsString { public: @@ -56,6 +56,8 @@ class NS_BASE nsString { void Truncate(PRInt32 anIndex=0); virtual void EnsureCapacityFor(PRInt32 aNewLength); + virtual void SizeOf(nsISizeOfHandler* aHandler) const; + ///accessor methods //@{ PRUnichar* GetUnicode(void) const; @@ -218,6 +220,8 @@ public: nsAutoString(const PRUnichar* us, PRInt32 uslen = -1); virtual ~nsAutoString(); + virtual void SizeOf(nsISizeOfHandler* aHandler) const; + static void SelfTest(); protected: diff --git a/mozilla/xpcom/ds/nsVoidArray.cpp b/mozilla/xpcom/ds/nsVoidArray.cpp index 804eb684f67..ee875757a65 100644 --- a/mozilla/xpcom/ds/nsVoidArray.cpp +++ b/mozilla/xpcom/ds/nsVoidArray.cpp @@ -17,6 +17,7 @@ */ #include "nsVoidArray.h" #include "nsCRT.h" +#include "nsISizeOfHandler.h" static PRInt32 kGrowArrayBy = 8; @@ -47,9 +48,15 @@ nsVoidArray& nsVoidArray::operator=(const nsVoidArray& other) nsVoidArray::~nsVoidArray() { if (nsnull != mArray) { - delete mArray; + delete [] mArray; } } +void +nsVoidArray::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this)); + aHandler->Add(sizeof(void*) * mArraySize); +} void* nsVoidArray::ElementAt(PRInt32 aIndex) const { @@ -84,16 +91,16 @@ PRBool nsVoidArray::InsertElementAt(void* aElement, PRInt32 aIndex) // We have to grow the array PRInt32 newCount = oldCount + kGrowArrayBy; void** newArray = new void*[newCount]; - if (mArray != nsnull && aIndex != 0) - nsCRT::memcpy(newArray, mArray, aIndex * sizeof(void*)); + if (mArray != nsnull && aIndex != 0) + nsCRT::memcpy(newArray, mArray, aIndex * sizeof(void*)); PRInt32 slide = oldCount - aIndex; if (0 != slide) { // Slide data over to make room for the insertion nsCRT::memcpy(newArray + aIndex + 1, mArray + aIndex, slide * sizeof(void*)); } - if (mArray != nsnull) - delete mArray; + if (mArray != nsnull) + delete [] mArray; mArray = newArray; mArraySize = newCount; } else { @@ -164,10 +171,12 @@ void nsVoidArray::Compact() PRInt32 count = mCount; if (mArraySize != count) { void** newArray = new void*[count]; - nsCRT::memcpy(newArray, mArray, count * sizeof(void*)); - delete mArray; - mArray = newArray; - mArraySize = count; + if (nsnull != newArray) { + nsCRT::memcpy(newArray, mArray, count * sizeof(void*)); + delete [] mArray; + mArray = newArray; + mArraySize = count; + } } } diff --git a/mozilla/xpcom/ds/nsVoidArray.h b/mozilla/xpcom/ds/nsVoidArray.h index 5f0345658ad..86ff1aeb518 100644 --- a/mozilla/xpcom/ds/nsVoidArray.h +++ b/mozilla/xpcom/ds/nsVoidArray.h @@ -19,6 +19,7 @@ #define nsVoidArray_h___ #include "nscore.h" +class nsISizeOfHandler; // Enumerator callback function. Return PR_FALSE to stop typedef PRBool (*nsVoidArrayEnumFunc)(void* aElement, void *aData); @@ -31,6 +32,8 @@ public: nsVoidArray& operator=(const nsVoidArray& other); + void SizeOf(nsISizeOfHandler* aHandler) const; + PRInt32 Count() const { return mCount; } diff --git a/mozilla/xpcom/glue/nsVoidArray.cpp b/mozilla/xpcom/glue/nsVoidArray.cpp index 804eb684f67..ee875757a65 100644 --- a/mozilla/xpcom/glue/nsVoidArray.cpp +++ b/mozilla/xpcom/glue/nsVoidArray.cpp @@ -17,6 +17,7 @@ */ #include "nsVoidArray.h" #include "nsCRT.h" +#include "nsISizeOfHandler.h" static PRInt32 kGrowArrayBy = 8; @@ -47,9 +48,15 @@ nsVoidArray& nsVoidArray::operator=(const nsVoidArray& other) nsVoidArray::~nsVoidArray() { if (nsnull != mArray) { - delete mArray; + delete [] mArray; } } +void +nsVoidArray::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this)); + aHandler->Add(sizeof(void*) * mArraySize); +} void* nsVoidArray::ElementAt(PRInt32 aIndex) const { @@ -84,16 +91,16 @@ PRBool nsVoidArray::InsertElementAt(void* aElement, PRInt32 aIndex) // We have to grow the array PRInt32 newCount = oldCount + kGrowArrayBy; void** newArray = new void*[newCount]; - if (mArray != nsnull && aIndex != 0) - nsCRT::memcpy(newArray, mArray, aIndex * sizeof(void*)); + if (mArray != nsnull && aIndex != 0) + nsCRT::memcpy(newArray, mArray, aIndex * sizeof(void*)); PRInt32 slide = oldCount - aIndex; if (0 != slide) { // Slide data over to make room for the insertion nsCRT::memcpy(newArray + aIndex + 1, mArray + aIndex, slide * sizeof(void*)); } - if (mArray != nsnull) - delete mArray; + if (mArray != nsnull) + delete [] mArray; mArray = newArray; mArraySize = newCount; } else { @@ -164,10 +171,12 @@ void nsVoidArray::Compact() PRInt32 count = mCount; if (mArraySize != count) { void** newArray = new void*[count]; - nsCRT::memcpy(newArray, mArray, count * sizeof(void*)); - delete mArray; - mArray = newArray; - mArraySize = count; + if (nsnull != newArray) { + nsCRT::memcpy(newArray, mArray, count * sizeof(void*)); + delete [] mArray; + mArray = newArray; + mArraySize = count; + } } } diff --git a/mozilla/xpcom/glue/nsVoidArray.h b/mozilla/xpcom/glue/nsVoidArray.h index 5f0345658ad..86ff1aeb518 100644 --- a/mozilla/xpcom/glue/nsVoidArray.h +++ b/mozilla/xpcom/glue/nsVoidArray.h @@ -19,6 +19,7 @@ #define nsVoidArray_h___ #include "nscore.h" +class nsISizeOfHandler; // Enumerator callback function. Return PR_FALSE to stop typedef PRBool (*nsVoidArrayEnumFunc)(void* aElement, void *aData); @@ -31,6 +32,8 @@ public: nsVoidArray& operator=(const nsVoidArray& other); + void SizeOf(nsISizeOfHandler* aHandler) const; + PRInt32 Count() const { return mCount; } diff --git a/mozilla/xpcom/string/obsolete/nsString.cpp b/mozilla/xpcom/string/obsolete/nsString.cpp index 0a3f4c9d43b..b2690e01391 100644 --- a/mozilla/xpcom/string/obsolete/nsString.cpp +++ b/mozilla/xpcom/string/obsolete/nsString.cpp @@ -24,7 +24,7 @@ #include "nsDebug.h" #include "prprf.h" #include "prdtoa.h" - +#include "nsISizeOfHandler.h" const PRInt32 kGrowthDelta = 8; const PRInt32 kNotFound = -1; @@ -116,6 +116,13 @@ nsString::~nsString() mCapacity=mLength=0; } +void +nsString::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this)); + aHandler->Add(mCapacity * sizeof(chartype)); +} + /*------------------------------------------------------- * This method gets called when the internal buffer needs * to grow to a given size. @@ -2029,6 +2036,15 @@ nsAutoString::~nsAutoString() } } +void +nsAutoString::SizeOf(nsISizeOfHandler* aHandler) const +{ + aHandler->Add(sizeof(*this)); + if (mStr != mBuf) { + aHandler->Add(mCapacity * sizeof(chartype)); + } +} + /**------------------------------------------------------- * diff --git a/mozilla/xpcom/string/obsolete/nsString.h b/mozilla/xpcom/string/obsolete/nsString.h index 075b014e1a8..d8d3018d6a8 100644 --- a/mozilla/xpcom/string/obsolete/nsString.h +++ b/mozilla/xpcom/string/obsolete/nsString.h @@ -35,7 +35,7 @@ #include "nsIAtom.h" #include #include - +class nsISizeOfHandler; class NS_BASE nsString { public: @@ -56,6 +56,8 @@ class NS_BASE nsString { void Truncate(PRInt32 anIndex=0); virtual void EnsureCapacityFor(PRInt32 aNewLength); + virtual void SizeOf(nsISizeOfHandler* aHandler) const; + ///accessor methods //@{ PRUnichar* GetUnicode(void) const; @@ -218,6 +220,8 @@ public: nsAutoString(const PRUnichar* us, PRInt32 uslen = -1); virtual ~nsAutoString(); + virtual void SizeOf(nsISizeOfHandler* aHandler) const; + static void SelfTest(); protected: