Krita Source Code Documentation
Loading...
Searching...
No Matches
KisTileHashTableTraits2< T > Class Template Reference

#include <kis_tile_hash_table2.h>

Classes

struct  MemoryReclaimer
 

Public Types

typedef T TileType
 
typedef KisSharedPtr< T > TileTypeSP
 
typedef KisWeakSharedPtr< T > TileTypeWSP
 

Public Member Functions

void addTile (TileTypeSP tile)
 
void clear ()
 
void debugMaxListLength (qint32 &min, qint32 &max)
 
void debugPrintInfo ()
 
KisTileDatadefaultTileData ()
 
bool deleteTile (qint32 col, qint32 row)
 
bool deleteTile (TileTypeSP tile)
 
TileTypeSP getExistingTile (qint32 col, qint32 row)
 
TileTypeSP getReadOnlyTileLazy (qint32 col, qint32 row, bool &existingTile)
 
TileTypeSP getTileLazy (qint32 col, qint32 row, bool &newTile)
 
bool isEmpty ()
 
 KisTileHashTableTraits2 (const KisTileHashTableTraits2< T > &ht, KisMementoManager *mm)
 
 KisTileHashTableTraits2 (KisMementoManager *mm)
 
qint32 numTiles ()
 
KisTileDatarefAndFetchDefaultTileData ()
 
void setDefaultTileData (KisTileData *defaultTileData)
 
bool tileExists (qint32 col, qint32 row)
 
 ~KisTileHashTableTraits2 ()
 

Private Types

typedef ConcurrentMap< quint32, TileType * > LockFreeTileMap
 
typedef LockFreeTileMap::Mutator LockFreeTileMapMutator
 

Private Member Functions

quint32 calculateHash (qint32 col, qint32 row)
 
quint32 calculateHashImpl (qint32 col, qint32 row)
 
quint32 calculateHashSafe (qint32 col, qint32 row)
 
bool erase (quint32 idx)
 
void insert (quint32 idx, TileTypeSP item)
 
 Q_STATIC_ASSERT_X (isInherited, "Template must inherit KisShared")
 

Private Attributes

QReadWriteLock m_defaultPixelDataLock
 
KisTileDatam_defaultTileData
 
QReadWriteLock m_iteratorLock
 
LockFreeTileMap m_map
 
KisMementoManagerm_mementoManager
 
QAtomicInt m_numTiles
 

Static Private Attributes

static constexpr bool isInherited = std::is_convertible<T*, KisShared*>::value
 

Friends

class KisTileHashTableIteratorTraits2< T >
 

Detailed Description

template<class T>
class KisTileHashTableTraits2< T >

Definition at line 35 of file kis_tile_hash_table2.h.

Member Typedef Documentation

◆ LockFreeTileMap

template<class T >
typedef ConcurrentMap<quint32, TileType*> KisTileHashTableTraits2< T >::LockFreeTileMap
private

Definition at line 200 of file kis_tile_hash_table2.h.

◆ LockFreeTileMapMutator

template<class T >
typedef LockFreeTileMap::Mutator KisTileHashTableTraits2< T >::LockFreeTileMapMutator
private

Definition at line 201 of file kis_tile_hash_table2.h.

◆ TileType

template<class T >
typedef T KisTileHashTableTraits2< T >::TileType

Definition at line 41 of file kis_tile_hash_table2.h.

◆ TileTypeSP

template<class T >
typedef KisSharedPtr<T> KisTileHashTableTraits2< T >::TileTypeSP

Definition at line 42 of file kis_tile_hash_table2.h.

◆ TileTypeWSP

template<class T >
typedef KisWeakSharedPtr<T> KisTileHashTableTraits2< T >::TileTypeWSP

Definition at line 43 of file kis_tile_hash_table2.h.

Constructor & Destructor Documentation

◆ KisTileHashTableTraits2() [1/2]

Definition at line 272 of file kis_tile_hash_table2.h.

◆ KisTileHashTableTraits2() [2/2]

Definition at line 278 of file kis_tile_hash_table2.h.

280{
282
283 QWriteLocker locker(&ht.m_iteratorLock);
285
286 while (iter.isValid()) {
287 TileTypeSP tile = new TileType(*iter.getValue(), m_mementoManager);
288 insert(iter.getKey(), tile);
289 iter.next();
290 }
291}
void insert(quint32 idx, TileTypeSP item)
KisTileHashTableTraits2(KisMementoManager *mm)
void setDefaultTileData(KisTileData *defaultTileData)

