Bug 1573229 - part 1 - Add infallible versions of the memory-allocation functions in CountingAllocatorBase. r=njn

Differential Revision: https://phabricator.services.mozilla.com/D41720

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jonathan Kew 2019-08-15 00:49:55 +00:00
parent 673c1a7a72
commit 49e3e4c0e0

View File

@ -9,6 +9,7 @@
#include "mozilla/Assertions.h"
#include "mozilla/Atomics.h"
#include "mozilla/mozalloc.h"
#include "nsIMemoryReporter.h"
namespace mozilla {
@ -110,6 +111,36 @@ class CountingAllocatorBase {
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