summaryrefslogtreecommitdiff
path: root/src/node_http2.cc
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-02-18 22:58:27 +0100
committerAnna Henningsen <anna@addaleax.net>2019-02-25 02:01:11 +0100
commit84e02b178ad14fae0df2a514e8a39bfa50ffdc2d (patch)
treeddc0435b6bd0b7811e0bf47687777c56b2857fd0 /src/node_http2.cc
parent6c257cdf271384555d0ced77104a1d6b0480e246 (diff)
downloadandroid-node-v8-84e02b178ad14fae0df2a514e8a39bfa50ffdc2d.tar.gz
android-node-v8-84e02b178ad14fae0df2a514e8a39bfa50ffdc2d.tar.bz2
android-node-v8-84e02b178ad14fae0df2a514e8a39bfa50ffdc2d.zip
src: allocate Buffer memory using ArrayBuffer allocator
Always use the right allocator for memory that is turned into an `ArrayBuffer` at a later point. This enables embedders to use their own `ArrayBuffer::Allocator`s, and is inspired by Electron’s electron/node@f61bae3440e. It should render their downstream patch unnecessary. Refs: https://github.com/electron/node/commit/f61bae3440e1bfcc83bba6ff0785adfb89b4045e PR-URL: https://github.com/nodejs/node/pull/26207 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'src/node_http2.cc')
-rw-r--r--src/node_http2.cc23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/node_http2.cc b/src/node_http2.cc
index e5451d1fc7..7fc21c9cae 100644
--- a/src/node_http2.cc
+++ b/src/node_http2.cc
@@ -12,7 +12,6 @@
namespace node {
using v8::ArrayBuffer;
-using v8::ArrayBufferCreationMode;
using v8::Boolean;
using v8::Context;
using v8::Float64Array;
@@ -1767,18 +1766,22 @@ Http2Stream* Http2Session::SubmitRequest(
return stream;
}
+uv_buf_t Http2Session::OnStreamAlloc(size_t suggested_size) {
+ return env()->AllocateManaged(suggested_size).release();
+}
+
// Callback used to receive inbound data from the i/o stream
-void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
+void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
HandleScope handle_scope(env()->isolate());
Context::Scope context_scope(env()->context());
Http2Scope h2scope(this);
CHECK_NOT_NULL(stream_);
Debug(this, "receiving %d bytes", nread);
CHECK(stream_buf_ab_.IsEmpty());
+ AllocatedBuffer buf(env(), buf_);
// Only pass data on if nread > 0
if (nread <= 0) {
- free(buf.base);
if (nread < 0) {
PassReadErrorToPreviousListener(nread);
}
@@ -1786,13 +1789,13 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
}
// Shrink to the actual amount of used data.
- char* base = Realloc(buf.base, nread);
+ buf.Resize(nread);
- IncrementCurrentSessionMemory(nread);
+ IncrementCurrentSessionMemory(buf.size());
OnScopeLeave on_scope_leave([&]() {
// Once finished handling this write, reset the stream buffer.
// The memory has either been free()d or was handed over to V8.
- DecrementCurrentSessionMemory(nread);
+ DecrementCurrentSessionMemory(buf.size());
stream_buf_ab_ = Local<ArrayBuffer>();
stream_buf_ = uv_buf_init(nullptr, 0);
});
@@ -1803,17 +1806,13 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
// Remember the current buffer, so that OnDataChunkReceived knows the
// offset of a DATA frame's data into the socket read buffer.
- stream_buf_ = uv_buf_init(base, nread);
+ stream_buf_ = uv_buf_init(buf.data(), nread);
Isolate* isolate = env()->isolate();
// Create an array buffer for the read data. DATA frames will be emitted
// as slices of this array buffer to avoid having to copy memory.
- stream_buf_ab_ =
- ArrayBuffer::New(isolate,
- base,
- nread,
- ArrayBufferCreationMode::kInternalized);
+ stream_buf_ab_ = buf.ToArrayBuffer();
statistics_.data_received += nread;
ssize_t ret = Write(&stream_buf_, 1);