Add StreamBuffer::open

This commit is contained in:
2024-09-04 23:14:39 -04:00
parent 43a02db7f3
commit 2d8015c2ae
2 changed files with 22 additions and 12 deletions

View File

@@ -22,10 +22,10 @@
enum class InvocationKind { enum class InvocationKind {
INVALID, INVALID,
LUA_CALL, LUA_INVOKE,
LUA_PROBE,
LUA_EXPR, LUA_EXPR,
LUA_SOURCE, LUA_SOURCE,
LUA_PROBE,
FLUSH_PRINTS, FLUSH_PRINTS,
TICK, TICK,
}; };

View File

@@ -350,6 +350,14 @@ public:
write_cursor_ = buf_hi_; write_cursor_ = buf_hi_;
} }
// Modify an existing streambuffer to read from an external block of bytes.
//
void open(std::string_view data) {
if (owned_ && (buf_lo_ != 0)) CoreHandler::basebuffer_free(buf_lo_);
init(true, false, const_cast<char *>(data.data()), data.size());
write_cursor_ = buf_hi_;
}
// Destructor. Frees the buffer, if any. // Destructor. Frees the buffer, if any.
// //
~BaseBuffer() { ~BaseBuffer() {
@@ -420,19 +428,21 @@ public:
} }
// Discard all data. Reset total read and write counts. // Discard all data. Reset total read and write counts.
// Releases the allocated buffer, if any. // May release the allocated buffer, if it is large.
// //
void clear() { void clear() {
assert(owned_); if (!owned_) {
if ((!fixed_size_) && (buf_lo_ != nullptr) && ((buf_hi_ - buf_lo_) > 100000)) { open("");
CoreHandler::basebuffer_free(buf_lo_); } else {
buf_lo_ = nullptr; if ((!fixed_size_) && (buf_lo_ != nullptr) && ((buf_hi_ - buf_lo_) > 100000)) {
buf_hi_ = nullptr; CoreHandler::basebuffer_free(buf_lo_);
buf_lo_ = nullptr;
buf_hi_ = nullptr;
}
read_cursor_ = buf_lo_;
write_cursor_ = buf_lo_;
pre_read_count_ = 0;
} }
owned_ = true;
read_cursor_ = buf_lo_;
write_cursor_ = buf_lo_;
pre_read_count_ = 0;
} }
// Write block of bytes into the buffer. // Write block of bytes into the buffer.