Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

udp/stream: use MaybeStackBuffers to manage buffer memory management #8626

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/stream_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
14 changes: 2 additions & 12 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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;
Expand Down