References ConcurrentMap< K, V, KT, VT >::Iterator::getKey(), ConcurrentMap< K, V, KT, VT >::Iterator::getValue(), KisTileHashTableTraits2< T >::insert(), ConcurrentMap< K, V, KT, VT >::Iterator::isValid(), KisTileHashTableTraits2< T >::m_defaultTileData, KisTileHashTableTraits2< T >::m_iteratorLock, KisTileHashTableTraits2< T >::m_map, KisTileHashTableTraits2< T >::m_mementoManager, ConcurrentMap< K, V, KT, VT >::Iterator::next(), and KisTileHashTableTraits2< T >::setDefaultTileData().

◆ ~KisTileHashTableTraits2()

template<class T >
KisTileHashTableTraits2< T >::~KisTileHashTableTraits2 ( )

Definition at line 294 of file kis_tile_hash_table2.h.

Member Function Documentation

◆ addTile()

template<class T >
void KisTileHashTableTraits2< T >::addTile ( TileTypeSP tile)

Definition at line 437 of file kis_tile_hash_table2.h.

438{
439 quint32 idx = calculateHash(tile->col(), tile->row());
440 insert(idx, tile);
441}
quint32 calculateHash(qint32 col, qint32 row)

◆ calculateHash()

template<class T >
quint32 KisTileHashTableTraits2< T >::calculateHash ( qint32 col,
qint32 row )
inlineprivate

Definition at line 136 of file kis_tile_hash_table2.h.

137 {
138#ifdef SANITY_CHECK
139 KIS_ASSERT_RECOVER_NOOP(qAbs(row) < 0x7FFF && qAbs(col) < 0x7FFF);
140#endif // SANITY_CHECK
141
142 return calculateHashImpl(col, row);
143 }
quint32 calculateHashImpl(qint32 col, qint32 row)
#define KIS_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:97

References KisTileHashTableTraits2< T >::calculateHashImpl(), and KIS_ASSERT_RECOVER_NOOP.

◆ calculateHashImpl()

template<class T >
quint32 KisTileHashTableTraits2< T >::calculateHashImpl ( qint32 col,
qint32 row )
inlineprivate

Definition at line 126 of file kis_tile_hash_table2.h.

127 {
128 if (col == 0 && row == 0) {
129 col = 0x7FFF;
130 row = 0x7FFF;
131 }
132
133 return ((static_cast<quint32>(row) << 16) | (static_cast<quint32>(col) & 0xFFFF));
134 }

◆ calculateHashSafe()

template<class T >
quint32 KisTileHashTableTraits2< T >::calculateHashSafe ( qint32 col,
qint32 row )
inlineprivate

A version of the hash function that returns an invalid hash in case the requested tile is out of range

Definition at line 149 of file kis_tile_hash_table2.h.

150 {
151 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(qAbs(row) < 0x7FFF && qAbs(col) < 0x7FFF, 0);
152 return calculateHashImpl(col, row);
153 }
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129

References KisTileHashTableTraits2< T >::calculateHashImpl(), and KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE.

◆ clear()

template<class T >
void KisTileHashTableTraits2< T >::clear ( )

Definition at line 462 of file kis_tile_hash_table2.h.

463{
464 {
465 QWriteLocker locker(&m_iteratorLock);
466
468 TileType *tile = 0;
469
470 while (iter.isValid()) {
472 tile = m_map.erase(iter.getKey());
473
474 if (tile) {
475 tile->notifyDetachedFromDataManager();
476 m_map.getGC().enqueue(&MemoryReclaimer::destroy, new MemoryReclaimer(tile));
477 }
479
480 iter.next();
481 }
482
483 m_numTiles.storeRelaxed(0);
484 }
485
486 // garbage collection must **not** be run with locks held
487 m_map.getGC().update();
488}
Value erase(Key key)
void lockRawPointerAccess()
Definition qsbr.h:105
void enqueue(void(T::*pmf)(), T *target, bool migration=false)
Definition qsbr.h:71
void unlockRawPointerAccess()
Definition qsbr.h:110
void update()
Definition qsbr.h:93

References ConcurrentMap< K, V, KT, VT >::Iterator::getKey(), ConcurrentMap< K, V, KT, VT >::Iterator::isValid(), and ConcurrentMap< K, V, KT, VT >::Iterator::next().

