|
1 | 1 | #include "lcf/dbstring.h" |
| 2 | +#include <unordered_map> |
| 3 | +#include <memory> |
| 4 | +#include <iostream> |
2 | 5 |
|
3 | 6 | namespace lcf { |
4 | 7 |
|
5 | 8 | constexpr DBString::size_type DBString::npos; |
| 9 | +alignas(DBString::size_type) constexpr char DBString::_empty_str[sizeof(size_type)]; |
6 | 10 |
|
7 | | -static constexpr size_t AllocSize(size_t len) { |
8 | | - return sizeof(DBString::size_type) + len + 1; |
9 | | -} |
| 11 | +struct DBStringData { |
| 12 | + using size_type = DBString::size_type; |
10 | 13 |
|
11 | | -static char* Alloc(size_t len) { |
12 | | - return reinterpret_cast<char*>(::operator new(AllocSize(len))); |
13 | | -} |
| 14 | + size_type refcnt; |
| 15 | + size_type size; |
14 | 16 |
|
15 | | -static char* Dup(const char* other, size_t size) { |
16 | | - if (size > 0) { |
17 | | - auto* s = Alloc(size); |
18 | | - std::memcpy(s, other, AllocSize(size)); |
19 | | - return s; |
| 17 | + const char* data() const { |
| 18 | + return reinterpret_cast<const char*>(this + 1); |
20 | 19 | } |
21 | | - return nullptr; |
22 | | -} |
23 | | - |
24 | | -static void Free(void* p) { |
25 | | - ::operator delete(p); |
26 | | -} |
27 | | - |
28 | | -DBString::DBString(const char* s, size_t len) |
29 | | -{ |
30 | | - if (len > 0) { |
31 | | - auto* ptr = Alloc(len); |
32 | | - _storage = ptr; |
33 | 20 |
|
34 | | - *reinterpret_cast<size_type*>(ptr) = len; |
35 | | - ptr += sizeof(size_type); |
| 21 | + char* data() { |
| 22 | + return reinterpret_cast<char*>(this + 1); |
| 23 | + } |
36 | 24 |
|
37 | | - std::memcpy(ptr, s, len); |
38 | | - ptr += len; |
| 25 | + static size_type alloc_size(StringView str) { |
| 26 | + return sizeof(DBStringData) + str.size() + 1; |
| 27 | + } |
39 | 28 |
|
40 | | - *ptr = '\0'; |
| 29 | + static DBStringData* from_data(char* s) { |
| 30 | + return reinterpret_cast<DBStringData*>(s) - 1; |
41 | 31 | } |
| 32 | +}; |
| 33 | + |
| 34 | +struct DBStringDataDeleter { |
| 35 | + void operator()(DBStringData* p); |
| 36 | +}; |
| 37 | + |
| 38 | +using DBStringDataPtr = std::unique_ptr<DBStringData,DBStringDataDeleter>; |
| 39 | + |
| 40 | +class DBStringAllocator { |
| 41 | + public: |
| 42 | + using size_type = DBString::size_type; |
| 43 | + |
| 44 | + static DBStringDataPtr Alloc(StringView str) { |
| 45 | + auto* raw = ::operator new(DBStringData::alloc_size(str)); |
| 46 | + auto data = DBStringDataPtr(new (raw) DBStringData()); |
| 47 | + data->refcnt = 1; |
| 48 | + data->size = str.size(); |
| 49 | + std::memcpy(data->data(), str.data(), data->size); |
| 50 | + data->data()[data->size] = '\0'; |
| 51 | + |
| 52 | + return data; |
| 53 | + } |
| 54 | + |
| 55 | + static void Free(DBStringData* data) { |
| 56 | + data->~DBStringData(); |
| 57 | + ::operator delete(data); |
| 58 | + } |
| 59 | + |
| 60 | + const char* Acquire(StringView str) { |
| 61 | + if (str.empty()) { |
| 62 | + return DBString::empty_str(); |
| 63 | + } |
| 64 | + |
| 65 | + auto iter = _map.find(str); |
| 66 | + if (iter != _map.end()) { |
| 67 | + iter->second->refcnt += 1; |
| 68 | + } else { |
| 69 | + auto ptr = Alloc(str); |
| 70 | + auto sv = StringView(ptr->data(), ptr->size); |
| 71 | + // FIXME: Double hash lookup because the key changes.. |
| 72 | + iter = _map.insert({ sv, std::move(ptr) }).first; |
| 73 | + } |
| 74 | + return iter->second->data(); |
| 75 | + } |
| 76 | + |
| 77 | + const char* Dup(const char* s) { |
| 78 | + if (s != DBString::empty_str()) { |
| 79 | + auto* data = DBStringData::from_data(const_cast<char*>(s)); |
| 80 | + data->refcnt += 1; |
| 81 | + } |
| 82 | + return s; |
| 83 | + } |
| 84 | + |
| 85 | + void Release(StringView str) { |
| 86 | + if (str.empty()) { |
| 87 | + // This is needed, due to global DBStrings which are initialized to null. |
| 88 | + // They may be destroyed *after* DBStringAllocator is destroyed! |
| 89 | + // FIMXE: To fix this, use a hash table with constexpr default constructor. |
| 90 | + return; |
| 91 | + } |
| 92 | + auto iter = _map.find(str); |
| 93 | + if (iter != _map.end()) { |
| 94 | + auto& data = iter->second; |
| 95 | + data->refcnt -= 1; |
| 96 | + assert(data->refcnt >= 0); |
| 97 | + if (data->refcnt == 0) { |
| 98 | + _map.erase(iter); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + static DBStringAllocator& instance() { |
| 104 | + static DBStringAllocator alloc; |
| 105 | + return alloc; |
| 106 | + } |
| 107 | + private: |
| 108 | + DBStringAllocator() = default; |
| 109 | + private: |
| 110 | + std::unordered_map<StringView,DBStringDataPtr> _map; |
| 111 | +}; |
| 112 | + |
| 113 | +void DBStringDataDeleter::operator()(DBStringData* p) { |
| 114 | + DBStringAllocator::Free(p); |
| 115 | +} |
| 116 | + |
| 117 | +DBString::DBString(StringView s) |
| 118 | + : _storage(DBStringAllocator::instance().Acquire(s)) |
| 119 | +{ |
42 | 120 | } |
43 | 121 |
|
44 | 122 | DBString::DBString(const DBString& o) |
45 | | - : _storage(Dup(o._storage, o.size())) |
46 | | -{ } |
| 123 | + : _storage(DBStringAllocator::instance().Dup(o._storage)) |
| 124 | +{ |
| 125 | +} |
47 | 126 |
|
48 | 127 | DBString& DBString::operator=(const DBString& o) { |
49 | 128 | if (this != &o) { |
| 129 | + // What is strings are the same, skip double lookup? |
50 | 130 | _reset(); |
51 | | - _storage = Dup(o._storage, o.size()); |
| 131 | + _storage = DBStringAllocator::instance().Dup(o._storage); |
52 | 132 | } |
53 | 133 | return *this; |
54 | 134 | } |
55 | 135 |
|
56 | | - |
57 | | - |
58 | 136 | void DBString::_reset() noexcept { |
59 | | - Free(_storage); |
60 | | - _storage = nullptr; |
| 137 | + assert(_storage != nullptr); |
| 138 | + DBStringAllocator::instance().Release(*this); |
61 | 139 | } |
62 | 140 |
|
63 | 141 | } // namespace lcf |
0 commit comments