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
|
/* This file is part of KDevelop
Copyright 2008 David Nolden <david.nolden.kdevelop@art-master.de>
Copyright 2016 Milian Wolff <mail@milianw.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "indexedstring.h"
#include "serialization/stringrepository.h"
#include "referencecounting.h"
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;
}
};
inline void increase(uint& val)
{
++val;
}
inline void decrease(uint& val)
{
--val;
}
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, false>;
using IndexedStringRepositoryManagerBase = RepositoryManager<IndexedStringRepository, true, false>;
class IndexedStringRepositoryManager
: public IndexedStringRepositoryManagerBase
{
public:
IndexedStringRepositoryManager()
: IndexedStringRepositoryManagerBase(QStringLiteral("String Index"))
{
repository()->setMutex(&m_mutex);
}
private:
// non-recursive mutex to increase speed
QMutex m_mutex;
};
IndexedStringRepository* globalIndexedStringRepository()
{
static IndexedStringRepositoryManager manager;
return manager.repository();
}
template <typename ReadAction>
auto readRepo(ReadAction action)->decltype(action(globalIndexedStringRepository()))
{
const auto* repo = globalIndexedStringRepository();
QMutexLocker lock(repo->mutex());
return action(repo);
}
template <typename EditAction>
auto editRepo(EditAction action)->decltype(action(globalIndexedStringRepository()))
{
auto* repo = globalIndexedStringRepository();
QMutexLocker lock(repo->mutex());
return action(repo);
}
inline void ref(IndexedString* string)
{
const uint index = string->index();
if (index && !isSingleCharIndex(index)) {
if (shouldDoDUChainReferenceCounting(string)) {
editRepo([index](IndexedStringRepository* repo) {
increase(repo->dynamicItemFromIndexSimple(index)->refCount);
});
}
}
}
inline void deref(IndexedString* string)
{
const uint index = string->index();
if (index && !isSingleCharIndex(index)) {
if (shouldDoDUChainReferenceCounting(string)) {
editRepo([index](IndexedStringRepository* repo) {
decrease(repo->dynamicItemFromIndexSimple(index)->refCount);
});
}
}
}
}
IndexedString::IndexedString()
{
}
///@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 = editRepo([request, refcount](IndexedStringRepository* repo) {
auto index = repo->index(request);
if (refcount) {
increase(repo->dynamicItemFromIndexSimple(index)->refCount);
}
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(const QString& 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())
{}
IndexedString::~IndexedString()
{
deref(this);
}
IndexedString::IndexedString(const IndexedString& rhs)
: m_index(rhs.m_index)
{
ref(this);
}
IndexedString& IndexedString::operator=(const IndexedString& rhs)
{
if (m_index == rhs.m_index) {
return *this;
}
deref(this);
m_index = rhs.m_index;
ref(this);
return *this;
}
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 readRepo([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 readRepo([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 readRepo([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 readRepo([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 editRepo([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();
}
|