Skip to content

Commit c2715b8

Browse files
committed
src: add functions to manage allocated buffers to the Environment class
Signed-off-by: Darshan Sen <[email protected]>
1 parent 755a3fe commit c2715b8

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

src/env-inl.h

Lines changed: 25 additions & 0 deletions
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,30 @@ 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+
const NoArrayBufferZeroFillScope no_zero_fill_scope(isolate_data());
973+
std::unique_ptr<v8::BackingStore> bs =
974+
v8::ArrayBuffer::NewBackingStore(isolate(), suggested_size);
975+
const uv_buf_t buf =
976+
uv_buf_init(static_cast<char*>(bs->Data()), bs->ByteLength());
977+
released_allocated_buffers()->emplace(buf.base, std::move(bs));
978+
return buf;
979+
}
980+
981+
inline std::unique_ptr<v8::BackingStore> Environment::release_managed_buffer(
982+
const uv_buf_t& buf) {
983+
std::unique_ptr<v8::BackingStore> bs;
984+
if (buf.base != nullptr) {
985+
auto map = released_allocated_buffers();
986+
auto it = map->find(buf.base);
987+
CHECK_NE(it, map->end());
988+
bs = std::move(it->second);
989+
map->erase(it);
990+
}
991+
return bs;
992+
}
993+
969994
std::unordered_map<char*, std::unique_ptr<v8::BackingStore>>*
970995
Environment::released_allocated_buffers() {
971996
return &released_allocated_buffers_;

src/env.h

Lines changed: 3 additions & 0 deletions
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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -683,16 +683,7 @@ void UDPWrap::OnAlloc(uv_handle_t* handle,
683683
}
684684

685685
uv_buf_t UDPWrap::OnAlloc(size_t suggested_size) {
686-
Environment* env = this->env();
687-
uv_buf_t buf;
688-
{
689-
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
690-
std::unique_ptr<BackingStore> bs =
691-
ArrayBuffer::NewBackingStore(env->isolate(), suggested_size);
692-
buf = uv_buf_init(static_cast<char*>(bs->Data()), bs->ByteLength());
693-
env->released_allocated_buffers()->emplace(buf.base, std::move(bs));
694-
}
695-
return buf;
686+
return this->env()->allocate_managed_buffer(suggested_size);
696687
}
697688

698689
void UDPWrap::OnRecv(uv_udp_t* handle,
@@ -710,22 +701,13 @@ void UDPWrap::OnRecv(ssize_t nread,
710701
unsigned int flags) {
711702
Environment* env = this->env();
712703
Isolate* isolate = env->isolate();
713-
714-
std::unique_ptr<BackingStore> bs;
715-
if (buf_.base != nullptr) {
716-
auto map = env->released_allocated_buffers();
717-
auto it = map->find(buf_.base);
718-
CHECK_NE(it, map->end());
719-
bs = std::move(it->second);
720-
map->erase(it);
721-
}
722-
704+
std::unique_ptr<BackingStore> bs = env->release_managed_buffer(buf_);
723705
if (nread == 0 && addr == nullptr) {
724706
return;
725707
}
726708

727-
HandleScope handle_scope(isolate);
728-
Context::Scope context_scope(env->context());
709+
const HandleScope handle_scope(isolate);
710+
const Context::Scope context_scope(env->context());
729711

730712
Local<Value> argv[] = {
731713
Integer::New(isolate, static_cast<int32_t>(nread)),
@@ -739,7 +721,7 @@ void UDPWrap::OnRecv(ssize_t nread,
739721
} else if (nread == 0) {
740722
bs = ArrayBuffer::NewBackingStore(isolate, 0);
741723
} else {
742-
NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
724+
const NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
743725
bs = BackingStore::Reallocate(isolate, std::move(bs), nread);
744726
}
745727

0 commit comments

Comments
 (0)