Skip to content

Commit

Permalink
Bug 1102525 (part 1) - Add InfallibleAllocPolicy to mozalloc. r=gland…
Browse files Browse the repository at this point in the history
…ium.

--HG--
extra : rebase_source : a79162fb7f73e52c5c8df29c8229efc64c3451e5
  • Loading branch information
nnethercote committed Dec 8, 2014
1 parent 4d39bfe commit e4c12e9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
46 changes: 45 additions & 1 deletion memory/mozalloc/mozalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#if defined(__cplusplus)
#include "mozilla/fallible.h"
#include "mozilla/NullPtr.h"
#include "mozilla/TemplateLib.h"
#endif
#include "mozilla/Attributes.h"

Expand Down Expand Up @@ -294,7 +296,49 @@ void operator delete[](void* ptr, const mozilla::fallible_t&) MOZALLOC_THROW_IF_
moz_free(ptr);
}

#endif /* ifdef __cplusplus */

/*
* 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 */
6 changes: 6 additions & 0 deletions memory/replace/dmd/DMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions mfbt/AllocPolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class MallocAllocPolicy
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)));
}

Expand All @@ -69,8 +70,9 @@ class MallocAllocPolicy
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)));
}

Expand Down

0 comments on commit e4c12e9

Please sign in to comment.