Skip to content

Commit 528f922

Browse files
RaisinTentargos
authored andcommitted
src: remove usage of AllocatedBuffer from udp_wrap.cc
Signed-off-by: Darshan Sen <[email protected]> PR-URL: #40151 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 547fc86 commit 528f922

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"
@@ -971,6 +972,29 @@ inline IsolateData* Environment::isolate_data() const {
971972
return isolate_data_;
972973
}
973974

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

src/env.h

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

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

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)