Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 39f42d1

Browse filesBrowse files
jasnellcodebytere
authored andcommitted
src: extract AllocatedBuffer from env.h
Cleanup up env.h by removing things that are not specific to `Environment`. PR-URL: #33291 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: David Carlier <devnexen@gmail.com>
1 parent 6cadb00 commit 39f42d1
Copy full SHA for 39f42d1
Expand file treeCollapse file tree

23 files changed

+241
-161
lines changed
Open diff view settings
Collapse file

‎node.gyp‎

Copy file name to clipboardExpand all lines: node.gyp
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@
642642
'src/aliased_buffer.h',
643643
'src/aliased_struct.h',
644644
'src/aliased_struct-inl.h',
645+
'src/allocated_buffer.h',
646+
'src/allocated_buffer-inl.h'
645647
'src/async_wrap.h',
646648
'src/async_wrap-inl.h',
647649
'src/base_object.h',
Collapse file

‎src/allocated_buffer-inl.h‎

Copy file name to clipboard
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#ifndef SRC_ALLOCATED_BUFFER_INL_H_
2+
#define SRC_ALLOCATED_BUFFER_INL_H_
3+
4+
#include "allocated_buffer.h"
5+
#include "base_object-inl.h"
6+
#include "node_buffer.h"
7+
#include "env-inl.h"
8+
#include "uv.h"
9+
#include "v8.h"
10+
#include "util-inl.h"
11+
#include "node_internals.h"
12+
13+
namespace node {
14+
15+
AllocatedBuffer AllocatedBuffer::AllocateManaged(
16+
Environment* env,
17+
size_t size,
18+
int flags) {
19+
char* data = flags & ALLOCATE_MANAGED_UNCHECKED ?
20+
env->AllocateUnchecked(size) :
21+
env->Allocate(size);
22+
if (data == nullptr) size = 0;
23+
return AllocatedBuffer(env, uv_buf_init(data, size));
24+
}
25+
26+
inline AllocatedBuffer::AllocatedBuffer(Environment* env, uv_buf_t buf)
27+
: env_(env), buffer_(buf) {}
28+
29+
inline void AllocatedBuffer::Resize(size_t len) {
30+
// The `len` check is to make sure we don't end up with `nullptr` as our base.
31+
char* new_data = env_->Reallocate(buffer_.base, buffer_.len,
32+
len > 0 ? len : 1);
33+
CHECK_NOT_NULL(new_data);
34+
buffer_ = uv_buf_init(new_data, len);
35+
}
36+
37+
inline uv_buf_t AllocatedBuffer::release() {
38+
uv_buf_t ret = buffer_;
39+
buffer_ = uv_buf_init(nullptr, 0);
40+
return ret;
41+
}
42+
43+
inline char* AllocatedBuffer::data() {
44+
return buffer_.base;
45+
}
46+
47+
inline const char* AllocatedBuffer::data() const {
48+
return buffer_.base;
49+
}
50+
51+
inline size_t AllocatedBuffer::size() const {
52+
return buffer_.len;
53+
}
54+
55+
inline AllocatedBuffer::AllocatedBuffer(Environment* env)
56+
: env_(env), buffer_(uv_buf_init(nullptr, 0)) {}
57+
58+
inline AllocatedBuffer::AllocatedBuffer(AllocatedBuffer&& other)
59+
: AllocatedBuffer() {
60+
*this = std::move(other);
61+
}
62+
63+
inline AllocatedBuffer& AllocatedBuffer::operator=(AllocatedBuffer&& other) {
64+
clear();
65+
env_ = other.env_;
66+
buffer_ = other.release();
67+
return *this;
68+
}
69+
70+
inline AllocatedBuffer::~AllocatedBuffer() {
71+
clear();
72+
}
73+
74+
inline void AllocatedBuffer::clear() {
75+
uv_buf_t buf = release();
76+
if (buf.base != nullptr) {
77+
CHECK_NOT_NULL(env_);
78+
env_->Free(buf.base, buf.len);
79+
}
80+
}
81+
82+
inline v8::MaybeLocal<v8::Object> AllocatedBuffer::ToBuffer() {
83+
CHECK_NOT_NULL(env_);
84+
v8::MaybeLocal<v8::Object> obj = Buffer::New(env_, data(), size(), false);
85+
if (!obj.IsEmpty()) release();
86+
return obj;
87+
}
88+
89+
inline v8::Local<v8::ArrayBuffer> AllocatedBuffer::ToArrayBuffer() {
90+
CHECK_NOT_NULL(env_);
91+
uv_buf_t buf = release();
92+
auto callback = [](void* data, size_t length, void* deleter_data){
93+
CHECK_NOT_NULL(deleter_data);
94+
95+
static_cast<v8::ArrayBuffer::Allocator*>(deleter_data)
96+
->Free(data, length);
97+
};
98+
std::unique_ptr<v8::BackingStore> backing =
99+
v8::ArrayBuffer::NewBackingStore(buf.base,
100+
buf.len,
101+
callback,
102+
env_->isolate()
103+
->GetArrayBufferAllocator());
104+
return v8::ArrayBuffer::New(env_->isolate(),
105+
std::move(backing));
106+
}
107+
108+
} // namespace node
109+
110+
#endif // SRC_ALLOCATED_BUFFER_INL_H_
Collapse file

