home/src/shared_string.hh

73 lines
1.8 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"
#include <numeric>
namespace Kakoune
{
2015-03-01 13:03:08 +01:00
struct StringData : UseMemoryDomain<MemoryDomain::SharedString>
{
uint32_t refcount;
const int length;
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}; }
private:
StringData(int len) : refcount(0), length(len) {}
static constexpr uint32_t interned_flag = 1 << 31;
static constexpr uint32_t refcount_mask = ~interned_flag;
struct PtrPolicy
{
static void inc_ref(StringData* r, void*) noexcept { ++r->refcount; }
static void dec_ref(StringData* r, void*) noexcept
{
if ((--r->refcount & refcount_mask) == 0)
{
if (r->refcount & interned_flag)
Registry::instance().remove(r->strview());
StringData::operator delete(r, sizeof(StringData) + r->length + 1);
}
}
static void ptr_moved(StringData*, void*, void*) noexcept {}
};
public:
using Ptr = RefPtr<StringData, PtrPolicy>;
class Registry : public Singleton<Registry>
{
public:
void debug_stats() const;
Ptr intern(StringView str);
void remove(StringView str);
private:
UnorderedMap<StringView, StringData*, MemoryDomain::SharedString> m_strings;
};
static Ptr create(ArrayView<const StringView> strs);
};
using StringDataPtr = StringData::Ptr;
using StringRegistry = StringData::Registry;
inline StringDataPtr intern(StringView str)
{
return StringRegistry::instance().intern(str);
}
}
#endif // shared_string_hh_INCLUDED