Skip to content

Commit

Permalink
src: replace NoArrayBufferZeroFillScope with v8 option
Browse files Browse the repository at this point in the history
NoArrayBufferZeroFillScope was added before the v8 option to
create uninitialized backing stores was added. We can start
moving away from it.
  • Loading branch information
jasnell committed Jan 19, 2025
1 parent 009d53e commit 78e5b98
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace encoding_binding {

using v8::ArrayBuffer;
using v8::BackingStore;
using v8::BackingStoreInitializationMode;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::Isolate;
Expand Down Expand Up @@ -124,9 +125,8 @@ void BindingData::EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {

Local<ArrayBuffer> ab;
{
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
std::unique_ptr<BackingStore> bs =
ArrayBuffer::NewBackingStore(isolate, length);
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
isolate, length, BackingStoreInitializationMode::kUninitialized);

CHECK(bs);

Expand Down
14 changes: 9 additions & 5 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ namespace node {

using errors::TryCatchScope;
using v8::Array;
using v8::ArrayBuffer;
using v8::BackingStore;
using v8::BackingStoreInitializationMode;
using v8::Boolean;
using v8::Context;
using v8::CppHeap;
Expand Down Expand Up @@ -742,17 +745,18 @@ void Environment::add_refs(int64_t diff) {
}

uv_buf_t Environment::allocate_managed_buffer(const size_t suggested_size) {
NoArrayBufferZeroFillScope no_zero_fill_scope(isolate_data());
std::unique_ptr<v8::BackingStore> bs =
v8::ArrayBuffer::NewBackingStore(isolate(), suggested_size);
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
isolate(),
suggested_size,
BackingStoreInitializationMode::kUninitialized);
uv_buf_t buf = uv_buf_init(static_cast<char*>(bs->Data()), bs->ByteLength());
released_allocated_buffers_.emplace(buf.base, std::move(bs));
return buf;
}

std::unique_ptr<v8::BackingStore> Environment::release_managed_buffer(
std::unique_ptr<BackingStore> Environment::release_managed_buffer(
const uv_buf_t& buf) {
std::unique_ptr<v8::BackingStore> bs;
std::unique_ptr<BackingStore> bs;
if (buf.base != nullptr) {
auto it = released_allocated_buffers_.find(buf.base);
CHECK_NE(it, released_allocated_buffers_.end());
Expand Down
20 changes: 8 additions & 12 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace Buffer {
using v8::ArrayBuffer;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::BackingStoreInitializationMode;
using v8::Context;
using v8::EscapableHandleScope;
using v8::FastApiTypedArray;
Expand Down Expand Up @@ -372,9 +373,8 @@ MaybeLocal<Object> New(Environment* env, size_t length) {

Local<ArrayBuffer> ab;
{
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
std::unique_ptr<BackingStore> bs =
ArrayBuffer::NewBackingStore(isolate, length);
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
isolate, length, BackingStoreInitializationMode::kUninitialized);

CHECK(bs);

Expand Down Expand Up @@ -413,18 +413,14 @@ MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) {
return Local<Object>();
}

Local<ArrayBuffer> ab;
{
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
std::unique_ptr<BackingStore> bs =
ArrayBuffer::NewBackingStore(isolate, length);
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
isolate, length, BackingStoreInitializationMode::kUninitialized);

CHECK(bs);
CHECK(bs);

memcpy(bs->Data(), data, length);
memcpy(bs->Data(), data, length);

ab = ArrayBuffer::New(isolate, std::move(bs));
}
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, std::move(bs));

MaybeLocal<Object> obj =
New(env, ab, 0, ab->ByteLength())
Expand Down
32 changes: 14 additions & 18 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ using v8::Array;
using v8::ArrayBuffer;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::BackingStoreInitializationMode;
using v8::Boolean;
using v8::Context;
using v8::EscapableHandleScope;
Expand Down Expand Up @@ -292,11 +293,10 @@ Local<Value> Http2Settings::Pack(
size_t count,
const nghttp2_settings_entry* entries) {
EscapableHandleScope scope(env->isolate());
std::unique_ptr<BackingStore> bs;
{
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs = ArrayBuffer::NewBackingStore(env->isolate(), count * 6);
}
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
env->isolate(),
count * 6,
BackingStoreInitializationMode::kUninitialized);
if (nghttp2_pack_settings_payload(static_cast<uint8_t*>(bs->Data()),
bs->ByteLength(),
entries,
Expand Down Expand Up @@ -457,13 +457,11 @@ Origins::Origins(
return;
}

{
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs_ = ArrayBuffer::NewBackingStore(env->isolate(),
alignof(nghttp2_origin_entry) - 1 +
count_ * sizeof(nghttp2_origin_entry) +
origin_string_len);
}
bs_ = ArrayBuffer::NewBackingStore(
env->isolate(),
alignof(nghttp2_origin_entry) - 1 +
count_ * sizeof(nghttp2_origin_entry) + origin_string_len,
BackingStoreInitializationMode::kUninitialized);

// Make sure the start address is aligned appropriately for an nghttp2_nv*.
char* start = nbytes::AlignUp(static_cast<char*>(bs_->Data()),
Expand Down Expand Up @@ -2064,12 +2062,10 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
// happen, we concatenate the data we received with the already-stored
// pending input data, slicing off the already processed part.
size_t pending_len = stream_buf_.len - stream_buf_offset_;
std::unique_ptr<BackingStore> new_bs;
{
NoArrayBufferZeroFillScope no_zero_fill_scope(env()->isolate_data());
new_bs = ArrayBuffer::NewBackingStore(env()->isolate(),
pending_len + nread);
}
std::unique_ptr<BackingStore> new_bs = ArrayBuffer::NewBackingStore(
env()->isolate(),
pending_len + nread,
BackingStoreInitializationMode::kUninitialized);
memcpy(static_cast<char*>(new_bs->Data()),
stream_buf_.base + stream_buf_offset_,
pending_len);
Expand Down
13 changes: 7 additions & 6 deletions src/stream_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace node {
using v8::Array;
using v8::ArrayBuffer;
using v8::BackingStore;
using v8::BackingStoreInitializationMode;
using v8::ConstructorBehavior;
using v8::Context;
using v8::DontDelete;
Expand Down Expand Up @@ -243,8 +244,8 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {

std::unique_ptr<BackingStore> bs;
if (storage_size > 0) {
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs = ArrayBuffer::NewBackingStore(isolate, storage_size);
bs = ArrayBuffer::NewBackingStore(
isolate, storage_size, BackingStoreInitializationMode::kUninitialized);
}

offset = 0;
Expand Down Expand Up @@ -398,14 +399,14 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {

if (try_write) {
// Copy partial data
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs = ArrayBuffer::NewBackingStore(isolate, buf.len);
bs = ArrayBuffer::NewBackingStore(
isolate, buf.len, BackingStoreInitializationMode::kUninitialized);
memcpy(static_cast<char*>(bs->Data()), buf.base, buf.len);
data_size = buf.len;
} else {
// Write it
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs = ArrayBuffer::NewBackingStore(isolate, storage_size);
bs = ArrayBuffer::NewBackingStore(
isolate, storage_size, BackingStoreInitializationMode::kUninitialized);
data_size = StringBytes::Write(isolate,
static_cast<char*>(bs->Data()),
storage_size,
Expand Down

0 comments on commit 78e5b98

Please sign in to comment.