◆ debugMaxListLength()

template<class T >
void KisTileHashTableTraits2< T >::debugMaxListLength ( qint32 & min,
qint32 & max )

Definition at line 528 of file kis_tile_hash_table2.h.

529{
530}

◆ debugPrintInfo()

template<class T >
void KisTileHashTableTraits2< T >::debugPrintInfo ( )

Definition at line 523 of file kis_tile_hash_table2.h.

524{
525}

◆ defaultTileData()

template<class T >
KisTileData * KisTileHashTableTraits2< T >::defaultTileData ( )
inline

Definition at line 507 of file kis_tile_hash_table2.h.

508{
509 QReadLocker locker(&m_defaultPixelDataLock);
510 return m_defaultTileData;
511}

◆ deleteTile() [1/2]

template<class T >
bool KisTileHashTableTraits2< T >::deleteTile ( qint32 col,
qint32 row )

when invalid tile index is requested, just do nothing

Definition at line 450 of file kis_tile_hash_table2.h.

451{
452 const quint32 idx = calculateHashSafe(col, row);
453 if (!idx) {
455 return false;
456 }
457
458 return erase(idx);
459}
quint32 calculateHashSafe(qint32 col, qint32 row)

◆ deleteTile() [2/2]

template<class T >
bool KisTileHashTableTraits2< T >::deleteTile ( TileTypeSP tile)

Definition at line 444 of file kis_tile_hash_table2.h.

445{
446 return deleteTile(tile->col(), tile->row());
447}
bool deleteTile(TileTypeSP tile)

◆ erase()

template<class T >
bool KisTileHashTableTraits2< T >::erase ( quint32 idx)
inlineprivate

Definition at line 178 of file kis_tile_hash_table2.h.

179 {
181
182 bool wasDeleted = false;
183 TileType *tile = m_map.erase(idx);
184
185 if (tile) {
186 tile->notifyDetachedFromDataManager();
187
188 wasDeleted = true;
189 m_numTiles.fetchAndSubRelaxed(1);
190 m_map.getGC().enqueue(&MemoryReclaimer::destroy, new MemoryReclaimer(tile));
191 }
192
194
195 m_map.getGC().update();
196 return wasDeleted;
197 }

References KisTileHashTableTraits2< T >::MemoryReclaimer::destroy(), QSBR::enqueue(), ConcurrentMap< K, V, KT, VT >::erase(), ConcurrentMap< K, V, KT, VT >::getGC(), QSBR::lockRawPointerAccess(), KisTileHashTableTraits2< T >::m_map, KisTileHashTableTraits2< T >::m_numTiles, QSBR::unlockRawPointerAccess(), and QSBR::update().

◆ getExistingTile()

template<class T >
KisTileHashTableTraits2< T >::TileTypeSP KisTileHashTableTraits2< T >::getExistingTile ( qint32 col,
qint32 row )

Returns a tile in position (col,row). If no tile exists, returns null.

Parameters
colcolumn of the tile
rowrow of the tile

a tile with invalid index obviously doesn't exist

Definition at line 307 of file kis_tile_hash_table2.h.

308{
309 const quint32 idx = calculateHashSafe(col, row);
310 if (!idx) {
312 return TileTypeSP();
313 }
314
316 TileTypeSP tile = m_map.get(idx);
318
319 m_map.getGC().update();
320 return tile;
321}
Value get(Key key)

◆ getReadOnlyTileLazy()

template<class T >
KisTileHashTableTraits2< T >::TileTypeSP KisTileHashTableTraits2< T >::getReadOnlyTileLazy ( qint32 col,
qint32 row,
bool & existingTile )

Returns a tile in position (col,row). If no tile exists, creates nothing, but returns shared default tile object of the table. Be careful, this object has column and row parameters set to (qint32_MIN, qint32_MIN).

Parameters
colcolumn of the tile
rowrow of the tile
existingTilereturns true if the tile actually exists in the table and it is not a lazily created default wrapper tile

when invalid tile index is requested, just return a detached tile with the default data

we pretend as if this tile hasn't existed, it will allow the calling code to avoid modifying the extent manager (note, that is opposite to what happens in getTileLazy())

Definition at line 404 of file kis_tile_hash_table2.h.

