From 42966317b8c91f907a681dabdce2d0fdb17bbbd9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 25 Jan 2015 22:36:05 +0000 Subject: Tweak SharedString --- src/shared_string.hh | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/shared_string.hh') diff --git a/src/shared_string.hh b/src/shared_string.hh index 3ed571e8..a3ffcd92 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -13,27 +13,28 @@ struct StringStorage : UseMemoryDomain { int refcount; int length; - char data[1]; - StringView strview() const { return {data, length}; } + char* data() { return reinterpret_cast(this + 1); } + const char* data() const { return reinterpret_cast(this + 1); } + StringView strview() const { return {data(), length}; } static StringStorage* create(StringView str, char back = 0) { const int len = (int)str.length() + (back != 0 ? 1 : 0); - void* ptr = StringStorage::operator new(sizeof(StringStorage) + len); + void* ptr = StringStorage::operator new(sizeof(StringStorage) + len + 1); StringStorage* res = reinterpret_cast(ptr); - memcpy(res->data, str.data(), (int)str.length()); + 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; + res->data()[len-1] = back; + res->data()[len] = 0; return res; } static void destroy(StringStorage* s) { - StringStorage::operator delete(s, sizeof(StringStorage) + s->length); + StringStorage::operator delete(s, sizeof(StringStorage) + s->length + 1); } friend void inc_ref_count(StringStorage* s) { ++s->refcount; } -- cgit v1.2.3