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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
/*
SPDX-FileCopyrightText: 2008 David Nolden <david.nolden.kdevelop@art-master.de>
SPDX-FileCopyrightText: 2016 Milian Wolff <mail@milianw.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "indexedstring.h"
#include "itemrepository.h"
#include "referencecounting.h"
#include "repositorymanager.h"
#include <utility>
using namespace KDevelop;
namespace {
struct IndexedStringData
{
unsigned short length;
uint refCount;
IndexedStringData& operator=(const IndexedStringData& rhs) = delete;
uint itemSize() const
{
return sizeof(IndexedStringData) + length;
}
uint hash() const
{
IndexedString::RunningHash running;
const char* str = reinterpret_cast<const char*>(this) + sizeof(IndexedStringData);
for (int a = length - 1; a >= 0; --a) {
running.append(*str);
++str;
}
return running.hash;
}
};
struct IndexedStringRepositoryItemRequest
{
//The text is supposed to be utf8 encoded
IndexedStringRepositoryItemRequest(const char* text, uint hash, unsigned short length)
: m_hash(hash)
, m_length(length)
, m_text(text)
{
}
enum {
AverageSize = 10 //This should be the approximate average size of an Item
};
using HashType = uint;
//Should return the hash-value associated with this request(For example the hash of a string)
HashType hash() const
{
return m_hash;
}
//Should return the size of an item created with createItem
uint itemSize() const
{
return sizeof(IndexedStringData) + m_length;
}
//Should create an item where the information of the requested item is permanently stored. The pointer
//@param item equals an allocated range with the size of itemSize().
void createItem(IndexedStringData* item) const
{
item->length = m_length;
item->refCount = 0;
void* itemText = reinterpret_cast<void*>(item + 1);
memcpy(itemText, m_text, m_length);
}
static void destroy(IndexedStringData* item, AbstractItemRepository&)
{
Q_UNUSED(item);
//Nothing to do here (The object is not intelligent)
}
static bool persistent(const IndexedStringData* item)
{
return ( bool )item->refCount;
}
//Should return whether the here requested item equals the given item
bool equals(const IndexedStringData* item) const
{
return item->length == m_length && (memcmp(++item, m_text, m_length) == 0);
}
uint m_hash;
unsigned short m_length;
const char* m_text;
};
inline const char* c_strFromItem(const IndexedStringData* item)
{
return reinterpret_cast<const char*>(item + 1);
}
///@param item must be valid(nonzero)
inline QString stringFromItem(const IndexedStringData* item)
{
return QString::fromUtf8(c_strFromItem(item), item->length);
}
inline QByteArray arrayFromItem(const IndexedStringData* item)
{
return QByteArray(c_strFromItem(item), item->length);
}
inline bool isSingleCharIndex(uint index)
{
return (index & 0xffff0000) == 0xffff0000;
}
inline uint charToIndex(char c)
{
return 0xffff0000 | c;
}
inline char indexToChar(uint index)
{
Q_ASSERT(isSingleCharIndex(index));
return static_cast<char>(index & 0xff);
}
using IndexedStringRepository = ItemRepository<IndexedStringData, IndexedStringRepositoryItemRequest, false>;
}
namespace KDevelop
{
template<>
class ItemRepositoryFor<IndexedString>
{
friend struct LockedItemRepository;
static IndexedStringRepository& repo()
{
static QMutex mutex;
static RepositoryManager<IndexedStringRepository, true, false> manager { QStringLiteral("String Index"),
&mutex };
return *manager.repository();
}
};
}
namespace
{
class ReferenceCountChanger
{
public:
static ReferenceCountChanger increase(unsigned index)
{
return {index, 1};
}
static ReferenceCountChanger decrease(unsigned index)
{
return {index, static_cast<Summand>(-1)}; // unsigned integer overflow is fine
}
void editRepo() const
{
if (m_index && !isSingleCharIndex(m_index)) {
LockedItemRepository::write<IndexedString>(*this);
}
}
void operator()(IndexedStringRepository& repo) const
{
repo.dynamicItemFromIndexSimple(m_index)->refCount += m_summand;
}
private:
using Summand = decltype(IndexedStringData::refCount);
ReferenceCountChanger(unsigned i, Summand s)
: m_index{i}
, m_summand{s}
{}
unsigned m_index;
Summand m_summand;
};
inline void ref(unsigned index)
{
ReferenceCountChanger::increase(index).editRepo();
}
inline void deref(unsigned index)
{
ReferenceCountChanger::decrease(index).editRepo();
}
}
///@param str must be a utf8 encoded string, does not need to be 0-terminated.
///@param length must be its length in bytes.
IndexedString::IndexedString(const char* str, unsigned short length, uint hash)
{
if (!length) {
m_index = 0;
} else if (length == 1) {
m_index = charToIndex(str[0]);
} else {
const auto request = IndexedStringRepositoryItemRequest(str, hash ? hash : hashString(str, length), length);
bool refcount = shouldDoDUChainReferenceCounting(this);
m_index = LockedItemRepository::write<IndexedString>([request, refcount](IndexedStringRepository& repo) {
auto index = repo.index(request);
if (refcount) {
ReferenceCountChanger::increase(index)(repo);
}
return index;
});
}
}
IndexedString::IndexedString(char c)
: m_index(charToIndex(c))
{}
IndexedString::IndexedString(const QUrl& url)
: IndexedString(url.isLocalFile() ? url.toLocalFile() : url.toString())
{
Q_ASSERT(url.isEmpty() || !url.isRelative());
#if !defined(QT_NO_DEBUG)
if (url != url.adjusted(QUrl::NormalizePathSegments)) {
qWarning() << "wrong url" << url << url.adjusted(QUrl::NormalizePathSegments);
}
#endif
Q_ASSERT(url == url.adjusted(QUrl::NormalizePathSegments));
}
IndexedString::IndexedString(QStringView string)
: IndexedString(string.toUtf8())
{}
IndexedString::IndexedString(const char* str)
: IndexedString(str, str ? qstrlen(str) : 0)
{}
IndexedString::IndexedString(const QByteArray& str)
: IndexedString(str.constData(), str.length())
{}
// NOTE: the definitions of ref() and deref() are so complex that they can throw exceptions
// for many reasons. Yet the functions below, which call ref() and/or deref(), are
// implicitly (the destructor) or explicitly (the rest) noexcept. The noexcept-ness of
// these functions is important for correctness and performance. This is safe at the moment,
// because the entire KDevPlatformSerialization library, that contains IndexedString, is
// compiled with exceptions disabled (-fno-exceptions), which already prevents exception
// propagation to a caller of any non-inline function in this library.
IndexedString::~IndexedString()
{
if (shouldDoDUChainReferenceCounting(this)) {
deref(m_index);
}
}
IndexedString::IndexedString(const IndexedString& rhs) noexcept
: m_index(rhs.m_index)
{
if (shouldDoDUChainReferenceCounting(this)) {
ref(m_index);
}
}
IndexedString& IndexedString::operator=(const IndexedString& rhs) noexcept
{
if (m_index == rhs.m_index) {
return *this;
}
if (shouldDoDUChainReferenceCounting(this)) {
deref(m_index);
ref(rhs.m_index);
}
m_index = rhs.m_index;
return *this;
}
namespace KDevelop {
void swap(IndexedString& a, IndexedString& b) noexcept
{
using std::swap;
if (a.m_index == b.m_index) {
return;
}
swap(a.m_index, b.m_index);
const bool aRc = shouldDoDUChainReferenceCounting(&a);
const bool bRc = shouldDoDUChainReferenceCounting(&b);
if (aRc == bRc) {
return;
}
auto noLongerRefCountedIndex = b.m_index;
auto newlyRefCountedIndex = a.m_index;
if (bRc) {
swap(noLongerRefCountedIndex, newlyRefCountedIndex);
}
deref(noLongerRefCountedIndex);
ref(newlyRefCountedIndex);
}
}
QUrl IndexedString::toUrl() const
{
if (isEmpty()) {
return {};
}
QUrl ret = QUrl::fromUserInput(str());
Q_ASSERT(!ret.isRelative());
return ret;
}
QString IndexedString::str() const
{
if (!m_index) {
return QString();
} else if (isSingleCharIndex(m_index)) {
return QString(QLatin1Char(indexToChar(m_index)));
} else {
const uint index = m_index;
return LockedItemRepository::read<IndexedString>([index](const IndexedStringRepository& repo) {
return stringFromItem(repo.itemFromIndex(index));
});
}
}
int IndexedString::length() const
{
return lengthFromIndex(m_index);
}
int IndexedString::lengthFromIndex(uint index)
{
if (!index) {
return 0;
} else if (isSingleCharIndex(index)) {
return 1;
} else {
return LockedItemRepository::read<IndexedString>([index](const IndexedStringRepository& repo) {
return repo.itemFromIndex(index)->length;
});
}
}
const char* IndexedString::c_str() const
{
if (!m_index) {
return nullptr;
} else if (isSingleCharIndex(m_index)) {
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
const uint offset = 0;
#else
const uint offset = 3;
#endif
return reinterpret_cast<const char*>(&m_index) + offset;
} else {
const uint index = m_index;
return LockedItemRepository::read<IndexedString>([index](const IndexedStringRepository& repo) {
return c_strFromItem(repo.itemFromIndex(index));
});
}
}
QByteArray IndexedString::byteArray() const
{
if (!m_index) {
return QByteArray();
} else if (isSingleCharIndex(m_index)) {
return QByteArray(1, indexToChar(m_index));
} else {
const uint index = m_index;
return LockedItemRepository::read<IndexedString>([index](const IndexedStringRepository& repo) {
return arrayFromItem(repo.itemFromIndex(index));
});
}
}
uint IndexedString::hashString(const char* str, unsigned short length)
{
RunningHash running;
for (int a = length - 1; a >= 0; --a) {
running.append(*str);
++str;
}
return running.hash;
}
uint IndexedString::indexForString(const char* str, short unsigned length, uint hash)
{
if (!length) {
return 0;
} else if (length == 1) {
return charToIndex(str[0]);
} else {
const auto request = IndexedStringRepositoryItemRequest(str, hash ? hash : hashString(str, length), length);
return LockedItemRepository::write<IndexedString>([request](IndexedStringRepository& repo) {
return repo.index(request);
});
}
}
uint IndexedString::indexForString(const QString& str, uint hash)
{
const QByteArray array(str.toUtf8());
return indexForString(array.constBegin(), array.size(), hash);
}
QDebug operator<<(QDebug s, const IndexedString& string)
{
s.nospace() << string.str();
return s.space();
}
|