405{
406 const quint32 idx = calculateHashSafe(col, row);
407 if (!idx) {
410
415 existingTile = false;
416
417 QReadLocker locker(&m_defaultPixelDataLock);
418 return new TileType(col, row, m_defaultTileData, 0);
419 }
420
422 TileTypeSP tile = m_map.get(idx);
424
425 existingTile = tile;
426
427 if (!existingTile) {
428 QReadLocker locker(&m_defaultPixelDataLock);
429 tile = new TileType(col, row, m_defaultTileData, 0);
430 }
431
432 m_map.getGC().update();
433 return tile;
434}

◆ getTileLazy()

template<class T >
KisTileHashTableTraits2< T >::TileTypeSP KisTileHashTableTraits2< T >::getTileLazy ( qint32 col,
qint32 row,
bool & newTile )

Returns a tile in position (col,row). If no tile exists, creates a new one, attaches it to the list and returns.

Parameters
colcolumn of the tile
rowrow of the tile
newTileout-parameter, returns true if a new tile was created

when invalid tile index is requested, just return a detached tile with the default data

we pretend as if this tile has already existed, it will allow the calling code to avoid modifying the extent manager

Definition at line 324 of file kis_tile_hash_table2.h.

325{
326 newTile = false;
327 const quint32 idx = calculateHashSafe(col, row);
328 if (!idx) {
331
335 newTile = false;
336
337 QReadLocker locker(&m_defaultPixelDataLock);
338 return new TileType(col, row, m_defaultTileData, 0);
339 }
340
341 // we are going to assign a raw-pointer tile from the table
342 // to a shared pointer...
344
345 TileTypeSP tile = m_map.get(idx);
346
347 while (!tile) {
348 // we shouldn't try to acquire **any** lock with
349 // raw-pointer lock held
351
352 {
353 QReadLocker locker(&m_defaultPixelDataLock);
354 tile = new TileType(col, row, m_defaultTileData, 0);
355 }
356
357 TileTypeSP::ref(&tile, tile.data());
358 TileType *discardedTile = 0;
359
360 // iterator lock should be taken **before**
361 // the pointers are locked
362 m_iteratorLock.lockForRead();
363
364 // and now lock raw-pointers again
366
367 // mutator might have become invalidated when
368 // we released raw pointers, so we need to reinitialize it
370 if (!mutator.getValue()) {
371 discardedTile = mutator.exchangeValue(tile.data());
372 } else {
373 discardedTile = tile.data();
374 }
375
376 m_iteratorLock.unlock();
377
378 if (discardedTile) {
379 // we've got our tile back, it didn't manage to
380 // get into the table. Now release the allocated
381 // tile and push TO/GA switch.
382 tile = 0;
383
384 discardedTile->notifyDeadWithoutDetaching();
385 m_map.getGC().enqueue(&MemoryReclaimer::destroy, new MemoryReclaimer(discardedTile));
386
387 tile = m_map.get(idx);
388 continue;
389
390 } else {
391 newTile = true;
392 m_numTiles.fetchAndAddRelaxed(1);
393
394 tile->notifyAttachedToDataManager(m_mementoManager);
395 }
396 }
398
399 m_map.getGC().update();
400 return tile;
401}
Mutator insertOrFind(Key key)
void ref() const
LockFreeTileMap::Mutator LockFreeTileMapMutator
struct Tile * newTile(struct rect r)
Definition pixels.c:231

References KisSharedPtr< T >::data(), and newTile().

◆ insert()

template<class T >
void KisTileHashTableTraits2< T >::insert ( quint32 idx,
TileTypeSP item )
inlineprivate

Definition at line 155 of file kis_tile_hash_table2.h.

156 {
157 TileTypeSP::ref(&item, item.data());
158 TileType *tile = 0;
159
160 {
161 QReadLocker locker(&m_iteratorLock);
163 tile = m_map.assign(idx, item.data());
164 }
165
166 if (tile) {
167 tile->notifyDeadWithoutDetaching();
168 m_map.getGC().enqueue(&MemoryReclaimer::destroy, new MemoryReclaimer(tile));
169 } else {
170 m_numTiles.fetchAndAddRelaxed(1);
171 }
172
174
175 m_map.getGC().update();
176 }
Value assign(Key key, Value desired)

