File: referencecounting.cpp

package info (click to toggle)
kdevelop 4%3A22.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,096 kB
  • sloc: cpp: 284,635; javascript: 3,558; python: 3,422; sh: 1,319; ansic: 685; xml: 331; php: 95; lisp: 66; makefile: 39; sed: 12
file content (218 lines) | stat: -rw-r--r-- 6,178 bytes parent folder | download | duplicates (3)
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
/*
    SPDX-FileCopyrightText: 2009 David Nolden <david.nolden.kdevelop@art-master.de>
    SPDX-FileCopyrightText: 2020 Igor Kushnir <igorkuo@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "referencecounting.h"
#include "itemrepository.h"

#include <QAtomicInt>

#include <algorithm>

namespace KDevelop {
void DUChainReferenceCounting::Interval::assign(Pointer newStart, unsigned newSize) noexcept
{
    start = newStart;
    size = newSize;
    refCount = 1;
}

auto DUChainReferenceCounting::findInterval(Pointer start, unsigned size) noexcept -> Interval*
{
    return std::find_if(intervals, intervals + count, [start, size](Interval interval) {
        return interval.start == start && interval.size == size;
    });
}

void DUChainReferenceCounting::enable(Pointer start, unsigned size)
{
    auto* const interval = findInterval(start, size);
    if (interval == intervals + count) {
        if (count == maxIntervalCount) {
            qFatal("DUChainReferenceCounting interval count limit of %zu exceeded!", count);
        }
        // "push_back"
        Q_ASSERT(count < maxIntervalCount);
        interval->assign(start, size);
        ++count;
    } else {
        Q_ASSERT(interval < intervals + count);
        ++interval->refCount;
    }

#ifdef TEST_REFERENCE_COUNTING
    Q_ASSERT(shouldDo(start));
    Q_ASSERT(shouldDo(start + size - 1));
#endif
}

void DUChainReferenceCounting::disable(Pointer start, unsigned size)
{
    auto* const interval = findInterval(start, size);
    Q_ASSERT(interval < intervals + count);

    if (interval->refCount == 1) {
        // "erase" interval
        std::move(interval + 1, intervals + count, interval);
        --count;
    } else {
        Q_ASSERT(interval->refCount > 1);
        --interval->refCount;
    }
}

void enableDUChainReferenceCounting(const void* start, unsigned size)
{
    DUChainReferenceCounting::instance().enable(reinterpret_cast<DUChainReferenceCounting::Pointer>(start), size);
}

void disableDUChainReferenceCounting(const void* start, unsigned size)
{
    DUChainReferenceCounting::instance().disable(reinterpret_cast<DUChainReferenceCounting::Pointer>(start), size);
}
}

#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