Bug 1052579 - Add new JSAPI calls for allocating string buffers r=sfink

Currently, JSAPI malloc calls can only allocate in MallocArena. Now there
are calls for when the user intends to allocate a buffer that will be
"stolen" by one of the NewString calls.

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Chris Martin 2019-04-12 17:09:26 +00:00
parent 14f11a2e84
commit 7f9d62dd87
3 changed files with 32 additions and 3 deletions

View File

@ -42,6 +42,18 @@ extern JS_PUBLIC_API void* JS_realloc(JSContext* cx, void* p, size_t oldBytes,
*/
extern JS_PUBLIC_API void JS_free(JSContext* cx, void* p);
/**
* Same as above, but for buffers that will be used with the BYOB
* (Bring Your Own Buffer) JSString creation functions, such as
* JS_NewLatin1String and JS_NewUCString
*/
extern JS_PUBLIC_API void* JS_string_malloc(JSContext* cx, size_t nbytes);
extern JS_PUBLIC_API void* JS_string_realloc(JSContext* cx, void* p,
size_t oldBytes, size_t newBytes);
extern JS_PUBLIC_API void JS_string_free(JSContext* cx, void* p);
/**
* A wrapper for |js_free(p)| that may delay |js_free(p)| invocation as a
* performance optimization as specified by the given JSFreeOp instance.

View File

@ -1132,6 +1132,23 @@ JS_PUBLIC_API void* JS_realloc(JSContext* cx, void* p, size_t oldBytes,
JS_PUBLIC_API void JS_free(JSContext* cx, void* p) { return js_free(p); }
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::MallocArena));
}
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::MallocArena));
}
JS_PUBLIC_API void JS_string_free(JSContext* cx, void* p) { return js_free(p); }
JS_PUBLIC_API void JS_freeop(JSFreeOp* fop, void* p) {
return FreeOp::get(fop)->free_(p);
}

View File

@ -265,8 +265,8 @@ bool XPCConvert::NativeData2JS(JSContext* cx, MutableHandleValue d,
using UniqueLatin1Chars =
js::UniquePtr<JS::Latin1Char[], JS::FreePolicy>;
UniqueLatin1Chars buffer(
static_cast<JS::Latin1Char*>(JS_malloc(cx, allocLen.value())));
UniqueLatin1Chars buffer(static_cast<JS::Latin1Char*>(
JS_string_malloc(cx, allocLen.value())));
if (!buffer) {
return false;
}
@ -299,7 +299,7 @@ bool XPCConvert::NativeData2JS(JSContext* cx, MutableHandleValue d,
}
JS::UniqueTwoByteChars buffer(
static_cast<char16_t*>(JS_malloc(cx, allocLen.value())));
static_cast<char16_t*>(JS_string_malloc(cx, allocLen.value())));
if (!buffer) {
return false;
}