Bug 1573229 - part 1 - Add infallible versions of the memory-allocation functions in CountingAllocatorBase. r=njn
authorJonathan Kew <jkew@mozilla.com>
Thu, 15 Aug 2019 00:49:55 +0000
changeset 488205 3fd3531df50ecc4d4e4a0d1603d40eb1d6031a3e
parent 488204 1dccb45167d39cb443d42afc46fab8ad55426af5
child 488206 f1d84baef0c346d83245fc92884d4132c921c99e
push id36437
push userncsoregi@mozilla.com
push dateThu, 15 Aug 2019 19:33:18 +0000
treeherdermozilla-central@44aac6fc3352 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnjn
bugs1573229
milestone70.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 1573229 - part 1 - Add infallible versions of the memory-allocation functions in CountingAllocatorBase. r=njn Differential Revision: https://phabricator.services.mozilla.com/D41720
xpcom/base/CountingAllocatorBase.h
--- a/xpcom/base/CountingAllocatorBase.h
+++ b/xpcom/base/CountingAllocatorBase.h
@@ -4,16 +4,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef CountingAllocatorBase_h
 #define CountingAllocatorBase_h
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Atomics.h"
+#include "mozilla/mozalloc.h"
 #include "nsIMemoryReporter.h"
 
 namespace mozilla {
 
 // This CRTP class handles several details of wrapping allocators and should
 // be preferred to manually counting with MOZ_DEFINE_MALLOC_SIZE_OF_ON_ALLOC
 // and MOZ_DEFINE_MALLOC_SIZE_OF_ON_FREE.  The typical use is in a memory
 // reporter for a particular third party library:
@@ -105,16 +106,46 @@ class CountingAllocatorBase {
     return CountingRealloc(p, size);
   }
 
   static void CountingFree(void* p) {
     sAmount -= MallocSizeOfOnFree(p);
     free(p);
   }
 
+  // Infallible-allocation wrappers for the counting malloc/calloc/realloc
+  // functions, for clients that don't safely handle allocation failures
+  // themselves.
+  static void* InfallibleCountingMalloc(size_t size) {
+    void* p = moz_xmalloc(size);
+    sAmount += MallocSizeOfOnAlloc(p);
+    return p;
+  }
+
+  static void* InfallibleCountingCalloc(size_t nmemb, size_t size) {
+    void* p = moz_xcalloc(nmemb, size);
+    sAmount += MallocSizeOfOnAlloc(p);
+    return p;
+  }
+
+  static void* InfallibleCountingRealloc(void* p, size_t size) {
+    size_t oldsize = MallocSizeOfOnFree(p);
+    void* pnew = moz_xrealloc(p, size);
+    if (pnew) {
+      size_t newsize = MallocSizeOfOnAlloc(pnew);
+      sAmount += newsize - oldsize;
+    } else if (size == 0) {
+      // See comment in CountingRealloc above.
+      sAmount -= oldsize;
+    } else {
+      // realloc failed.  The amount allocated hasn't changed.
+    }
+    return pnew;
+  }
+
  private:
   // |sAmount| can be (implicitly) accessed by multiple threads, so it
   // must be thread-safe. It may be written during GC, so accesses are not
   // recorded.
   typedef Atomic<size_t, SequentiallyConsistent,
                  recordreplay::Behavior::DontPreserve>
       AmountType;
   static AmountType sAmount;