Bug 1475228 - Add JSMallocAllocPolicy to let gecko allocate data structures using the JS heap r=jandem

This commit is contained in:
Jon Coppeard 2018-07-17 14:30:23 +01:00
parent 827b83bae3
commit 1cf260e716
2 changed files with 31 additions and 21 deletions

View File

@ -26,8 +26,9 @@ enum class AllocFunction {
Calloc,
Realloc
};
/* Policy for using system memory functions and doing no error reporting. */
class SystemAllocPolicy
/* Base class allocation policies providing allocation methods. */
class AllocPolicyBase
{
public:
template <typename T> T* maybe_pod_malloc(size_t numElems) { return js_pod_malloc<T>(numElems); }
@ -41,6 +42,12 @@ class SystemAllocPolicy
return maybe_pod_realloc<T>(p, oldSize, newSize);
}
template <typename T> void free_(T* p, size_t numElems = 0) { js_free(p); }
};
/* Policy for using system memory functions and doing no error reporting. */
class SystemAllocPolicy : public AllocPolicyBase
{
public:
void reportAllocOverflow() const {}
bool checkSimulatedOOM() const {
return !js::oom::ShouldFailWithOOM();
@ -58,7 +65,7 @@ MOZ_COLD JS_FRIEND_API(void) ReportOutOfMemory(JSContext* cx);
* FIXME bug 647103 - rewrite this in terms of temporary allocation functions,
* not the system ones.
*/
class TempAllocPolicy
class TempAllocPolicy : public AllocPolicyBase
{
JSContext* const cx_;
@ -80,24 +87,9 @@ class TempAllocPolicy
public:
MOZ_IMPLICIT TempAllocPolicy(JSContext* cx) : cx_(cx) {}
template <typename T>
T* maybe_pod_malloc(size_t numElems) {
return js_pod_malloc<T>(numElems);
}
template <typename T>
T* maybe_pod_calloc(size_t numElems) {
return js_pod_calloc<T>(numElems);
}
template <typename T>
T* maybe_pod_realloc(T* prior, size_t oldSize, size_t newSize) {
return js_pod_realloc<T>(prior, oldSize, newSize);
}
template <typename T>
T* pod_malloc(size_t numElems) {
T* p = maybe_pod_malloc<T>(numElems);
T* p = this->maybe_pod_malloc<T>(numElems);
if (MOZ_UNLIKELY(!p))
p = onOutOfMemoryTyped<T>(AllocFunction::Malloc, numElems);
return p;
@ -105,7 +97,7 @@ class TempAllocPolicy
template <typename T>
T* pod_calloc(size_t numElems) {
T* p = maybe_pod_calloc<T>(numElems);
T* p = this->maybe_pod_calloc<T>(numElems);
if (MOZ_UNLIKELY(!p))
p = onOutOfMemoryTyped<T>(AllocFunction::Calloc, numElems);
return p;
@ -113,7 +105,7 @@ class TempAllocPolicy
template <typename T>
T* pod_realloc(T* prior, size_t oldSize, size_t newSize) {
T* p2 = maybe_pod_realloc<T>(prior, oldSize, newSize);
T* p2 = this->maybe_pod_realloc<T>(prior, oldSize, newSize);
if (MOZ_UNLIKELY(!p2))
p2 = onOutOfMemoryTyped<T>(AllocFunction::Realloc, newSize, prior);
return p2;

View File

@ -1329,6 +1329,24 @@ JS_freeop(JSFreeOp* fop, void* p);
extern JS_PUBLIC_API(void)
JS_updateMallocCounter(JSContext* cx, size_t nbytes);
/*
* A replacement for MallocAllocPolicy that allocates in the JS heap and adds no
* extra behaviours.
*
* This is currently used for allocating source buffers for parsing. Since these
* are temporary and will not be freed by GC, the memory is not tracked by the
* usual accounting.
*/
class JS_PUBLIC_API(JSMallocAllocPolicy) : public js::AllocPolicyBase
{
public:
void reportAllocOverflow() const {}
MOZ_MUST_USE bool checkSimulatedOOM() const {
return true;
}
};
/**
* Set the size of the native stack that should not be exceed. To disable
* stack size checking pass 0.