Bug 1590643 - Be more consistent about arena_id_t arguments in MallocProvider.h. r=jwalden

Add separate pod_arena_* overloads and remove the optional arena_id_t arguments.

This makes both the naming and argument order more consistent with similar code
elsewhere (for example js_pod_arena_malloc, AllocPolicyBase::pod_arena_malloc).

Differential Revision: https://phabricator.services.mozilla.com/D50785

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jan de Mooij 2019-10-30 15:51:04 +00:00
parent 3c264295d6
commit d326a20314
11 changed files with 90 additions and 51 deletions

View File

@ -3639,8 +3639,8 @@ bool js::str_fromCodePoint(JSContext* cx, unsigned argc, Value* vp) {
static_assert(
ARGS_LENGTH_MAX < std::numeric_limits<decltype(args.length())>::max() / 2,
"|args.length() * 2| does not overflow");
auto elements =
cx->make_pod_array<char16_t>(args.length() * 2, js::StringBufferArena);
auto elements = cx->make_pod_arena_array<char16_t>(js::StringBufferArena,
args.length() * 2);
if (!elements) {
return false;
}

View File

@ -575,7 +575,7 @@ void* js::Nursery::allocateZeroedBuffer(
}
}
void* buffer = zone->pod_calloc<uint8_t>(nbytes, arena);
void* buffer = zone->pod_arena_calloc<uint8_t>(arena, nbytes);
if (buffer && !registerMallocedBuffer(buffer)) {
js_free(buffer);
return nullptr;
@ -589,7 +589,7 @@ void* js::Nursery::allocateZeroedBuffer(
MOZ_ASSERT(nbytes > 0);
if (!IsInsideNursery(obj)) {
return obj->zone()->pod_calloc<uint8_t>(nbytes, arena);
return obj->zone()->pod_arena_calloc<uint8_t>(arena, nbytes);
}
return allocateZeroedBuffer(obj->zone(), nbytes, arena);
}

View File

@ -1185,15 +1185,15 @@ JS_PUBLIC_API void* JS_string_malloc(JSContext* cx, size_t nbytes) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
return static_cast<void*>(
cx->maybe_pod_malloc<uint8_t>(nbytes, js::StringBufferArena));
cx->maybe_pod_arena_malloc<uint8_t>(js::StringBufferArena, nbytes));
}
JS_PUBLIC_API void* JS_string_realloc(JSContext* cx, void* p, size_t oldBytes,
size_t newBytes) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
return static_cast<void*>(cx->maybe_pod_realloc<uint8_t>(
static_cast<uint8_t*>(p), oldBytes, newBytes, js::StringBufferArena));
return static_cast<void*>(cx->maybe_pod_arena_realloc<uint8_t>(
js::StringBufferArena, static_cast<uint8_t*>(p), oldBytes, newBytes));
}
JS_PUBLIC_API void JS_string_free(JSContext* cx, void* p) { return js_free(p); }

View File

