Skip to content

Commit 1865ace

Browse files
committed
src: remove usage of AllocatedBuffer from udp_wrap.cc
Signed-off-by: Darshan Sen <[email protected]>
1 parent 6bfe5a6 commit 1865ace

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

src/env-inl.h

+24
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
2626

2727
#include "aliased_buffer.h"
28+
#include "allocated_buffer-inl.h"
2829
#include "callback_queue-inl.h"
2930
#include "env.h"
3031
#include "node.h"
@@ -966,6 +967,29 @@ inline IsolateData* Environment::isolate_data() const {
966967
return isolate_data_;
967968
}
968969

970+
inline uv_buf_t Environment::allocate_managed_buffer(
971+
const size_t suggested_size) {
972+
NoArrayBufferZeroFillScope no_zero_fill_scope(isolate_data());
973+
std::unique_ptr<v8::BackingStore> bs =
974+
v8::ArrayBuffer::NewBackingStore(isolate(), suggested_size);
975+
uv_buf_t buf = uv_buf_init(static_cast<char*>(bs->Data()), bs->ByteLength());
976+
released_allocated_buffers()->emplace(buf.base, std::move(bs));
977+
return buf;
978+
}
979+
980+
inline std::unique_ptr<v8::BackingStore> Environment::release_managed_buffer(
981+
const uv_buf_t& buf) {
982+
std::unique_ptr<v8::BackingStore> bs;
983+
if (buf.base != nullptr) {
984+
auto map = released_allocated_buffers();
985+
auto it = map->find(buf.base);
986+
CHECK_NE(it, map->end());
987+
bs = std::move(it->second);
988+
map->erase(it);
989+
}
990+
return bs;
991+
}
992+
969993
std::unordered_map<char*, std::unique_ptr<v8::BackingStore>>*
970994
Environment::released_allocated_buffers() {
971995
return &released_allocated_buffers_;

src/env.h

+3
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,9 @@ class Environment : public MemoryRetainer {
14371437
void RunAndClearNativeImmediates(bool only_refed = false);
14381438
void RunAndClearInterrupts();
14391439

1440+
inline uv_buf_t allocate_managed_buffer(const size_t suggested_size);
1441+
inline std::unique_ptr<v8::BackingStore> release_managed_buffer(
1442+
const uv_buf_t& buf);
14401443
inline std::unordered_map<char*, std::unique_ptr<v8::BackingStore>>*
14411444
released_allocated_buffers();
14421445

src/udp_wrap.cc

+18-9
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@
3131
namespace node {
3232

3333
using v8::Array;
34+
using v8::ArrayBuffer;
35+
using v8::BackingStore;
36+
using v8::Boolean;
3437
using v8::Context;
3538
using v8::DontDelete;
3639
using v8::FunctionCallbackInfo;
3740
using v8::FunctionTemplate;
3841
using v8::HandleScope;
3942
using v8::Integer;
43+
using v8::Isolate;
4044
using v8::Local;
4145
using v8::MaybeLocal;
4246
using v8::Object;
@@ -314,7 +318,7 @@ void UDPWrap::BufferSize(const FunctionCallbackInfo<Value>& args) {
314318

315319
CHECK(args[0]->IsUint32());
316320
CHECK(args[1]->IsBoolean());
317-
bool is_recv = args[1].As<v8::Boolean>()->Value();
321+
bool is_recv = args[1].As<Boolean>()->Value();
318322
const char* uv_func_name = is_recv ? "uv_recv_buffer_size" :
319323
"uv_send_buffer_size";
320324

@@ -679,7 +683,7 @@ void UDPWrap::OnAlloc(uv_handle_t* handle,
679683
}
680684

681685
uv_buf_t UDPWrap::OnAlloc(size_t suggested_size) {
682-
return AllocatedBuffer::AllocateManaged(env(), suggested_size).release();
686+
return env()->allocate_managed_buffer(suggested_size);
683687
}
684688

685689
void UDPWrap::OnRecv(uv_udp_t* handle,
@@ -696,27 +700,32 @@ void UDPWrap::OnRecv(ssize_t nread,
696700
const sockaddr* addr,
697701
unsigned int flags) {
698702
Environment* env = this->env();
699-
AllocatedBuffer buf(env, buf_);
703+
Isolate* isolate = env->isolate();
704+
std::unique_ptr<BackingStore> bs = env->release_managed_buffer(buf_);
700705
if (nread == 0 && addr == nullptr) {
701706
return;
702707
}
703708

704-
HandleScope handle_scope(env->isolate());
709+
HandleScope handle_scope(isolate);
705710
Context::Scope context_scope(env->context());
706711

707712
Local<Value> argv[] = {
708-
Integer::New(env->isolate(), static_cast<int32_t>(nread)),
713+
Integer::New(isolate, static_cast<int32_t>(nread)),
709714
object(),
710-
Undefined(env->isolate()),
711-
Undefined(env->isolate())};
715+
Undefined(isolate),
716+
Undefined(isolate)};
712717

713718
if (nread < 0) {
714719
MakeCallback(env->onmessage_string(), arraysize(argv), argv);
715720
return;
721+
} else if (nread == 0) {
722+
bs = ArrayBuffer::NewBackingStore(isolate, 0);
723+
} else {
724+
bs = BackingStore::Reallocate(isolate, std::move(bs), nread);
716725
}
717726

718-
buf.Resize(nread);
719-
argv[2] = buf.ToBuffer().ToLocalChecked();
727+
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, std::move(bs));
728+
argv[2] = Buffer::New(env, ab, 0, ab->ByteLength()).ToLocalChecked();
720729
argv[3] = AddressToJS(env, addr);
721730
MakeCallback(env->onmessage_string(), arraysize(argv), argv);
722731
}

0 commit comments

Comments
 (0)