2015-01-15 14:54:38 +01:00
|
|
|
#ifndef shared_string_hh_INCLUDED
|
|
|
|
#define shared_string_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "string.hh"
|
2015-01-18 19:23:58 +01:00
|
|
|
#include "ref_ptr.hh"
|
2015-01-15 14:54:38 +01:00
|
|
|
#include "utils.hh"
|
|
|
|
#include "unordered_map.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
class SharedString : public StringView
|
|
|
|
{
|
|
|
|
public:
|
2015-01-18 19:23:58 +01:00
|
|
|
struct Storage
|
|
|
|
{
|
|
|
|
int refcount = 0;
|
|
|
|
Vector<char, MemoryDomain::SharedString> content;
|
|
|
|
|
|
|
|
Storage(StringView str)
|
|
|
|
{
|
|
|
|
content.reserve((int)str.length() + 1);
|
|
|
|
content.assign(str.begin(), str.end());
|
|
|
|
content.push_back('\0');
|
|
|
|
}
|
|
|
|
StringView strview() const { return {&content.front(), &content.back()}; }
|
|
|
|
|
|
|
|
friend void inc_ref_count(Storage* s) { ++s->refcount; }
|
|
|
|
friend void dec_ref_count(Storage* s) { if (--s->refcount == 0) delete s; }
|
|
|
|
};
|
|
|
|
|
2015-01-15 14:54:38 +01:00
|
|
|
SharedString() = default;
|
|
|
|
SharedString(StringView str)
|
|
|
|
{
|
|
|
|
if (not str.empty())
|
|
|
|
{
|
2015-01-18 19:23:58 +01:00
|
|
|
m_storage = new Storage{str};
|
|
|
|
StringView::operator=(m_storage->strview());
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-15 20:58:08 +01:00
|
|
|
struct NoCopy{};
|
|
|
|
SharedString(StringView str, NoCopy) : StringView(str) {}
|
|
|
|
|
2015-01-15 14:54:38 +01:00
|
|
|
SharedString(const char* str) : SharedString(StringView{str}) {}
|
|
|
|
|
|
|
|
SharedString acquire_substr(ByteCount from, ByteCount length = INT_MAX) const
|
|
|
|
{
|
|
|
|
return SharedString{StringView::substr(from, length), m_storage};
|
|
|
|
}
|
|
|
|
SharedString acquire_substr(CharCount from, CharCount length = INT_MAX) const
|
|
|
|
{
|
|
|
|
return SharedString{StringView::substr(from, length), m_storage};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-01-18 19:23:58 +01:00
|
|
|
SharedString(StringView str, ref_ptr<Storage> storage)
|
2015-01-15 14:54:38 +01:00
|
|
|
: StringView{str}, m_storage(std::move(storage)) {}
|
|
|
|
|
|
|
|
friend class StringRegistry;
|
2015-01-18 19:23:58 +01:00
|
|
|
ref_ptr<Storage> m_storage;
|
2015-01-15 14:54:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline size_t hash_value(const SharedString& str)
|
|
|
|
{
|
|
|
|
return hash_data(str.data(), (int)str.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
class StringRegistry : public Singleton<StringRegistry>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void debug_stats() const;
|
|
|
|
SharedString intern(StringView str);
|
|
|
|
void purge_unused();
|
|
|
|
|
|
|
|
private:
|
2015-01-18 19:23:58 +01:00
|
|
|
UnorderedMap<StringView, ref_ptr<SharedString::Storage>, MemoryDomain::SharedString> m_strings;
|
2015-01-15 14:54:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline SharedString intern(StringView str)
|
|
|
|
{
|
|
|
|
return StringRegistry::instance().intern(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // shared_string_hh_INCLUDED
|