1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * An allocation policy concept, usable for structures and algorithms to
9 * control how memory is allocated and how failures are handled.
12 #ifndef mozilla_AllocPolicy_h
13 #define mozilla_AllocPolicy_h
15 #include "mozilla/Attributes.h"
16 #include "mozilla/Assertions.h"
17 #include "mozilla/TemplateLib.h"
25 * Allocation policies are used to implement the standard allocation behaviors
26 * in a customizable way. Additionally, custom behaviors may be added to these
27 * behaviors, such as additionally reporting an error through an out-of-band
28 * mechanism when OOM occurs. The concept modeled here is as follows:
30 * - public copy constructor, assignment, destructor
31 * - template <typename T> T* maybe_pod_malloc(size_t)
32 * Fallible, but doesn't report an error on OOM.
33 * - template <typename T> T* maybe_pod_calloc(size_t)
34 * Fallible, but doesn't report an error on OOM.
35 * - template <typename T> T* maybe_pod_realloc(T*, size_t, size_t)
36 * Fallible, but doesn't report an error on OOM. The old allocation
37 * size is passed in, in addition to the new allocation size requested.
38 * - template <typename T> T* pod_malloc(size_t)
39 * Responsible for OOM reporting when null is returned.
40 * - template <typename T> T* pod_calloc(size_t)
41 * Responsible for OOM reporting when null is returned.
42 * - template <typename T> T* pod_realloc(T*, size_t, size_t)
43 * Responsible for OOM reporting when null is returned. The old allocation
44 * size is passed in, in addition to the new allocation size requested.
46 * - void reportAllocOverflow() const
47 * Called on allocation overflow (that is, an allocation implicitly tried
48 * to allocate more than the available memory space -- think allocating an
49 * array of large-size objects, where N * size overflows) before null is
51 * - bool checkSimulatedOOM() const
52 * Some clients generally allocate memory yet in some circumstances won't
53 * need to do so. For example, appending to a vector with a small amount of
54 * inline storage generally allocates memory, but no allocation occurs
55 * unless appending exceeds inline storage. But for testing purposes, it
56 * can be useful to treat *every* operation as allocating.
57 * Clients (such as this hypothetical append method implementation) should
58 * call this method in situations that don't allocate, but could generally,
59 * to support this. The default behavior should return true; more
60 * complicated behavior might be to return false only after a certain
61 * number of allocations-or-check-simulated-OOMs (coordinating with the
62 * other AllocPolicy methods) have occurred.
64 * mfbt provides (and typically uses by default) only MallocAllocPolicy, which
65 * does nothing more than delegate to the malloc/alloc/free functions.
69 * A policy that straightforwardly uses malloc/calloc/realloc/free and adds no
72 class MallocAllocPolicy
76 T
* maybe_pod_malloc(size_t aNumElems
)
78 if (aNumElems
& mozilla::tl::MulOverflowMask
<sizeof(T
)>::value
) {
81 return static_cast<T
*>(malloc(aNumElems
* sizeof(T
)));
85 T
* maybe_pod_calloc(size_t aNumElems
)
87 return static_cast<T
*>(calloc(aNumElems
, sizeof(T
)));
91 T
* maybe_pod_realloc(T
* aPtr
, size_t aOldSize
, size_t aNewSize
)
93 if (aNewSize
& mozilla::tl::MulOverflowMask
<sizeof(T
)>::value
) {
96 return static_cast<T
*>(realloc(aPtr
, aNewSize
* sizeof(T
)));
100 T
* pod_malloc(size_t aNumElems
)
102 return maybe_pod_malloc
<T
>(aNumElems
);
105 template <typename T
>
106 T
* pod_calloc(size_t aNumElems
)
108 return maybe_pod_calloc
<T
>(aNumElems
);
111 template <typename T
>
112 T
* pod_realloc(T
* aPtr
, size_t aOldSize
, size_t aNewSize
)
114 return maybe_pod_realloc
<T
>(aPtr
, aOldSize
, aNewSize
);
117 void free_(void* aPtr
)
122 void reportAllocOverflow() const
126 MOZ_MUST_USE
bool checkSimulatedOOM() const
133 * A policy which always fails to allocate memory, returning nullptr. Methods
134 * which expect an existing allocation assert.
136 * This type should be used in situations where you want to use a MFBT type with
137 * inline storage, and don't want to allow it to allocate on the heap.
139 class NeverAllocPolicy
142 template <typename T
>
143 T
* maybe_pod_malloc(size_t aNumElems
)
148 template <typename T
>
149 T
* maybe_pod_calloc(size_t aNumElems
)
154 template <typename T
>
155 T
* maybe_pod_realloc(T
* aPtr
, size_t aOldSize
, size_t aNewSize
)
157 MOZ_CRASH("NeverAllocPolicy::maybe_pod_realloc");
160 template <typename T
>
161 T
* pod_malloc(size_t aNumElems
)
166 template <typename T
>
167 T
* pod_calloc(size_t aNumElems
)
172 template <typename T
>
173 T
* pod_realloc(T
* aPtr
, size_t aOldSize
, size_t aNewSize
)
175 MOZ_CRASH("NeverAllocPolicy::pod_realloc");
178 void free_(void* aPtr
)
180 MOZ_CRASH("NeverAllocPolicy::free_");
183 void reportAllocOverflow() const
187 MOZ_MUST_USE
bool checkSimulatedOOM() const
193 } // namespace mozilla
195 #endif /* mozilla_AllocPolicy_h */