From b1281d225d5baa73dc0763627d18bb45a4dc97b2 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 19 Jan 2015 19:31:56 +0000 Subject: rename SharedString::Storage to StringStorage and use directly in Buffer --- src/shared_string.hh | 63 +++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 30 deletions(-) (limited to 'src/shared_string.hh') diff --git a/src/shared_string.hh b/src/shared_string.hh index a0a4a8e1..489d9326 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -9,44 +9,44 @@ namespace Kakoune { -class SharedString : public StringView +struct StringStorage : UseMemoryDomain { -public: - struct Storage : UseMemoryDomain - { - int refcount; - int length; - char data[1]; + int refcount; + int length; + char data[1]; - StringView strview() const { return {data, length}; } + StringView strview() const { return {data, length}; } - static Storage* create(StringView str) - { - const int len = (int)str.length(); - void* ptr = Storage::operator new(sizeof(Storage) + len); - Storage* res = reinterpret_cast(ptr); - memcpy(res->data, str.data(), len); - res->refcount = 0; - res->length = len; - res->data[len] = 0; - return res; - } + static StringStorage* create(StringView str) + { + const int len = (int)str.length(); + void* ptr = StringStorage::operator new(sizeof(StringStorage) + len); + StringStorage* res = reinterpret_cast(ptr); + memcpy(res->data, str.data(), len); + res->refcount = 0; + res->length = len; + res->data[len] = 0; + return res; + } - static void destroy(Storage* s) - { - Storage::operator delete(s, sizeof(Storage) + s->length); - } + static void destroy(StringStorage* s) + { + StringStorage::operator delete(s, sizeof(StringStorage) + s->length); + } - friend void inc_ref_count(Storage* s) { ++s->refcount; } - friend void dec_ref_count(Storage* s) { if (--s->refcount == 0) Storage::destroy(s); } - }; + friend void inc_ref_count(StringStorage* s) { ++s->refcount; } + friend void dec_ref_count(StringStorage* s) { if (--s->refcount == 0) StringStorage::destroy(s); } +}; +class SharedString : public StringView +{ +public: SharedString() = default; SharedString(StringView str) { if (not str.empty()) { - m_storage = Storage::create(str); + m_storage = StringStorage::create(str); StringView::operator=(m_storage->strview()); } } @@ -64,12 +64,15 @@ public: return SharedString{StringView::substr(from, length), m_storage}; } + explicit SharedString(ref_ptr storage) + : StringView{storage->strview()}, m_storage(std::move(storage)) {} + private: - SharedString(StringView str, ref_ptr storage) + SharedString(StringView str, ref_ptr storage) : StringView{str}, m_storage(std::move(storage)) {} friend class StringRegistry; - ref_ptr m_storage; + ref_ptr m_storage; }; inline size_t hash_value(const SharedString& str) @@ -85,7 +88,7 @@ public: void purge_unused(); private: - UnorderedMap, MemoryDomain::SharedString> m_strings; + UnorderedMap, MemoryDomain::SharedString> m_strings; }; inline SharedString intern(StringView str) -- cgit v1.2.3