References ConcurrentMap< K, V, KT, VT >::assign(), KisSharedPtr< T >::data(), KisTileHashTableTraits2< T >::MemoryReclaimer::destroy(), QSBR::enqueue(), ConcurrentMap< K, V, KT, VT >::getGC(), QSBR::lockRawPointerAccess(), KisTileHashTableTraits2< T >::m_iteratorLock, KisTileHashTableTraits2< T >::m_map, KisTileHashTableTraits2< T >::m_numTiles, KisSharedPtr< T >::ref(), QSBR::unlockRawPointerAccess(), and QSBR::update().

◆ isEmpty()

template<class T >
bool KisTileHashTableTraits2< T >::isEmpty ( )
inline

Definition at line 49 of file kis_tile_hash_table2.h.

50 {
51 return !m_numTiles.loadRelaxed();
52 }

References KisTileHashTableTraits2< T >::m_numTiles.

◆ numTiles()

template<class T >
qint32 KisTileHashTableTraits2< T >::numTiles ( )
inline

Definition at line 102 of file kis_tile_hash_table2.h.

103 {
104 return m_numTiles.loadRelaxed();
105 }

References KisTileHashTableTraits2< T >::m_numTiles.

◆ Q_STATIC_ASSERT_X()

template<class T >
KisTileHashTableTraits2< T >::Q_STATIC_ASSERT_X ( isInherited ,
"Template must inherit KisShared"  )
private

◆ refAndFetchDefaultTileData()

template<class T >
KisTileData * KisTileHashTableTraits2< T >::refAndFetchDefaultTileData ( )
inline

Returns a pointer to the default tile data object with ref counter increased by one. Make sure you call deref() after you finished using this object.

Definition at line 514 of file kis_tile_hash_table2.h.

515{
516 QReadLocker locker(&m_defaultPixelDataLock);
518 return m_defaultTileData;
519}
bool ref() const

◆ setDefaultTileData()

template<class T >
void KisTileHashTableTraits2< T >::setDefaultTileData ( KisTileData * defaultTileData)
inline

Definition at line 491 of file kis_tile_hash_table2.h.

492{
493 QWriteLocker locker(&m_defaultPixelDataLock);
494
495 if (m_defaultTileData) {
498 }
499
500 if (defaultTileData) {
503 }
504}

References KisTileData::acquire().

◆ tileExists()

template<class T >
bool KisTileHashTableTraits2< T >::tileExists ( qint32 col,
qint32 row )

Definition at line 301 of file kis_tile_hash_table2.h.

302{
303 return getExistingTile(col, row);
304}
TileTypeSP getExistingTile(qint32 col, qint32 row)

Friends And Related Symbol Documentation

◆ KisTileHashTableIteratorTraits2< T >

template<class T >
friend class KisTileHashTableIteratorTraits2< T >
friend

Definition at line 108 of file kis_tile_hash_table2.h.

Member Data Documentation

◆ isInherited

template<class T >
constexpr bool KisTileHashTableTraits2< T >::isInherited = std::is_convertible<T*, KisShared*>::value
staticconstexprprivate

Definition at line 37 of file kis_tile_hash_table2.h.

◆ m_defaultPixelDataLock

template<class T >
QReadWriteLock KisTileHashTableTraits2< T >::m_defaultPixelDataLock
private

We still need something to guard changes in m_defaultTileData, otherwise there will be concurrent read/writes, resulting in broken memory.

Definition at line 208 of file kis_tile_hash_table2.h.

◆ m_defaultTileData

template<class T >
KisTileData* KisTileHashTableTraits2< T >::m_defaultTileData
private

Definition at line 212 of file kis_tile_hash_table2.h.

◆ m_iteratorLock

template<class T >
QReadWriteLock KisTileHashTableTraits2< T >::m_iteratorLock
mutableprivate

Definition at line 209 of file kis_tile_hash_table2.h.

◆ m_map

template<class T >
LockFreeTileMap KisTileHashTableTraits2< T >::m_map
mutableprivate

Definition at line 202 of file kis_tile_hash_table2.h.

◆ m_mementoManager

template<class T >
KisMementoManager* KisTileHashTableTraits2< T >::m_mementoManager
private

Definition at line 213 of file kis_tile_hash_table2.h.

◆ m_numTiles

template<class T >
QAtomicInt KisTileHashTableTraits2< T >::m_numTiles
private

Definition at line 211 of file kis_tile_hash_table2.h.


The documentation for this class was generated from the following file: