diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-02-18 22:58:27 +0100 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-02-25 02:01:11 +0100 |
commit | 84e02b178ad14fae0df2a514e8a39bfa50ffdc2d (patch) | |
tree | ddc0435b6bd0b7811e0bf47687777c56b2857fd0 /src/stream_base.cc | |
parent | 6c257cdf271384555d0ced77104a1d6b0480e246 (diff) | |
download | android-node-v8-84e02b178ad14fae0df2a514e8a39bfa50ffdc2d.tar.gz android-node-v8-84e02b178ad14fae0df2a514e8a39bfa50ffdc2d.tar.bz2 android-node-v8-84e02b178ad14fae0df2a514e8a39bfa50ffdc2d.zip |
src: allocate Buffer memory using ArrayBuffer allocator
Always use the right allocator for memory that is turned into
an `ArrayBuffer` at a later point.
This enables embedders to use their own `ArrayBuffer::Allocator`s,
and is inspired by Electron’s electron/node@f61bae3440e. It should
render their downstream patch unnecessary.
Refs: https://github.com/electron/node/commit/f61bae3440e1bfcc83bba6ff0785adfb89b4045e
PR-URL: https://github.com/nodejs/node/pull/26207
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'src/stream_base.cc')
-rw-r--r-- | src/stream_base.cc | 47 |
1 files changed, 21 insertions, 26 deletions
diff --git a/src/stream_base.cc b/src/stream_base.cc index a55cb60cfc..ebd9beb984 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -111,9 +111,9 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) { } } - MallocedBuffer<char> storage; + AllocatedBuffer storage; if (storage_size > 0) - storage = MallocedBuffer<char>(storage_size); + storage = env->AllocateManaged(storage_size); offset = 0; if (!all_buffers) { @@ -129,8 +129,8 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) { // Write string CHECK_LE(offset, storage_size); - char* str_storage = storage.data + offset; - size_t str_size = storage_size - offset; + char* str_storage = storage.data() + offset; + size_t str_size = storage.size() - offset; Local<String> string = chunk->ToString(env->context()).ToLocalChecked(); enum encoding encoding = ParseEncoding(env->isolate(), @@ -149,7 +149,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) { StreamWriteResult res = Write(*bufs, count, nullptr, req_wrap_obj); SetWriteResult(res); if (res.wrap != nullptr && storage_size > 0) { - res.wrap->SetAllocatedStorage(storage.release(), storage_size); + res.wrap->SetAllocatedStorage(std::move(storage)); } return res.err; } @@ -239,18 +239,18 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) { CHECK_EQ(count, 1); } - MallocedBuffer<char> data; + AllocatedBuffer data; if (try_write) { // Copy partial data - data = MallocedBuffer<char>(buf.len); - memcpy(data.data, buf.base, buf.len); + data = env->AllocateManaged(buf.len); + memcpy(data.data(), buf.base, buf.len); data_size = buf.len; } else { // Write it - data = MallocedBuffer<char>(storage_size); + data = env->AllocateManaged(storage_size); data_size = StringBytes::Write(env->isolate(), - data.data, + data.data(), storage_size, string, enc); @@ -258,7 +258,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) { CHECK_LE(data_size, storage_size); - buf = uv_buf_init(data.data, data_size); + buf = uv_buf_init(data.data(), data_size); uv_stream_t* send_handle = nullptr; @@ -278,7 +278,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) { SetWriteResult(res); if (res.wrap != nullptr) { - res.wrap->SetAllocatedStorage(data.release(), data_size); + res.wrap->SetAllocatedStorage(std::move(data)); } return res.err; @@ -343,35 +343,30 @@ void StreamResource::ClearError() { // No-op } - -uv_buf_t StreamListener::OnStreamAlloc(size_t suggested_size) { - return uv_buf_init(Malloc(suggested_size), suggested_size); +uv_buf_t EmitToJSStreamListener::OnStreamAlloc(size_t suggested_size) { + CHECK_NOT_NULL(stream_); + Environment* env = static_cast<StreamBase*>(stream_)->stream_env(); + return env->AllocateManaged(suggested_size).release(); } - -void EmitToJSStreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) { +void EmitToJSStreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) { CHECK_NOT_NULL(stream_); StreamBase* stream = static_cast<StreamBase*>(stream_); Environment* env = stream->stream_env(); HandleScope handle_scope(env->isolate()); Context::Scope context_scope(env->context()); + AllocatedBuffer buf(env, buf_); if (nread <= 0) { - free(buf.base); if (nread < 0) stream->CallJSOnreadMethod(nread, Local<ArrayBuffer>()); return; } - CHECK_LE(static_cast<size_t>(nread), buf.len); - char* base = Realloc(buf.base, nread); + CHECK_LE(static_cast<size_t>(nread), buf.size()); + buf.Resize(nread); - Local<ArrayBuffer> obj = ArrayBuffer::New( - env->isolate(), - base, - nread, - v8::ArrayBufferCreationMode::kInternalized); // Transfer ownership to V8. - stream->CallJSOnreadMethod(nread, obj); + stream->CallJSOnreadMethod(nread, buf.ToArrayBuffer()); } |