[Utils]: Cleanup VA (#878)

This commit is contained in:
Edo 2023-03-27 21:45:47 +01:00 committed by GitHub
parent ae6d50dad8
commit c45fd2d5e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 21 deletions

View File

@ -7,8 +7,8 @@ namespace Utils::String
{ {
const char* VA(const char* fmt, ...) const char* VA(const char* fmt, ...)
{ {
static VAProvider<4, 100> globalProvider; static VAProvider<4, 256> globalProvider;
static thread_local VAProvider<8, 256> provider; static thread_local VAProvider<8, 1024> provider;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);

View File

@ -11,66 +11,65 @@ namespace Utils::String
public: public:
static_assert(Buffers != 0 && MinBufferSize != 0, "Buffers and MinBufferSize mustn't be 0"); static_assert(Buffers != 0 && MinBufferSize != 0, "Buffers and MinBufferSize mustn't be 0");
VAProvider() : currentBuffer(0) {} VAProvider() : currentBuffer_(0) {}
~VAProvider() = default;
[[nodiscard]] const char* get(const char* format, va_list ap) [[nodiscard]] const char* get(const char* format, va_list ap)
{ {
++this->currentBuffer %= ARRAY_COUNT(this->stringPool); ++this->currentBuffer_ %= ARRAY_COUNT(this->stringPool_);
auto entry = &this->stringPool[this->currentBuffer]; auto entry = &this->stringPool_[this->currentBuffer_];
if (!entry->size || !entry->buffer) if (!entry->size_ || !entry->buffer_)
{ {
throw std::runtime_error("String pool not initialized"); throw std::runtime_error("String pool not initialized");
} }
while (true) while (true)
{ {
const auto res = vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap); const auto res = vsnprintf_s(entry->buffer_, entry->size_, _TRUNCATE, format, ap);
if (res > 0) break; // Success if (res > 0) break; // Success
if (res == 0) return ""; // Error if (res == 0) return ""; // Error
entry->doubleSize(); entry->doubleSize();
} }
return entry->buffer; return entry->buffer_;
} }
private: private:
class Entry class Entry
{ {
public: public:
Entry(std::size_t _size = MinBufferSize) : size(_size), buffer(nullptr) Entry(std::size_t size = MinBufferSize) : size_(size), buffer_(nullptr)
{ {
if (this->size < MinBufferSize) this->size = MinBufferSize; if (this->size_ < MinBufferSize) this->size_ = MinBufferSize;
this->allocate(); this->allocate();
} }
~Entry() ~Entry()
{ {
if (this->buffer) Memory::GetAllocator()->free(this->buffer); if (this->buffer_) Memory::GetAllocator()->free(this->buffer_);
this->size = 0; this->size_ = 0;
this->buffer = nullptr; this->buffer_ = nullptr;
} }
void allocate() void allocate()
{ {
if (this->buffer) Memory::GetAllocator()->free(this->buffer); if (this->buffer_) Memory::GetAllocator()->free(this->buffer_);
this->buffer = Memory::GetAllocator()->allocateArray<char>(this->size + 1); this->buffer_ = Memory::GetAllocator()->allocateArray<char>(this->size_ + 1);
} }
void doubleSize() void doubleSize()
{ {
this->size *= 2; this->size_ *= 2;
this->allocate(); this->allocate();
} }
std::size_t size; std::size_t size_;
char* buffer; char* buffer_;
}; };
std::size_t currentBuffer; std::size_t currentBuffer_;
Entry stringPool[Buffers]; Entry stringPool_[Buffers];
}; };
template <typename Arg> // This should display a nice "nullptr" instead of a number template <typename Arg> // This should display a nice "nullptr" instead of a number