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