Bug 826521 (part 1) - Add SizeOfIncludingThisEvenIfShared() for strings. r=bz.
authorNicholas Nethercote <nnethercote@mozilla.com>
Thu, 17 Jan 2013 21:21:35 -0800
changeset 119232 b924819d71a5e1e30d00cd36820440562ce48a3d
parent 119231 fa8e691ed6bc23e88bf16da8017e0dddb6b2149a
child 119233 de2ab911692d91f6ea017d1d0f9d0a6d295ce6b5
push id21548
push usernnethercote@mozilla.com
push dateFri, 18 Jan 2013 05:25:32 +0000
treeherdermozilla-inbound@de2ab911692d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs826521
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 826521 (part 1) - Add SizeOfIncludingThisEvenIfShared() for strings. r=bz.
xpcom/string/public/nsStringBuffer.h
xpcom/string/public/nsTSubstring.h
xpcom/string/src/nsSubstring.cpp
xpcom/string/src/nsTSubstring.cpp
--- a/xpcom/string/public/nsStringBuffer.h
+++ b/xpcom/string/public/nsStringBuffer.h
@@ -143,11 +143,22 @@ class nsStringBuffer
        * unshared.  This is checked.
        */
       size_t SizeOfIncludingThisMustBeUnshared(nsMallocSizeOfFun aMallocSizeOf) const;
 
       /**
        * This measures the size only if the StringBuffer is unshared.
        */
       size_t SizeOfIncludingThisIfUnshared(nsMallocSizeOfFun aMallocSizeOf) const;
+
+      /**
+       * This measures the size regardless of whether the StringBuffer is
+       * unshared.
+       *
+       * WARNING: Only use this if you really know what you are doing, because
+       * it can easily lead to double-counting strings.  If you do use them,
+       * please explain clearly in a comment why it's safe and won't lead to
+       * double-counting.
+       */
+      size_t SizeOfIncludingThisEvenIfShared(nsMallocSizeOfFun aMallocSizeOf) const;
   };
 
 #endif /* !defined(nsStringBuffer_h__ */
--- a/xpcom/string/public/nsTSubstring.h
+++ b/xpcom/string/public/nsTSubstring.h
@@ -625,16 +625,27 @@ class nsTSubstring_CharT
       size_t SizeOfIncludingThisMustBeUnshared(nsMallocSizeOfFun mallocSizeOf)
         const;
 
       size_t SizeOfExcludingThisIfUnshared(nsMallocSizeOfFun mallocSizeOf)
         const;
       size_t SizeOfIncludingThisIfUnshared(nsMallocSizeOfFun mallocSizeOf)
         const;
 
+        /**
+         * WARNING: Only use these functions if you really know what you are
+         * doing, because they can easily lead to double-counting strings.  If
+         * you do use them, please explain clearly in a comment why it's safe
+         * and won't lead to double-counting.
+         */
+      size_t SizeOfExcludingThisEvenIfShared(nsMallocSizeOfFun mallocSizeOf)
+        const;
+      size_t SizeOfIncludingThisEvenIfShared(nsMallocSizeOfFun mallocSizeOf)
+        const;
+
     protected:
 
       friend class nsTObsoleteAStringThunk_CharT;
       friend class nsTSubstringTuple_CharT;
 
       // XXX GCC 3.4 needs this :-(
       friend class nsTPromiseFlatString_CharT;
 
--- a/xpcom/string/src/nsSubstring.cpp
+++ b/xpcom/string/src/nsSubstring.cpp
@@ -288,16 +288,22 @@ nsStringBuffer::SizeOfIncludingThisIfUns
   {
     if (!IsReadonly())
       {
         return SizeOfIncludingThisMustBeUnshared(aMallocSizeOf);
       }
     return 0;
   }
 
+size_t
+nsStringBuffer::SizeOfIncludingThisEvenIfShared(nsMallocSizeOfFun aMallocSizeOf) const
+  {
+    return aMallocSizeOf(this);
+  }
+
 // ---------------------------------------------------------------------------
 
 
   // define nsSubstring
 #include "string-template-def-unichar.h"
 #include "nsTSubstring.cpp"
 #include "string-template-undef.h"
 
--- a/xpcom/string/src/nsTSubstring.cpp
+++ b/xpcom/string/src/nsTSubstring.cpp
@@ -883,17 +883,17 @@ nsTSubstring_CharT::DoAppendFloat( doubl
 
 size_t
 nsTSubstring_CharT::SizeOfExcludingThisMustBeUnshared(
     nsMallocSizeOfFun mallocSizeOf) const
 {
   if (mFlags & F_SHARED) {
     return nsStringBuffer::FromData(mData)->
              SizeOfIncludingThisMustBeUnshared(mallocSizeOf);
-  } 
+  }
   if (mFlags & F_OWNED) {
     return mallocSizeOf(mData);
   }
 
   // If we reach here, exactly one of the following must be true:
   // - F_VOIDED is set, and mData points to sEmptyBuffer;
   // - F_FIXED is set, and mData points to a buffer within a string
   //   object (e.g. nsAutoString);
@@ -916,21 +916,44 @@ nsTSubstring_CharT::SizeOfExcludingThisI
   }
   if (mFlags & F_OWNED) {
     return mallocSizeOf(mData);
   }
   return 0;
 }
 
 size_t
+nsTSubstring_CharT::SizeOfExcludingThisEvenIfShared(
+    nsMallocSizeOfFun mallocSizeOf) const
+{
+  // This is identical to SizeOfExcludingThisMustBeUnshared except for the
+  // F_SHARED case.
+  if (mFlags & F_SHARED) {
+    return nsStringBuffer::FromData(mData)->
+             SizeOfIncludingThisEvenIfShared(mallocSizeOf);
+  }
+  if (mFlags & F_OWNED) {
+    return mallocSizeOf(mData);
+  }
+  return 0;
+}
+
+size_t
 nsTSubstring_CharT::SizeOfIncludingThisMustBeUnshared(
     nsMallocSizeOfFun mallocSizeOf) const
 {
   return mallocSizeOf(this) + SizeOfExcludingThisMustBeUnshared(mallocSizeOf);
 }
 
 size_t
 nsTSubstring_CharT::SizeOfIncludingThisIfUnshared(
     nsMallocSizeOfFun mallocSizeOf) const
 {
   return mallocSizeOf(this) + SizeOfExcludingThisIfUnshared(mallocSizeOf);
 }
 
+size_t
+nsTSubstring_CharT::SizeOfIncludingThisEvenIfShared(
+    nsMallocSizeOfFun mallocSizeOf) const
+{
+  return mallocSizeOf(this) + SizeOfExcludingThisEvenIfShared(mallocSizeOf);
+}
+