‎src/allocated_buffer.h‎

Copy file name to clipboard
+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef SRC_ALLOCATED_BUFFER_H_
2+
#define SRC_ALLOCATED_BUFFER_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include "base_object.h"
7+
#include "uv.h"
8+
#include "v8.h"
9+
10+
namespace node {
11+
12+
class Environment;
13+
14+
// A unique-pointer-ish object that is compatible with the JS engine's
15+
// ArrayBuffer::Allocator.
16+
struct AllocatedBuffer {
17+
public:
18+
enum AllocateManagedFlags {
19+
ALLOCATE_MANAGED_FLAG_NONE,
20+
ALLOCATE_MANAGED_UNCHECKED
21+
};
22+
23+
// Utilities that allocate memory using the Isolate's ArrayBuffer::Allocator.
24+
// In particular, using AllocateManaged() will provide a RAII-style object
25+
// with easy conversion to `Buffer` and `ArrayBuffer` objects.
26+
inline static AllocatedBuffer AllocateManaged(
27+
Environment* env,
28+
size_t size,
29+
int flags = ALLOCATE_MANAGED_FLAG_NONE);
30+
31+
explicit inline AllocatedBuffer(Environment* env = nullptr);
32+
inline AllocatedBuffer(Environment* env, uv_buf_t buf);
33+
inline ~AllocatedBuffer();
34+
inline void Resize(size_t len);
35+
36+
inline uv_buf_t release();
37+
inline char* data();
38+
inline const char* data() const;
39+
inline size_t size() const;
40+
inline void clear();
41+
42+
inline v8::MaybeLocal<v8::Object> ToBuffer();
43+
inline v8::Local<v8::ArrayBuffer> ToArrayBuffer();
44+
45+
inline AllocatedBuffer(AllocatedBuffer&& other);
46+
inline AllocatedBuffer& operator=(AllocatedBuffer&& other);
47+
AllocatedBuffer(const AllocatedBuffer& other) = delete;
48+
AllocatedBuffer& operator=(const AllocatedBuffer& other) = delete;
49+
50+
private:
51+
Environment* env_;
52+
// We do not pass this to libuv directly, but uv_buf_t is a convenient way
53+
// to represent a chunk of memory, and plays nicely with other parts of core.
54+
uv_buf_t buffer_;
55+
56+
friend class Environment;
57+
};
58+
59+
} // namespace node
60+
61+
#endif // NODE_WANT_INTERNALS
62+
63+
#endif // SRC_ALLOCATED_BUFFER_H_
Collapse file

‎src/env-inl.h‎

