File indexing completed on 2024-05-12 15:56:58

0001 /*
0002  *  SPDX-FileCopyrightText: 2006 Cyrille Berger <cberger@cberger.net>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_shared.h"
0008 #include "kis_debug.h"
0009 
0010 
0011 /**
0012  * NOTE: The description of how Weak shared pointers system works:
0013  *
0014  * Every KisShared object has his own _sharedWeakReference pointer.
0015  * This pointer holds QAtomicInt counter. When KisShared constructs
0016  * itself, it increments _sharedWeakReference by one. When it dies -
0017  * it decrements it. This is the only way how the number in the
0018  * counter can become odd. Obviously, when the number falls to zero,
0019  * the counter object is deleted.
0020  *
0021  * When a weak shared pointer is created, it gets the pointer and
0022  * increments the counter by 2, so the parity of the number is kept
0023  * unchanged. Now the counter is shared between the KisShared and
0024  * KisWeakSharedPtr and the latter one can check the correctness
0025  * of the pointer by checking parity!
0026  */
0027 
0028 KisShared::KisShared()
0029 {
0030     /**
0031      * We can defer creation of a weak reference until
0032      * better days... It gives us 41% better performance. ;)
0033      */
0034     _sharedWeakReference = 0;
0035 }
0036 
0037 KisShared::~KisShared()
0038 {
0039     /**
0040      * Check no-one references us
0041      */
0042     Q_ASSERT(_ref == 0);
0043 
0044     if(_sharedWeakReference && !_sharedWeakReference->deref())
0045         delete _sharedWeakReference;
0046 }