Commit 99acd0bc authored by Gabriel Nützi's avatar Gabriel Nützi Committed by Wouter van Oortmerssen

DetachedBuffer cannot be rewrapped (#4885)

* Simple ReleaseRaw implemented

* [doc]

* clear_buffer and clear_allocator introduced

* auto

* typos

* rename because of -Werror=shadow
parent d0321df8
...@@ -573,15 +573,12 @@ class vector_downward { ...@@ -573,15 +573,12 @@ class vector_downward {
scratch_(nullptr) {} scratch_(nullptr) {}
~vector_downward() { ~vector_downward() {
if (buf_) Deallocate(allocator_, buf_, reserved_); clear_buffer();
if (own_allocator_ && allocator_) { delete allocator_; } clear_allocator();
} }
void reset() { void reset() {
if (buf_) { clear_buffer();
Deallocate(allocator_, buf_, reserved_);
buf_ = nullptr;
}
clear(); clear();
} }
...@@ -599,6 +596,29 @@ class vector_downward { ...@@ -599,6 +596,29 @@ class vector_downward {
scratch_ = buf_; scratch_ = buf_;
} }
void clear_allocator() {
if (own_allocator_ && allocator_) { delete allocator_; }
allocator_ = nullptr;
own_allocator_ = false;
}
void clear_buffer() {
if (buf_) Deallocate(allocator_, buf_, reserved_);
buf_ = nullptr;
}
// Relinquish the pointer to the caller.
uint8_t *release_raw(size_t &allocated_bytes, size_t &offset) {
auto *buf = buf_;
allocated_bytes = reserved_;
offset = static_cast<size_t>(cur_ - buf_);
buf_ = nullptr;
clear_allocator();
clear();
return buf;
}
// Relinquish the pointer to the caller. // Relinquish the pointer to the caller.
DetachedBuffer release() { DetachedBuffer release() {
DetachedBuffer fb(allocator_, own_allocator_, buf_, reserved_, cur_, DetachedBuffer fb(allocator_, own_allocator_, buf_, reserved_, cur_,
...@@ -827,6 +847,19 @@ class FlatBufferBuilder { ...@@ -827,6 +847,19 @@ class FlatBufferBuilder {
return buf_.release(); return buf_.release();
} }
/// @brief Get the released pointer to the serialized buffer.
/// @param The size of the memory block containing
/// the serialized `FlatBuffer`.
/// @param The offset from the released pointer where the finished
/// `FlatBuffer` starts.
/// @return A raw pointer to the start of the memory block containing
/// the serialized `FlatBuffer`.
/// @remark If the allocator is owned, it gets deleted during this call.
uint8_t *ReleaseRaw(size_t &size, size_t &offset) {
Finished();
return buf_.release_raw(size, offset);
}
/// @brief get the minimum alignment this buffer needs to be accessed /// @brief get the minimum alignment this buffer needs to be accessed
/// properly. This is only known once all elements have been written (after /// properly. This is only known once all elements have been written (after
/// you call Finish()). You can use this information if you need to embed /// you call Finish()). You can use this information if you need to embed
......
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