diff options
author | Cody Hazelwood <codyhazelwood@users.noreply.github.com> | 2018-10-12 20:40:49 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-10-15 13:03:08 +0300 |
commit | 810c0d99d1103c5b7228f161bb9d773fc4859419 (patch) | |
tree | 0f2bf2bb56e87413d2a28942eaccbe44d6570497 /src/stream_base.cc | |
parent | 37dfdccbb600d0e69dd25eab621a73a49c77a1f8 (diff) |
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 <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Diffstat (limited to 'src/stream_base.cc')
-rw-r--r-- | src/stream_base.cc | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/src/stream_base.cc b/src/stream_base.cc index f44e188b5b8..c6cce9c2d09 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<LATIN1>( const FunctionCallbackInfo<Value>& args); -struct Free { - void operator()(char* ptr) const { free(ptr); } -}; - - int StreamBase::ReadStartJS(const FunctionCallbackInfo<Value>& args) { return ReadStart(); } @@ -127,9 +123,9 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) { } } - std::unique_ptr<char[], Free> storage; + MallocedBuffer<char> storage; if (storage_size > 0) - storage = std::unique_ptr<char[], Free>(Malloc(storage_size)); + storage = MallocedBuffer<char>(storage_size); offset = 0; if (!all_buffers) { @@ -145,7 +141,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& 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> string = chunk->ToString(env->context()).ToLocalChecked(); @@ -164,7 +160,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& 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<Value>& args) { CHECK_EQ(count, 1); } - std::unique_ptr<char[], Free> data; + MallocedBuffer<char> data; if (try_write) { // Copy partial data - data = std::unique_ptr<char[], Free>(Malloc(buf.len)); - memcpy(data.get(), buf.base, buf.len); + data = MallocedBuffer<char>(buf.len); + memcpy(data.data, buf.base, buf.len); data_size = buf.len; } else { // Write it - data = std::unique_ptr<char[], Free>(Malloc(storage_size)); + data = MallocedBuffer<char>(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<Value>& 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; |