From 24b0e91d8096f84a114f662e3eb42c83d0d3878d Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Mon, 9 Jul 2018 03:35:34 +0200 Subject: Move buffers between V8 and native * send()/recv() now operate on TypedArrays rather than ArrayBuffers. * Remove a copy (through ArrayBuffer.slice()) from the send path. * Remove a copy (through v8::ArrayBuffer::New()) from the return path. * After moving a buffer from JS to native, the ArrayBuffer object and it's views are made inaccessible ('neutered'). * `struct deno_buf` now holds two [ptr, length] tuples, one for the actual memory allocation, and one for the logical data contained therein. This is necessary because flatbuffers fills it's buffer bottom-up, so the serialized blob doesn't start at beginning of the buffer, but somewhere in the middle. --- src/flatbuffer_builder.cc | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/flatbuffer_builder.cc (limited to 'src/flatbuffer_builder.cc') diff --git a/src/flatbuffer_builder.cc b/src/flatbuffer_builder.cc new file mode 100644 index 000000000..8a7a0c649 --- /dev/null +++ b/src/flatbuffer_builder.cc @@ -0,0 +1,78 @@ +// Copyright 2018 Bert Belder +// All rights reserved. MIT License. + +#include +#include +#include + +#include "deno.h" +#include "flatbuffer_builder.h" +#include "flatbuffers/flatbuffers.h" + +namespace deno { + +deno_buf FlatBufferBuilder::ExportBuf() { + uint8_t* data_ptr = GetBufferPointer(); + size_t data_len = GetSize(); + return allocator_.GetAndKeepBuf(data_ptr, data_len); +} + +deno_buf FlatBufferBuilder::Allocator::GetAndKeepBuf(uint8_t* data_ptr, + size_t data_len) { + // The builder will typically allocate one chunk of memory with some + // default size. After that, it'll only call allocate() again if the + // initial allocation wasn't big enough, which is then immediately + // followed by deallocate() to release the buffer that was too small. + // + // Therefore we can assume that the `data_ptr` points somewhere inside + // the last allocation, and that we never have to protect earlier + // allocations from being released. + // + // Each builder gets it's own Allocator instance, so multiple builders + // can be exist at the same time without conflicts. + + assert(last_alloc_ptr_ != nullptr); // Must have allocated. + assert(keep_alloc_ptr_ == nullptr); // Didn't export any buffer so far. + assert(data_ptr >= last_alloc_ptr_); // Data must be within allocation. + assert(data_ptr + data_len <= last_alloc_ptr_ + last_alloc_len_); + + keep_alloc_ptr_ = last_alloc_ptr_; + + deno_buf buf; + buf.alloc_ptr = last_alloc_ptr_; + buf.alloc_len = last_alloc_len_; + buf.data_ptr = data_ptr; + buf.data_len = data_len; + return buf; +} + +uint8_t* FlatBufferBuilder::Allocator::allocate(size_t size) { + auto ptr = reinterpret_cast(malloc(size)); + if (ptr == nullptr) { + return nullptr; + } + + last_alloc_ptr_ = ptr; + last_alloc_len_ = size; + + return ptr; +} + +void FlatBufferBuilder::Allocator::deallocate(uint8_t* ptr, size_t size) { + if (ptr == last_alloc_ptr_) { + last_alloc_ptr_ = nullptr; + last_alloc_len_ = 0; + } + + if (ptr == keep_alloc_ptr_) { + // This allocation became an exported buffer, so don't free it. + // Clearing keep_alloc_ptr_ makes it possible to export another + // buffer later (after the builder is reset with `Reset()`). + keep_alloc_ptr_ = nullptr; + return; + } + + free(ptr); +} + +} // namespace deno -- cgit v1.2.3