From 2d8015c2aeacee5e3a3d3bcef1e26007245b30c3 Mon Sep 17 00:00:00 2001 From: jyelon Date: Wed, 4 Sep 2024 23:14:39 -0400 Subject: [PATCH] Add StreamBuffer::open --- luprex/cpp/core/enginewrapper.hpp | 4 ++-- luprex/ext/base-buffer.hpp | 30 ++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/luprex/cpp/core/enginewrapper.hpp b/luprex/cpp/core/enginewrapper.hpp index 41ffaba3..477b9233 100644 --- a/luprex/cpp/core/enginewrapper.hpp +++ b/luprex/cpp/core/enginewrapper.hpp @@ -22,10 +22,10 @@ enum class InvocationKind { INVALID, - LUA_CALL, + LUA_INVOKE, + LUA_PROBE, LUA_EXPR, LUA_SOURCE, - LUA_PROBE, FLUSH_PRINTS, TICK, }; diff --git a/luprex/ext/base-buffer.hpp b/luprex/ext/base-buffer.hpp index b5ded800..07971072 100644 --- a/luprex/ext/base-buffer.hpp +++ b/luprex/ext/base-buffer.hpp @@ -349,6 +349,14 @@ public: init(true, false, const_cast(data.data()), data.size()); 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(data.data()), data.size()); + write_cursor_ = buf_hi_; + } // Destructor. Frees the buffer, if any. // @@ -420,19 +428,21 @@ public: } // 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() { - assert(owned_); - if ((!fixed_size_) && (buf_lo_ != nullptr) && ((buf_hi_ - buf_lo_) > 100000)) { - CoreHandler::basebuffer_free(buf_lo_); - buf_lo_ = nullptr; - buf_hi_ = nullptr; + if (!owned_) { + open(""); + } else { + if ((!fixed_size_) && (buf_lo_ != nullptr) && ((buf_hi_ - buf_lo_) > 100000)) { + 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.