diff options
author | Paul Kiddie <paul@paulkiddie.com> | 2016-09-17 10:45:00 +0100 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2016-09-20 18:59:10 +0200 |
commit | fffb9a386a6abc86664307a4d21a7f70518f0331 (patch) | |
tree | 3682ebf97621b57e21d1f0194a572ed5d9b13b0e /src | |
parent | c1e47ed8c883afba3c240e89d763a28a57ec1373 (diff) | |
download | node-new-fffb9a386a6abc86664307a4d21a7f70518f0331.tar.gz |
src: use MaybeStackBuffer on DoSend/Writev
instead of creating own buffer, use MaybeStackBuffer on DoSend to
take advantage of its destructor to free up memory, so buffer
never leaks memory - even if code in DoSend throws.
Use MaybeStackBuffer in Writev to take advantage of destructor
on MaybeStackBuffer to clear itself up, rather than Writev managing
resources itself.
PR-URL: https://github.com/nodejs/node/pull/8626
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src')
-rw-r--r-- | src/stream_base.cc | 12 | ||||
-rw-r--r-- | src/udp_wrap.cc | 14 |
2 files changed, 4 insertions, 22 deletions
diff --git a/src/stream_base.cc b/src/stream_base.cc index 585b84885c..a12a1efc73 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -102,8 +102,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) { size_t count = chunks->Length() >> 1; - uv_buf_t bufs_[16]; - uv_buf_t* bufs = bufs_; + MaybeStackBuffer<uv_buf_t, 16> bufs(count); // Determine storage size first size_t storage_size = 0; @@ -132,9 +131,6 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) { if (storage_size > INT_MAX) return UV_ENOBUFS; - if (arraysize(bufs_) < count) - bufs = new uv_buf_t[count]; - WriteWrap* req_wrap = WriteWrap::New(env, req_wrap_obj, this, @@ -174,11 +170,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) { bytes += str_size; } - int err = DoWrite(req_wrap, bufs, count, nullptr); - - // Deallocate space - if (bufs != bufs_) - delete[] bufs; + int err = DoWrite(req_wrap, *bufs, count, nullptr); req_wrap->object()->Set(env->async(), True(env->isolate())); req_wrap->object()->Set(env->bytes_string(), diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index f191b3a755..7fba279fca 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -275,13 +275,7 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) { SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback); size_t msg_size = 0; - // allocate uv_buf_t of the correct size - // if bigger than 16 elements - uv_buf_t bufs_[16]; - uv_buf_t* bufs = bufs_; - - if (arraysize(bufs_) < count) - bufs = new uv_buf_t[count]; + MaybeStackBuffer<uv_buf_t, 16> bufs(count); // construct uv_buf_t array for (size_t i = 0; i < count; i++) { @@ -313,16 +307,12 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) { if (err == 0) { err = uv_udp_send(&req_wrap->req_, &wrap->handle_, - bufs, + *bufs, count, reinterpret_cast<const sockaddr*>(&addr), OnSend); } - // Deallocate space - if (bufs != bufs_) - delete[] bufs; - req_wrap->Dispatched(); if (err) delete req_wrap; |