Copy file name to clipboardExpand all lines: src/env-inl.h
-88Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -867,68 +867,6 @@ inline void Environment::Free(char* data, size_t size) {
867867
isolate_data()->allocator()->Free(data, size);
868868
}
869869

870-
inline AllocatedBuffer Environment::AllocateManaged(size_t size, bool checked) {
871-
char* data = checked ? Allocate(size) : AllocateUnchecked(size);
872-
if (data == nullptr) size = 0;
873-
return AllocatedBuffer(this, uv_buf_init(data, size));
874-
}
875-
876-
inline AllocatedBuffer::AllocatedBuffer(Environment* env, uv_buf_t buf)
877-
: env_(env), buffer_(buf) {}
878-
879-
inline void AllocatedBuffer::Resize(size_t len) {
880-
// The `len` check is to make sure we don't end up with `nullptr` as our base.
881-
char* new_data = env_->Reallocate(buffer_.base, buffer_.len,
882-
len > 0 ? len : 1);
883-
CHECK_NOT_NULL(new_data);
884-
buffer_ = uv_buf_init(new_data, len);
885-
}
886-
887-
inline uv_buf_t AllocatedBuffer::release() {
888-
uv_buf_t ret = buffer_;
889-
buffer_ = uv_buf_init(nullptr, 0);
890-
return ret;
891-
}
892-
893-
inline char* AllocatedBuffer::data() {
894-
return buffer_.base;
895-
}
896-
897-
inline const char* AllocatedBuffer::data() const {
898-
return buffer_.base;
899-
}
900-
901-
inline size_t AllocatedBuffer::size() const {
902-
return buffer_.len;
903-
}
904-
905-
inline AllocatedBuffer::AllocatedBuffer(Environment* env)
906-
: env_(env), buffer_(uv_buf_init(nullptr, 0)) {}
907-
908-
inline AllocatedBuffer::AllocatedBuffer(AllocatedBuffer&& other)
909-
: AllocatedBuffer() {
910-
*this = std::move(other);
911-
}
912-
913-
inline AllocatedBuffer& AllocatedBuffer::operator=(AllocatedBuffer&& other) {
914-
clear();
915-
env_ = other.env_;
916-
buffer_ = other.release();
917-
return *this;
918-
}
919-
920-
inline AllocatedBuffer::~AllocatedBuffer() {
921-
clear();
922-
}
923-
924-
inline void AllocatedBuffer::clear() {
925-
uv_buf_t buf = release();
926-
if (buf.base != nullptr) {
927-
CHECK_NOT_NULL(env_);
928-
env_->Free(buf.base, buf.len);
929-
}
930-
}
931-
932870
// It's a bit awkward to define this Buffer::New() overload here, but it
933871
// avoids a circular dependency with node_internals.h.
934872
namespace Buffer {
@@ -938,32 +876,6 @@ v8::MaybeLocal<v8::Object> New(Environment* env,
938876
bool uses_malloc);
939877
}
940878

941-
inline v8::MaybeLocal<v8::Object> AllocatedBuffer::ToBuffer() {
942-
CHECK_NOT_NULL(env_);
943-
v8::MaybeLocal<v8::Object> obj = Buffer::New(env_, data(), size(), false);
944-
if (!obj.IsEmpty()) release();
945-
return obj;
946-
}
947-
948-
inline v8::Local<v8::ArrayBuffer> AllocatedBuffer::ToArrayBuffer() {
949-
CHECK_NOT_NULL(env_);
950-
uv_buf_t buf = release();
951-
auto callback = [](void* data, size_t length, void* deleter_data){
952-
CHECK_NOT_NULL(deleter_data);
953-
954-
static_cast<v8::ArrayBuffer::Allocator*>(deleter_data)
955-
->Free(data, length);
956-
};
957-
std::unique_ptr<v8::BackingStore> backing =
958-
v8::ArrayBuffer::NewBackingStore(buf.base,
959-
buf.len,
960-
callback,
961-
env_->isolate()
962-
->GetArrayBufferAllocator());
963-
return v8::ArrayBuffer::New(env_->isolate(),
964-
std::move(backing));
965-
}
966-
967879
inline void Environment::ThrowError(const char* errmsg) {
968880
ThrowError(v8::Exception::Error, errmsg);
969881
}
Collapse file

