core: remove support for moving deno_buf ownership from C++ to JavaScript

The functionality hasn't been in use for a long time. Without this feature,
the `alloc_ptr` and `alloc_len` fields are no longer necessary.
This commit is contained in:
Bert Belder 2019-05-02 00:18:18 +02:00
parent c05cbc8eac
commit ae0544b7ce
6 changed files with 40 additions and 144 deletions

View file

@ -167,57 +167,33 @@ v8::Local<v8::Uint8Array> ImportBuf(DenoIsolate* d, deno_buf buf) {
return v8::Local<v8::Uint8Array>();
}
if (buf.alloc_ptr == nullptr) {
// If alloc_ptr isn't set, we memcpy.
// This is currently used for flatbuffers created in Rust.
// To avoid excessively allocating new ArrayBuffers, we try to reuse a
// single global ArrayBuffer. The caveat is that users must extract data
// from it before the next tick. We only do this for ArrayBuffers less than
// 1024 bytes.
v8::Local<v8::ArrayBuffer> ab;
void* data;
if (buf.data_len > GLOBAL_IMPORT_BUF_SIZE) {
// Simple case. We allocate a new ArrayBuffer for this.
ab = v8::ArrayBuffer::New(d->isolate_, buf.data_len);
data = ab->GetContents().Data();
} else {
// Fast case. We reuse the global ArrayBuffer.
if (d->global_import_buf_.IsEmpty()) {
// Lazily initialize it.
DCHECK_NULL(d->global_import_buf_ptr_);
ab = v8::ArrayBuffer::New(d->isolate_, GLOBAL_IMPORT_BUF_SIZE);
d->global_import_buf_.Reset(d->isolate_, ab);
d->global_import_buf_ptr_ = ab->GetContents().Data();
} else {
DCHECK(d->global_import_buf_ptr_);
ab = d->global_import_buf_.Get(d->isolate_);
}
data = d->global_import_buf_ptr_;
}
memcpy(data, buf.data_ptr, buf.data_len);
auto view = v8::Uint8Array::New(ab, 0, buf.data_len);
return view;
// To avoid excessively allocating new ArrayBuffers, we try to reuse a single
// global ArrayBuffer. The caveat is that users must extract data from it
// before the next tick. We only do this for ArrayBuffers less than 1024
// bytes.
v8::Local<v8::ArrayBuffer> ab;
void* data;
if (buf.data_len > GLOBAL_IMPORT_BUF_SIZE) {
// Simple case. We allocate a new ArrayBuffer for this.
ab = v8::ArrayBuffer::New(d->isolate_, buf.data_len);
data = ab->GetContents().Data();
} else {
auto ab = v8::ArrayBuffer::New(
d->isolate_, reinterpret_cast<void*>(buf.alloc_ptr), buf.alloc_len,
v8::ArrayBufferCreationMode::kInternalized);
auto view =
v8::Uint8Array::New(ab, buf.data_ptr - buf.alloc_ptr, buf.data_len);
return view;
// Fast case. We reuse the global ArrayBuffer.
if (d->global_import_buf_.IsEmpty()) {
// Lazily initialize it.
DCHECK_NULL(d->global_import_buf_ptr_);
ab = v8::ArrayBuffer::New(d->isolate_, GLOBAL_IMPORT_BUF_SIZE);
d->global_import_buf_.Reset(d->isolate_, ab);
d->global_import_buf_ptr_ = ab->GetContents().Data();
} else {
DCHECK(d->global_import_buf_ptr_);
ab = d->global_import_buf_.Get(d->isolate_);
}
data = d->global_import_buf_ptr_;
}
}
static deno_buf GetContents(v8::Isolate* isolate,
v8::Local<v8::ArrayBufferView> view) {
auto ab = view->Buffer();
auto contents = ab->GetContents();
deno_buf buf;
buf.alloc_ptr = reinterpret_cast<uint8_t*>(contents.Data());
buf.alloc_len = contents.ByteLength();
buf.data_ptr = buf.alloc_ptr + view->ByteOffset();
buf.data_len = view->ByteLength();
return buf;
memcpy(data, buf.data_ptr, buf.data_len);
auto view = v8::Uint8Array::New(ab, 0, buf.data_len);
return view;
}
// Sets the recv_ callback.
@ -247,13 +223,12 @@ void Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope handle_scope(isolate);
deno_buf control = {nullptr, 0u, nullptr, 0u};
if (args.Length() > 0) {
v8::Local<v8::Value> control_v = args[0];
if (control_v->IsArrayBufferView()) {
control =
GetContents(isolate, v8::Local<v8::ArrayBufferView>::Cast(control_v));
}
deno_buf control = {nullptr, 0};
if (args[0]->IsArrayBufferView()) {
auto view = v8::Local<v8::ArrayBufferView>::Cast(args[0]);
auto data =
reinterpret_cast<uint8_t*>(view->Buffer()->GetContents().Data());
control = {data + view->ByteOffset(), view->ByteLength()};
}
PinnedBuf zero_copy =