Bug 819791 - Part 1: Remove nsTArrayDefaultAllocator, replacing it unconditionally with nsTArrayInfallibleAllocator. r=bz
authorJustin Lebar <justin.lebar@gmail.com>
Tue, 18 Dec 2012 20:16:06 -0500
changeset 116477 24e3a92d51d499d118a0f85a6eed1fba60d0b876
parent 116476 2c74293986cf6c016ec5c3fa9d9e4bc1c668d5e9
child 116478 28b5c8d25fb9cd733b1a3fa9d30c34a0698d3b78
push id24052
push useremorley@mozilla.com
push dateWed, 19 Dec 2012 19:30:46 +0000
treeherdermozilla-central@27a1c1839d42 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs819791
milestone20.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 819791 - Part 1: Remove nsTArrayDefaultAllocator, replacing it unconditionally with nsTArrayInfallibleAllocator. r=bz
content/base/src/nsAttrValue.h
docshell/shistory/public/nsISHistoryInternal.idl
xpcom/glue/nsTArray.h
--- a/content/base/src/nsAttrValue.h
+++ b/content/base/src/nsAttrValue.h
@@ -19,17 +19,16 @@
 #include "nsMargin.h"
 #include "nsCOMPtr.h"
 #include "SVGAttrValueWrapper.h"
 
 class nsAString;
 class nsIAtom;
 class nsIDocument;
 template<class E, class A> class nsTArray;
