Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_chunk_allocator.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2010 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#ifndef __KIS_CHUNK_LIST_H
8#define __KIS_CHUNK_LIST_H
9
10#include <QLinkedList>
11#include "kritaimage_export.h"
12
13#define MiB (1ULL << 20)
14
15#define DEFAULT_STORE_SIZE (4096*MiB)
16#define DEFAULT_SLAB_SIZE (64*MiB)
17
18
19//#define DEBUG_SLAB_FAILS
20
21#ifdef DEBUG_SLAB_FAILS
22
23#define WINDOW_SIZE 2000
24#define DECLARE_FAIL_COUNTER() quint64 __failCount
25#define INIT_FAIL_COUNTER() __failCount = 0
26#define START_COUNTING() quint64 __numSteps = 0
27#define REGISTER_STEP() if(++__numSteps > WINDOW_SIZE) {__numSteps=0; __failCount++;}
28#define REGISTER_FAIL() __failCount++
29#define DEBUG_FAIL_COUNTER() qInfo() << "Slab fail count:\t" << __failCount
30
31#else
32
33#define DECLARE_FAIL_COUNTER()
34#define INIT_FAIL_COUNTER()
35#define START_COUNTING()
36#define REGISTER_STEP()
37#define REGISTER_FAIL()
38#define DEBUG_FAIL_COUNTER()
39
40#endif /* DEBUG_SLAB_FAILS */
41
42
43
44class KisChunkData;
45
46typedef QLinkedList<KisChunkData> KisChunkDataList;
47typedef KisChunkDataList::iterator KisChunkDataListIterator;
48
49class KRITAIMAGE_EXPORT KisChunkData
50{
51public:
52 KisChunkData(quint64 begin, quint64 size)
53 {
54 setChunk(begin, size);
55 }
56
57 inline void setChunk(quint64 begin, quint64 size) {
58 m_begin = begin;
59 m_end = begin + size - 1;
60 }
61
62 inline quint64 size() const {
63 return m_end - m_begin +1;
64 }
65
66 bool operator== (const KisChunkData& other) const
67 {
68 Q_ASSERT(m_begin!=other.m_begin || m_end==other.m_end);
69
74 return m_begin == other.m_begin;
75 }
76
77 quint64 m_begin;
78 quint64 m_end;
79};
80
81class KRITAIMAGE_EXPORT KisChunk
82{
83public:
85
87 : m_iterator(iterator)
88 {
89 }
90
91 inline quint64 begin() const {
92 return m_iterator->m_begin;
93 }
94
95 inline quint64 end() const {
96 return m_iterator->m_end;
97 }
98
99 inline quint64 size() const {
100 return m_iterator->size();
101 }
102
104 return m_iterator;
105 }
106
107 inline const KisChunkData& data() {
108 return *m_iterator;
109 }
110
111private:
113};
114
115
116class KRITAIMAGE_EXPORT KisChunkAllocator
117{
118public:
119 KisChunkAllocator(quint64 slabSize = DEFAULT_SLAB_SIZE,
120 quint64 storeSize = DEFAULT_STORE_SIZE);
122
123 inline quint64 numChunks() const {
124 return m_list.size();
125 }
126
127 KisChunk getChunk(quint64 size);
128 void freeChunk(KisChunk chunk);
129
130 void debugChunks();
131 bool sanityCheck(bool pleaseCrash = true);
132 qreal debugFragmentation(bool toStderr = true);
133
134private:
135 bool tryInsertChunk(KisChunkDataList &list,
136 KisChunkDataListIterator &iterator,
137 quint64 size);
138
139private:
142
143
146 quint64 m_storeSize;
148};
149
150#endif /* __KIS_CHUNK_ALLOCATOR_H */
151
bool operator==(const KisRegion &lhs, const KisRegion &rhs)
KisChunkDataList m_list
quint64 numChunks() const
KisChunkDataListIterator m_iterator
KisChunkData(quint64 begin, quint64 size)
void setChunk(quint64 begin, quint64 size)
quint64 size() const
KisChunkDataListIterator m_iterator
quint64 begin() const
const KisChunkData & data()
KisChunk(KisChunkDataListIterator iterator)
KisChunkDataListIterator position()
quint64 end() const
quint64 size() const
QLinkedList< KisChunkData > KisChunkDataList
#define DEFAULT_STORE_SIZE
#define DEFAULT_SLAB_SIZE
KisChunkDataList::iterator KisChunkDataListIterator
#define DECLARE_FAIL_COUNTER()