1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#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
{
class SharedString : public StringView
{
public:
struct Storage : UseMemoryDomain<MemoryDomain::SharedString>
{
int refcount = 0;
Vector<char, MemoryDomain::SharedString> content;
Storage(StringView str)
{
content.reserve((int)str.length() + 1);
content.assign(str.begin(), str.end());
content.push_back('\0');
}
StringView strview() const { return {&content.front(), &content.back()}; }
friend void inc_ref_count(Storage* s) { ++s->refcount; }
friend void dec_ref_count(Storage* s) { if (--s->refcount == 0) delete s; }
};
SharedString() = default;
SharedString(StringView str)
{
if (not str.empty())
{
m_storage = new Storage{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};
}
private:
SharedString(StringView str, ref_ptr<Storage> storage)
: StringView{str}, m_storage(std::move(storage)) {}
friend class StringRegistry;
ref_ptr<Storage> 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:
UnorderedMap<StringView, ref_ptr<SharedString::Storage>, MemoryDomain::SharedString> m_strings;
};
inline SharedString intern(StringView str)
{
return StringRegistry::instance().intern(str);
}
}
#endif // shared_string_hh_INCLUDED
|