Bug 1102525 (part 1) - Add InfallibleAllocPolicy to mozalloc. r=glandium.

--HG--
extra : rebase_source : a79162fb7f73e52c5c8df29c8229efc64c3451e5
This commit is contained in:
Nicholas Nethercote 2014-12-08 14:45:10 -08:00
parent a878e7c6cd
commit a380fc6e28
3 changed files with 57 additions and 5 deletions

View File

@ -21,6 +21,8 @@
#if defined(__cplusplus)
#include "mozilla/fallible.h"
#include "mozilla/NullPtr.h"
#include "mozilla/TemplateLib.h"
#endif
#include "mozilla/Attributes.h"
@ -294,7 +296,49 @@ void operator delete[](void* ptr, const mozilla::fallible_t&) MOZALLOC_THROW_IF_
moz_free(ptr);
}
/*
* This policy is identical to MallocAllocPolicy, except it uses
* moz_xmalloc/moz_xcalloc/moz_xrealloc/moz_free instead of
* malloc/calloc/realloc/free.
*/
class InfallibleAllocPolicy
{
public:
template <typename T>
T* pod_malloc(size_t aNumElems)
{
if (aNumElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
return nullptr;
}
return static_cast<T*>(moz_xmalloc(aNumElems * sizeof(T)));
}
template <typename T>
T* pod_calloc(size_t aNumElems)
{
return static_cast<T*>(moz_xcalloc(aNumElems, sizeof(T)));
}
template <typename T>
T* pod_realloc(T* aPtr, size_t aOldSize, size_t aNewSize)
{
if (aNewSize & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
return nullptr;
}
return static_cast<T*>(moz_xrealloc(aPtr, aNewSize * sizeof(T)));
}
void free_(void* aPtr)
{
moz_free(aPtr);
}
void reportAllocOverflow() const
{
}
};
#endif /* ifdef __cplusplus */
#endif /* ifndef mozilla_mozalloc_h */

View File

@ -111,12 +111,18 @@ static bool gIsDMDInitialized = false;
// This provides infallible allocations (they abort on OOM). We use it for all
// of DMD's own allocations, which fall into the following three cases.
//
// - Direct allocations (the easy case).
//
// - Indirect allocations in js::{Vector,HashSet,HashMap} -- this class serves
// as their AllocPolicy.
//
// - Other indirect allocations (e.g. NS_StackWalk) -- see the comments on
// Thread::mBlockIntercepts and in replace_malloc for how these work.
//
// It would be nice if we could use the InfallibleAllocPolicy from mozalloc,
// but DMD cannot use mozalloc.
//
class InfallibleAllocPolicy
{
static void ExitOnFailure(const void* aP);

View File

@ -55,8 +55,9 @@ public:
template <typename T>
T* pod_malloc(size_t aNumElems)
{
if (aNumElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value)
return nullptr;
if (aNumElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
return nullptr;
}
return static_cast<T*>(malloc(aNumElems * sizeof(T)));
}
@ -69,8 +70,9 @@ public:
template <typename T>
T* pod_realloc(T* aPtr, size_t aOldSize, size_t aNewSize)
{
if (aNewSize & mozilla::tl::MulOverflowMask<sizeof(T)>::value)
return nullptr;
if (aNewSize & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
return nullptr;
}
return static_cast<T*>(realloc(aPtr, aNewSize * sizeof(T)));
}