Switching StringBuffer to use a raw pointer.

This commit is contained in:
Ben Vanik 2015-06-10 00:24:17 -07:00
parent 070d34cd02
commit 56a4620cdf
4 changed files with 31 additions and 24 deletions

View File

@ -65,6 +65,10 @@ void* Arena::Alloc(size_t size) {
return p; return p;
} }
void Arena::Rewind(size_t size) {
active_chunk_->offset -= size;
}
void* Arena::CloneContents() { void* Arena::CloneContents() {
size_t total_length = 0; size_t total_length = 0;
Chunk* chunk = head_chunk_; Chunk* chunk = head_chunk_;

View File

@ -28,6 +28,7 @@ class Arena {
T* Alloc() { T* Alloc() {
return reinterpret_cast<T*>(Alloc(sizeof(T))); return reinterpret_cast<T*>(Alloc(sizeof(T)));
} }
void Rewind(size_t size);
void* CloneContents(); void* CloneContents();

View File

@ -12,24 +12,29 @@
#include <algorithm> #include <algorithm>
#include <cstdarg> #include <cstdarg>
#include "xenia/base/math.h"
namespace xe { namespace xe {
StringBuffer::StringBuffer(size_t initial_capacity) { StringBuffer::StringBuffer(size_t initial_capacity) : buffer_offset_(0) {
buffer_.reserve(std::max(initial_capacity, static_cast<size_t>(1024))); buffer_capacity_ = std::max(initial_capacity, static_cast<size_t>(16 * 1024));
buffer_ = reinterpret_cast<char*>(malloc(buffer_capacity_));
} }
StringBuffer::~StringBuffer() = default; StringBuffer::~StringBuffer() { free(buffer_); }
void StringBuffer::Reset() { buffer_.resize(0); } void StringBuffer::Reset() { buffer_offset_ = 0; }
void StringBuffer::Grow(size_t additional_length) { void StringBuffer::Grow(size_t additional_length) {
size_t old_capacity = buffer_.capacity(); if (buffer_offset_ + additional_length <= buffer_capacity_) {
if (buffer_.size() + additional_length <= old_capacity) {
return; return;
} }
size_t old_capacity = buffer_capacity_;
size_t new_capacity = size_t new_capacity =
std::max(buffer_.size() + additional_length, old_capacity * 2); std::max(xe::round_up(buffer_offset_ + additional_length, 16 * 1024),
buffer_.reserve(new_capacity); old_capacity * 2);
buffer_ = reinterpret_cast<char*>(realloc(buffer_, new_capacity));
buffer_capacity_ = new_capacity;
} }
void StringBuffer::Append(char c) { void StringBuffer::Append(char c) {
@ -53,29 +58,25 @@ void StringBuffer::AppendFormat(const char* format, ...) {
void StringBuffer::AppendVarargs(const char* format, va_list args) { void StringBuffer::AppendVarargs(const char* format, va_list args) {
int length = vsnprintf(nullptr, 0, format, args); int length = vsnprintf(nullptr, 0, format, args);
auto offset = buffer_.size();
Grow(length + 1); Grow(length + 1);
buffer_.resize(buffer_.size() + length + 1); vsnprintf(buffer_ + buffer_offset_, buffer_capacity_, format, args);
vsnprintf(buffer_.data() + offset, buffer_.capacity(), format, args); buffer_offset_ += length;
buffer_[buffer_.size() - 1] = 0; buffer_[buffer_offset_] = 0;
buffer_.resize(buffer_.size() - 1);
} }
void StringBuffer::AppendBytes(const uint8_t* buffer, size_t length) { void StringBuffer::AppendBytes(const uint8_t* buffer, size_t length) {
auto offset = buffer_.size();
Grow(length + 1); Grow(length + 1);
buffer_.resize(buffer_.size() + length + 1); memcpy(buffer_ + buffer_offset_, buffer, length);
memcpy(buffer_.data() + offset, buffer, length); buffer_offset_ += length;
buffer_[buffer_.size() - 1] = 0; buffer_[buffer_offset_] = 0;
buffer_.resize(buffer_.size() - 1);
} }
const char* StringBuffer::GetString() const { return buffer_.data(); } const char* StringBuffer::GetString() const { return buffer_; }
std::string StringBuffer::to_string() { std::string StringBuffer::to_string() {
return std::string(buffer_.data(), buffer_.size()); return std::string(buffer_, buffer_offset_);
} }
char* StringBuffer::ToString() { return strdup(buffer_.data()); } char* StringBuffer::ToString() { return strdup(buffer_); }
} // namespace xe } // namespace xe

View File

@ -12,7 +12,6 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <vector>
namespace xe { namespace xe {
@ -21,7 +20,7 @@ class StringBuffer {
StringBuffer(size_t initial_capacity = 0); StringBuffer(size_t initial_capacity = 0);
~StringBuffer(); ~StringBuffer();
size_t length() const { return buffer_.size(); } size_t length() const { return buffer_offset_; }
void Reset(); void Reset();
@ -39,7 +38,9 @@ class StringBuffer {
private: private:
void Grow(size_t additional_length); void Grow(size_t additional_length);
std::vector<char> buffer_; char* buffer_;
size_t buffer_offset_;
size_t buffer_capacity_;
}; };
} // namespace xe } // namespace xe