From 33351a112d5c0546e97050c3b84ce2e65cd7d5eb Mon Sep 17 00:00:00 2001 From: Cody Hazelwood Date: Fri, 12 Oct 2018 10:40:49 -0700 Subject: [PATCH] src: use MallocedBuffer abstraction for buffers Drop `Free` and `std::unique_ptr` in favor of Node's `MallocedBuffer` for `char[]` buffer memory mangement. PR-URL: https://github.com/nodejs/node/pull/23543 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Gireesh Punathil Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Franziska Hinkelmann --- src/stream_base.cc | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/stream_base.cc b/src/stream_base.cc index f44e188b5b818c..c6cce9c2d09ba9 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -8,6 +8,7 @@ #include "env-inl.h" #include "js_stream.h" #include "string_bytes.h" +#include "util.h" #include "util-inl.h" #include "v8.h" @@ -37,11 +38,6 @@ template int StreamBase::WriteString( const FunctionCallbackInfo& args); -struct Free { - void operator()(char* ptr) const { free(ptr); } -}; - - int StreamBase::ReadStartJS(const FunctionCallbackInfo& args) { return ReadStart(); } @@ -127,9 +123,9 @@ int StreamBase::Writev(const FunctionCallbackInfo& args) { } } - std::unique_ptr storage; + MallocedBuffer storage; if (storage_size > 0) - storage = std::unique_ptr(Malloc(storage_size)); + storage = MallocedBuffer(storage_size); offset = 0; if (!all_buffers) { @@ -145,7 +141,7 @@ int StreamBase::Writev(const FunctionCallbackInfo& args) { // Write string CHECK_LE(offset, storage_size); - char* str_storage = storage.get() + offset; + char* str_storage = storage.data + offset; size_t str_size = storage_size - offset; Local string = chunk->ToString(env->context()).ToLocalChecked(); @@ -164,7 +160,7 @@ int StreamBase::Writev(const FunctionCallbackInfo& args) { StreamWriteResult res = Write(*bufs, count, nullptr, req_wrap_obj); SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res); - if (res.wrap != nullptr && storage) { + if (res.wrap != nullptr && storage_size > 0) { res.wrap->SetAllocatedStorage(storage.release(), storage_size); } return res.err; @@ -263,18 +259,18 @@ int StreamBase::WriteString(const FunctionCallbackInfo& args) { CHECK_EQ(count, 1); } - std::unique_ptr data; + MallocedBuffer data; if (try_write) { // Copy partial data - data = std::unique_ptr(Malloc(buf.len)); - memcpy(data.get(), buf.base, buf.len); + data = MallocedBuffer(buf.len); + memcpy(data.data, buf.base, buf.len); data_size = buf.len; } else { // Write it - data = std::unique_ptr(Malloc(storage_size)); + data = MallocedBuffer(storage_size); data_size = StringBytes::Write(env->isolate(), - data.get(), + data.data, storage_size, string, enc); @@ -282,7 +278,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo& args) { CHECK_LE(data_size, storage_size); - buf = uv_buf_init(data.get(), data_size); + buf = uv_buf_init(data.data, data_size); uv_stream_t* send_handle = nullptr;