Skip to content

Commit

Permalink
src: remove usage of AllocatedBuffer from udp_wrap.cc
Browse files Browse the repository at this point in the history
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
  • Loading branch information
RaisinTen committed Sep 19, 2021
1 parent 6bfe5a6 commit 3b8daf8
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,24 @@
namespace node {

using v8::Array;
using v8::ArrayBuffer;
using v8::BackingStore;
using v8::Boolean;
using v8::Context;
using v8::DontDelete;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::PropertyAttribute;
using v8::ReadOnly;
using v8::Signature;
using v8::Uint32;
using v8::Uint8Array;
using v8::Undefined;
using v8::Value;

Expand Down Expand Up @@ -314,7 +319,7 @@ void UDPWrap::BufferSize(const FunctionCallbackInfo<Value>& args) {

CHECK(args[0]->IsUint32());
CHECK(args[1]->IsBoolean());
bool is_recv = args[1].As<v8::Boolean>()->Value();
bool is_recv = args[1].As<Boolean>()->Value();
const char* uv_func_name = is_recv ? "uv_recv_buffer_size" :
"uv_send_buffer_size";

Expand Down Expand Up @@ -679,7 +684,16 @@ void UDPWrap::OnAlloc(uv_handle_t* handle,
}

uv_buf_t UDPWrap::OnAlloc(size_t suggested_size) {
return AllocatedBuffer::AllocateManaged(env(), suggested_size).release();
Environment* env = this->env();
uv_buf_t buf;
{
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
std::unique_ptr<BackingStore> bs =
ArrayBuffer::NewBackingStore(env->isolate(), suggested_size);
buf = uv_buf_init(static_cast<char*>(bs->Data()), bs->ByteLength());
env->released_allocated_buffers()->emplace(buf.base, std::move(bs));
}
return buf;
}

void UDPWrap::OnRecv(uv_udp_t* handle,
Expand All @@ -696,27 +710,42 @@ void UDPWrap::OnRecv(ssize_t nread,
const sockaddr* addr,
unsigned int flags) {
Environment* env = this->env();
AllocatedBuffer buf(env, buf_);
Isolate* isolate = env->isolate();

std::unique_ptr<BackingStore> bs;
if (buf_.base != nullptr) {
auto map = env->released_allocated_buffers();
auto it = map->find(buf_.base);
CHECK_NE(it, map->end());
bs = std::move(it->second);
map->erase(it);
}

if (nread == 0 && addr == nullptr) {
return;
}

HandleScope handle_scope(env->isolate());
HandleScope handle_scope(isolate);
Context::Scope context_scope(env->context());

Local<Value> argv[] = {
Integer::New(env->isolate(), static_cast<int32_t>(nread)),
Integer::New(isolate, static_cast<int32_t>(nread)),
object(),
Undefined(env->isolate()),
Undefined(env->isolate())};
Undefined(isolate),
Undefined(isolate)};

if (nread < 0) {
MakeCallback(env->onmessage_string(), arraysize(argv), argv);
return;
} else if (nread == 0) {
bs = ArrayBuffer::NewBackingStore(isolate, 0);
} else {
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
bs = BackingStore::Reallocate(isolate, std::move(bs), nread);
}

buf.Resize(nread);
argv[2] = buf.ToBuffer().ToLocalChecked();
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, std::move(bs));
argv[2] = Buffer::New(env, ab, 0, ab->ByteLength()).ToLocalChecked();
argv[3] = AddressToJS(env, addr);
MakeCallback(env->onmessage_string(), arraysize(argv), argv);
}
Expand Down

0 comments on commit 3b8daf8

Please sign in to comment.