From f86b129ea622e138d24a5e757f367aa02ccfffe7 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 23 Sep 2019 18:07:18 -0700 Subject: [PATCH] ircd::json::stack: Add class member counters; assertions; conditions. --- include/ircd/json/stack.h | 19 +++++++---- ircd/json.cc | 66 +++++++++++++++++++++++++++++---------- 2 files changed, 62 insertions(+), 23 deletions(-) diff --git a/include/ircd/json/stack.h b/include/ircd/json/stack.h index 21b108b81..d6fbc1e44 100644 --- a/include/ircd/json/stack.h +++ b/include/ircd/json/stack.h @@ -54,6 +54,8 @@ struct ircd::json::stack flush_callback flusher; std::exception_ptr eptr; checkpoint *cp {nullptr}; + size_t appended {0}; + size_t flushed {0}; size_t hiwat; ///< autoflush watermark size_t lowat; ///< flush(false) call min watermark @@ -308,9 +310,9 @@ ircd::json::stack::member::append(const json::tuple &t) _post_append(); }}; - s->append(serialized(t), [this, &t](mutable_buffer buf) + s->append(serialized(t), [&t](mutable_buffer buf) { - return size(stringify(buf, t)); + return ircd::size(stringify(buf, t)); }); } @@ -326,7 +328,7 @@ ircd::json::stack::array::append(const json::tuple &t) s->append(serialized(t), [&t](mutable_buffer buf) { - return size(stringify(buf, t)); + return ircd::size(stringify(buf, t)); }); } @@ -336,10 +338,15 @@ ircd::json::stack::object::append(const json::tuple &t) { for_each(t, [this](const auto &name, const auto &_value) { - const json::value value(_value); - if(defined(value)) json::stack::member + const json::value value { - *this, name, value + _value }; + + if(defined(value)) + json::stack::member + { + *this, name, value + }; }); } diff --git a/ircd/json.cc b/ircd/json.cc index 77f408a37..b7c4739c8 100644 --- a/ircd/json.cc +++ b/ircd/json.cc @@ -722,9 +722,11 @@ void ircd::json::stack::append(const string_view &s) noexcept { - append(size(s), [&s](const mutable_buffer &buf) + append(s.size(), [&s] + (const mutable_buffer &buf) { - return copy(buf, s); + assert(ircd::size(buf) >= s.size()); + return ircd::copy(buf, s); }); } @@ -733,7 +735,7 @@ ircd::json::stack::append(const size_t &expect, const window_buffer::closure &closure) noexcept try { - if(unlikely(failed())) + if(!expect || failed()) return; // Since all appends are atomic, we need to have buffer available to print @@ -763,18 +765,27 @@ noexcept try } // Print the JSON to the buffer and advance the window pointer - buf([&expect, &closure] - (const mutable_buffer &buf) + const const_buffer appended { - const size_t ret + buf([&expect, &closure](const mutable_buffer &buf) { - closure(buf) - }; + const size_t appended + { + closure(buf) + }; - assert(ret <= size(buf)); - assert(ret == expect); - return ret; - }); + assert(appended <= size(buf)); + assert(appended == expect); + return const_buffer + { + data(buf), appended + }; + }) + }; + + this->appended += expect; + assert(size(appended) >= expect); + assert(this->appended >= size(appended)); // Conditions to courtesy flush after a sufficiently large dump; when // there's no buffer remaining we'll inevitably have to flush; the call @@ -816,7 +827,7 @@ noexcept try if(!force && cp) return false; - if(cp) + if(unlikely(cp)) { const size_t invalidated { @@ -841,6 +852,7 @@ noexcept try }; assert(data(flushed) == data(buf.completed())); // Can only flush front sry + this->flushed += size(flushed); buf.shift(size(flushed)); return true; } @@ -865,18 +877,38 @@ ircd::json::stack::invalidate_checkpoints() void ircd::json::stack::clear() { - rewind(buf.consumed()); + const size_t rewound + { + rewind(buf.consumed()) + }; + this->eptr = std::exception_ptr{}; } size_t ircd::json::stack::rewind(const size_t &bytes) { - const size_t before(buf.consumed()); - const size_t &amount(std::min(bytes, before)); - const size_t after(size(buf.rewind(amount))); + const size_t before + { + buf.consumed() + }; + + assert(appended >= before); + const size_t &amount + { + std::min(bytes, before) + }; + + assert(appended >= amount); + const size_t after + { + size(buf.rewind(amount)) + }; + assert(before >= after); assert(before - after == amount); + appended -= amount; + assert(appended >= after); return amount; }