@ -64,7 +64,7 @@ UniqueChars js::DuplicateStringToArena(arena_id_t destArenaId, JSContext* cx,
UniqueChars js::DuplicateStringToArena(arena_id_t destArenaId, JSContext* cx,
const char* s, size_t n) {
auto ret = cx->make_pod_array<char>(n + 1, destArenaId);
auto ret = cx->make_pod_arena_array<char>(destArenaId, n + 1);
if (!ret) {
return nullptr;
}
@ -82,7 +82,7 @@ UniqueTwoByteChars js::DuplicateStringToArena(arena_id_t destArenaId,
UniqueTwoByteChars js::DuplicateStringToArena(arena_id_t destArenaId,
JSContext* cx, const char16_t* s,
size_t n) {
auto ret = cx->make_pod_array<char16_t>(n + 1, destArenaId);
auto ret = cx->make_pod_arena_array<char16_t>(destArenaId, n + 1);
if (!ret) {
return nullptr;
}

View File

@ -445,7 +445,8 @@ bool ArrayBufferObject::class_constructor(JSContext* cx, unsigned argc,
}
static uint8_t* AllocateArrayBufferContents(JSContext* cx, uint32_t nbytes) {
auto* p = cx->pod_callocCanGC<uint8_t>(nbytes, js::ArrayBufferContentsArena);
auto* p =
cx->pod_arena_callocCanGC<uint8_t>(js::ArrayBufferContentsArena, nbytes);
if (!p) {
ReportOutOfMemory(cx);
}

View File

@ -408,8 +408,8 @@ static CharsT InflateUTF8StringHelper(JSContext* cx, const InputCharsT src,
}
*outlen = len;
CharT* dst =
cx->template pod_malloc<CharT>(*outlen + 1, destArenaId); // +1 for NUL
CharT* dst = cx->pod_arena_malloc<CharT>(destArenaId,
*outlen + 1); // +1 for NUL
if (!dst) {
ReportOutOfMemory(cx);

View File

@ -74,7 +74,8 @@ class MOZ_NON_PARAM InlineCharBuffer {
}
MOZ_ASSERT(!heapStorage, "heap storage already allocated");
heapStorage = cx->make_pod_array<CharT>(length, js::StringBufferArena);
heapStorage =
cx->make_pod_arena_array<CharT>(js::StringBufferArena, length);
return !!heapStorage;
}
@ -86,7 +87,8 @@ class MOZ_NON_PARAM InlineCharBuffer {
}
if (!heapStorage) {
heapStorage = cx->make_pod_array<CharT>(newLength, js::StringBufferArena);
heapStorage =
cx->make_pod_arena_array<CharT>(js::StringBufferArena, newLength);
if (!heapStorage) {
return false;
}
@ -97,8 +99,8 @@ class MOZ_NON_PARAM InlineCharBuffer {
}
CharT* oldChars = heapStorage.release();
CharT* newChars =
cx->pod_realloc(oldChars, oldLength, newLength, js::StringBufferArena);
CharT* newChars = cx->pod_arena_realloc(js::StringBufferArena, oldChars,
oldLength, newLength);
if (!newChars) {
js_free(oldChars);
return false;

View File

@ -251,8 +251,8 @@ struct JSContext : public JS::RootingContext,
* on OOM and retry the allocation.
*/
template <typename T>
T* pod_callocCanGC(size_t numElems, arena_id_t arena = js::MallocArena) {
T* p = maybe_pod_calloc<T>(numElems, arena);
T* pod_arena_callocCanGC(arena_id_t arena, size_t numElems) {
T* p = maybe_pod_arena_calloc<T>(arena, numElems);
if (MOZ_LIKELY(!!p)) {
return p;
}

View File

@ -49,7 +49,7 @@ namespace js {
template <class Client>
struct MallocProvider {
template <class T>
T* maybe_pod_malloc(size_t numElems, arena_id_t arena = js::MallocArena) {
T* maybe_pod_arena_malloc(arena_id_t arena, size_t numElems) {
T* p = js_pod_arena_malloc<T>(arena, numElems);
if (MOZ_LIKELY(p)) {
client()->updateMallocCounter(numElems * sizeof(T));
@ -58,7 +58,7 @@ struct MallocProvider {
}
template <class T>
T* maybe_pod_calloc(size_t numElems, arena_id_t arena = js::MallocArena) {
T* maybe_pod_arena_calloc(arena_id_t arena, size_t numElems) {
T* p = js_pod_arena_calloc<T>(arena, numElems);
if (MOZ_LIKELY(p)) {
client()->updateMallocCounter(numElems * sizeof(T));
@ -67,8 +67,8 @@ struct MallocProvider {
}
template <class T>
T* maybe_pod_realloc(T* prior, size_t oldSize, size_t newSize,
arena_id_t arena = js::MallocArena) {
T* maybe_pod_arena_realloc(arena_id_t arena, T* prior, size_t oldSize,
size_t newSize) {
T* p = js_pod_arena_realloc<T>(arena, prior, oldSize, newSize);
if (MOZ_LIKELY(p)) {
// For compatibility we do not account for realloc that decreases
@ -80,14 +80,29 @@ struct MallocProvider {
return p;
}
template <class T>
T* maybe_pod_malloc(size_t numElems) {
return maybe_pod_arena_malloc<T>(js::MallocArena, numElems);
}
template <class T>
T* maybe_pod_calloc(size_t numElems) {
return maybe_pod_arena_calloc<T>(js::MallocArena, numElems);
}
template <class T>
T* maybe_pod_realloc(T* prior, size_t oldSize, size_t newSize) {
return maybe_pod_arena_realloc<T>(js::MallocArena, prior, oldSize, newSize);
}
template <class T>
T* pod_malloc() {
return pod_malloc<T>(1);
}
template <class T>
T* pod_malloc(size_t numElems, arena_id_t arena = js::MallocArena) {
T* p = maybe_pod_malloc<T>(numElems, arena);
T* pod_arena_malloc(arena_id_t arena, size_t numElems) {
T* p = maybe_pod_arena_malloc<T>(arena, numElems);
if (MOZ_LIKELY(p)) {
return p;
}
@ -103,20 +118,25 @@ struct MallocProvider {
return p;
}
template <class T>
T* pod_malloc(size_t numElems) {
return pod_arena_malloc<T>(js::MallocArena, numElems);
}
template <class T, class U>
T* pod_malloc_with_extra(size_t numExtra,
arena_id_t arena = js::MallocArena) {
T* pod_malloc_with_extra(size_t numExtra) {
size_t bytes;
if (MOZ_UNLIKELY((!CalculateAllocSizeWithExtra<T, U>(numExtra, &bytes)))) {
client()->reportAllocationOverflow();
return nullptr;
}
T* p = static_cast<T*>(js_arena_malloc(arena, bytes));
T* p = static_cast<T*>(js_malloc(bytes));
if (MOZ_LIKELY(p)) {
client()->updateMallocCounter(bytes);
return p;
}
p = (T*)client()->onOutOfMemory(AllocFunction::Malloc, arena, bytes);
p = (T*)client()->onOutOfMemory(AllocFunction::Malloc, js::MallocArena,
bytes);
if (p) {
client()->updateMallocCounter(bytes);
}
@ -124,14 +144,19 @@ struct MallocProvider {
}
template <class T>
UniquePtr<T[], JS::FreePolicy> make_pod_array(
size_t numElems, arena_id_t arena = js::MallocArena) {
return UniquePtr<T[], JS::FreePolicy>(pod_malloc<T>(numElems, arena));
UniquePtr<T[], JS::FreePolicy> make_pod_arena_array(arena_id_t arena,
size_t numElems) {
return UniquePtr<T[], JS::FreePolicy>(pod_arena_malloc<T>(arena, numElems));
}
template <class T>
T* pod_calloc(size_t numElems = 1, arena_id_t arena = js::MallocArena) {
T* p = maybe_pod_calloc<T>(numElems, arena);
UniquePtr<T[], JS::FreePolicy> make_pod_array(size_t numElems) {
return make_pod_arena_array<T>(js::MallocArena, numElems);
}
template <class T>
T* pod_arena_calloc(arena_id_t arena, size_t numElems = 1) {
T* p = maybe_pod_arena_calloc<T>(arena, numElems);
if (MOZ_LIKELY(p)) {
return p;
}
@ -147,20 +172,25 @@ struct MallocProvider {
return p;
}
template <class T>
T* pod_calloc(size_t numElems = 1) {
return pod_arena_calloc<T>(js::MallocArena, numElems);
}
template <class T, class U>
T* pod_calloc_with_extra(size_t numExtra,
arena_id_t arena = js::MallocArena) {
T* pod_calloc_with_extra(size_t numExtra) {
size_t bytes;
if (MOZ_UNLIKELY((!CalculateAllocSizeWithExtra<T, U>(numExtra, &bytes)))) {
client()->reportAllocationOverflow();
return nullptr;
}
T* p = static_cast<T*>(js_arena_calloc(arena, bytes));
T* p = static_cast<T*>(js_calloc(bytes));
if (p) {
client()->updateMallocCounter(bytes);
return p;
}
p = (T*)client()->onOutOfMemory(AllocFunction::Calloc, arena, bytes);
p = (T*)client()->onOutOfMemory(AllocFunction::Calloc, js::MallocArena,
bytes);
if (p) {
client()->updateMallocCounter(bytes);
}
@ -168,15 +198,14 @@ struct MallocProvider {
}
template <class T>
UniquePtr<T[], JS::FreePolicy> make_zeroed_pod_array(
size_t numElems, arena_id_t arena = js::MallocArena) {
return UniquePtr<T[], JS::FreePolicy>(pod_calloc<T>(numElems, arena));
UniquePtr<T[], JS::FreePolicy> make_zeroed_pod_array(size_t numElems) {
return UniquePtr<T[], JS::FreePolicy>(pod_calloc<T>(numElems));
}
template <class T>
T* pod_realloc(T* prior, size_t oldSize, size_t newSize,
arena_id_t arena = js::MallocArena) {
T* p = maybe_pod_realloc(prior, oldSize, newSize, arena);
T* pod_arena_realloc(arena_id_t arena, T* prior, size_t oldSize,
size_t newSize) {
T* p = maybe_pod_arena_realloc(arena, prior, oldSize, newSize);
if (MOZ_LIKELY(p)) {
return p;
}
@ -193,6 +222,11 @@ struct MallocProvider {
return p;
}
template <class T>
T* pod_realloc(T* prior, size_t oldSize, size_t newSize) {
return pod_arena_realloc<T>(js::MallocArena, prior, oldSize, newSize);
}
JS_DECLARE_NEW_METHODS(new_, pod_malloc<uint8_t>, MOZ_ALWAYS_INLINE)
JS_DECLARE_NEW_ARENA_METHODS(
arena_new_,

View File

@ -406,7 +406,8 @@ static MOZ_ALWAYS_INLINE bool AllocChars(JSString* str, size_t length,
length > DOUBLING_MAX ? length + (length / 8) : RoundUpPow2(length);
JS_STATIC_ASSERT(JSString::MAX_LENGTH * sizeof(CharT) <= UINT32_MAX);
*chars = str->zone()->pod_malloc<CharT>(*capacity, js::StringBufferArena);
*chars =
str->zone()->pod_arena_malloc<CharT>(js::StringBufferArena, *capacity);
return *chars != nullptr;
}
@ -431,7 +432,7 @@ UniquePtr<CharT[], JS::FreePolicy> JSRope::copyCharsInternal(
UniquePtr<CharT[], JS::FreePolicy> out;
if (maybecx) {
out.reset(maybecx->pod_malloc<CharT>(n, destArenaId));
out.reset(maybecx->pod_arena_malloc<CharT>(destArenaId, n));
} else {
out.reset(js_pod_arena_malloc<CharT>(destArenaId, n));
}
@ -1575,7 +1576,7 @@ static JSLinearString* NewStringDeflated(JSContext* cx, const char16_t* s,
cx, mozilla::Range<const char16_t>(s, n));
}
auto news = cx->make_pod_array<Latin1Char>(n, js::StringBufferArena);
auto news = cx->make_pod_arena_array<Latin1Char>(js::StringBufferArena, n);
if (!news) {
if (!allowGC) {
cx->recoverFromOutOfMemory();
@ -1610,7 +1611,8 @@ static JSLinearString* NewStringDeflatedFromLittleEndianNoGC(
return str;
}
auto news = cx->make_pod_array<Latin1Char>(length, js::StringBufferArena);
auto news =
cx->make_pod_arena_array<Latin1Char>(js::StringBufferArena, length);
if (!news) {
cx->recoverFromOutOfMemory();
return nullptr;
@ -1734,7 +1736,7 @@ JSLinearString* NewStringCopyNDontDeflate(JSContext* cx, const CharT* s,
return NewInlineString<allowGC>(cx, mozilla::Range<const CharT>(s, n));
}
auto news = cx->make_pod_array<CharT>(n, js::StringBufferArena);
auto news = cx->make_pod_arena_array<CharT>(js::StringBufferArena, n);
if (!news) {
if (!allowGC) {
cx->recoverFromOutOfMemory();
@ -1776,7 +1778,7 @@ static JSLinearString* NewUndeflatedStringFromLittleEndianNoGC(
return str;
}
auto news = cx->make_pod_array<char16_t>(length, js::StringBufferArena);
auto news = cx->make_pod_arena_array<char16_t>(js::StringBufferArena, length);
if (!news) {
cx->recoverFromOutOfMemory();
return nullptr;

View File

@ -240,8 +240,8 @@ size_t TypedArrayObject::objectMoved(JSObject* obj, JSObject* old) {
AutoEnterOOMUnsafeRegion oomUnsafe;
nbytes = JS_ROUNDUP(nbytes, sizeof(Value));
void* data = newObj->zone()->pod_malloc<uint8_t>(
nbytes, js::ArrayBufferContentsArena);
void* data = newObj->zone()->pod_arena_malloc<uint8_t>(
js::ArrayBufferContentsArena, nbytes);
if (!data) {
oomUnsafe.crash(
"Failed to allocate typed array elements while tenuring.");