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
|
/*
* This file is part of KDevelop
*
* Copyright 2009 David Nolden <david.nolden.kdevelop@art-master.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library 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, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "referencecounting.h"
#include <QMutex>
#include <QMap>
#include <QAtomicInt>
#include "serialization/itemrepository.h"
namespace KDevelop {
bool doReferenceCounting = false;
//Protects the reference-counting data through a spin-lock
QMutex refCountingLock;
QMap<void*, QPair<uint, uint>>* refCountingRanges = new QMap<void*, QPair<uint, uint>>(); //ptr, <size, count>, leaked intentionally!
bool refCountingHasAdditionalRanges = false; //Whether 'refCountingRanges' is non-empty
//Speedup: In most cases there is only exactly one reference-counted range active,
//so the first reference-counting range can be marked here.
void* refCountingFirstRangeStart = nullptr;
QPair<uint, uint> refCountingFirstRangeExtent = qMakePair(0u, 0u);
}
void KDevelop::disableDUChainReferenceCounting(void* start)
{
QMutexLocker lock(&refCountingLock);
if (refCountingFirstRangeStart &&
reinterpret_cast<char*>(refCountingFirstRangeStart) <= reinterpret_cast<char*>(start) &&
reinterpret_cast<char*>(start) < reinterpret_cast<char*>(refCountingFirstRangeStart) + refCountingFirstRangeExtent.first) {
Q_ASSERT(refCountingFirstRangeExtent.second > 0);
--refCountingFirstRangeExtent.second;
if (refCountingFirstRangeExtent.second == 0) {
refCountingFirstRangeExtent = qMakePair<uint, uint>(0, 0);
refCountingFirstRangeStart = nullptr;
}
} else if (refCountingHasAdditionalRanges) {
QMap<void*, QPair<uint, uint>>::iterator it = refCountingRanges->upperBound(start);
if (it != refCountingRanges->begin()) {
--it;
if (reinterpret_cast<char*>(it.key()) <= reinterpret_cast<char*>(start) &&
reinterpret_cast<char*>(start) < reinterpret_cast<char*>(it.key()) + it.value().first) {
//Contained
} else {
Q_ASSERT(0);
}
}
Q_ASSERT(it.value().second > 0);
--it.value().second;
if (it.value().second == 0)
refCountingRanges->erase(it);
refCountingHasAdditionalRanges = !refCountingRanges->isEmpty();
} else {
Q_ASSERT(0);
}
if (!refCountingFirstRangeStart && !refCountingHasAdditionalRanges)
doReferenceCounting = false;
}
void KDevelop::enableDUChainReferenceCounting(void* start, unsigned int size)
{
QMutexLocker lock(&refCountingLock);
doReferenceCounting = true;
if (refCountingFirstRangeStart &&
reinterpret_cast<char*>(refCountingFirstRangeStart) <= reinterpret_cast<char*>(start) &&
reinterpret_cast<char*>(start) < reinterpret_cast<char*>(refCountingFirstRangeStart) + refCountingFirstRangeExtent.first) {
//Increase the count for the first range
++refCountingFirstRangeExtent.second;
} else if (refCountingHasAdditionalRanges || refCountingFirstRangeStart) {
//There is additional ranges in the ranges-structure. Add any new ranges there as well.
QMap<void*, QPair<uint, uint>>::iterator it = refCountingRanges->upperBound(start);
if (it != refCountingRanges->begin()) {
--it;
if (reinterpret_cast<char*>(it.key()) <= reinterpret_cast<char*>(start) &&
reinterpret_cast<char*>(start) < reinterpret_cast<char*>(it.key()) + it.value().first) {
//Contained, count up
} else {
it = refCountingRanges->end(); //Insert own item
}
} else if (it != refCountingRanges->end() && it.key() > start) {
//The item is behind
it = refCountingRanges->end();
}
if (it == refCountingRanges->end()) {
QMap<void*, QPair<uint, uint>>::iterator inserted = refCountingRanges->insert(start, qMakePair(size, 1u));
//Merge following ranges
QMap<void*, QPair<uint, uint>>::iterator it = inserted;
++it;
while (it != refCountingRanges->end() && it.key() < reinterpret_cast<char*>(start) + size) {
inserted.value().second += it.value().second; //Accumulate count
if (reinterpret_cast<char*>(start) + size < reinterpret_cast<char*>(inserted.key()) + it.value().first) {
//Update end position
inserted.value().first = (reinterpret_cast<char*>(inserted.key()) + it.value().first) - reinterpret_cast<char*>(start);
}
it = refCountingRanges->erase(it);
}
} else {
++it.value().second;
if (it.value().first < size)
it.value().first = size;
}
refCountingHasAdditionalRanges = true;
} else {
refCountingFirstRangeStart = start;
refCountingFirstRangeExtent.first = size;
refCountingFirstRangeExtent.second = 1;
}
Q_ASSERT(refCountingHasAdditionalRanges == (refCountingRanges && !refCountingRanges->isEmpty()));
#ifdef TEST_REFERENCE_COUNTING
Q_ASSERT(shouldDoDUChainReferenceCounting(start));
Q_ASSERT(shouldDoDUChainReferenceCounting(reinterpret_cast<char*>(start) + (size - 1)));
#endif
}
#ifdef TEST_REFERENCE_COUNTING
QAtomicInt& id()
{
static QAtomicInt& ret(KDevelop::globalItemRepositoryRegistry().getCustomCounter("referencer ids", 1));
return ret;
}
namespace KDevelop {
ReferenceCountManager::ReferenceCountManager() : m_id(id().fetchAndAddRelaxed(1))
{
}
struct ReferenceCountItem
{
///Item entries:
ReferenceCountItem(uint id, uint target) : m_id(id)
, m_targetId(target)
{
}
ReferenceCountItem& operator=(const ReferenceCountItem& rhs) = delete;
//Every item has to implement this function, and return a valid hash.
//Must be exactly the same hash value as ReferenceCountItemRequest::hash() has returned while creating the item.
unsigned int hash() const
{
return KDevHash() << m_id << m_targetId;
}
//Every item has to implement this function, and return the complete size this item takes in memory.
//Must be exactly the same value as ReferenceCountItemRequest::itemSize() has returned while creating the item.
unsigned short int itemSize() const
{
return sizeof(ReferenceCountItem);
}
uint m_id;
uint m_targetId;
///Request entries:
enum {
AverageSize = 8
};
void createItem(ReferenceCountItem* item) const
{
* item = *this;
}
static void destroy(ReferenceCountItem* /*item*/, AbstractItemRepository&)
{
}
static bool persistent(const ReferenceCountItem*)
{
return true;
}
bool equals(const ReferenceCountItem* item) const
{
return m_id == item->m_id && m_targetId == item->m_targetId;
}
};
static RepositoryManager<ItemRepository<ReferenceCountItem, ReferenceCountItem, false, true,
sizeof(ReferenceCountItem)>, false>& references()
{
static RepositoryManager<ItemRepository<ReferenceCountItem, ReferenceCountItem, false, true,
sizeof(ReferenceCountItem)>, false> referencesObject("Reference Count Debugging");
return referencesObject;
}
static RepositoryManager<ItemRepository<ReferenceCountItem, ReferenceCountItem, false, true,
sizeof(ReferenceCountItem)>, false>& oldReferences()
{
static RepositoryManager<ItemRepository<ReferenceCountItem, ReferenceCountItem, false, true,
sizeof(ReferenceCountItem)>, false> oldReferencesObject("Old Reference Count Debugging");
return oldReferencesObject;
}
void KDevelop::initReferenceCounting()
{
references();
oldReferences();
}
ReferenceCountManager::~ReferenceCountManager()
{
//Make sure everything is cleaned up when the object is destroyed
// Q_ASSERT(!references().contains(m_id));
}
ReferenceCountManager::ReferenceCountManager(const ReferenceCountManager& rhs) : m_id(id().fetchAndAddRelaxed(1))
{
//New id
}
ReferenceCountManager& ReferenceCountManager::ReferenceCountManager::operator=(const ReferenceCountManager& rhs)
{
//Keep id
return *this;
}
// bool ReferenceCountManager::hasReferenceCount() const {
// return references->findIndex(ReferenceCountItem);
// }
void ReferenceCountManager::increase(uint& ref, uint targetId)
{
Q_ASSERT(shouldDoDUChainReferenceCounting(this));
Q_ASSERT(!references->findIndex(ReferenceCountItem(m_id, targetId)));
++ref;
{
int oldIndex = oldReferences->findIndex(ReferenceCountItem(m_id, targetId));
if (oldIndex)
oldReferences->deleteItem(oldIndex);
}
Q_ASSERT(references->index(ReferenceCountItem(m_id, targetId)));
}
void ReferenceCountManager::decrease(uint& ref, uint targetId)
{
Q_ASSERT(ref > 0);
Q_ASSERT(shouldDoDUChainReferenceCounting(this));
Q_ASSERT(!oldReferences->findIndex(ReferenceCountItem(m_id, targetId)));
uint refIndex = references->findIndex(ReferenceCountItem(m_id, targetId));
Q_ASSERT(refIndex);
--ref;
references->deleteItem(refIndex);
oldReferences->index(ReferenceCountItem(m_id, targetId));
}
}
#else
namespace KDevelop {
void initReferenceCounting()
{
}
}
#endif
|