2015-01-15 16:54:38 +03:00
|
|
|
#ifndef shared_string_hh_INCLUDED
|
|
|
|
#define shared_string_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "string.hh"
|
2015-01-18 21:23:58 +03:00
|
|
|
#include "ref_ptr.hh"
|
2015-01-15 16:54:38 +03:00
|
|
|
#include "utils.hh"
|
|
|
|
#include "unordered_map.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-01-19 22:31:56 +03:00
|
|
|
struct StringStorage : UseMemoryDomain<MemoryDomain::SharedString>
|
2015-01-15 16:54:38 +03:00
|
|
|
{
|
2015-01-19 22:31:56 +03:00
|
|
|
int refcount;
|
|
|
|
int length;
|
2015-01-18 21:23:58 +03:00
|
|
|
|
2015-01-26 22:41:26 +03:00
|
|
|
[[gnu::always_inline]]
|
2015-01-26 01:36:05 +03:00
|
|
|
char* data() { return reinterpret_cast<char*>(this + 1); }
|
2015-01-26 22:41:26 +03:00
|
|
|
[[gnu::always_inline]]
|
2015-01-26 01:36:05 +03:00
|
|
|
const char* data() const { return reinterpret_cast<const char*>(this + 1); }
|
2015-01-26 22:41:26 +03:00
|
|
|
[[gnu::always_inline]]
|
2015-01-26 01:36:05 +03:00
|
|
|
StringView strview() const { return {data(), length}; }
|
2015-01-19 16:25:04 +03:00
|
|
|
|
2015-02-23 23:39:56 +03:00
|
|
|
static RefPtr<StringStorage> create(StringView str, char back = 0)
|
2015-01-19 22:31:56 +03:00
|
|
|
{
|
2015-01-22 16:39:29 +03:00
|
|
|
const int len = (int)str.length() + (back != 0 ? 1 : 0);
|
2015-01-26 01:36:05 +03:00
|
|
|
void* ptr = StringStorage::operator new(sizeof(StringStorage) + len + 1);
|
2015-01-19 22:31:56 +03:00
|
|
|
StringStorage* res = reinterpret_cast<StringStorage*>(ptr);
|
2015-02-11 02:09:30 +03:00
|
|
|
std::copy(str.begin(), str.end(), res->data());
|
2015-01-19 22:31:56 +03:00
|
|
|
res->refcount = 0;
|
|
|
|
res->length = len;
|
2015-01-22 16:39:29 +03:00
|
|
|
if (back != 0)
|
2015-01-26 01:36:05 +03:00
|
|
|
res->data()[len-1] = back;
|
|
|
|
res->data()[len] = 0;
|
2015-02-23 23:39:56 +03:00
|
|
|
return RefPtr<StringStorage>(res);
|
2015-01-19 22:31:56 +03:00
|
|
|
}
|
2015-01-19 16:25:04 +03:00
|
|
|
|
2015-01-19 22:31:56 +03:00
|
|
|
static void destroy(StringStorage* s)
|
|
|
|
{
|
2015-01-26 01:36:05 +03:00
|
|
|
StringStorage::operator delete(s, sizeof(StringStorage) + s->length + 1);
|
2015-01-19 22:31:56 +03:00
|
|
|
}
|
2015-01-18 21:23:58 +03:00
|
|
|
|
2015-02-23 23:39:56 +03:00
|
|
|
friend void inc_ref_count(StringStorage* s, void*) { ++s->refcount; }
|
|
|
|
friend void dec_ref_count(StringStorage* s, void*) { if (--s->refcount == 0) StringStorage::destroy(s); }
|
2015-01-19 22:31:56 +03:00
|
|
|
};
|
2015-01-18 21:23:58 +03:00
|
|
|
|
2015-02-19 16:58:25 +03:00
|
|
|
inline RefPtr<StringStorage> operator"" _ss(const char* ptr, size_t len)
|
2015-01-22 16:39:29 +03:00
|
|
|
{
|
|
|
|
return StringStorage::create({ptr, (int)len});
|
|
|
|
}
|
|
|
|
|
2015-01-19 22:31:56 +03:00
|
|
|
class SharedString : public StringView
|
|
|
|
{
|
|
|
|
public:
|
2015-01-15 16:54:38 +03:00
|
|
|
SharedString() = default;
|
|
|
|
SharedString(StringView str)
|
|
|
|
{
|
|
|
|
if (not str.empty())
|
|
|
|
{
|
2015-01-19 22:31:56 +03:00
|
|
|
m_storage = StringStorage::create(str);
|
2015-01-18 21:23:58 +03:00
|
|
|
StringView::operator=(m_storage->strview());
|
2015-01-15 16:54:38 +03:00
|
|
|
}
|
|
|
|
}
|
2015-01-15 22:58:08 +03:00
|
|
|
struct NoCopy{};
|
|
|
|
SharedString(StringView str, NoCopy) : StringView(str) {}
|
|
|
|
|
2015-01-15 16:54:38 +03: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};
|
|
|
|
}
|
|
|
|
|
2015-02-19 16:58:25 +03:00
|
|
|
explicit SharedString(RefPtr<StringStorage> storage)
|
2015-01-19 22:31:56 +03:00
|
|
|
: StringView{storage->strview()}, m_storage(std::move(storage)) {}
|
|
|
|
|
2015-01-15 16:54:38 +03:00
|
|
|
private:
|
2015-02-19 16:58:25 +03:00
|
|
|
SharedString(StringView str, RefPtr<StringStorage> storage)
|
2015-01-15 16:54:38 +03:00
|
|
|
: StringView{str}, m_storage(std::move(storage)) {}
|
|
|
|
|
|
|
|
friend class StringRegistry;
|
2015-02-19 16:58:25 +03:00
|
|
|
RefPtr<StringStorage> m_storage;
|
2015-01-15 16:54:38 +03: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-02-19 16:58:25 +03:00
|
|
|
UnorderedMap<StringView, RefPtr<StringStorage>, MemoryDomain::SharedString> m_strings;
|
2015-01-15 16:54:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
inline SharedString intern(StringView str)
|
|
|
|
{
|
|
|
|
return StringRegistry::instance().intern(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // shared_string_hh_INCLUDED
|