summaryrefslogtreecommitdiff
path: root/src/shared_string.hh
diff options
context:
space:
mode:
authorIgor Ramazanov <igor.ramazanov@protonmail.com>2024-06-04 23:19:47 +0200
committerGitHub <noreply@github.com>2024-06-04 23:19:47 +0200
commit4d21fbb3b0b66ef48270e5655bf2687a1f78becf (patch)
tree479c9ca7cd6e9fcf16d7abc7ebea8c8fe0b502e9 /src/shared_string.hh
parent7e8c430ad0706604c0b919207f322a5c14150575 (diff)
parent727d2391c7695056ce6bb170b127c6e6ca9e1ab4 (diff)
Merge branch 'mawww:master' into contrib/gendocs.sh
Diffstat (limited to 'src/shared_string.hh')
-rw-r--r--src/shared_string.hh32
1 files changed, 25 insertions, 7 deletions
diff --git a/src/shared_string.hh b/src/shared_string.hh
index 17bb72d8..78e9fef4 100644
--- a/src/shared_string.hh
+++ b/src/shared_string.hh
@@ -7,6 +7,7 @@
#include "hash_map.hh"
#include <numeric>
+#include <cstring>
namespace Kakoune
{
@@ -32,12 +33,13 @@ private:
static void inc_ref(StringData* r, void*) noexcept { ++r->refcount; }
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);
- }
+ if ((--r->refcount & refcount_mask) > 0)
+ return;
+ if (r->refcount & interned_flag)
+ Registry::instance().remove(r->strview());
+ auto alloc_len = sizeof(StringData) + r->length + 1;
+ r->~StringData();
+ operator delete(r, alloc_len);
}
static void ptr_moved(StringData*, void*, void*) noexcept {}
};
@@ -57,7 +59,23 @@ public:
HashMap<StringView, StringData*, MemoryDomain::SharedString> m_strings;
};
- static Ptr create(ArrayView<const StringView> strs);
+ static Ptr create(ConvertibleTo<StringView> auto&&... strs)
+ {
+ const int len = ((int)StringView{strs}.length() + ...);
+ void* ptr = operator new(sizeof(StringData) + len + 1);
+ auto* res = new (ptr) StringData(len);
+ auto* data = reinterpret_cast<char*>(res + 1);
+ auto append = [&](StringView str) {
+ if (str.empty()) // memcpy(..., nullptr, 0) is UB
+ return;
+ memcpy(data, str.begin(), (size_t)str.length());
+ data += (int)str.length();
+ };
+ (append(strs), ...);
+ *data = 0;
+ return RefPtr<StringData, PtrPolicy>{res};
+ }
+
};
using StringDataPtr = StringData::Ptr;