This commit is contained in:
Mathias Agopian
2023-12-13 23:15:53 -08:00
committed by Benjamin Doherty
parent aae48c1121
commit c81ece5c3c
4 changed files with 24 additions and 29 deletions

View File

@@ -35,6 +35,10 @@ namespace utils::io {
ostream::~ostream() = default;
ostream& flush(ostream& s) noexcept {
return s.flush();
}
ostream::Buffer& ostream::getBuffer() noexcept {
return mImpl->mData;
}
@@ -69,12 +73,12 @@ ostream& ostream::print(const char* format, ...) noexcept {
// figure out how much size to we need
va_start(args0, format);
va_copy(args1, args0);
ssize_t s = vsnprintf(nullptr, 0, format, args0);
ssize_t const s = vsnprintf(nullptr, 0, format, args0);
va_end(args0);
{ // scope for the lock
std::lock_guard lock(mImpl->mLock);
std::lock_guard const lock(mImpl->mLock);
Buffer& buf = getBuffer();
@@ -205,14 +209,14 @@ ostream::Buffer::~Buffer() noexcept {
void ostream::Buffer::advance(ssize_t n) noexcept {
if (n > 0) {
size_t written = n < size ? size_t(n) : size;
size_t const written = n < size ? size_t(n) : size;
curr += written;
size -= written;
}
}
void ostream::Buffer::reserve(size_t newSize) noexcept {
size_t offset = curr - buffer;
size_t const offset = curr - buffer;
if (buffer == nullptr) {
buffer = (char*)malloc(newSize);
} else {
@@ -237,8 +241,8 @@ void ostream::Buffer::reset() noexcept {
std::pair<char*, size_t> ostream::Buffer::grow(size_t s) noexcept {
if (UTILS_UNLIKELY(size < s)) {
size_t used = curr - buffer;
size_t newCapacity = std::max(size_t(32), used + (s * 3 + 1) / 2); // 32 bytes minimum
size_t const used = curr - buffer;
size_t const newCapacity = std::max(size_t(32), used + (s * 3 + 1) / 2); // 32 bytes minimum
reserve(newCapacity);
assert(size >= s);
}