Skip to content

Commit

Permalink
Bug 1380410 - Add NeverAllocPolicy, r=erahm
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: 8U38Oj3vyaF
  • Loading branch information
mystor committed Jul 25, 2017
1 parent a5c3b22 commit 0969ebe
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions mfbt/AllocPolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define mozilla_AllocPolicy_h

#include "mozilla/Attributes.h"
#include "mozilla/Assertions.h"
#include "mozilla/TemplateLib.h"

#include <stddef.h>
Expand Down Expand Up @@ -128,6 +129,67 @@ class MallocAllocPolicy
}
};

/*
* A policy which always fails to allocate memory, returning nullptr. Methods
* which expect an existing allocation assert.
*
* This type should be used in situations where you want to use a MFBT type with
* inline storage, and don't want to allow it to allocate on the heap.
*/
class NeverAllocPolicy
{
public:
template <typename T>
T* maybe_pod_malloc(size_t aNumElems)
{
return nullptr;
}

template <typename T>
T* maybe_pod_calloc(size_t aNumElems)
{
return nullptr;
}

template <typename T>
T* maybe_pod_realloc(T* aPtr, size_t aOldSize, size_t aNewSize)
{
MOZ_CRASH("NeverAllocPolicy::maybe_pod_realloc");
}

template <typename T>
T* pod_malloc(size_t aNumElems)
{
return nullptr;
}

template <typename T>
T* pod_calloc(size_t aNumElems)
{
return nullptr;
}

template <typename T>
T* pod_realloc(T* aPtr, size_t aOldSize, size_t aNewSize)
{
MOZ_CRASH("NeverAllocPolicy::pod_realloc");
}

void free_(void* aPtr)
{
MOZ_CRASH("NeverAllocPolicy::free_");
}

void reportAllocOverflow() const
{
}

MOZ_MUST_USE bool checkSimulatedOOM() const
{
return true;
}
};

} // namespace mozilla

#endif /* mozilla_AllocPolicy_h */

0 comments on commit 0969ebe

Please sign in to comment.