Skip to content

Commit

Permalink
termbench: Refactor Buffer implementation to always (or never) write …
Browse files Browse the repository at this point in the history
…full frames
  • Loading branch information
christianparpart committed Mar 18, 2024
1 parent 5a286a2 commit 869f12f
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions libtermbench/termbench.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,26 @@ struct TerminalSize
struct Buffer
{
public:
explicit Buffer(size_t _maxWriteSizeMB) noexcept: maxWriteSize { _maxWriteSizeMB * 1024 * 1024 } {}
explicit Buffer(size_t _maxWriteSizeMB) noexcept: _maxSize { _maxWriteSizeMB * 1024 * 1024 } {}

bool good() const noexcept { return nwritten < maxWriteSize; }
bool good() const noexcept { return _data.size() < _maxSize; }

void write(std::string_view _data) noexcept
bool write(std::string_view chunk) noexcept
{
auto const n = nwritten + _data.size() < maxWriteSize ? _data.size() : maxWriteSize - nwritten;
auto i = _data.data();
auto p = data.data() + nwritten;
auto e = data.data() + nwritten + n;
while (p != e)
*p++ = *i++;
// std::memcpy(data.data() + nwritten, _data.data(), n);
nwritten += n;
if (_data.size() + chunk.size() > _maxSize)
return false;
_data.append(chunk.data(), chunk.size());
return true;
}

std::string_view output() const noexcept { return std::string_view { data.data(), nwritten }; }
std::string_view output() const noexcept { return std::string_view { _data.data(), _data.size() }; }

void clear() noexcept { nwritten = 0; }
bool empty() const noexcept { return nwritten == 0; }
void clear() noexcept { _data.clear(); }
bool empty() const noexcept { return _data.empty(); }

private:
size_t maxWriteSize = 4 * 1024 * 1024;

std::array<char, 64 * 1024 * 1024> data {};
size_t nwritten = 0;
std::string _data;
std::size_t _maxSize;
};

/// Describes a single test.
Expand Down

0 comments on commit 869f12f

Please sign in to comment.