‎src/env.cc‎

Copy file name to clipboardExpand all lines: src/env.cc
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "env.h"
2-
2+
#include "allocated_buffer-inl.h"
33
#include "async_wrap.h"
44
#include "base_object-inl.h"
55
#include "debug_utils-inl.h"
Collapse file

‎src/env.h‎

Copy file name to clipboardExpand all lines: src/env.h
+1-36Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ constexpr size_t kFsStatsBufferLength =
472472
V(url_constructor_function, v8::Function)
473473

474474
class Environment;
475+
struct AllocatedBuffer;
475476

476477
class IsolateData : public MemoryRetainer {
477478
public:
@@ -558,38 +559,6 @@ struct ContextInfo {
558559

559560
class EnabledDebugList;
560561

561-
// A unique-pointer-ish object that is compatible with the JS engine's
562-
// ArrayBuffer::Allocator.
563-
struct AllocatedBuffer {
564-
public:
565-
explicit inline AllocatedBuffer(Environment* env = nullptr);
566-
inline AllocatedBuffer(Environment* env, uv_buf_t buf);
567-
inline ~AllocatedBuffer();
568-
inline void Resize(size_t len);
569-
570-
inline uv_buf_t release();
571-
inline char* data();
572-
inline const char* data() const;
573-
inline size_t size() const;
574-
inline void clear();
575-
576-
inline v8::MaybeLocal<v8::Object> ToBuffer();
577-
inline v8::Local<v8::ArrayBuffer> ToArrayBuffer();
578-
579-
inline AllocatedBuffer(AllocatedBuffer&& other);
580-
inline AllocatedBuffer& operator=(AllocatedBuffer&& other);
581-
AllocatedBuffer(const AllocatedBuffer& other) = delete;
582-
AllocatedBuffer& operator=(const AllocatedBuffer& other) = delete;
583-
584-
private:
585-
Environment* env_;
586-
// We do not pass this to libuv directly, but uv_buf_t is a convenient way
587-
// to represent a chunk of memory, and plays nicely with other parts of core.
588-
uv_buf_t buffer_;
589-
590-
friend class Environment;
591-
};
592-
593562
class KVStore {
594563
public:
595564
KVStore() = default;
@@ -957,10 +926,6 @@ class Environment : public MemoryRetainer {
957926

958927
inline IsolateData* isolate_data() const;
959928

960-
// Utilities that allocate memory using the Isolate's ArrayBuffer::Allocator.
961-
// In particular, using AllocateManaged() will provide a RAII-style object
962-
// with easy conversion to `Buffer` and `ArrayBuffer` objects.
963-
inline AllocatedBuffer AllocateManaged(size_t size, bool checked = true);
964929
inline char* Allocate(size_t size);
965930
inline char* AllocateUnchecked(size_t size);
966931
char* Reallocate(char* data, size_t old_size, size_t size);
Collapse file

‎src/inspector_io.cc‎

Copy file name to clipboardExpand all lines: src/inspector_io.cc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "inspector_socket_server.h"
44
#include "inspector/main_thread_interface.h"
55
#include "inspector/node_string.h"
6+
#include "allocated_buffer-inl.h" // Inlined functions needed by node_crypto.h.
67
#include "base_object-inl.h"
78
#include "debug_utils-inl.h"
89
#include "node.h"
Collapse file

‎src/node.cc‎

Copy file name to clipboardExpand all lines: src/node.cc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "node_version.h"
4242

4343
#if HAVE_OPENSSL
44+
#include "allocated_buffer-inl.h" // Inlined functions needed by node_crypto.h
4445
#include "node_crypto.h"
4546
#endif
4647

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.