mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
Remove dead code: src/flatbuffer_builder.cc
This commit is contained in:
parent
36d82374cf
commit
b6912e718a
4 changed files with 0 additions and 234 deletions
17
BUILD.gn
17
BUILD.gn
|
@ -78,12 +78,10 @@ rust_test("test_rs") {
|
|||
v8_executable("test_cc") {
|
||||
testonly = true
|
||||
sources = [
|
||||
"src/flatbuffer_builder_test.cc",
|
||||
"src/test.cc",
|
||||
]
|
||||
deps = [
|
||||
":deno_base_test",
|
||||
":deno_flatbuffer_builder",
|
||||
"//testing/gtest:gtest",
|
||||
]
|
||||
configs = [ ":deno_config" ]
|
||||
|
@ -141,24 +139,9 @@ v8_source_set("deno_base_test") {
|
|||
configs = [ ":deno_config" ]
|
||||
}
|
||||
|
||||
v8_source_set("deno_flatbuffer_builder") {
|
||||
sources = [
|
||||
"src/flatbuffer_builder.cc",
|
||||
"src/flatbuffer_builder.h",
|
||||
]
|
||||
deps = [
|
||||
":deno_base",
|
||||
]
|
||||
public_deps = [
|
||||
"build_extra/flatbuffers:flatbuffers",
|
||||
]
|
||||
configs = [ ":deno_config" ]
|
||||
}
|
||||
|
||||
v8_source_set("deno_bindings") {
|
||||
deps = [
|
||||
":deno_base",
|
||||
":deno_flatbuffer_builder",
|
||||
]
|
||||
configs = [ ":deno_config" ]
|
||||
}
|
||||
|
|
|
@ -1,77 +0,0 @@
|
|||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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<uint8_t*>(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
|
|
@ -1,63 +0,0 @@
|
|||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||
#ifndef FLATBUFFER_BUILDER_H_
|
||||
#define FLATBUFFER_BUILDER_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "deno.h"
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
namespace deno {
|
||||
|
||||
// Wrap the default FlatBufferBuilder class, because the default one can't give
|
||||
// us a pointer to the output buffer that we own. Nominally,
|
||||
// FlatBufferBuilder::Release() should do that, but it returns some
|
||||
// smart-pointer-like object (DetachedBuffer) that frees the buffer when it goes
|
||||
// out of scope.
|
||||
//
|
||||
// This wrapper adds the `ExportBuf` method that returns a deno_buf, which
|
||||
// is really not owned at all -- the caller is responsible for releasing the
|
||||
// allocation with free().
|
||||
//
|
||||
// The alternative allocator also uses malloc()/free(), rather than
|
||||
// new/delete[], so that the exported buffer can be later be converted to an
|
||||
// ArrayBuffer; the (default) V8 ArrayBuffer allocator also uses free().
|
||||
class FlatBufferBuilder : public flatbuffers::FlatBufferBuilder {
|
||||
static const size_t kDefaultInitialSize = 1024;
|
||||
|
||||
class Allocator : public flatbuffers::Allocator {
|
||||
uint8_t* keep_alloc_ptr_ = nullptr;
|
||||
uint8_t* last_alloc_ptr_ = nullptr;
|
||||
size_t last_alloc_len_ = 0;
|
||||
|
||||
public:
|
||||
deno_buf GetAndKeepBuf(uint8_t* data_ptr, size_t data_len);
|
||||
|
||||
protected:
|
||||
virtual uint8_t* allocate(size_t size);
|
||||
virtual void deallocate(uint8_t* ptr, size_t size);
|
||||
};
|
||||
|
||||
Allocator allocator_;
|
||||
|
||||
public:
|
||||
explicit FlatBufferBuilder(size_t initial_size = kDefaultInitialSize)
|
||||
: flatbuffers::FlatBufferBuilder(initial_size, &allocator_) {}
|
||||
|
||||
// Export the finalized flatbuffer as a deno_buf structure. The caller takes
|
||||
// ownership of the underlying memory allocation, which must be released with
|
||||
// free().
|
||||
// Afer calling ExportBuf() the FlatBufferBuilder should no longer be used;
|
||||
// However it can be used again once it is reset with the Reset() method.
|
||||
deno_buf ExportBuf();
|
||||
|
||||
// Don't use these.
|
||||
flatbuffers::DetachedBuffer Release() = delete;
|
||||
flatbuffers::DetachedBuffer ReleaseBufferPointer() = delete;
|
||||
};
|
||||
|
||||
} // namespace deno
|
||||
|
||||
#endif // FLATBUFFER_BUILDER_H_
|
|
@ -1,77 +0,0 @@
|
|||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
#include "deno.h"
|
||||
#include "flatbuffer_builder.h"
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::size_t countof(T const (&)[N]) noexcept {
|
||||
return N;
|
||||
}
|
||||
|
||||
TEST(FlatBufferBuilderTest, ExportBuf) {
|
||||
const uint32_t nums[] = {1, 2, 3};
|
||||
const char str[] = "hello mars";
|
||||
deno_buf nums_buf;
|
||||
deno_buf str_buf;
|
||||
// Use scope so builder gets destroyed after building buffers.
|
||||
{
|
||||
deno::FlatBufferBuilder builder;
|
||||
// Build first flatbuffer.
|
||||
auto nums_fb = builder.CreateVector(nums, countof(nums));
|
||||
builder.Finish(nums_fb);
|
||||
nums_buf = builder.ExportBuf();
|
||||
// Reset builder.
|
||||
builder.Reset();
|
||||
// Build second flatbuffer using the same builder.
|
||||
auto str_fb = builder.CreateString(str);
|
||||
builder.Finish(str_fb);
|
||||
str_buf = builder.ExportBuf();
|
||||
}
|
||||
// Allocations should be different.
|
||||
EXPECT_NE(nums_buf.alloc_ptr, str_buf.alloc_ptr);
|
||||
// Logical buffer data should be contained inside their allocations.
|
||||
EXPECT_GE(nums_buf.data_ptr, nums_buf.alloc_ptr);
|
||||
EXPECT_LE(nums_buf.data_ptr + nums_buf.data_len,
|
||||
nums_buf.alloc_ptr + nums_buf.alloc_len);
|
||||
EXPECT_GE(str_buf.data_ptr, str_buf.alloc_ptr);
|
||||
EXPECT_LE(str_buf.data_ptr + str_buf.data_len,
|
||||
str_buf.alloc_ptr + str_buf.alloc_len);
|
||||
// Since there is no way to parse these buffers without generating code,
|
||||
// just check whether the data is contained in the raw content.
|
||||
// Both the numbers vector and the string start at offset 8 in the flatbuffer.
|
||||
auto nums_data = reinterpret_cast<uint32_t*>(nums_buf.data_ptr + 8);
|
||||
for (size_t i = 0; i < countof(nums); i++) {
|
||||
EXPECT_EQ(nums_data[i], nums[i]);
|
||||
}
|
||||
auto str_data = str_buf.data_ptr + 8;
|
||||
for (size_t i = 0; i < countof(str); i++) {
|
||||
EXPECT_EQ(str_data[i], str[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(FlatBufferBuilderTest, CanGrowBuffer) {
|
||||
static const size_t kSmallInitialSize = 32;
|
||||
static const char zeroes[1024] = {0};
|
||||
{
|
||||
// Create buffer with small initial size.
|
||||
deno::FlatBufferBuilder builder(kSmallInitialSize);
|
||||
// Write 1 byte and export buffer.
|
||||
builder.Finish(builder.CreateVector(zeroes, 1));
|
||||
auto buf = builder.ExportBuf();
|
||||
// Exported buffer should have initial size.
|
||||
EXPECT_EQ(buf.alloc_len, kSmallInitialSize);
|
||||
}
|
||||
{
|
||||
// Create buffer with small initial size.
|
||||
deno::FlatBufferBuilder builder(kSmallInitialSize);
|
||||
// Write 1024 bytes and export buffer.
|
||||
builder.Finish(builder.CreateVector(zeroes, countof(zeroes)));
|
||||
auto buf = builder.ExportBuf();
|
||||
// Exported buffer have grown.
|
||||
EXPECT_GT(buf.alloc_len, kSmallInitialSize);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue