From cb197f57ba77ae16ed90d0e470f79d3245e16a70 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 22 Jan 2015 13:39:29 +0000 Subject: Avoid temporary strings on buffer load/reload Pass directly a Vector> to the buffer --- src/shared_string.hh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src/shared_string.hh') diff --git a/src/shared_string.hh b/src/shared_string.hh index 489d9326..b8db3c89 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -17,14 +17,16 @@ struct StringStorage : UseMemoryDomain StringView strview() const { return {data, length}; } - static StringStorage* create(StringView str) + static StringStorage* create(StringView str, char back = 0) { - const int len = (int)str.length(); + const int len = (int)str.length() + (back != 0 ? 1 : 0); void* ptr = StringStorage::operator new(sizeof(StringStorage) + len); StringStorage* res = reinterpret_cast(ptr); - memcpy(res->data, str.data(), len); + memcpy(res->data, str.data(), (int)str.length()); res->refcount = 0; res->length = len; + if (back != 0) + res->data[len-1] = back; res->data[len] = 0; return res; } @@ -38,6 +40,11 @@ struct StringStorage : UseMemoryDomain friend void dec_ref_count(StringStorage* s) { if (--s->refcount == 0) StringStorage::destroy(s); } }; +inline ref_ptr operator""_ss(const char* ptr, size_t len) +{ + return StringStorage::create({ptr, (int)len}); +} + class SharedString : public StringView { public: -- cgit v1.2.3