-struct nsTArrayDefaultAllocator;
 class nsStyledElementNotElementCSSInlineStyle;
 struct MiscContainer;
 
 namespace mozilla {
 namespace css {
 class StyleRule;
 struct URLValue;
 struct ImageValue;
@@ -421,17 +420,17 @@ private:
                           nsresult* aErrorCode,
                           bool aCanBePercent = false,
                           bool* aIsPercent = nullptr) const;
   // Given an enum table and a particular entry in that table, return
   // the actual integer value we should store.
   int32_t EnumTableEntryToValue(const EnumTable* aEnumTable,
                                 const EnumTable* aTableEntry);  
 
-  static nsTArray<const EnumTable*, nsTArrayDefaultAllocator>* sEnumTableArray;
+  static nsTArray<const EnumTable*>* sEnumTableArray;
 
   uintptr_t mBits;
 };
 
 inline const nsAttrValue&
 nsAttrValue::operator=(const nsAttrValue& aOther)
 {
   SetTo(aOther);
--- a/docshell/shistory/public/nsISHistoryInternal.idl
+++ b/docshell/shistory/public/nsISHistoryInternal.idl
@@ -13,21 +13,21 @@ interface nsIDocShell;
 
 %{C++
 #define NS_SHISTORY_INTERNAL_CID \
 { 0x9c47c121, 0x1c6e, 0x4d8f, \
   { 0xb9, 0x04, 0x3a, 0xc9, 0x68, 0x11, 0x6e, 0x88 } }
 
 #define NS_SHISTORY_INTERNAL_CONTRACTID "@mozilla.org/browser/shistory-internal;1"
 
-template<class E, class A> class nsTArray;
-struct nsTArrayDefaultAllocator;
+struct nsTArrayInfallibleAllocator;
+template<class E, class Alloc> class nsTArray;
 %}
 
-[ref] native nsDocshellIDArray(nsTArray<uint64_t, nsTArrayDefaultAllocator>);
+[ref] native nsDocshellIDArray(nsTArray<uint64_t, nsTArrayInfallibleAllocator>);
 
 [scriptable, uuid(f9348014-0239-11e2-b029-3d38e719eb2d)]
 interface nsISHistoryInternal: nsISupports
 {
   /**
    * Add a new Entry to the History List
    * @param aEntry - The entry to add
    * @param aPersist - If true this specifies that the entry should persist
--- a/xpcom/glue/nsTArray.h
+++ b/xpcom/glue/nsTArray.h
@@ -70,18 +70,18 @@ struct nsTArrayInfallibleAllocator
   }
 
   static void SizeTooBig() {
     mozalloc_abort("Trying to allocate an infallible array that's too big");
   }
 };
 
 #else
+#include <stdlib.h>
 
-#include <stdlib.h>
 struct nsTArrayFallibleAllocator
 {
   static void* Malloc(size_t size) {
     return malloc(size);
   }
 
   static void* Realloc(void* ptr, size_t size) {
     return realloc(ptr, size);
@@ -90,22 +90,50 @@ struct nsTArrayFallibleAllocator
   static void Free(void* ptr) {
     free(ptr);
   }
 
   static void SizeTooBig() {
   }
 };
 
-#endif
+struct nsTArrayInfallibleAllocator
+{
+  static void* Malloc(size_t size) {
+    void* ptr = malloc(size);
+    if (MOZ_UNLIKELY(!ptr)) {
+      HandleOOM();
+    }
+    return ptr;
+  }
 
-#if defined(MOZALLOC_HAVE_XMALLOC)
-struct nsTArrayDefaultAllocator : public nsTArrayInfallibleAllocator { };
-#else
-struct nsTArrayDefaultAllocator : public nsTArrayFallibleAllocator { };
+  static void* Realloc(void* ptr, size_t size) {
+    void* newptr = realloc(ptr, size);
+    if (MOZ_UNLIKELY(!ptr && size)) {
+      HandleOOM();
+    }
+    return newptr;
+  }
+
+  static void Free(void* ptr) {
+    free(ptr);
+  }
+
+  static void SizeTooBig() {
+    HandleOOM();
+  }
+
+private:
+  static void HandleOOM() {
+    fputs("Out of memory allocating nsTArray buffer.\n", stderr);
+    MOZ_CRASH();
+    MOZ_NOT_REACHED();
+  }
+};
+
 #endif
 
 // nsTArray_base stores elements into the space allocated beyond
 // sizeof(*this).  This is done to minimize the size of the nsTArray
 // object when it is empty.
 struct NS_COM_GLUE nsTArrayHeader
 {
   static nsTArrayHeader sEmptyHdr;
@@ -409,17 +437,17 @@ template <class E> class FallibleTArray;
 // The Alloc template parameter can be used to choose between
 // "fallible" and "infallible" nsTArray (if available), defaulting to
 // fallible.  If the *fallible* allocator is used, the return value of
 // methods that might allocate needs to be checked; Append() is
 // one such method.  These return values don't need to be checked if
 // the *in*fallible allocator is chosen.  When in doubt, choose the
 // infallible allocator.
 //
-template<class E, class Alloc=nsTArrayDefaultAllocator>
+template<class E, class Alloc=nsTArrayInfallibleAllocator>
 class nsTArray : public nsTArray_base<Alloc>,
                  public nsTArray_SafeElementAtHelper<E, nsTArray<E, Alloc> >
 {
 public:
   typedef nsTArray_base<Alloc>           base_type;
   typedef typename base_type::size_type  size_type;
   typedef typename base_type::index_type index_type;
   typedef E                              elem_type;
@@ -1431,17 +1459,17 @@ private:
   union {
     char mAutoBuf[sizeof(nsTArrayHeader) + N * sizeof(elem_type)];
     // Do the max operation inline to ensure that it is a compile-time constant.
     mozilla::AlignedElem<(MOZ_ALIGNOF(Header) > MOZ_ALIGNOF(elem_type))
                          ? MOZ_ALIGNOF(Header) : MOZ_ALIGNOF(elem_type)> mAlign;
   };
 };
 
-template<class E, uint32_t N, class Alloc=nsTArrayDefaultAllocator>
+template<class E, uint32_t N, class Alloc=nsTArrayInfallibleAllocator>
 class nsAutoTArray : public nsAutoArrayBase<nsTArray<E, Alloc>, N>
 {
   typedef nsAutoArrayBase<nsTArray<E, Alloc>, N> Base;
 
 public:
   nsAutoTArray() {}
 
   template<typename Allocator>
@@ -1496,18 +1524,18 @@ public:
     Base::AppendElements(other);
   }
 };
 #endif
 
 // specializations for N = 0. this makes the inheritance model easier for
 // templated users of nsAutoTArray.
 template<class E>
-class nsAutoTArray<E, 0, nsTArrayDefaultAllocator> :
-  public nsAutoArrayBase< nsTArray<E, nsTArrayDefaultAllocator>, 0>
+class nsAutoTArray<E, 0, nsTArrayInfallibleAllocator> :
+  public nsAutoArrayBase< nsTArray<E, nsTArrayInfallibleAllocator>, 0>
 {
 public:
   nsAutoTArray() {}
 };
 
 template<class E>
 class AutoFallibleTArray<E, 0> :
   public nsAutoArrayBase< FallibleTArray<E>, 0>