Bug 1591276 - Refactor code to reallocate malloced buffers associated with nursery cells r=sfink

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jon Coppeard 2020-04-15 17:27:14 +00:00
Родитель 761c93b2e7
Коммит ca56cec302
3 изменённых файлов: 15 добавлений и 48 удалений

Просмотреть файл

@ -131,7 +131,7 @@ static inline T* ReallocateObjectBuffer(JSContext* cx, JSObject* obj,
return obj->zone()->pod_realloc<T>(oldBuffer, oldCount, newCount);
}
T* buffer = static_cast<T*>(cx->nursery().reallocateBuffer(
obj, oldBuffer, oldCount * sizeof(T), newCount * sizeof(T)));
obj->zone(), obj, oldBuffer, oldCount * sizeof(T), newCount * sizeof(T)));
if (!buffer) {
ReportOutOfMemory(cx);
}
@ -154,12 +154,12 @@ static inline JS::BigInt::Digit* AllocateBigIntDigits(JSContext* cx,
}
static inline JS::BigInt::Digit* ReallocateBigIntDigits(
JSContext* cx, JS::BigInt* obj, JS::BigInt::Digit* oldDigits,
JSContext* cx, JS::BigInt* bi, JS::BigInt::Digit* oldDigits,
uint32_t oldLength, uint32_t newLength) {
if (cx->isHelperThreadContext()) {
MOZ_ASSERT(!cx->nursery().isInside(oldDigits));
return obj->zone()->pod_realloc<JS::BigInt::Digit>(oldDigits, oldLength,
newLength);
return bi->zone()->pod_realloc<JS::BigInt::Digit>(oldDigits, oldLength,
newLength);
}
size_t oldBytes =
@ -167,8 +167,8 @@ static inline JS::BigInt::Digit* ReallocateBigIntDigits(
size_t newBytes =
RoundUp(newLength * sizeof(JS::BigInt::Digit), sizeof(Value));
auto* buffer = static_cast<JS::BigInt::Digit*>(
cx->nursery().reallocateBuffer(obj, oldDigits, oldBytes, newBytes));
auto* buffer = static_cast<JS::BigInt::Digit*>(cx->nursery().reallocateBuffer(
bi->zone(), bi, oldDigits, oldBytes, newBytes));
if (!buffer) {
ReportOutOfMemory(cx);
}

Просмотреть файл

@ -625,16 +625,15 @@ void* js::Nursery::allocateZeroedBuffer(
return allocateZeroedBuffer(obj->zone(), nbytes, arena);
}
void* js::Nursery::reallocateBuffer(JSObject* obj, void* oldBuffer,
void* js::Nursery::reallocateBuffer(Zone* zone, Cell* cell, void* oldBuffer,
size_t oldBytes, size_t newBytes) {
if (!IsInsideNursery(obj)) {
return obj->zone()->pod_realloc<uint8_t>((uint8_t*)oldBuffer, oldBytes,
newBytes);
if (!IsInsideNursery(cell)) {
return zone->pod_realloc<uint8_t>((uint8_t*)oldBuffer, oldBytes, newBytes);
}
if (!isInside(oldBuffer)) {
void* newBuffer = obj->zone()->pod_realloc<uint8_t>((uint8_t*)oldBuffer,
oldBytes, newBytes);
void* newBuffer =
zone->pod_realloc<uint8_t>((uint8_t*)oldBuffer, oldBytes, newBytes);
if (newBuffer && oldBuffer != newBuffer) {
MOZ_ALWAYS_TRUE(mallocedBuffers.rekeyAs(oldBuffer, newBuffer, newBuffer));
}
@ -646,7 +645,7 @@ void* js::Nursery::reallocateBuffer(JSObject* obj, void* oldBuffer,
return oldBuffer;
}
void* newBuffer = allocateBuffer(obj->zone(), newBytes);
void* newBuffer = allocateBuffer(zone, newBytes);
if (newBuffer) {
PodCopy((uint8_t*)newBuffer, (uint8_t*)oldBuffer, oldBytes);
}
@ -663,34 +662,6 @@ void* js::Nursery::allocateBuffer(JS::BigInt* bi, size_t nbytes) {
return allocateBuffer(bi->zone(), nbytes);
}
void* js::Nursery::reallocateBuffer(JS::BigInt* bi, void* oldDigits,
size_t oldBytes, size_t newBytes) {
if (!IsInsideNursery(bi)) {
return bi->zone()->pod_realloc<uint8_t>((uint8_t*)oldDigits, oldBytes,
newBytes);
}
if (!isInside(oldDigits)) {
void* newDigits = bi->zone()->pod_realloc<uint8_t>((uint8_t*)oldDigits,
oldBytes, newBytes);
if (newDigits && oldDigits != newDigits) {
MOZ_ALWAYS_TRUE(mallocedBuffers.rekeyAs(oldDigits, newDigits, newDigits));
}
return newDigits;
}
// The nursery cannot make use of the returned digits data.
if (newBytes < oldBytes) {
return oldDigits;
}
void* newDigits = allocateBuffer(bi->zone(), newBytes);
if (newDigits) {
PodCopy((uint8_t*)newDigits, (uint8_t*)oldDigits, oldBytes);
}
return newDigits;
}
void js::Nursery::freeBuffer(void* buffer) {
if (!isInside(buffer)) {
removeMallocedBuffer(buffer);

Просмотреть файл

@ -308,18 +308,14 @@ class Nursery {
void* allocateZeroedBuffer(JSObject* obj, size_t nbytes,
arena_id_t arena = js::MallocArena);
// Resize an existing object buffer.
void* reallocateBuffer(JSObject* obj, void* oldBuffer, size_t oldBytes,
size_t newBytes);
// Resize an existing buffer.
void* reallocateBuffer(JS::Zone* zone, gc::Cell* cell, void* oldBuffer,
size_t oldBytes, size_t newBytes);
// Allocate a digits buffer for a given BigInt, using the nursery if possible
// and |bi| is in the nursery.
void* allocateBuffer(JS::BigInt* bi, size_t nbytes);
// Resize an existing BigInt digits buffer.
void* reallocateBuffer(JS::BigInt* bi, void* oldDigits, size_t oldBytes,
size_t newBytes);
// Free an object buffer.
void freeBuffer(void* buffer);