Commit e2373668 authored by Wouter van Oortmerssen's avatar Wouter van Oortmerssen

Merge branch 'master' of https://github.com/google/flatbuffers

parents 8c1a723b ab7949dc
...@@ -539,17 +539,7 @@ class vector_downward { ...@@ -539,17 +539,7 @@ class vector_downward {
uint8_t *make_space(size_t len) { uint8_t *make_space(size_t len) {
if (len > static_cast<size_t>(cur_ - buf_)) { if (len > static_cast<size_t>(cur_ - buf_)) {
auto old_size = size(); reallocate(len);
auto largest_align = AlignOf<largest_scalar_t>();
reserved_ += (std::max)(len, growth_policy(reserved_));
// Round up to avoid undefined behavior from unaligned loads and stores.
reserved_ = (reserved_ + (largest_align - 1)) & ~(largest_align - 1);
auto new_buf = allocator_.allocate(reserved_);
auto new_cur = new_buf + reserved_ - old_size;
memcpy(new_cur, cur_, old_size);
cur_ = new_cur;
allocator_.deallocate(buf_);
buf_ = new_buf;
} }
cur_ -= len; cur_ -= len;
// Beyond this, signed offsets may not have enough range: // Beyond this, signed offsets may not have enough range:
...@@ -593,6 +583,20 @@ class vector_downward { ...@@ -593,6 +583,20 @@ class vector_downward {
uint8_t *buf_; uint8_t *buf_;
uint8_t *cur_; // Points at location between empty (below) and used (above). uint8_t *cur_; // Points at location between empty (below) and used (above).
const simple_allocator &allocator_; const simple_allocator &allocator_;
void reallocate(size_t len) {
auto old_size = size();
auto largest_align = AlignOf<largest_scalar_t>();
reserved_ += (std::max)(len, growth_policy(reserved_));
// Round up to avoid undefined behavior from unaligned loads and stores.
reserved_ = (reserved_ + (largest_align - 1)) & ~(largest_align - 1);
auto new_buf = allocator_.allocate(reserved_);
auto new_cur = new_buf + reserved_ - old_size;
memcpy(new_cur, cur_, old_size);
cur_ = new_cur;
allocator_.deallocate(buf_);
buf_ = new_buf;
}
}; };
// Converts a Field ID to a virtual table offset. // Converts a Field ID to a virtual table offset.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment