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"
|
|
|
|
|
2017-01-30 13:05:04 +01:00
|
|
|
#include <numeric>
|
|
|
|
|
2015-01-15 14:54:38 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-03-01 13:03:08 +01:00
|
|
|
struct StringData : UseMemoryDomain<MemoryDomain::SharedString>
|
2015-01-15 14:54:38 +01:00
|
|
|
{
|
2017-02-23 00:56:52 +01:00
|
|
|
uint32_t refcount;
|
|
|
|
const int length;
|
2015-01-18 19:23:58 +01:00
|
|
|
|
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-01-19 14:25:04 +01:00
|
|
|
|
2017-02-23 00:56:52 +01:00
|
|
|
private:
|
|
|
|
StringData(int len) : refcount(0), length(len) {}
|
|
|
|
|
|
|
|
static constexpr uint32_t interned_flag = 1 << 31;
|
|
|
|
static constexpr uint32_t refcount_mask = ~interned_flag;
|
|
|
|
|
2016-12-03 13:32:16 +01:00
|
|
|
struct PtrPolicy
|
|
|
|
{
|
2017-01-28 14:04:55 +01:00
|
|
|
static void inc_ref(StringData* r, void*) noexcept { ++r->refcount; }
|
2017-02-23 00:56:52 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2016-12-03 13:32:16 +01:00
|
|
|
static void ptr_moved(StringData*, void*, void*) noexcept {}
|
|
|
|
};
|
|
|
|
|
2017-02-23 00:56:52 +01:00
|
|
|
public:
|
|
|
|
using Ptr = RefPtr<StringData, PtrPolicy>;
|
2015-01-19 14:25:04 +01:00
|
|
|
|
2017-02-23 00:56:52 +01:00
|
|
|
class Registry : public Singleton<Registry>
|
2015-01-19 20:31:56 +01:00
|
|
|
{
|
2017-02-23 00:56:52 +01:00
|
|
|
public:
|
|
|
|
void debug_stats() const;
|
|
|
|
Ptr intern(StringView str);
|
|
|
|
void remove(StringView str);
|
2015-01-18 19:23:58 +01:00
|
|
|
|
2017-02-23 00:56:52 +01:00
|
|
|
private:
|
|
|
|
UnorderedMap<StringView, StringData*, MemoryDomain::SharedString> m_strings;
|
|
|
|
};
|
2015-01-15 14:54:38 +01:00
|
|
|
|
2017-02-23 00:56:52 +01:00
|
|
|
static Ptr create(ArrayView<const StringView> strs);
|
2015-01-15 14:54:38 +01:00
|
|
|
};
|
|
|
|
|
2017-02-23 00:56:52 +01:00
|
|
|
using StringDataPtr = StringData::Ptr;
|
|
|
|
using StringRegistry = StringData::Registry;
|
|
|
|
|
2016-02-05 01:20:45 +01:00
|
|
|
inline StringDataPtr intern(StringView str)
|
2015-01-15 14:54:38 +01:00
|
|
|
{
|
|
|
|
return StringRegistry::instance().intern(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // shared_string_hh_INCLUDED
|