Browse Source

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>
v6.x
Paul Kiddie 9 years ago
committed by Jeremiah Senkpiel
parent
commit
52f0f64e79
  1. 12
      src/stream_base.cc
  2. 14
      src/udp_wrap.cc

12
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(),

14
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;

Loading…
Cancel
Save