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"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-03-01 13:03:08 +01:00
|
|
|
struct StringData : UseMemoryDomain<MemoryDomain::SharedString>
|
2015-01-15 14:54:38 +01:00
|
|
|
{
|
2015-01-19 20:31:56 +01:00
|
|
|
int refcount;
|
|
|
|
int length;
|
2015-01-18 19:23:58 +01:00
|
|
|
|
2015-05-14 20:13:52 +02:00
|
|
|
StringData(int ref, int len) : refcount(ref), length(len) {}
|
2015-02-28 18:09:29 +01:00
|
|
|
|
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-01-19 14:25:04 +01:00
|
|
|
|
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; }
|
|
|
|
static void dec_ref(StringData* r, void*) noexcept { if (--r->refcount == 0) destroy(r); }
|
2016-12-03 13:32:16 +01:00
|
|
|
static void ptr_moved(StringData*, void*, void*) noexcept {}
|
|
|
|
};
|
|
|
|
|
|
|
|
static RefPtr<StringData, PtrPolicy> create(StringView str, char back = 0)
|
2015-01-19 20:31:56 +01:00
|
|
|
{
|
2015-01-22 14:39:29 +01:00
|
|
|
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);
|
2017-01-08 23:30:15 +01:00
|
|
|
auto* res = new (ptr) StringData(0, len);
|
2015-02-11 00:09:30 +01:00
|
|
|
std::copy(str.begin(), str.end(), res->data());
|
2015-01-22 14:39:29 +01:00
|
|
|
if (back != 0)
|
2015-01-25 23:36:05 +01:00
|
|
|
res->data()[len-1] = back;
|
|
|
|
res->data()[len] = 0;
|
2016-12-03 13:32:16 +01:00
|
|
|
return RefPtr<StringData, PtrPolicy>{res};
|
2015-01-19 20:31:56 +01:00
|
|
|
}
|
2015-01-19 14:25:04 +01:00
|
|
|
|
2015-03-01 13:03:08 +01:00
|
|
|
static void destroy(StringData* s)
|
2015-01-19 20:31:56 +01:00
|
|
|
{
|
2015-03-01 13:03:08 +01:00
|
|
|
StringData::operator delete(s, sizeof(StringData) + s->length + 1);
|
2015-01-19 20:31:56 +01:00
|
|
|
}
|
|
|
|
};
|
2015-01-18 19:23:58 +01:00
|
|
|
|
2016-12-03 13:32:16 +01:00
|
|
|
using StringDataPtr = RefPtr<StringData, StringData::PtrPolicy>;
|
2015-03-01 13:06:19 +01:00
|
|
|
|
2015-01-15 14:54:38 +01:00
|
|
|
class StringRegistry : public Singleton<StringRegistry>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void debug_stats() const;
|
2016-02-05 01:20:45 +01:00
|
|
|
StringDataPtr intern(StringView str);
|
2015-01-15 14:54:38 +01:00
|
|
|
void purge_unused();
|
|
|
|
|
|
|
|
private:
|
2016-02-05 01:20:45 +01:00
|
|
|
UnorderedMap<StringView, StringDataPtr, MemoryDomain::SharedString> m_strings;
|
2015-01-15 14:54:38 +01:00
|
|
|
};
|
|
|
|
|
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
|