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 24, 2021
1 parent 6bfe5a6 commit 1865ace
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
24 changes: 24 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "aliased_buffer.h"
#include "allocated_buffer-inl.h"
#include "callback_queue-inl.h"
#include "env.h"
#include "node.h"
Expand Down Expand Up @@ -966,6 +967,29 @@ inline IsolateData* Environment::isolate_data() const {
return isolate_data_;
}

inline uv_buf_t Environment::allocate_managed_buffer(
const size_t suggested_size) {
NoArrayBufferZeroFillScope no_zero_fill_scope(isolate_data());
std::unique_ptr<v8::BackingStore> bs =
v8::ArrayBuffer::NewBackingStore(isolate(), suggested_size);
uv_buf_t buf = uv_buf_init(static_cast<char*>(bs->Data()), bs->ByteLength());
released_allocated_buffers()->emplace(buf.base, std::move(bs));
return buf;
}

inline std::unique_ptr<v8::BackingStore> Environment::release_managed_buffer(
const uv_buf_t& buf) {
std::unique_ptr<v8::BackingStore> bs;
if (buf.base != nullptr) {
auto map = released_allocated_buffers();
auto it = map->find(buf.base);
CHECK_NE(it, map->end());
bs = std::move(it->second);
map->erase(it);
}
return bs;
}

std::unordered_map<char*, std::unique_ptr<v8::BackingStore>>*
Environment::released_allocated_buffers() {
return &released_allocated_buffers_;
Expand Down
3 changes: 3 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,9 @@ class Environment : public MemoryRetainer {
void RunAndClearNativeImmediates(bool only_refed = false);
void RunAndClearInterrupts();

inline uv_buf_t allocate_managed_buffer(const size_t suggested_size);
inline std::unique_ptr<v8::BackingStore> release_managed_buffer(
const uv_buf_t& buf);
inline std::unordered_map<char*, std::unique_ptr<v8::BackingStore>>*
released_allocated_buffers();

Expand Down
27 changes: 18 additions & 9 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@
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;
Expand Down Expand Up @@ -314,7 +318,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 +683,7 @@ void UDPWrap::OnAlloc(uv_handle_t* handle,
}

uv_buf_t UDPWrap::OnAlloc(size_t suggested_size) {
return AllocatedBuffer::AllocateManaged(env(), suggested_size).release();
return env()->allocate_managed_buffer(suggested_size);
}

void UDPWrap::OnRecv(uv_udp_t* handle,
Expand All @@ -696,27 +700,32 @@ 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 = env->release_managed_buffer(buf_);
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 {
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 1865ace

Please sign in to comment.