Bug 697335 - Add nsBaseHashtable::SizeOfExcludingThis(). r=bz.
authorNicholas Nethercote <nnethercote@mozilla.com>
Sun, 18 Dec 2011 14:39:47 -0800
changeset 82939 e255fa32719cbe3ff63ce3238ab2b9f601556275
parent 82938 5efce975a9e7bef07cf2840268a33fefeff5de41
child 82940 692d80735b7e90d94e595b20244891f85e73f7e0
push id4182
push usernnethercote@mozilla.com
push dateMon, 19 Dec 2011 04:09:32 +0000
treeherdermozilla-inbound@692d80735b7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs697335
milestone11.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 697335 - Add nsBaseHashtable::SizeOfExcludingThis(). r=bz.
xpcom/glue/nsBaseHashtable.h
--- a/xpcom/glue/nsBaseHashtable.h
+++ b/xpcom/glue/nsBaseHashtable.h
@@ -239,16 +239,56 @@ public:
                                   &enumData);
   }
 
   /**
    * reset the hashtable, removing all entries
    */
   void Clear() { nsTHashtable<EntryType>::Clear(); }
 
+  /**
+   * client must provide a SizeOfEntryExcludingThisFun function for
+   *   SizeOfExcludingThis.
+   * @param     aKey the key being enumerated
+   * @param     aData Reference to data being enumerated.
+   * @param     mallocSizeOf the function used to measure heap-allocated blocks
+   * @param     userArg passed unchanged from SizeOf{In,Ex}cludingThis
+   * @return    summed size of the things pointed to by the entries
+   */
+  typedef size_t
+    (* SizeOfEntryExcludingThisFun)(KeyType           aKey,
+                                    const DataType    &aData,
+                                    nsMallocSizeOfFun mallocSizeOf,
+                                    void*             userArg);
+
+  /**
+   * Measure the size of the table's entry storage, and if
+   * |sizeOfEntryExcludingThis| is non-NULL, measure the size of things pointed
+   * to by entries.
+   * 
+   * @param     sizeOfEntryExcludingThis the
+   *            <code>SizeOfEntryExcludingThisFun</code> function to call
+   * @param     mallocSizeOf the function used to measure heap-allocated blocks
+   * @param     userArg a pointer to pass to the
+   *            <code>SizeOfEntryExcludingThisFun</code> function
+   * @return    the summed size of all the entries
+   */
+  size_t SizeOfExcludingThis(SizeOfEntryExcludingThisFun sizeOfEntryExcludingThis,
+                             nsMallocSizeOfFun mallocSizeOf, void *userArg)
+  {
+    if (IsInitialized()) {
+      s_SizeOfArgs args = { sizeOfEntryExcludingThis, userArg };
+      return PL_DHashTableSizeOfExcludingThis(&this->mTable,
+                                              s_SizeOfStub,
+                                              mallocSizeOf,
+                                              &args);
+    }
+    return 0;
+  }
+
 protected:
   /**
    * used internally during EnumerateRead.  Allocated on the stack.
    * @param func the enumerator passed to EnumerateRead
    * @param userArg the userArg passed to EnumerateRead
    */
   struct s_EnumReadArgs
   {
@@ -266,16 +306,26 @@ protected:
     EnumFunction func;
     void* userArg;
   };
 
   static PLDHashOperator s_EnumStub(PLDHashTable      *table,
                                     PLDHashEntryHdr   *hdr,
                                     PRUint32           number,
                                     void              *arg);
+
+  struct s_SizeOfArgs
+  {
+    SizeOfEntryExcludingThisFun func;
+    void* userArg;
+  };
+  
+  static size_t s_SizeOfStub(PLDHashEntryHdr *entry,
+                             nsMallocSizeOfFun mallocSizeOf,
+                             void *arg);
 };
 
 /**
  * This class is a thread-safe version of nsBaseHashtable.
  */
 template<class KeyClass,class DataType,class UserDataType>
 class nsBaseHashtableMT :
   protected nsBaseHashtable<KeyClass,DataType,UserDataType>
@@ -359,16 +409,26 @@ nsBaseHashtable<KeyClass,DataType,UserDa
   (PLDHashTable *table, PLDHashEntryHdr *hdr, PRUint32 number, void* arg)
 {
   EntryType* ent = static_cast<EntryType*>(hdr);
   s_EnumArgs* eargs = (s_EnumArgs*) arg;
 
   return (eargs->func)(ent->GetKey(), ent->mData, eargs->userArg);
 }
 
+template<class KeyClass,class DataType,class UserDataType>
+size_t
+nsBaseHashtable<KeyClass,DataType,UserDataType>::s_SizeOfStub
+  (PLDHashEntryHdr *hdr, nsMallocSizeOfFun mallocSizeOf, void *arg)
+{
+  EntryType* ent = static_cast<EntryType*>(hdr);
+  s_SizeOfArgs* eargs = static_cast<s_SizeOfArgs*>(arg);
+
+  return (eargs->func)(ent->GetKey(), ent->mData, mallocSizeOf, eargs->userArg);
+}
 
 //
 // nsBaseHashtableMT  definitions
 //
 
 template<class KeyClass,class DataType,class UserDataType>
 nsBaseHashtableMT<KeyClass,DataType,UserDataType>::~nsBaseHashtableMT()
 {