Bug 701210 - nsTArray::SizeOf() should use malloc_usable_size. r=roc
authorJustin Lebar <justin.lebar@gmail.com>
Wed, 09 Nov 2011 20:40:09 -0500
changeset 80134 71ca2fa34ca9ebeb3625cdde8b754b1be0731385
parent 80133 525329d5642a1c1a03aefdc413070685b6ae6fb4
child 80135 6ea9d4ad53a08c40163ca1cc10e412cc5464a9f7
push id323
push userrcampbell@mozilla.com
push dateTue, 15 Nov 2011 21:58:36 +0000
treeherderfx-team@3ea216303184 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc
bugs701210
milestone11.0a1
Bug 701210 - nsTArray::SizeOf() should use malloc_usable_size. r=roc
xpcom/glue/nsTArray.h
--- a/xpcom/glue/nsTArray.h
+++ b/xpcom/glue/nsTArray.h
@@ -437,16 +437,17 @@ public:
   typedef typename base_type::size_type  size_type;
   typedef typename base_type::index_type index_type;
   typedef E                              elem_type;
   typedef nsTArray<E, Alloc>             self_type;
   typedef nsTArrayElementTraits<E>       elem_traits;
   typedef nsTArray_SafeElementAtHelper<E, self_type> safeelementat_helper_type;
 
   using safeelementat_helper_type::SafeElementAt;
+  using base_type::EmptyHdr;
 
   // A special value that is used to indicate an invalid or unknown index
   // into the array.
   enum {
     NoIndex = index_type(-1)
   };
 
   using base_type::Length;
@@ -510,20 +511,23 @@ public:
 
   template<typename Allocator>
   nsTArray& operator=(const nsTArray<E, Allocator>& other) {
     ReplaceElementsAt(0, Length(), other.Elements(), other.Length());
     return *this;
   }
 
   // @return The amount of memory taken used by this nsTArray, not including
-  // sizeof(this)
+  // sizeof(*this).
   size_t SizeOf() const {
-    return this->UsesAutoArrayBuffer() ?
-      0 : this->Capacity() * sizeof(elem_type) + sizeof(*this->Hdr());
+    if (this->UsesAutoArrayBuffer() || Hdr() == EmptyHdr())
+      return 0;
+    size_t usable = moz_malloc_usable_size(this->Hdr());
+    return usable ? usable : 
+      this->Capacity() * sizeof(elem_type) + sizeof(*this->Hdr());
   }
 
   //
   // Accessor methods
   //
 
   // This method provides direct access to the array elements.
   // @return A pointer to the first element of the array.  If the array is