17#ifndef FLATBUFFERS_VECTOR_DOWNWARD_H_
18#define FLATBUFFERS_VECTOR_DOWNWARD_H_
38 bool own_allocator,
size_t buffer_minalign,
39 const SizeT max_size = FLATBUFFERS_MAX_BUFFER_SIZE)
41 own_allocator_(own_allocator),
42 initial_size_(initial_size),
44 buffer_minalign_(buffer_minalign),
53 : allocator_(other.allocator_),
54 own_allocator_(other.own_allocator_),
55 initial_size_(other.initial_size_),
56 max_size_(other.max_size_),
57 buffer_minalign_(other.buffer_minalign_),
58 reserved_(other.reserved_),
62 scratch_(other.scratch_) {
66 other.own_allocator_ =
false;
70 other.scratch_ =
nullptr;
92 cur_ = buf_ + reserved_;
104 if (own_allocator_ && allocator_) {
delete allocator_; }
105 allocator_ =
nullptr;
106 own_allocator_ =
false;
110 if (buf_)
Deallocate(allocator_, buf_, reserved_);
117 allocated_bytes = reserved_;
130 DetachedBuffer fb(allocator_, own_allocator_, buf_, reserved_, cur_,
132 if (own_allocator_) {
133 allocator_ =
nullptr;
134 own_allocator_ =
false;
154 size_ +=
static_cast<SizeT
>(len);
163 size_t offset()
const {
return cur_ - buf_; }
166 inline SizeT
size()
const {
return size_; }
170 return static_cast<SizeT
>(cur_ - scratch_);
174 SizeT
scratch_size()
const {
return static_cast<SizeT
>(scratch_ - buf_); }
195 void push(
const uint8_t *bytes,
size_t num) {
196 if (num > 0) { memcpy(
make_space(num), bytes, num); }
200 template<
typename T>
void push_small(
const T &little_endian_t) {
202 *
reinterpret_cast<T *
>(cur_) = little_endian_t;
207 *
reinterpret_cast<T *
>(scratch_) = t;
208 scratch_ +=
sizeof(T);
213 void fill(
size_t zero_pad_bytes) {
215 for (
size_t i = 0; i < zero_pad_bytes; i++) cur_[i] = 0;
221 memset(
make_space(zero_pad_bytes), 0, zero_pad_bytes);
224 void pop(
size_t bytes_to_remove) {
225 cur_ += bytes_to_remove;
226 size_ -=
static_cast<SizeT
>(bytes_to_remove);
229 void scratch_pop(
size_t bytes_to_remove) { scratch_ -= bytes_to_remove; }
233 swap(allocator_, other.allocator_);
234 swap(own_allocator_, other.own_allocator_);
235 swap(initial_size_, other.initial_size_);
236 swap(buffer_minalign_, other.buffer_minalign_);
237 swap(reserved_, other.reserved_);
238 swap(size_, other.size_);
239 swap(max_size_, other.max_size_);
240 swap(buf_, other.buf_);
241 swap(cur_, other.cur_);
242 swap(scratch_, other.scratch_);
247 swap(allocator_, other.allocator_);
248 swap(own_allocator_, other.own_allocator_);
258 size_t initial_size_;
262 size_t buffer_minalign_;
269 void reallocate(
size_t len) {
270 auto old_reserved = reserved_;
271 auto old_size =
size();
274 (std::max)(len, old_reserved ? old_reserved / 2 : initial_size_);
275 reserved_ = (reserved_ + buffer_minalign_ - 1) & ~(buffer_minalign_ - 1);
278 old_size, old_scratch_size);
280 buf_ =
Allocate(allocator_, reserved_);
282 cur_ = buf_ + reserved_ - old_size;
283 scratch_ = buf_ + old_scratch_size;
#define FLATBUFFERS_ASSERT
void scratch_pop(size_t bytes_to_remove)
void fill(size_t zero_pad_bytes)
SizeT scratch_size() const
SizeT unused_buffer_size() const
void push(const uint8_t *bytes, size_t num)
vector_downward(size_t initial_size, Allocator *allocator, bool own_allocator, size_t buffer_minalign, const SizeT max_size=FLATBUFFERS_MAX_BUFFER_SIZE)
void swap_allocator(vector_downward &other)
uint8_t * make_space(size_t len)
uint8_t * release_raw(size_t &allocated_bytes, size_t &offset)
void swap(vector_downward &other)
void scratch_push_small(const T &t)
void fill_big(size_t zero_pad_bytes)
vector_downward & operator=(vector_downward &&other) noexcept
Allocator * get_custom_allocator()
void push_small(const T &little_endian_t)
vector_downward(vector_downward &&other) noexcept
void pop(size_t bytes_to_remove)
uint8_t * scratch_data() const
size_t ensure_space(size_t len)
uint8_t * data_at(size_t offset) const
uint8_t * scratch_end() const
uint8_t * ReallocateDownward(Allocator *allocator, uint8_t *old_p, size_t old_size, size_t new_size, size_t in_use_back, size_t in_use_front)
void Deallocate(Allocator *allocator, uint8_t *p, size_t size)
uint8_t * Allocate(Allocator *allocator, size_t size)