mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-03 19:04:12 +00:00
DetachedBuffer cannot be rewrapped (#4885)
* Simple ReleaseRaw implemented * [doc] * clear_buffer and clear_allocator introduced * auto * typos * rename because of -Werror=shadow
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
d0321df8cf
commit
99acd0bcd7
@@ -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 {
|
|||||||
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 {
|
|||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user