home/src/shared_string.hh

141 lines
3.7 KiB
C++
Raw Normal View History

#ifndef shared_string_hh_INCLUDED
#define shared_string_hh_INCLUDED
#include "string.hh"
#include "ref_ptr.hh"
#include "utils.hh"
#include "unordered_map.hh"
namespace Kakoune
{
2015-03-01 13:03:08 +01:00
struct StringData : UseMemoryDomain<MemoryDomain::SharedString>
{
int refcount;
int length;
2015-03-01 13:03:08 +01:00
StringData() = default;
constexpr StringData(int refcount, int length) : refcount(refcount), length(length) {}
2015-01-26 20:41:26 +01:00
[[gnu::always_inline]]
2015-01-25 23:36:05 +01:00
char* data() { return reinterpret_cast<char*>(this + 1); }
2015-01-26 20:41:26 +01:00
[[gnu::always_inline]]
2015-01-25 23:36:05 +01:00
const char* data() const { return reinterpret_cast<const char*>(this + 1); }
2015-01-26 20:41:26 +01:00
[[gnu::always_inline]]
2015-01-25 23:36:05 +01:00
StringView strview() const { return {data(), length}; }
2015-03-01 13:03:08 +01:00
static RefPtr<StringData> create(StringView str, char back = 0)
{
const int len = (int)str.length() + (back != 0 ? 1 : 0);
2015-03-01 13:03:08 +01:00
void* ptr = StringData::operator new(sizeof(StringData) + len + 1);
StringData* res = reinterpret_cast<StringData*>(ptr);
std::copy(str.begin(), str.end(), res->data());
res->refcount = 0;
res->length = len;
if (back != 0)
2015-01-25 23:36:05 +01:00
res->data()[len-1] = back;
res->data()[len] = 0;
2015-03-01 13:03:08 +01:00
return RefPtr<StringData>(res);
}
2015-03-01 13:03:08 +01:00
static void destroy(StringData* s)
{
2015-03-01 13:03:08 +01:00
StringData::operator delete(s, sizeof(StringData) + s->length + 1);
}
2015-03-01 13:03:08 +01:00
friend void inc_ref_count(StringData* s, void*)
{
if (s->refcount != -1)
++s->refcount;
}
2015-03-01 13:03:08 +01:00
friend void dec_ref_count(StringData* s, void*)
{
if (s->refcount != -1 and --s->refcount == 0)
2015-03-01 13:03:08 +01:00
StringData::destroy(s);
}
};
2015-03-01 13:03:08 +01:00
inline RefPtr<StringData> operator"" _ss(const char* ptr, size_t len)
{
2015-03-01 13:03:08 +01:00
return StringData::create({ptr, (int)len});
}
template<size_t len>
2015-03-01 13:03:08 +01:00
struct StaticStringData : StringData
{
template<size_t... I> constexpr
2015-03-01 13:03:08 +01:00
StaticStringData(const char (&literal)[len], IndexSequence<I...>)
: StringData{-1, len}, data{literal[I]...} {}
const char data[len];
};
template<size_t len>
2015-03-01 13:03:08 +01:00
constexpr StaticStringData<len> static_storage(const char (&literal)[len])
{
return { literal, make_index_sequence<len>() };
}
class SharedString : public StringView
{
public:
SharedString() = default;
SharedString(StringView str)
{
if (not str.empty())
{
2015-03-01 13:03:08 +01:00
m_storage = StringData::create(str);
StringView::operator=(m_storage->strview());
}
}
struct NoCopy{};
SharedString(StringView str, NoCopy) : StringView(str) {}
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-03-01 13:03:08 +01:00
explicit SharedString(RefPtr<StringData> storage)
: StringView{storage->strview()}, m_storage(std::move(storage)) {}
private:
2015-03-01 13:03:08 +01:00
SharedString(StringView str, RefPtr<StringData> storage)
: StringView{str}, m_storage(std::move(storage)) {}
friend class StringRegistry;
2015-03-01 13:03:08 +01:00
RefPtr<StringData> m_storage;
};
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-03-01 13:03:08 +01:00
UnorderedMap<StringView, RefPtr<StringData>, MemoryDomain::SharedString> m_strings;
};
inline SharedString intern(StringView str)
{
return StringRegistry::instance().intern(str);
}
}
#endif // shared_string_hh_INCLUDED